DOC PREVIEW
CALTECH CS 11 - Lecture Notes

This preview shows page 1-2-3-24-25-26-27-49-50-51 out of 51 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 11 Ocaml track: lecture 4The idea of modulesImplementation hidingInterface vs. implementationInterface of Newlist modulenewlist.mli (types)newlist.mli (exceptions)newlist.mli (functions)Implementation of Newlistnewlist.ml (types)newlist.ml (exceptions)newlist.ml (functions) (1)newlist.ml (functions) (2)newlist.ml (functions) (3)Compiling Newlist files (1)Compiling Newlist files (2)Compiling Newlist files (3)Using the Newlist moduleSlide 19Making lists abstractnewlist2.mlitype 'a t ????newlist2.mli (new interface)Slide 24More modulesA simple Set moduleA simple Set "signature"A simple Set "signature" in OcamlA simple Set "signature" in ocamlSlide 30Set implementation (1)Set implementation (2)Set implementation (3)Using the Set implementationNoteProblemSolutionFirst try (broken)Problem with first try (1)Problem with first try (2)Problem with first try (3)Second try (working)Using IntListSetModules with polymorphic typesInterfaceImplementationNotice anything odd?However...Sets with orderable elements(Preview)Next timeCS 11 Ocaml track: lecture 4Today:modulesThe idea of modulesWhat's the idea behind a "module"?Package up code so other people/programs can use itControl what parts of code arepart of the interfacepart of the implementation onlyand hide the implementation partImplementation hidingWhy hide the implementation?Might want to change laterchange fundamental data structuresdifferent efficiency tradeoffsDon't want all code that uses module to require major changesor, ideally, any changesInterface vs. implementationIn Ocaml: interface goes into .mli filesimplementation goes into .ml filesSimple example: listsWe'll create our own list moduleCall it NewlistInterface of Newlist moduleIn newlist.mliMust specify:publicly visible typespublicly visible functionspublicly visible exceptionsDon't have to define any of these!Except: need to define types if want users to pattern-match on themnewlist.mli (types)type 'a newlist = | Nil | Cons of 'a * 'a newlistIf didn't need ability to pattern-match, could do justtype 'a newlistThis would be an abstract typenewlist.mli (exceptions)exception List_error of stringnewlist.mli (functions)val hd : 'a newlist -> 'aval tl : 'a newlist -> 'a newlistval append : 'a newlist -> 'a newlist -> 'a newlistval ( @@ ) : 'a newlist -> 'a newlist -> 'a newlistval length : 'a newlist -> intJust the signature of the functionsAny functions not mentioned here are hiddenNote: operator @@ is exportedImplementation of NewlistIn newlist.mlMust define:all typesall functionsall exceptionswhether exported by module or notCopying of code from .mli file sometimes unavoidablenewlist.ml (types)type 'a newlist = | Nil | Cons of 'a * 'a newlistHere, had to copy code in newlist.mliNo way to avoid this!newlist.ml (exceptions)exception List_error of stringAgain, had to copy code in newlist.mlinewlist.ml (functions) (1)let hd nl = match nl with | Nil -> raise (List_error "head of empty list") | Cons (h, _) -> hlet tl nl = match nl with | Nil -> raise (List_error "tail of empty list") | Cons (_, t) -> tnewlist.ml (functions) (2)let rec append nl1 nl2 = match nl1, nl2 with | Nil, _ -> nl2 | Cons (h, t), _ -> Cons (h, append t nl2)let ( @@ ) = appendnewlist.ml (functions) (3)let rec length nl = match nl with | Nil -> 0 | Cons (_, t) -> 1 + length tCompiling Newlist files (1)To compile the .mli file, just do:ocamlc -c newlist.mliThis will give a compiled interface file (.cmi file)The .cmi file is required to compile any file that uses the Newlist moduleThe same .cmi file can be used for both bytecode and native-code compilationCompiling Newlist files (2)To compile the .ml file, just do:ocamlc -c newlist.ml(for bytecode compilation), or:ocamlopt.opt -c newlist.ml(for native-code compilation)We'll mostly use the bytecode compilerCompiling Newlist files (3)To compile an .ml file that uses the Newlist module, just do:ocamlc -c foobar.ml (* bytecode compilation *)Note that don't have to put .cmi file in command linecompiler searches for it automaticallyUsing the Newlist moduleIn a file named e.g. testlist.ml:open Newlistlet test1 = Cons (1, Cons (2, Cons (3, Cons (4, Nil))))let test2 = Cons (11, Cons (12, Cons (13, Cons (14, Nil))))Using the Newlist moduleWithout the open declaration:let test1 = Newlist.Cons (1, Newlist.Cons (2, Newlist.Cons (3, Newlist.Cons (4, Newlist.Nil))))(* etc. *)Making lists abstractDefine a new module called Newlist2files: newlist2.ml, newlist2.mliWe make one change: want the type to be completely abstractDownside: can't pattern-match on values of new list typeUpside: can change implementation without affecting code that uses itBIG win!newlist2.mliexception List_error of stringtype 'a t (* abstract type *)val empty : 'a t (* the empty list *)val cons : 'a -> 'a t -> 'a tval hd : 'a t -> 'aval tl : 'a t -> 'a tval append : 'a t -> 'a t -> 'a tval ( @@ ) : 'a t -> 'a t -> 'a tval length : 'a t -> inttype 'a t ????Abstract types often given names like "t" (for "type")Makes sense when using fully-qualified type name: Newlist2.tMeans "the type t defined in the Newlist2 module"More concise than e.g. Newlist2.newlistnewlist2.mli (new interface)exception List_error of stringtype 'a t (* abstract type *)val empty : 'a t (* the empty list *)val cons : 'a -> 'a t -> 'a tval hd : 'a t -> 'aval tl : 'a t -> 'a tval append : 'a t -> 'a t -> 'a tval ( @@ ) : 'a t -> 'a t -> 'a tval length : 'a t -> intnewlist2.mli (new interface)val empty : 'a t (* the empty list *)val cons : 'a -> 'a t -> 'a tval hd : 'a t -> 'aval tl : 'a t -> 'a tThese values/functions used instead of pattern matching and type constructorsCan create and pick apart Newlist2.t valuesMuch like lists in SchemeAll vals are functions except for empty valueMore modulesWhat we've seen is the most common way to use modulesModule is implicitly defined by .ml and .mli filesIt's also possible to explicitly define module types (interfaces) and module implementations inside a body of ocaml codeThat's what we'll look at nextWill lead us to functors (next week)A simple Set moduleWhat are the characteristics of a set?collection of elementsno duplicatesthere is an empty set valuecan add elements to setcan test whether elements are in


View Full Document

CALTECH CS 11 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?