-
Notifications
You must be signed in to change notification settings - Fork 1
/
bind.sml
75 lines (58 loc) · 2.44 KB
/
bind.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
structure Bind = struct
open Util
infixr 0 $
(* ['namespace] is just a tag to differentiate different bind types *)
datatype ('namespace, 'body) bind = Bind of 'body
(* a series of dependent binds ({name1 : classifier1} {name2 : classifier2} {name3 : classifier3}, inner) *)
datatype ('namespace, 'classifier, 'name, 'inner) binds =
BindNil of 'inner
| BindCons of 'classifier * ('namespace, 'name * ('namespace, 'classifier, 'name, 'inner) binds) bind
fun unBind (Bind a) = a
fun unfold_binds binds =
case binds of
BindNil inner => ([], inner)
| BindCons (classifier, Bind (name, binds)) =>
let val (name_classifiers, inner) = unfold_binds binds
in
((name, classifier) :: name_classifiers, inner)
end
fun fold_binds (binds, inner) =
foldr (fn ((name, classifier), binds) => BindCons (classifier, Bind (name, binds))) (BindNil inner) binds
fun binds_length binds = length $ fst $ unfold_binds binds
datatype idx_namespace = IdxNS
datatype type_namespace = TypeNS
type 'body ibind = (idx_namespace, 'body) bind
type 'body tbind = (type_namespace, 'body) bind
type ('classifier, 'name, 'inner) ibinds = (idx_namespace, 'classifier, 'name, 'inner) binds
type ('classifier, 'name, 'inner) tbinds = (type_namespace, 'classifier, 'name, 'inner) binds
fun visit_bind extend f env data =
let
val Bind (name, t) = data
val (env, name) = extend env name
val t = f env t
in
Bind (name, t)
end
end
(*
structure ExprUtil = struct
open Util
infixr 0 $
datatype 'a ibind = BindI of 'a
(* for a series of sorting binds ({name1 : anno1} {name2 : anno2} {name3 : anno3}, inner) *)
datatype ('anno, 'name, 'inner) ibinds =
NilIB of 'inner
| ConsIB of 'anno * ('name * ('anno, 'name, 'inner) ibinds) ibind
fun unfold_ibinds ibinds =
case ibinds of
NilIB inner => ([], inner)
| ConsIB (anno, BindI (name, ibinds)) =>
let val (name_annos, inner) = unfold_ibinds ibinds
in
((name, anno) :: name_annos, inner)
end
fun fold_ibinds (binds, inner) =
foldr (fn ((name, anno), ibinds) => ConsIB (anno, BindI (name, ibinds))) (NilIB inner) binds
fun ibinds_length ibinds = length $ fst $ unfold_ibinds ibinds
end
*)