View
219
Download
0
Embed Size (px)
Citation preview
Last Time Introduction Course Logistics Introduction to ML
Base Types Tuples and Records Functions & Polymorphism See Harper’s Intro to ML
Today Sign up to be ‘TA’ for a week More ML
Data types Type abbreviations Exceptions Modules
Mathematical Preliminaries Inductive Definitions
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]…
Val it = () : unit
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
Compilation Manager
% sml
-OS.FileSys.chDir “~/courses/510/a2”;
-CM.make(); looks for “sources.cm”, analyzes depdndencies
[compiling…] compiles files in group
[wrote…] saves binaries in ./CM/
-CM.make’ “myproj/”(); specify directory
sources.cmc.smlb.smla.sigGroup is
a.sigb.smlc.sml
Lists Lists are defined in two ways:
nil : ‘a list (empty list):: : ‘a * ‘a list -> ‘a list
3 :: 4 :: 5 :: nil : int list (fn x => x) :: niil : (‘a -> ‘a) list 3 :: true :: nil : ? Abbreivation :
[] (empty list) [x,y,z] (x :: y :: z :: nil)
List Processing Fun length(nil) = 0
l length (x :: l) = 1 + length l Functions over lists are usually defined by
case analysis (induction) over the structure of a list
. nil
. X :: ; Fun | div two nil = 0
|| div two (x::nil) = 0|| div two (x :: y :: z) = 1 + | divtwo z
List Processing Fun length l =
case l of[] => 0| _ :: l => | + length l;
wild card pattern
“don’t care”
Other Patterns Fun lessThree x =
case x of 0 | 1 | Z => true| z => false
Fun first (a,b) = a; Fun snd (a,b) = b; Patterns can be
Variables Integer, char, string constants Tuples Records Lists Wild cards And a few other things…
Datatypes-datatype bool = true/falseDatatypes can carry values:Datatype coin =
penny | Nickel | Dime | Quarter | Loonie | Twonie
Datatype bill = One | Five | Ten | Twenty
Datatype money = Coin of coin | Bill of bill | Forgery of int
Datatypes- Fun count money =
case money ofcoin(x) => count_coin x
| Bill (b) => count_bill bAnd count_coin c =
case c of Penny => 1
| Nickel => 5| …
And count_bill b = (case b of
One =>1| Two =>2| … > * 100
Pitfalls Datatype coin = penny | nickel; Val c = penny; Case c of
nickel => true| Penny => false
>?- case Nickel of
Penny => true| Nickel => false;
>?- case Nickel of
Penny => case Nickel of Penny => true| Nickel => false
| nickel => true>?
Problem Set #1 Datatype digit = One | Two | … Datatype number = D of digit | …
Type AbbreviationsType node = int;Type edges = node->node list;Type graph = node list * edges;Fun neighbours((g, n) : graph * node) *
let val (_,e) * g in e n end;
we may use the fact that the type graph is equivalent to node list * edges
Exceptions Exception Head; Fun head nil = raise Head
| head x :: +1 = x (head []) handle Head=> 3Careful again:- :F e, then e2 else e3 handle Head =>3
nil :: nil :: nil :: nil : (‘a list) list[[1,2,3],[3,4,5]]:int list list
Modules Signatures
Interfaces Structures
Implementations Functors
Parameterized structures Functions from structures to
structures
SignaturesSignautre QUEUE = signature name
sigtype ‘a queueexception Emptyval empty : ‘a queueval insert : ‘a * ‘a queue
-> ‘a queueval remove : ‘a queue
-> ‘a * ‘a queueend
Keywords delimit signature
Signature Inclusion Signature QUEUE_EMPTY =
siginclude QUEUEval is_empty : ‘a queue
->bool
End- equivalent to writing out QUEUE inside
QUEUE_EMPTY
Using Structures
= Queue : Empty;> Queue.Empty : ‘a Queue.queue- val a = Queue.insert (1,([6,5,4],[1,2,3]));- structure Q – Queue;- Q.insert _.- open Queue;- Empty- Q.Empty
Structures Structure Queue =
structtype ‘a queue = ‘a list * ‘a listexception Emptyval empty = (nil, nil)fun insert (x, q) = …fun remove q = …
endKeywords struct … end delimit
structureStructure x = … binds X to structure
Signature AscriptionRecall:
Signature QUEUE =sigtype ‘a queueexception Emptyval empty : ‘a queueend
Now we ascribe this signature to a structure:Structure Queue :> QUEUE =
structtype ‘a queue = ‘a list * ‘a listval empty = (nil, nil)exception Empty…
end
Signature Ascription Opaque ascription
Provides abstract typesStructure Queue :> QUEUE = …
Transparent ascription A special case of opaque ascription Hides fields but does not make types
abstractStructure Queue_E : QUEUE = …
SEE Harper, chapters 18-22 for more on modules
Data Types A mechanism for declaring a new type and constructors for
the new type and patterns for the new type Datatype colour = Red | Blue | Green> Type colour
con Red : colourcon Blue : colourcon Yellow : colour
- Red;> Red : colour- fun favourite colour =
case colour ofRed | Blue -> true| Green -> false;