last time introduction course logistics introduction to ml base types tuples and records functions...
Post on 22-Dec-2015
219 views
TRANSCRIPT
![Page 1: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/1.jpg)
Last Time Introduction Course Logistics Introduction to ML
Base Types Tuples and Records Functions & Polymorphism See Harper’s Intro to ML
![Page 2: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/2.jpg)
Today Sign up to be ‘TA’ for a week More ML
Data types Type abbreviations Exceptions Modules
Mathematical Preliminaries Inductive Definitions
![Page 3: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/3.jpg)
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
![Page 4: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/4.jpg)
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
![Page 5: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/5.jpg)
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
![Page 6: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/6.jpg)
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)
![Page 7: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/7.jpg)
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
![Page 8: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/8.jpg)
List Processing Fun length l =
case l of[] => 0| _ :: l => | + length l;
wild card pattern
“don’t care”
![Page 9: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/9.jpg)
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…
![Page 10: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/10.jpg)
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
![Page 11: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/11.jpg)
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
![Page 12: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/12.jpg)
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>?
![Page 13: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/13.jpg)
Problem Set #1 Datatype digit = One | Two | … Datatype number = D of digit | …
![Page 14: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/14.jpg)
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
![Page 15: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/15.jpg)
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
![Page 16: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/16.jpg)
Modules Signatures
Interfaces Structures
Implementations Functors
Parameterized structures Functions from structures to
structures
![Page 17: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/17.jpg)
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
![Page 18: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/18.jpg)
Signature Inclusion Signature QUEUE_EMPTY =
siginclude QUEUEval is_empty : ‘a queue
->bool
End- equivalent to writing out QUEUE inside
QUEUE_EMPTY
![Page 19: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/19.jpg)
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
![Page 20: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/20.jpg)
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
![Page 21: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/21.jpg)
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
![Page 22: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/22.jpg)
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
![Page 23: Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML](https://reader035.vdocument.in/reader035/viewer/2022062421/56649d815503460f94a66d5a/html5/thumbnails/23.jpg)
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;