annotate lab2/dict.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab2/dict.ml *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 (* Environments are implemented using a library module that
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 represents mappings by balanced binary trees. *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 type ident = string
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 type ptype =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 Integer
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 | Boolean
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 | Array of int * ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 | Void
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 (* |def| -- definitions in environment *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 type def =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 { d_tag: ident; (* Name *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 d_type: ptype; (* Type *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 d_lab: string } (* Global label *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 module IdMap = Map.Make(struct type t = ident let compare = compare end)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 type environment = Env of def IdMap.t
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 let can f x = try f x; true with Not_found -> false
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 (* |define| -- add a definition *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 let define d (Env e) =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 if can (IdMap.find d.d_tag) e then raise Exit;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 Env (IdMap.add d.d_tag d e)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 (* |lookup| -- find definition of an identifier *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 let lookup x (Env e) = IdMap.find x e
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 (* |init_env| -- empty environment *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 let init_env = Env IdMap.empty