Download presentation
Presentation is loading. Please wait.
Published byCory Johnston Modified over 10 years ago
1
Introduction to ML – Part 1 Frances Spalding
2
Assignment 1 http://www.cs.princeton.edu/courses/ar chive/fall05/cos441/assignments/a1.ht m http://www.cs.princeton.edu/courses/ar chive/fall05/cos441/assignments/a1.ht m Due next Monday (Oct 3 rd ) Make sure you’ve signed up for the mailing list
3
Standard ML Standard ML is a domain-specific language for building compilers Support for Complex data structures (abstract syntax, compiler intermediate forms) Memory management like Java Large projects with many modules Advanced type system for error detection
4
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Aquinas will do a second class on ML on Thursday Resources: Jeffrey Ullman “Elements of ML Programming” Robert Harper’s “an introduction to ML” See course webpage for pointers and info about how to get the software
5
Intro to ML Highlights Data Structures for compilers Data type definitions Pattern matching Strongly-typed language Every expression has a type Certain errors cannot occur Polymorphic types provide flexibility Flexible Module System Abstract Types Higher-order modules (functors)
6
Intro to ML Interactive Language Type in expressions Evaluate and print type and result Compiler as well High-level programming features Data types Pattern matching Exceptions Mutable data discouraged
7
Preliminaries Read – Eval – Print – Loop - 3 + 2;
8
Preliminaries Read – Eval – Print – Loop - 3 + 2; > 5: int
9
Preliminaries Read – Eval – Print – Loop - 3 + 2; > 5: int - it + 7; > 12 : int
10
Preliminaries Read – Eval – Print – Loop - 3 + 2; > 5: int - it + 7; > 12 : int - it – 3; > 9 : int - 4 + true; stdIn:17.1-17.9 Error: operator and operand don't agree [literal] operator domain: int * int operand: int * bool in expression: 4 + true
11
Preliminaries Read – Eval – Print – Loop - 3 div 0; Failure : Div- run-time error
12
Basic Values - (); > () : unit=> like “void” in C (sort of) => the uninteresting value/type - true; > true : bool - false; > false : bool - if it then 3+2 else 7;“else” clause is always necessary > 7 : int - false andalso loop_Forever; > false : booland also, or else short-circuit eval
13
Basic Values Integers - 3 + 2 > 5 : int - 3 + (if not true then 5 else 7); > 10 : intNo division between expressions and statements Strings - “Dave” ^ “ “ ^ “Walker”; > “Dave Walker” : string - print “foo\n”; foo > 3 : int Reals - 3.14; > 3.14 : real
14
Using SML/NJ Interactive mode is a good way to start learning and to debug programs, but… Type in a series of declarations into a “.sml” file - use “foo.sml” [opening foo.sml] … list of declarations with their types
15
Larger Projects SML has its own built in interactive “make” Pros: It automatically does the dependency analysis for you No crazy makefile syntax to learn Cons: May be more difficult to interact with other languages or tools
16
Compilation Manager % sml - OS.FileSys.chDir “ ~/courses/510/a2 ” ; - CM.make(); looks for “ sources.cm ”, analyzes dependencies [compiling … ] compiles files in group [wrote … ] saves binaries in./CM/ - CM.make ’ “ myproj/ ” (); specify directory sources.cm c.smlb.smla.sig Group is a.sig b.sml c.sml
17
What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “Dave”, ssn = 332177} Lists: 3::4::5::nil or [3,4]@[5] Datatypes Functions And more! Rather than list all the details, we will write a couple of programs
18
An interpreter Interpreters are usually implemented as a series of transformers: stream of characters abstract syntax lexing/ parsing evaluate abstract value print stream of characters
19
A little language (LL) An arithmetic expression e is a boolean value an if statement (if e1 then e2 else e3) an integer an add operation a test for zero (isZero e)
20
LL abstract syntax in ML datatype term = Bool of bool | If of term * term * term | Num of int | Add of term * term | IsZero of term -- constructors are capitalized -- constructors can take a single argument of a particular type type of a tuple another eg: string * char vertical bar separates alternatives
21
LL abstract syntax in ML Add (Num 2, Num 3) represents the expression “2 + 3” Add Num 23
22
LL abstract syntax in ML If (Bool true, Num 0, Add (Num 2, Num 3)) represents “if true then 0 else 2 + 3” Add Num 2 3 true BoolNum 0 If
23
Function declarations fun isValue t = case t of Num n => true | Bool b => true | _ => false function name function parameter default pattern matches anything
24
What is the type of the parameter t? Of the function? fun isValue t = case t of Num n => true | Bool b => true | _ => false function name function parameter default pattern matches anything
25
What is the type of the parameter t? Of the function? fun isValue (t:term) : bool = case t of Num n => true | Bool b => true | _ => false val isValue : term -> bool ML does type inference => you need not annotate functions yourself (but it can be helpful)
26
A type error fun isValue t = case t of Num _ => true | _ => false ex.sml:22.3-24.15 Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: _ => false
27
A type error Actually, ML will give you several errors in a row: ex.sml:22.3-25.15 Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: Successor t2 => true ex.sml:22.3-25.15 Error: types of rules don't agree [literal] earlier rule(s): term -> int this rule: term -> bool in rule: _ => false
28
A very subtle error fun isValue t = case t of num => true | _ => false The code above type checks. But when we test it refined the function always returns “true.” What has gone wrong?
29
A very subtle error fun isValue t = case t of Num 0 => 1 | Add(Num t1,Num t2) => t1 + t2 | _ => 0 The code above type checks. But when we test it refined the function always returns “true.” What has gone wrong? -- num is not capitalized (and has no argument) -- ML treats it like a variable pattern (matches anything!)
30
Exceptions exception Error of string fun debug s : unit = raise (Error s)
31
Exceptions exception Error of string fun debug s : unit = raise (Error s) - debug "hello"; uncaught exception Error raised at: ex.sml:15.28-15.35 in SML interpreter:
32
Evaluator fun isValue t =... exception NoRule fun eval t = case t of Bool _ | Num _ => t |...
33
Evaluator... fun eval t = case t of Bool _ | Num _ => t | If(t1,t2,t3) => let val v = eval t1 in case v of Bool b => if b then (eval t2) else (eval t3) | _ => raise NoRule end let statement for remembering temporary results
34
Evaluator exception NoRule fun eval1 t = case t of Bool _ | Num _ =>... |... | Add (t1,t2) => case (eval v1, eval v2) of (Num n1, Num n2) => Num (n1 + n2) | (_,_) => raise NoRule
35
Finishing the Evaluator fun eval1 t = case t of... |... | Add (t1,t2) =>... | IsZero t =>... be sure your case is exhaustive
36
Finishing the Evaluator fun eval1 t = case t of... |... | Add (t1,t2) =>... What if we forgot a case?
37
Finishing the Evaluator ex.sml:25.2-35.12 Warning: match nonexhaustive (Bool _ | Zero) =>... If (t1,t2,t3) =>... Add (t1,t2) =>... fun eval1 t = case t of... |... | Add (t1,t2) =>... What if we forgot a case?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.