







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Programming Languages
Monday, April 16, 2012 1
News
Monday, April 16, 2012 2 “Recursive” types
type nat = Zero | Succ of nat
Zero
Succ
Succ Succ
Next: Building datatypes
Next: Lets get cosy with Recursion
Monday, April 16, 2012 5
of_int : int -> nat let if (^) recn <= of_int 0 then n = else^ Zero Succ (of_int (n-1))
Base Expression Inductive Expression
Base pattern Inductive pattern
of_int 1 ====> Succ (of_int 0) ====> Succ (Zero)
of_int 0 ====> Zero
of_int 2 ====> Succ (of_int 1) ====> Succ (Succ (Zero)) Monday, April 16, 2012 6 to_int : nat -> int
|^ let Zero^ rec to_int-> 0 n^ =^ match^ n^ with nductive pattern^ Base pattern^ |^ Succ^ m^ ->^1 +^^ Base Expression to_int^ m Inductive Expression
to_int (Succ Zero) ====> 1 + to_int Zero ====> 1 + 0 ====> 1
to_int Zero ====> 0
to_int (Succ (Succ Zero)) ====> 1 + to_int (Succ Zero) ====> 1 + 1 ====> 2
plus: nat-> nat -> nat
plus (Succ Zero) (Succ (Succ Zero)) ====> Succ (plus Zero (Succ (Succ Zero)) ====> Succ (Succ (Succ Zero))
plus Zero (Succ (Succ Zero)) ====> Succ (Succ Zero)
let rec plus n m = |^ match Zero^ m^ with-> n | Succ m’ -> Succ (plus n m’)
Base Expression Inductive Expression
Base pattern Inductive pattern
Next: Lets get cosy with Recursion
Monday, April 16, 2012 13
Lists are recursive types!
type ‘a list = |^ NilCons of ‘a * ‘a list
Monday, April 16, 2012 14 Lists are recursive types!
type ‘a list = |^ NilCons of ‘a * ‘a list
Nil
Nil
Lists are recursive types!
type ‘a list = |^ NilCons of ‘a * ‘a list
Nil
Cons 3 ,
Cons(3,Nil) Nil
Lists are recursive types!
type ‘a list = |^ NilCons of ‘a * ‘a list
Cons(2,Cons(3,Nil))
Nil
Cons 3 ,
Cons 2 ,
Cons(3,Nil) Nil
Monday, April 16, 2012 17
Lists are recursive types!
type ‘a list = |^ NilCons of ‘a * ‘a list
Nil
Nil
Cons(3,Nil)
Cons 3 ,
Cons(2,Cons(3,Nil)) Cons 2 ,
Cons(1,Cons(2,Cons(3,Nil))) Cons 1 , Monday, April 16, 2012 18 Lists aren’t built-in!
type ‘a list = |^ NilCons of ‘a * ‘a list Next: Lets get cosy with Recursion
(* let val rec mem mem : x‘a ys -> = ‘a list -> bool*) match | [] ys with-> false | y::ys’ -> if x=y then true else mem x ys’
Base Expression Inductive Expression
Base pattern Ind pattern mem 2 (2::[]) ====> true mem 2 (1::2::[]) ====> mem 2 (2::[]) ====> true
Monday, April 16, 2012 25
(* let val rec mem mem : x‘a ys -> = ‘a list -> bool*) match | [] ys with-> false | y::ys’ -> if x=y then true else mem x ys’
Base Expression Inductive Expression
Base pattern Ind pattern mem 4 [] ====> false mem 4 (2::[]) ====> mem 4 [] ====> false mem 4 (1::2::[]) ====> mem 4 (2::[]) ====> false Monday, April 16, 2012 26
(* let val rec insert member : x‘a xs -> = ‘a list -> ‘a list *) Base Expression Inductive Expression
Base pattern Ind pattern
let rec append xs ys = Base Expression Inductive Expression
Base pattern Ind pattern
let rec append xs ys = Base Expression Inductive Expression
Base pattern Ind pattern
append [] (3::4::[]) ====> 3::4::[]
append (1::2::[]) (3::4::[]) ====> 1::2::(3::4::[]) ====> 1::2::3::4::[]
append [] [] ====> [] append (2::[]) (3::4::[]) ====> 2::(3::4::[]) ====> 2::3::4::[]
Monday, April 16, 2012 29
(* let val rec clone clone : nint x =-> ‘a -> ‘a list *)
Monday, April 16, 2012 30
Representing Trees
type | Leaf tree of = | Node of^ inttree*tree
Leaf
Leaf
Node
Node
Leaf
Node(Node(Leaf 1, Leaf 2), Leaf 3)
Monday, April 16, 2012 37
Representing Trees
type | Leaf tree of = | Node of^ inttree*tree
Leaf
Leaf
Node
Node
Leaf
Node(Node(Leaf 1, Leaf 2), Leaf 3)
Monday, April 16, 2012 38
Next: Lets get cosy with Recursion
sum_leaf: tree -> int
“Sum up the leaf values”. E.g.
- : int = 6
type | Leaf tree of = | Node of^ inttree*tree
sum_leaf: tree -> int
let rec sum_leaf t =
Monday, April 16, 2012 41
type | Leaf tree of = | Node of^ inttree*tree
sum_leaf: tree -> int
let rec sum_leaf t =
Base pattern Inductive pattern
Monday, April 16, 2012 42
type | Leaf tree of = | Node of^ inttree*tree
sum_leaf: tree -> int
let rec sum_leaf t = |Leaf^ match nt with-> |Node(t1,t2)->
Base pattern nductive pattern
Base pattern nductive pattern
type | Leaf tree of = | Node of^ inttree*tree
sum_leaf: tree -> int
let rec sum_leaf t = |Leaf^ match nt with-> n |Node(t1,t2)-> sum_leaf^ Base Expression t1 + sum_leaf t Inductive Expression
Base pattern Inductive pattern
Base pattern Inductive pattern
type | Num expr of float= | Add of exprexpr | | SubMul ofof exprexprexpr*expr
Want an arithmetic calculator to evaluate expressions like:
Monday, April 16, 2012 49
Build more funky expressions, evaluate them, to produce:
Monday, April 16, 2012 50
Build more funky expressions, evaluate them, to produce: