Skip to content

Instantly share code, notes, and snippets.

@aaronyoo
Created January 17, 2020 18:44
Show Gist options
  • Save aaronyoo/576cd8f9e3dc3d19ebd7878d91c29a48 to your computer and use it in GitHub Desktop.
Save aaronyoo/576cd8f9e3dc3d19ebd7878d91c29a48 to your computer and use it in GitHub Desktop.
exception ImplementMe
type t = True | False | If of t * t * t | Int of int | Plus of t * t | Greater of t * t
let isval t =
match t with
True|False -> true
| Int _ -> true
| _ -> false
(* Problem 1a. *)
exception NormalForm
let rec step t =
match t with
| If(True, t2, t3) -> t2
| If(False, t2, t3) -> t3
| If(t1, t2, t3) -> If(step t1, t2, t3)
| Plus (t1, t2) ->
(match (isval t1, isval t2) with
| (false, _) -> Plus (step t1, t2)
| (true, false) -> Plus (t1, step t2)
| (true, true) ->
(match (t1, t2) with
| (Int i1, Int i2) -> Int(i1 + i2)
| _ -> raise NormalForm))
| Greater (t1, t2) ->
(match (isval(t1), isval(t2)) with
| (false, _) -> Greater (step t1, t2)
| (true, false) -> Greater (t1, step t2)
| (true, true) ->
(match (t1, t2) with
| (Int i1, Int i2) -> if i1 > i2 then True else False
| _ -> raise NormalForm))
| _ -> raise NormalForm
(* Problem 1b. *)
(*let rec eval t = raise ImplementMe*)
step True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment