lec16.pdf - Generalized Algebraic Data Types CS3100 Fall 2019 Simple language Consider this simple language of integers and booleans In[1 type value = |

lec16.pdf - Generalized Algebraic Data Types CS3100 Fall...

This preview shows page 1 - 6 out of 40 pages.

Generalized Algebraic Data Types CS3100 Fall 2019 Simple language Consider this simple language of integers and booleans In [1]: Evaluator for the simple language We can write a simple evaluator for this language Out[1]: type value = Int of int | Bool of bool Out[1]: type expr = Val of value | Plus of expr * expr | Mult of expr * expr | Ite of expr * expr * expr type value = | Int of int | Bool of bool type expr = | Val of value | Plus of expr * expr | Mult of expr * expr | Ite of expr * expr * expr
Image of page 1
In [2]: Evaluator for the simple language The compiler warns that programs such as true + 10 is not handled. Our evaluator gets stuck when it encouters such an expression. File "[2]", line 6, characters 4-62: Warning 8: this pattern-matching is not exhaustive. Here is an example of a case that is not matched: ((Int _, Bool _)|(Bool _, _)) File "[2]", line 9, characters 4-62: Warning 8: this pattern-matching is not exhaustive. Here is an example of a case that is not matched: ((Int _, Bool _)|(Bool _, _)) File "[2]", line 12, characters 4-61: Warning 8: this pattern-matching is not exhaustive. Here is an example of a case that is not matched: Int _ Out[2]: val eval : expr -> value = <fun> let rec eval : expr -> value = fun e -> match e with | Val (Int i) -> Int i | Val (Bool i) -> Bool i | Plus (e1, e2) -> let Int i1, Int i2 = eval e1, eval e2 in Int (i1 + i2) | Mult (e1, e2) -> let Int i1, Int i2 = eval e1, eval e2 in Int (i1 * i2) | Ite (p,e1,e2) -> let Bool b = eval p in if b then eval e1 else eval e2
Image of page 2
In [3]: We need Types Well-typed programs do not get stuck! Phantom types We can add types to our values using a technique called phantom types In [4]: Observe that 'a only appears on the LHS. This 'a is called a phantom type variable. What is this useful for? Typed expression language We can add types to our expression language now using phantom type Exception: Match_failure ("[2]", 6, 4). Called from file "toplevel/toploop.ml", line 180, character s 17-56 Out[4]: type 'a value = Int of int | Bool of bool eval @@ Plus (Val (Bool true ), Val (Int 10 )) type 'a value = | Int of int | Bool of bool
Image of page 3
In [5]: Typed expression language Assign concerte type to the phantom type variable 'a . In [6]: Benefit of phantom types Out[5]: type 'a expr = Val of 'a value | Plus of int expr * int expr | Mult of int expr * int expr | Ite of bool expr * 'a expr * 'a expr Out[6]: val mk_int : int -> int expr = <fun> Out[6]: val mk_bool : bool -> bool expr = <fun> Out[6]: val plus : int expr -> int expr -> int expr = <fun> Out[6]: val mult : int expr -> int expr -> int expr = <fun> type 'a expr = | Val of 'a value | Plus of int expr * int expr | Mult of int expr * int expr | Ite of bool expr * 'a expr * 'a expr ( * Quiz: What types are inferred without type annotations? * ) let mk_int i : int expr = Val (Int i) let mk_bool b : bool expr = Val (Bool b) let plus e1 e2 : int expr = Plus (e1, e2) let mult e1 e2 : int expr = Mult (e1, e2)
Image of page 4
In [7]: Benefit of phantom types We no longer allow ill-typed expression if we use the helper functions. In [8]: Out[7]: val i : 'a expr = Val (Int 0) Out[7]: val i' : int expr = Val (Int 0) Out[7]: val b : 'a expr = Val (Bool true) Out[7]: val b' : bool expr = Val (Bool true) Out[7]: val p : 'a expr = Plus (Val (Int 0), Val (Int 0)) Out[7]: val p' : int expr = Plus (Val (Int 0), Val (Int 0)) File "[8]", line 1, characters 5-19: Error: This expression has type bool expr but an expression was expected of type int expr
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 40 pages?

  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors