CSE 341 Section 2 Nick Mooney Spring 2017

Slides:



Advertisements
Similar presentations
Modelling & Datatypes John Hughes. Software Software = Programs + Data.
Advertisements

Modelling & Datatypes Koen Lindström Claessen. Software Software = Programs + Data.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
Modelling & Datatypes Koen Lindström Claessen. Software Software = Programs + Data.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
CSE 341 Section 9 Nick Mooney Spring 2017
The Addition Rule.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
Section 2 – CSE341 Patrick Larson, Spring 2013.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Nicholas Shahan Spring 2016
Emily Leland (Not Nick) Spring 2017
CSE 341: Programming Languages Section 1
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2016.
Section 2 – CSE341 Konstantin Weitz.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE 341 Section 1 Nick Mooney Spring 2017
Nicholas Shahan Spring 2016
Preparing for MUPL! Justin Harjanto
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2013.
CSE 341 Section 9 Winter 2018 Adapted from slides by Eric Mullen, Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
CSE 341 Section 5 Winter 2018.
CSE 341 PL Section 2 Justin Harjanto.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2017.
Spencer Pearson Spring 2017
Adapted from slides by Nicholas Shahan and Dan Grossman
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
If-statements & Indefinite Loops
Section 12.2 Theoretical Probability
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341 Section 9 Fall 2017 Adapted from slides by Nick Mooney, Nicholas Shahan, Cody Schroeder, and Dan Grossman.
CSE 341 Section 2.
Section 12.2 Theoretical Probability
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
CSE341: Programming Languages Section 1
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2019.
Section 12.2 Theoretical Probability
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

CSE 341 Section 2 Nick Mooney Spring 2017 Adapted from slides by Nicholas Shahan, Patrick Larson, and Dan Grossman

Today’s Agenda Type synonyms Polymorphism and type generality Equality types Some sugar! Do we really need “if/then/else”?

Type Synonyms What does int * int * int represent? In HW1 we called it a date Wouldn’t it be nice to reflect this representation in the source code itself? type date = int * int * int

type vs datatype datatype introduces a new type name, distinct from all existing types datatype suit = Club | Diamond | Heart | Spade datatype rank = Jack | Queen | King | Ace | Num of int type is just another name The only way to construct values of the datatype is with the constructors Types are interchangeable in EVERY way. Write the date order example. REPL will print date or int * int Will print whatever it figures out first. type card = suit * rank

Type Synonyms Why? For now, just for convenience It doesn’t let us do anything new Later in the course we will see another use related to modularity.

Type Generality Write a function that appends two string lists…

Type Generality We would expect But the type checker found string list * string list -> string list But the type checker found ‘a list * ‘a list -> ‘a list Why is this OK?

More General Types The type is more general than the type ‘a list * ‘a list -> ‘a list is more general than the type string list * string list -> string list and “can be used” as any less general type, such as int list * int list -> int list But it is not more general than the type int list * string list -> int list

The Type Generality Rule The “more general” rule A type t1 is more general than the type t2 if you can take t1, replace its type variables consistently, and get t2 What does consistently mean? If we have our list appends function, it has type: ‘a list * ‘a list -> ‘a list We can replace every instance ‘a with the same thing. We cannot decide to replace the first ‘a with int and the second with string.

Equality Types Write a list contains function… Do this in the REPL What types do we see? How are these types different from the general types we have seen in the past?

Equality Types The double quoted variable arises from use of the = operator We can use = on most types like int, bool, string, tuples (that contain only “equality types”) Functions and real are not ”equality types” Generality rules work the same, except substitution must be some type which can be compared with = You can ignore warnings about “calling polyEqual” Not everything is an equality type (real is not) Tuples / record / lists are equality types if and only if the components are equality types

Syntactic Sugar If-then-else is implemented as syntactic sugar for a case statement

If-then-else We’ve just covered case statements How could we implement if-then-else? case x of true => “apple” | false => “banana” This is equivalent to the if/then/else version In fact, this is how if/then/else is implemented inside SML Which looks nicer? We use “if/then/else” because it’s kinda nice (sugar!!) but it’s not actually necessary given case expressions if x then “apple” else “banana”

Adventures in pattern matching Shape example Function-pattern syntax if we get to it