OCaml homeworks for CS 456: Programming Language.
AST functions, constant fold, and typeCheck and interp for a simple language with
- Constants / variables
- Binary operators (
+,-,/,*,<,>,=,&&,||) - If expressions
if ... then ... else ...
typeCheck and interp functions for a simple language with
- Constants / variables
- Binary operators (
+,-,/,*,<,>,=,&&,||) - If expressions
if ... then ... else ... - Bind expressions
let x = ... in ... - Sequencing
...; ... - References / deref
let x = ref ... in *x - Assignment
x := ... - While loops
while ... do ... - Print statements
typeCheck and interp functions for a simple language with
- Constants / variables
- Binary operators (
+,-,/,*,<,>,=,&&,||) - If expressions
if ... then ... else ... - Bind expressions
let x = ... in ... - Function binding
let sum (x: int, y: int) -> x + y in ... - Recursive function binding
letrec fact(n : int) : int -> if (n = 0) then 1 else n * fact(n - 1) in ... - Function calls
sum(...)
typeCheck and interp functions (implemented 2 ways: closures / environments, and eager substitution)
for a simple language with
- Constants / variables
- Binary operators (
+,-,/,*,<,>,=,&&,||) - If expressions
if ... then ... else ... - Bind expressions
let x = ... in ... - Function expressions
fun (x: int, y: int) => ... - Recursive function binding
letrec fact(n : int) : int -> if (n = 0) then 1 else n * fact(n - 1) in ... - Function calls
sum(...)
unify and infer functions implementing a type-inference algorithm for a simple language (same constructs as above):
false (* bool *)
let x = 3 in x + 4 (* int *)
let f = fun(x) => x in f (* X -> X *)
let f = fun(x) => x in f(3) (* int *)
let f = fun(x) => x in f(true) (* bool *)
let f = fun(x) => x + 3 in f (* int -> int *)
let f = fun(x, y, z) => if x then y else z in f (* (bool, X, X) -> X *)
let x = fun(f, x) => f(x) in x (* (X -> Y, X) -> Y *)
let y = 10 in let x = fun(f, z) => f(y) in x (* (int -> X, Y) -> X *)
...