DOC PREVIEW
UW CSE 341 - Lecture Notes

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 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 21 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 21 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 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1ReviewHow to build bigger typesExamplesRest of todayRecordsExampleBy name vs. by positionThe truth about tuplesSyntactic sugarDatatype bindingsThe values we makeUsing themCasePatternsWhy this way is betterUseful examplesDon’t do thisThat said…Expression TreesRecursionCSE341: Programming LanguagesLecture 4Records (“each of”), Datatypes (“one of”), Case ExpressionsDan GrossmanFall 2011Review•Done: functions, tuples, lists, local bindings, options•Done: syntax vs. semantics, environments, mutation-free•Today: Focus on compound types–New feature: records •New concept: syntactic sugar (tuples are records)–New features: datatypes, constructors, case expressionsFall 2011 2CSE341: Programming LanguagesHow to build bigger types•Already know:–Have various base types like int bool unit char–Ways to build (nested) compound types: tuples, lists, options•Today: more ways to build compound types•First: 3 most important type building blocks in any language–“Each of”: A t value contains values of each of t1 t2 … tn–“One of”: A t value contains values of one of t1 t2 … tn–“Self reference”: A t value can refer to other t valuesRemarkable: A lot of data can be described with just these building blocksNote: These are not the common names for these conceptsFall 2011 3CSE341: Programming LanguagesExamples•Tuples build each-of types–int * bool contains an int and a bool•Options build one-of types–int option contains an int or it contains no data•Lists use all three building blocks–int list contains an int and another int list or it contains no data•And of course we can nest compound types–((int * int) option) * (int list list)) optionFall 2011 4CSE341: Programming LanguagesRest of today•Another way to build each-of types in ML–Records: have named fields–Connection to tuples and idea of syntactic sugar•A way to build and use our own one-of types in ML–For example, a type that contains and int or a string–Will lead to pattern-matching (more next lecture), one of ML’s coolest and strangest-to-Java-programmers features–How OOP does one-of types discussed later in courseFall 2011 5CSE341: Programming LanguagesRecordsRecord values have fields (any name) holding valuesRecord types have fields (and name) holding typesThe order of fields in a record value or type never matters–REPL alphabetizes fields just for consistencyBuilding records:Accessing components:(Evaluation rules and type-checking as expected)Fall 2011 6CSE341: Programming Languages {f1 = v1, …, fn = vn} {f1 : t1, …, fn : tn} {f1 = e1, …, fn = en} #myfieldname eExampleEvaluates toAnd has typeIf some expression such as a variable x has this type, then get fields with: Note we didn’t have to declare any record types–The same program could also make a {id=true,ego=false} of type {id:bool,ego:bool}Fall 2011 7CSE341: Programming Languages {name = “Amelia”, id = 41123 - 12} {id = 41111, name = “Amelia”} {id : int, name : string} #id x #name xBy name vs. by position•Little difference between (4,7,9) and {f=4,g=7,h=9}–Tuples a little shorter–Records a little easier to remember “what is where”–Generally a matter of taste, but for many (6? 8? 12?) fields, a record is usually a better choice•A common decision for a construct’s syntax is whether to refer to things by position (as in tuples) or by some (field) name (as with records)–A common hybrid is like with Java method arguments (and ML functions as used so far):•Caller uses position•Callee uses variables•Could totally do it differently; some languages haveFall 2011 8CSE341: Programming LanguagesThe truth about tuplesLast week we gave tuples syntax, type-checking rules, and evaluation rulesBut we could have done this instead:–Tuple syntax is just a different way to write certain records–(e1,…,en) is another way of writing {1=e1,…,n=en}–t1*…*tn is another way of writing {1:t1,…,n:tn}–In other words, records with field names 1, 2, …In fact, this is how ML actually defines tuples–Other than special syntax in programs and printing, they don’t exist–You really can write {1=4,2=7,3=9}, but it’s bad styleFall 2011 9CSE341: Programming LanguagesSyntactic sugar“Tuples are just syntactic sugar forrecords with fields named 1, 2, … n”•Syntactic: Can describe the semantics entirely by the corresponding record syntax•Sugar: They make the language sweeter Will see many more examples of syntactic sugar–They simplify understanding the language–They simplify implementing the languageWhy? Because there are fewer semantics to worry about even though we have the syntactic convenience of tuplesFall 2011 10CSE341: Programming LanguagesDatatype bindingsA “strange” (?) and totally awesome (!) way to make one-of types:–A datatype bindingFall 2011 11CSE341: Programming Languagesdatatype mytype = TwoInts of int * int | Str of string | Pizza•Adds a new type mytype to the environment•Adds constructors to the environment: TwoInts, Str, and Pizza•A constructor is (among other things), a function that makes values of the new type (or is a value of the new type):–TwoInts : int * int -> mytype–Str : string -> mytype–Pizza : mytypeThe values we make•Any value of type mytype is made from one of the constructors•The value contains:−A “tag” for “which constructor” (e.g., TwoInts)−The corresponding data (e.g., (7,9))−Examples: −TwoInts(3+4,5+4) evaluates to TwoInts(7,9)−Str(if true then “hi” else “bye”) evaluates to Str(“hi”)−Pizza is a valueFall 2011 12CSE341: Programming Languagesdatatype mytype = TwoInts of int * int | Str of string | PizzaUsing themSo we know how to build datatype values; need to access themThere are two aspects to accessing a datatype value1. Check what variant it is (what constructor made it) 2. Extract the data (if that variant has any)Notice how our other one-of types used functions for this:•null and iSome check variants•hd, tl, and valOf extract data (raise exception on wrong variant)ML could have done the same for datatype bindings–For example, functions like “isStr” and “getStrData”–Instead it did something betterFall 2011 13CSE341: Programming LanguagesCaseML combines the two aspects of accessing a one-of value with a case expression and pattern-matching–Pattern-matching much more general/powerful


View Full Document

UW CSE 341 - Lecture Notes

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
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?