Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Similar documents
CMSC 330: Organization of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CSCE 314 Programming Languages. Type System

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CMSC330 Fall 2013 Practice Problems 6 Solutions

The role of semantic analysis in a compiler

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

CSCI-GA Scripting Languages

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Lecture 16: Static Semantics Overview 1

Semantic Analysis. Lecture 9. February 7, 2018

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Programming Languages Third Edition. Chapter 7 Basic Semantics

Weeks 6&7: Procedures and Parameter Passing

Introduction to Programming Using Java (98-388)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

CS558 Programming Languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Chapter 5. Names, Bindings, and Scopes

CSE 307: Principles of Programming Languages

Syntax Errors; Static Semantics

COS 320. Compiling Techniques

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Project Compiler. CS031 TA Help Session November 28, 2011

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Introduce C# as Object Oriented programming language. Explain, tokens,

Pierce Ch. 3, 8, 11, 15. Type Systems

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CMSC330 Spring 2008 Final Exam

G Programming Languages - Fall 2012

CS558 Programming Languages

CS558 Programming Languages

Anonymous Functions CMSC 330: Organization of Programming Languages

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Short Notes of CS201

Type Checking and Type Inference

Chapter 9. Subprograms

Programmiersprachen (Programming Languages)

CS558 Programming Languages

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CS201 - Introduction to Programming Glossary By

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

G Programming Languages - Fall 2012

Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

A Short Summary of Javali

CSE 374 Programming Concepts & Tools

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

Semantic Analysis and Type Checking

Types, Type Inference and Unification

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Chapter 9. Subprograms

A brief introduction to C programming for Java programmers

Names, Bindings, Scopes

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

CMSC330. Objects, Functional Programming, and lambda calculus

CSC 533: Organization of Programming Languages. Spring 2005

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

CS558 Programming Languages. Winter 2013 Lecture 3

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Overview of Semantic Analysis. Lecture 9

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Array. Prepared By - Rifat Shahriyar

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

CS 415 Midterm Exam Spring 2002

Derived and abstract data types. TDT4205 Lecture 15

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

OCaml Language Choices CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Topic 9: Type Checking

Topic 9: Type Checking

1 Lexical Considerations

Concepts of Programming Languages

Types and Type Inference

Types and Type Inference

Object Oriented Paradigm

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CS 415 Midterm Exam Spring SOLUTION

Transcription:

CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation Finite automata (scanners) Recursive descent parsers CMSC 330 2 Language Features Covered Thus Far Implicit declarations x = 1 Dynamic typing x = 1 ; x = foo Functional programming add 1 (add 2 3) Type inference let x = x+1 ( x : int ) Higher-order functions let rec x = fun y -> x y Static (lexical) scoping let x = let x = Parametric polymorphism let x y = y ( a -> a ) Modules module foo struct end Programming Languages Revisited Characteristics Artificial language for precisely describing algorithms Used to control behavior of machine / computer Defined by its syntax & semantics Syntax Combination of meaningful text symbols Examples: if, while, let, =, ==, &&, + Semantics Meaning associated with syntactic construct Examples: x = 1 vs. x == 1 CMSC 330 3 CMSC 330 4 Comparing Programming Languages Syntax Differences usually superficial C / Java if (x == 1) else if x == 1 else end if (x = 1) then else Can cope with differences easily with experience Though may be annoying initially You should be able to learn new syntax quickly Just keep language manual / examples handy Comparing Prog. Languages (cont.) Semantics Differences may be major / minor / subtle Java C Physical Equality a.equal?(b) Structural Equality a.equals(b) *a == *b a = b Explaining these differences a major goal for 330 Will be covering different features in upcoming lectures CMSC 330 5 CMSC 330 6 1

Programming Language Features Programming Language Features (cont.) Paradigm Functional Imperative Object oriented Multi-paradigm Higher-order functions Closures Declarations Explicit Implicit Type system Typed vs. untyped Static vs. dynamic Weak vs. strong (type safe) Names & binding Namespaces Static (lexical) scopes Dynamic scopes Parameter passing Call by value Call by reference Call by name Eager vs. lazy evaluation Polymorphism Ad-hoc Subtype Overloading Parametric Generics Parallelism Multithreading Message passing CMSC 330 7 CMSC 330 8 Explicit vs. Implicit Declarations Explicit declarations Variables must be declared before used Examples C, C++, Java, Type System Overview Typed vs. untyped Static vs. dynamic Type safety Weak (not type safe) vs. strong (type safe) Implicit declarations Variables do not need to be declared Examples CMSC 330 9 CMSC 330 10 Type vs. Untyped Languages Typed language Operations are only valid for specified types 2 * 3 = 6 foo * bar = undefined Helps catch program errors Either at compile or run time Untyped language All operations are valid for all values Treat all values as sequences of 0 s and 1 s Example Assembly languages, FORTH Static vs. Dynamic Types Static types Before program is run Type of all expressions are determined Usually by compiler Disallowed operations cause compile-time error Static types may be manifest or inferred Manifest specified in text (at variable declaration) C, C++, Java, C# Inferred compiler determines type based on usage ML, CMSC 330 11 CMSC 330 12 2

Static vs. Dynamic Types (cont.) Dynamic types While program is running Type of all expressions determined Values maintain tag indicating type Disallowed operations cause run-time exception Dynamic types are not manifest (obviously) Examples, Python, Javascript, Lisp Type Safety Determined by extent programming language allows type errors Language should only allow operations on values that are permitted by their type Non-type safe code example printf( %d, 3.12) // Allows float to be printed as int Definitions Type safe language strong type system Non-type safe language weak type system CMSC 330 13 CMSC 330 14 Weak vs. Strong Typing Weak typing Allows one type to be treated as another or provides (many) implicit casts Example (int treated as bool) C int i = 1 ; if (i) // checks for 0 printf( %d, i); Weak vs. Strong Typing (cont.) Strong typing Prevents one type from being treated as another (also known as type safe) Example (int not treated as bool) Java int i = 1 ; if (i) // error, not bool System.out.println(i); i = 1 if i puts i end; Example languages C, C++,, Perl, Javascript // checks for nil let i = 1 in if i then print_int i Example languages Java (rare exceptions), // error, not bool CMSC 330 15 CMSC 330 16 Weak/Strong vs. Static/Dynamic Types How do these properties interact? Weak/strong & static/dynamic are orthogonal Some literature confuse strong & static type Strong / static types More work for programmer Catches more errors at compile time Weak / dynamic types Less work for programmer More errors occur at run time Names & Binding Overview Order of bindings Namespaces Static (lexical) scopes Dynamic scopes Funargs CMSC 330 17 CMSC 330 18 3

Names and Binding Programs use names to refer to things E.g., in x = x + 1, x refers to a variable A binding is an association between a name and what it refers to int x; /* x is bound to a stack location containing an int */ int f (int) /* f is bound to a function */ class C /* C is bound to a class */ let x = e1 in e2 (* x is bound to e1 *) Name Restrictions Languages often have various restrictions on names to make scanning and parsing easier Names cannot be the same as keywords in the language function names must be lowercase type constructor and module names must be uppercase Names cannot include special characters like ;, : etc Usually names are upper- and lowercase letters, digits, and _ (where the first character can t be a digit) Some languages also allow more symbols like! or - CMSC 330 19 CMSC 330 20 Names and Scopes Good names are a precious commodity They help document your code They make it easy to remember what names correspond to what entities We want to be able to reuse names in different, non-overlapping regions of the code Names and Scopes (cont.) A scope is the region of a program where a binding is active The same name in a different scope can refer to a different binding (refer to a different program object) A name is in scope if it's bound to something within the particular scope we re referring to CMSC 330 21 CMSC 330 22 Example Ordering of Bindings void w(int i) void x(float j) void y(float i) void z(void) int j; char *i; i is in scope in the body of w, the body of y, and after the declaration of j in z but all those i s are different j is in scope in the body of x and z Languages make various choices for when declarations of things are in scope CMSC 330 23 CMSC 330 24 4

Order of Bindings let x = e1 in e2 x is bound to e1 in scope of e2 let rec x = e1 in e2 x is bound in e1 and in e2 let x = 3 in let y = x + 3 in (* x is in scope here *) let x = 3 + x in (* error, x not in scope *) let rec length = function [] -> 0 (h::t) -> 1 + (length t) (* ok, length in scope *) in Order of Bindings C All declarations are in scope from the declaration onward int j = i; /* ok, i is in scope */ i = 3; /* also ok */ void f() int j = j + 3; /* error */ f(); /* ok, f declared */ f(); /* may be error; need prototype (or oldstyle C) */ void f() CMSC 330 25 CMSC 330 26 Order of Bindings Java Declarations are in scope from the declaration onward, except for methods and fields, which are in scope throughout the class Shadowing Names Shadowing is rebinding a name in an inner scope to have a different meaning May or may not be allowed by the language class C void f() g() // OK void g() C void f(float i) char *i = NULL; let g = 3;; let g x = x + 3;; Java void h(int i) float i; // not allowed CMSC 330 27 CMSC 330 28 Namespaces Languages have a top-level or outermost scope Many things go in this scope; hard to control collisions Common solution seems to be to add a hierarchy : Modules List.hd, String.length, etc. open to add names into current scope Java: Packages java.lang.string, java.awt.point, etc. import to add names into current scope C++: Namespaces namespace f class g, f::g b, etc. using namespace to add names to current scope Mangled Names What happens when these names need to be seen by other languages? What if a C program wants to call a C++ method? C doesn t know about C++ s naming conventions For multilingual communication, names are often mangled into some flat form E.g., class C int f(int *x, int y) becomes symbol ZN1C3fEPii in g++ E.g., native valueof(int) in java.lang.string corresponds to the C function Java_java_lang_String_valueOf I CMSC 330 29 CMSC 330 30 5

Static Scope Recall In static scoping, a name refers to its closest binding, going from inner to outer scope in the program text Languages like C, C++, Java,, and are statically scoped Free and Bound Variables The bound variables of a scope are those names that are declared in it If a variable is not bound in a scope, it is free The bindings of variables which are free in a scope are inherited from declarations of those variables in outer scopes in static scoping int j; float i; j is bound in scope 1 j is free in /* 1 */ int j; /* 2 */ float i; i is bound in scope 2 j = (int) i; j = (int) i; scope 2 CMSC 330 31 CMSC 330 32 Static Scoping and Nested Functions To allow arbitrary nested functions with higherorder functions and static scoping, we needed closures let add x = (fun y -> x + y) (add 3) 4 <closure> 4 7 CMSC 330 33 Functional Arguments (Funargs) Funarg problem Difficult to implement functions as first-class objects in stack-based programming languages Downwards funargs Passing function as parameter to another function call Can be implemented efficiently Since stack frame will still be on stack when funarg is used Techniques such as access links / displays (see CMSC 430) Upwards funargs Returning a function from a function call Implementation requires closures (stored on heap) CMSC 330 34 Example Downward Funarg Example let f x = let g y = x + y in g 3 f 1 x 1 y 3 let app f z = f z let f x = let g y = x + y in app g 3 f 1 x 1 f g z 3 y 3 When g is called, x is still on the stack when is Answer: is when g called? f fis is called with parameter x CMSC 330 35 Function g is passed as parameter to app I.e., g is a downward funarg When g is called, x is still on the stack Closure is not needed CMSC 330 36 6

Upward Funarg Example let add x = fun y -> x + y 2 ((add 1) 2) x 1 y Dynamic Scope In a language with dynamic scoping, a name refers to its closest binding at runtime LISP was the common example Function (fun y -> ) is returned by add I.e., it is an upward funarg When (fun y -> ) is called Add has already exited x is no longer on the stack Closure is needed Scheme (top-level scope only is dynamic) (define f (lambda () a)) ; defines a no-argument function which returns a (define a 3) ; bind a to 3 (f) ; calls f and returns 3 (define a 4) (f) ; calls f and returns 4 CMSC 330 37 CMSC 330 38 Previous Call Stack Example Nested Dynamic Scopes let map (f, n) = match n with [] -> [] (h::t) -> (f h)::(map (f, t)) let addn (n, l) = let add x = n + x in map (add, l) addn (3, [1; 2; 3]) n 3 l f n x 1 <list> <add> Full dynamic scopes can be nested Static scope relates to the program text Dynamic scope relates to program execution trace Perl (the keyword local introduces dynamic scope) $l = "global"; How to determine value of n in add? Dynamic scope: reads it off the stack (n = <list>) Static scope: lexical binding (n = param n to addn) CMSC 330 39 sub A local $l = "local"; B(); sub B print "$l\n"; B(); A(); B(); global local global CMSC 330 40 Static vs. Dynamic Scope Static scoping Local understanding of function behavior Know at compile-time what each name refers to A little more work to implement (keep a link to the lexical nesting scope in stack frame) Dynamic scoping Can be hard to understand behavior of functions Requires finding name bindings at runtime Easier to implement (keep a global table of stacks of variable/value bindings) CMSC 330 41 7