On this page:
8.1 Pyret to OCaml Reference
6.1
8 OCaml, Type Checking, and Type Inference

In this minor lab, you’re going to learn enough OCaml to implement a (simplified) version of basic-interp, with single-armed let defined via substitution. The goal isn’t just to produce an interpreter; you already know you can do that. The point is that along the way, you’ll see the kinds of type errors that OCaml reports, and see what errors could still occur once your program runs.

Here’s the starter file:

interp-subst.ml

You can run it with:

$ ocaml interp-subst.ml

As you work through implementing the interpreter, understand and keep track of the interesting errors you get (as in, copy-paste them into a separate file). Which ones stopped the program from running, which were just warnings, and which were runtime errors? What runtime errors are left that are possible when the program is running?

At some point (when you’re done, or when you want to pause), try removing all the annotations from the function headers, and re-run the program. If it runs fine, introduce a type error, leave the annotations off, and re-run it. Do you still get errors? Play with removing some, but not all, annotations. What is OCaml doing?

8.1 Pyret to OCaml Reference

This page is a quick reference of expressions in Pyret and OCaml that correspond.

Matching

Pyret:

cases(Expr) e: | e-num(n) => ... | e-bool(b) => ... | e-let(name, expr, body) => ... end

OCaml:

match e with

  | ENum(n) => ...

  | EBool(b) => ...

  | ELet(name, expr, body) => ...

Equality

The e1 == e2 expression in Pyret is e1 = e2 in OCaml

Binding Names

Pyret:

x = 5 y = 10 x + y

OCaml:

let x = 5 in

let y = 10 in

x + y

Calling Functions

Pyret:

f(x, y, z)

OCaml:

(f x y z)

Conditionals

Pyret:

if x == 0: ... else: ... end

OCaml:

if x = 0 then ... else ...

Function header

Pyret (with annotations):

fun f(arg :: Ann1, arg2 :: Ann2) -> ReturnAnn: ... end

OCaml (with annotations):

let rec f (arg : Ann1) (arg2 : Ann2) : ReturnAnn =

  ...

Pyret (without annotations):

fun f(arg, arg2): ... end

OCaml (without annotations):

let rec f arg arg2 =

  ...