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
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 462:
Warning 8: this patternmatching is not exhaustive.
Here is an example of a case that is not matched:
((Int _, Bool _)(Bool _, _))
File "[2]", line 9, characters 462:
Warning 8: this patternmatching is not exhaustive.
Here is an example of a case that is not matched:
((Int _, Bool _)(Bool _, _))
File "[2]", line 12, characters 461:
Warning 8: this patternmatching 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
In [3]:
We need
Types
Welltyped 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 1756
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
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)
In [7]:
Benefit of phantom types
We no longer allow illtyped 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 519:
Error: This expression has type bool expr
but an expression was expected of type int expr
You've reached the end of your free preview.
Want to read all 40 pages?
 Fall '19