Correct locations of public_html
[hcoop/domtool2.git] / src / env.sml
CommitLineData
492c1cff
AC
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
dac62e84 17 *)
492c1cff
AC
18
19(* Domtool type-checking and reduction environments *)
20
21structure Env :> ENV = struct
22
23open Ast
24
25structure SS = StringSet
26structure SM = StringMap
27
a3698041
AC
28type typeRule = exp -> bool
29val typeRules : typeRule SM.map ref = ref SM.empty
6be996d4
AC
30fun registerType (name, rule) = typeRules := SM.insert (!typeRules, name, rule)
31fun typeRule name = SM.find (!typeRules, name)
32
a3698041
AC
33type env_vars = exp SM.map
34type action = env_vars * Ast.exp list -> env_vars
35val actions : action SM.map ref = ref SM.empty
36fun registerAction (name, action) = actions := SM.insert (!actions, name, action)
37fun action name = SM.find (!actions, name)
38
39val containers : (action * (unit -> unit)) SM.map ref = ref SM.empty
40fun registerContainer (name, befor, after) =
41 containers := SM.insert (!containers, name, (befor, after))
42fun container name = SM.find (!containers, name)
43
e0b0abd2
AC
44local
45 val pr = ref (fn () => ())
46in
47
48fun registerPre f =
49 let
50 val old = !pr
51 in
52 pr := (fn () => (old (); f ()))
53 end
54fun pre () = !pr ()
55
56end
57
58local
59 val pst = ref (fn () => ())
60in
61
62fun registerPost f =
63 let
64 val old = !pst
65 in
66 pst := (fn () => (old (); f ()))
67 end
68fun post () = !pst ()
69
70end
71
12adf55a
AC
72local
73 val pr = ref (fn () => ())
74in
75
76fun registerPreTycheck f =
77 let
78 val old = !pr
79 in
80 pr := (fn () => (old (); f ()))
81 end
82fun preTycheck () = !pr ()
83
84end
85
629a34f6
AC
86fun badArgs (name, args) =
87 (print ("Invalid arguments to " ^ name ^ "\n");
88 app (fn arg => Print.preface ("Argument: ", Print.p_exp arg)) args;
89 raise Domain)
90fun badArg (func, arg, v) =
91 (print ("Invalid " ^ arg ^ " argument to " ^ func ^ "\n");
92 Print.preface ("Argument: ", Print.p_exp v);
93 raise Domain)
94
95type 'a arg = exp -> 'a option
96
97fun int (EInt n, _) = SOME n
98 | int _ = NONE
99
100fun string (EString s, _) = SOME s
101 | string _ = NONE
102
8a7c40fa
AC
103fun bool (EVar "false", _) = SOME false
104 | bool (EVar "true", _) = SOME true
105 | bool _ = NONE
106
629a34f6
AC
107fun mapFail f [] = SOME []
108 | mapFail f (h :: t) =
109 case f h of
110 NONE => NONE
111 | SOME h' =>
112 case mapFail f t of
113 NONE => NONE
114 | SOME t' => SOME (h' :: t')
115
116fun list f (EList ls, _) = mapFail f ls
117 | list _ _ = NONE
118
ed9fda3a
AC
119fun none func f (_, []) = (f ();
120 SM.empty)
121 | none func _ (_, es) = badArgs (func, es)
122
629a34f6
AC
123fun one func (name, arg) f (_, [e]) =
124 (case arg e of
125 NONE => badArg (func, name, e)
126 | SOME v => (f v;
127 SM.empty))
128 | one func _ _ (_, es) = badArgs (func, es)
129
130fun two func (name1, arg1, name2, arg2) f (_, [e1, e2]) =
131 (case (arg1 e1, arg2 e2) of
132 (NONE, _) => badArg (func, name1, e1)
133 | (_, NONE) => badArg (func, name2, e2)
134 | (SOME v1, SOME v2) => (f (v1, v2);
135 SM.empty))
136 | two func _ _ (_, es) = badArgs (func, es)
137
f8dfbbcc
AC
138fun three func (name1, arg1, name2, arg2, name3, arg3) f (_, [e1, e2, e3]) =
139 (case (arg1 e1, arg2 e2, arg3 e3) of
140 (NONE, _, _) => badArg (func, name1, e1)
141 | (_, NONE, _) => badArg (func, name2, e2)
142 | (_, _, NONE) => badArg (func, name3, e3)
143 | (SOME v1, SOME v2, SOME v3) => (f (v1, v2, v3);
144 SM.empty))
145 | three func _ _ (_, es) = badArgs (func, es)
6ae327f8
AC
146
147fun oneV func (name, arg) f (evs, [e]) =
148 (case arg e of
149 NONE => badArg (func, name, e)
150 | SOME v => (f (evs, v);
151 SM.empty))
152 | oneV func _ _ (_, es) = badArgs (func, es)
153
e0b0abd2
AC
154fun twoV func (name1, arg1, name2, arg2) f (evs, [e1, e2]) =
155 (case (arg1 e1, arg2 e2) of
156 (NONE, _) => badArg (func, name1, e1)
157 | (_, NONE) => badArg (func, name2, e2)
158 | (SOME v1, SOME v2) => (f (evs, v1, v2);
159 SM.empty))
160 | twoV func _ _ (_, es) = badArgs (func, es)
161
6ae327f8
AC
162
163fun env arg (evs, name) =
164 case SM.find (evs, name) of
165 NONE => raise Fail ("Unavailable environment variable " ^ name)
166 | SOME e =>
167 case arg e of
168 NONE => raise Fail ("Bad format for environment variable " ^ name)
169 | SOME v => v
170
629a34f6
AC
171fun type_one func arg f =
172 registerType (func, fn e =>
173 case arg e of
174 NONE => false
175 | SOME v => f v)
176
ed9fda3a 177fun action_none name f = registerAction (name, none name f)
629a34f6
AC
178fun action_one name args f = registerAction (name, one name args f)
179fun action_two name args f = registerAction (name, two name args f)
f8dfbbcc 180fun action_three name args f = registerAction (name, three name args f)
629a34f6 181
e0b0abd2 182fun actionV_none name f = registerAction (name, fn (env, _) => (f env; env))
6ae327f8 183fun actionV_one name args f = registerAction (name, oneV name args f)
e0b0abd2 184fun actionV_two name args f = registerAction (name, twoV name args f)
6ae327f8
AC
185
186fun container_none name (f, g) = registerContainer (name, none name f, g)
629a34f6 187fun container_one name args (f, g) = registerContainer (name, one name args f, g)
a3698041 188
6ae327f8
AC
189fun containerV_one name args (f, g) = registerContainer (name, oneV name args f, g)
190
095de39e
AC
191type env = SS.set * (typ * exp option) SM.map * SS.set
192val empty : env = (SS.empty, SM.empty, SS.empty)
492c1cff 193
095de39e
AC
194fun lookupType (ts, _, _) name = SS.member (ts, name)
195fun lookupVal (_, vs, _) name =
492c1cff
AC
196 case SM.find (vs, name) of
197 NONE => NONE
198 | SOME (t, _) => SOME t
095de39e 199fun lookupEquation (_, vs, _) name =
492c1cff
AC
200 case SM.find (vs, name) of
201 NONE => NONE
202 | SOME (_, eqo) => eqo
095de39e 203fun lookupContext (_, _, cs) name = SS.member (cs, name)
492c1cff 204
095de39e
AC
205fun bindType (ts, vs, cs) name = (SS.add (ts, name), vs, cs)
206fun bindVal (ts, vs, cs) (name, t, eqo) = (ts, SM.insert (vs, name, (t, eqo)), cs)
207fun bindContext (ts, vs, cs) name = (ts, vs, SS.add (cs, name))
208
209fun types (ts, _, _) = ts
210fun vals (_, vs, _) = SM.foldli (fn (name, _, vs) => SS.add (vs, name)) SS.empty vs
211fun contexts (_, _, cs) = cs
492c1cff
AC
212
213end