typing and ml typing and ml
play

Typing and ML Typing and ML Definition Program organization and - PowerPoint PPT Presentation

Typing Typing Uses/Merits Uses/Merits Type errors Type errors Typing and ML Typing and ML Definition Program organization and documentation A name for a set of values and some operations A type error occurs when execution of


  1. Typing Typing Uses/Merits Uses/Merits Type errors Type errors Typing and ML Typing and ML Definition Program organization and documentation “A name for a set of values and some operations • A type error occurs when execution of program • Separate types for separate concepts is not faithful to the intended semantics, i.e., the which can be performed on that set of values.” • Indicate intended use of declared identifiers programmer’s intended interpretation. CSC324 “A collection of computational entities that share Identify and prevent errors Hardware errors some common property.” Winter 2007 • Compile-time or run-time checking can prevent • function call y() where y is not a function E.g., Sheila McIlraith meaningless computation such as • may cause jump to instruction that does not reals 5 + true - Charlotte contain a legal op code integers Acknowledgement: strings The material in these notes is derived from a variety Unintended semantics Support optimization of sources, including: int � bool • int_add(3, 4.5) • Compiler can generate better code if it knows • not a hardware error but the bits representing 4.5 Elements of ML Programming (Ullman), (int � int) � bool what’s in each variable, e.g., short integers will be interpreted as an integer Concepts in Programming Languages (Mitchell) require fewer bits. and the notes of Wael Aboelsaddat, Tony Bonner, What constitutes a type is language dependent. • Access record component by known offset Eric Joanis, Gerald Penn, and Suzanne Stevenson. 1 2 3 4 Type Safety Type Checking Type Safety Type Checking Compile- Compile - vs. Run vs. Run- -time time Type Inference Type Inference & Type Checking vs. Type Inference & Type Checking vs. Type Inference This is type inference: E.g. A3 := B4 + 1; • Scheme: run-time (dynamic) type checking Standard Type Checking: • A programming language is type safe if no Q: What type is A3 and B4 ? (car x) checks first to make sure x is a list int f(int x) { return x+1;}; program is allowed to violate its type A: Must be integer E.g. if test then … int g(int y) {return f(y+1)*2;}; distinctions. Q: What type is test ? • ML and Java: compile-time (static) type checking – Look at body of each function and use declared – Scheme, ML and Java are type safe. A: Must be Boolean types to check for agreement. f(x) must have f: A � B and x:A – C and C++ are not. Sound type system: a type system in which all types can always be inferred in any valid program. Type Inference: Trade-off: • The process of verifying and enforcing the • Looks at code without type info and figures out constraints of types is called type checking . • Both prevent type errors ML’s Type Inference Algorithm (Mitchell): what types could have been declared. • Run-time checking slows down execution 1. Assign a type to the expression and each • ML is designed to make type inference subexpression by using the known type of a • Compile-time checking restricts program flexibility tractable. symbol of a type variable. • Type checking can either occur at compile- E.g., Scheme list elements can have diff. types, 2. Generate a set of constraints on types by using • A cool algorithm! time (static) or at run-time (dynamic). the parse tree of the expression. ML lists elements must have the same type • Widely regarded as an important language 3. Solve these constraints by using unification, which • Static typing can make programming more difficult, innovation. is a substitution-based algorithm for solving initially. It’s harder to get things to compile, and systems of equations. • ML type inference gives you some idea of how other static analysis algorithms might work. It uses constraint satisfaction techniques. 5 6 7 8

  2. ML Patterns & Declarations ML ML ML: Main Features ML: Main Features ML: Tutorial Review ML: Tutorial Review Patterns & Declarations (see your tutorial notes for complete details) Patterns can be used in place of variables Functional Language Developed at Edinburgh (early ’80s) as Meta- <pat> ::= <id>|<tuple>|<cons>|<record>|… HOFs, recursion strongly encouraged, etc. Language for a program verification system SML environment basics Combination of Lisp and Algol features • Now a general purpose language Value declaration (general form): Strong, static typing w/ type inference • There are two basic dialects of ML Each ML expression has a type associated w/ it. val <pat> = <exp> Quite a fancy type system! – Standard ML (1991) & ML 2000 • Interpreter builds the type expression Polymorphism – Caml (including Objective Caml, or OCaml) • Cannot mix types in expressions E.g. (Declarations), a function can take arguments of various types - val myTuple = (“Jen”,”Brad”); • Must explicitly coerce/type-case Abstract & recursive data types val myTuple = (“Jen",“Brad") : string * string A pure functional language e.g. real(2) + 3.0 : real supported through an elegant type system, • Based on typed lambda calculus the ability to construct new types, and - val(x,y) = myTuple; • Grew out of frustration with Lisp! Data types (w/ operators): val x = “Jen" : string constructs that restrict access to objects of a • Major programs can be written w/o variables Basic: unit, bool, integer, real, string given type through a fixed set of ops defined for val y = “Brad" : string Constructors : list, tuple, array, record, function that type. Widely accepted operators infix, can be overloaded. Pattern matching - val myList = [1,2,3,4]; • reasonable performance (claimed) Function as a template val myList = [1,2,3,4] : int list • can be compiled Read-eval-print Exception handling • syntax not as arcane as LISP (nor as simple…) - val x::rest = myList; • Compiler infers type before compiling & executing. Allow you to handle errors/exception val x = 1 : int E.g., Elaborate module system - (5+3)-2; val rest = [2,3,4] : int list Most highly developed of any language > val it = 6 : int - If 5>3 then “Bob” else “Carol”; >val it=“Bob” : string 9 10 11 12 ML ML ML ML Declarations Pattern Matching Pattern Matching Functions Declarations Pattern Matching Pattern Matching Functions Pattern matching is powerful: Like Scheme there are: ML has let too! • Allows programmers to see the arguments Record pattern matching • Defined functions • No more heads and tails (cars/cdrs) -type stInfo={name:string, id:int, gpa:real}; • Anonymous functions Local declarations: type stInfo = {gpa:real, id:int, name:string} • Recursive functions - let val x = 2+3 in x*4 end; Tupple pattern matching • Higher-order functions -val st1:stInfo={name=“jen", id=123, gpa=4.0}; val it = 20 : int -val v=((2, "Test"),(3.2,#"A")); • And you can pass functions as parameters, and return val st1 = {gpa=4.0,id=123,name="jen"} : stInfo val v = ((2,"Test"),(3.2,#"A")) : (int * string) * (real * char) them as values. - let -val {name=N, gpa=G, id=_}=st1; (* order doesn't matter! *) -val ((i,s),(r,c))=v; Unlike Scheme, val m=3 (* ; is optional *) val G = 4.0 : real val i = 2 : int val n=m*m • we call these things “functions” not “procedures” val N = “jen" : string val s = "Test" : string in val r = 3.2 : real f: A � B means m+n -val {gpa,id, name}=st1; (* this is an abbreviation in ML *) val c = #"A" : char end; for every x � A, val gpa = 4.0 : real some element y=f(x) � B val it = 12 : int val id = 123 : int -val (p1,p2)=v;val p1 = (2,"Test") : int * string val name = “jen" : string f(x) = run forever val p2 = (3.2,#"A") : real * char terminate by raising an exception -val {name,...}=st1l; (* to specify subset of fields *) -val (_,(r,_))=v; (*_ (“don’t care”) matches anything!*) val name = “jen" : string val r = 3.2 : real A function maps a type to another one: accepts only one argument. What if we need multiple arguments? 13 14 15 16

Recommend


More recommend