1 / 23

Last Time

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

harper
Download Presentation

Last Time

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Last Time • Introduction • Course Logistics • Introduction to ML • Base Types • Tuples and Records • Functions & Polymorphism • See Harper’s Intro to ML

  2. Today • Sign up to be ‘TA’ for a week • More ML • Data types • Type abbreviations • Exceptions • Modules • Mathematical Preliminaries • Inductive Definitions

  3. 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

  4. 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

  5. Compilation Manager sources.cm a.sig b.sml c.sml Group is a.sig b.sml c.sml • % 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

  6. 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)

  7. 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

  8. List Processing • Fun length l = case l of [] => 0 | _ :: l => | + length l; wild card pattern “don’t care”

  9. 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…

  10. Datatypes -datatype bool = true/false Datatypes 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

  11. Datatypes - Fun count money = case money of coin(x) => count_coin x | Bill (b) => count_bill b And count_coin c = case c of Penny => 1 | Nickel => 5 | … And count_bill b = (case b of One =>1 | Two =>2 | … > * 100

  12. 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 >?

  13. Problem Set #1 • Datatype digit = One | Two | … • Datatype number = D of digit | …

  14. Type Abbreviations Type 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

  15. Exceptions • Exception Head; • Fun head nil = raise Head | head x :: +1 = x • (head []) handle Head=> 3 Careful 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

  16. Modules • Signatures • Interfaces • Structures • Implementations • Functors • Parameterized structures • Functions from structures to structures

  17. Signatures Signautre QUEUE = signature name sig type ‘a queue exception Empty val empty : ‘a queue val insert : ‘a * ‘a queue -> ‘a queue val remove : ‘a queue -> ‘a * ‘a queue end Keywords delimit signature

  18. Signature Inclusion • Signature QUEUE_EMPTY = sig include QUEUE val is_empty : ‘a queue -> bool End - equivalent to writing out QUEUE inside QUEUE_EMPTY

  19. 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

  20. Structures • Structure Queue = struct type ‘a queue = ‘a list * ‘a list exception Empty val empty = (nil, nil) fun insert (x, q) = … fun remove q = … end Keywords struct … end delimit structure Structure x = … binds X to structure

  21. Signature Ascription Recall: Signature QUEUE = sig type ‘a queue exception Empty val empty : ‘a queue end Now we ascribe this signature to a structure: Structure Queue :> QUEUE = struct type ‘a queue = ‘a list * ‘a list val empty = (nil, nil) exception Empty … end

  22. Signature Ascription • Opaque ascription • Provides abstract types Structure Queue :> QUEUE = … • Transparent ascription • A special case of opaque ascription • Hides fields but does not make types abstract Structure Queue_E : QUEUE = … • SEE Harper, chapters 18-22 for more on modules

  23. 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 : colour con Blue : colour con Yellow : colour - Red; > Red : colour - fun favourite colour = case colour of Red | Blue -> true | Green -> false;

More Related