1 / 11

Algebraic Types: Define and Use Your Own Types

Learn the motivation behind defining new types, revisit type domains, and understand constructors and usage of algebraic types. Also, learn how algebraic types differ from type synonyms.

hcrawford
Download Presentation

Algebraic Types: Define and Use Your Own Types

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. GC16/3011 Functional ProgrammingLecture 11Algebraic Types

  2. Contents • Motivation: why do we need more types? • Type domains revisited • Define your own types! • Constructors • Usage • Different from type synonyms!

  3. Motivation • Types seen so far: char, num, bool, *, (), [], -> • Why do we need more types? • Readability • Built-in validation • Example: if a type called “dice” has legal values from 1 to 6 only, then: • f :: dice -> bool is more informative • A value of 7 will be detected as a type error

  4. Type Domains Revisited • The type bool has just two legal values: True and False • The type [char] has potentially infinite legal values: [], [‘A’], [‘B’], [‘h’,’e’,’l’,’l’,’o’] etc • A type is defined by the legal values permitted for that type • Pattern-matching allows equality checks against these legal values

  5. Define your own Types • mybool ::= Mytrue | Myfalse ­­ MUST use capitals!!!!! (the set of legal values for this “algebraic type”) • f :: bool -> num f True = 34 f any = 13 • g :: mybool -> num g Mytrue = 34 g any = 13

  6. Constructors • wrong_dice ::= 1 | 2 | 3 | 4 | 5 | 6 • also_wrong_dice ::= “one” | “two” | “three” | “four” | “five” | “six” • dice ::= One | Two | Three | Four | Five | Six • Constructors: start with a capital letter • Built-in type bool adheres to this rule • Built-in types num and char break the rule (for convenience)

  7. Constructors (2) • Each legal value must have a constructor • Each legal value may also have some extra data (if so, we specify its type following the constructor): fluid ::= Gallons num | Litres num tanklevel ::= Emptytank | Fulltank | Parttank fluid x :: tanklevel x = Parttank (Gallons 3)

  8. Usage coord3D ::= Coord (num, num, num) origin::coord3D origin = Coord (0, 0, 0) midpoint:: coord3D -> coord3D -> coord3D midpoint (Coord (x1, y1, z1)) (Coord (x2, y2, z2)) = Coord ( (x1 + x2)/2, (y1+y2)/2, (z1+z2)/2 )

  9. Usage (2) studentdata ::= StudentByname [char] num | StudentByregid num num topmarks :: [studentdata] -> [num] topmarks [] = [] topmarks ((StudentByname any x) : rest) = (x : (topmarks rest)), if x>=70 = topmarks rest, otherwise topmarks ((StudentByregid id x) : rest) = (x : (topmarks rest)), if x >= 70 = topmarks rest, otherwise

  10. Different from type synonyms syncoord == (num, num, num) x::syncoord x = (3, 4, 16) coord3D ::= Coord (num, num, num) y::coord3D y = Coord (3, 4, 16)

  11. Summary • Motivation: why do we need more types? • Type domains revisited • Define your own types! • Constructors • Usage • Different from type synonyms

More Related