Mercurial > urweb
comparison src/core_env.sml @ 16:bc7b76ca57e0
Conversion to Core
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sun, 08 Jun 2008 13:59:29 -0400 |
parents | |
children | 1ab48e37d0ef |
comparison
equal
deleted
inserted
replaced
15:1e645beb3f3b | 16:bc7b76ca57e0 |
---|---|
1 (* Copyright (c) 2008, Adam Chlipala | |
2 * All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are met: | |
6 * | |
7 * - Redistributions of source code must retain the above copyright notice, | |
8 * this list of conditions and the following disclaimer. | |
9 * - Redistributions in binary form must reproduce the above copyright notice, | |
10 * this list of conditions and the following disclaimer in the documentation | |
11 * and/or other materials provided with the distribution. | |
12 * - The names of contributors may not be used to endorse or promote products | |
13 * derived from this software without specific prior written permission. | |
14 * | |
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 * POSSIBILITY OF SUCH DAMAGE. | |
26 *) | |
27 | |
28 structure CoreEnv :> CORE_ENV = struct | |
29 | |
30 open Core | |
31 | |
32 structure U = CoreUtil | |
33 | |
34 structure IM = IntBinaryMap | |
35 | |
36 | |
37 (* AST utility functions *) | |
38 | |
39 val liftConInCon = | |
40 U.Con.mapB {kind = fn k => k, | |
41 con = fn bound => fn c => | |
42 case c of | |
43 CRel xn => | |
44 if xn < bound then | |
45 c | |
46 else | |
47 CRel (xn + 1) | |
48 | _ => c, | |
49 bind = fn (bound, U.Con.Rel _) => bound + 1 | |
50 | (bound, _) => bound} | |
51 | |
52 val lift = liftConInCon 0 | |
53 | |
54 | |
55 (* Back to environments *) | |
56 | |
57 exception UnboundRel of int | |
58 exception UnboundNamed of int | |
59 | |
60 type env = { | |
61 relC : (string * kind) list, | |
62 namedC : (string * kind * con option) IM.map, | |
63 | |
64 relE : (string * con) list, | |
65 namedE : (string * con) IM.map | |
66 } | |
67 | |
68 val empty = { | |
69 relC = [], | |
70 namedC = IM.empty, | |
71 | |
72 relE = [], | |
73 namedE = IM.empty | |
74 } | |
75 | |
76 fun pushCRel (env : env) x k = | |
77 {relC = (x, k) :: #relC env, | |
78 namedC = IM.map (fn (x, k, co) => (x, k, Option.map lift co)) (#namedC env), | |
79 | |
80 relE = map (fn (x, c) => (x, lift c)) (#relE env), | |
81 namedE = IM.map (fn (x, c) => (x, lift c)) (#namedE env)} | |
82 | |
83 fun lookupCRel (env : env) n = | |
84 (List.nth (#relC env, n)) | |
85 handle Subscript => raise UnboundRel n | |
86 | |
87 fun pushCNamed (env : env) x n k co = | |
88 {relC = #relC env, | |
89 namedC = IM.insert (#namedC env, n, (x, k, co)), | |
90 | |
91 relE = #relE env, | |
92 namedE = #namedE env} | |
93 | |
94 fun lookupCNamed (env : env) n = | |
95 case IM.find (#namedC env, n) of | |
96 NONE => raise UnboundNamed n | |
97 | SOME x => x | |
98 | |
99 fun pushERel (env : env) x t = | |
100 {relC = #relC env, | |
101 namedC = #namedC env, | |
102 | |
103 relE = (x, t) :: #relE env, | |
104 namedE = #namedE env} | |
105 | |
106 fun lookupERel (env : env) n = | |
107 (List.nth (#relE env, n)) | |
108 handle Subscript => raise UnboundRel n | |
109 | |
110 fun pushENamed (env : env) x n t = | |
111 {relC = #relC env, | |
112 namedC = #namedC env, | |
113 | |
114 relE = #relE env, | |
115 namedE = IM.insert (#namedE env, n, (x, t))} | |
116 | |
117 fun lookupENamed (env : env) n = | |
118 case IM.find (#namedE env, n) of | |
119 NONE => raise UnboundNamed n | |
120 | SOME x => x | |
121 | |
122 fun declBinds env (d, _) = | |
123 case d of | |
124 DCon (x, n, k, c) => pushCNamed env x n k (SOME c) | |
125 | DVal (x, n, t, _) => pushENamed env x n t | |
126 | |
127 val ktype = (KType, ErrorMsg.dummySpan) | |
128 | |
129 fun bbind env x = | |
130 case ElabEnv.lookupC ElabEnv.basis x of | |
131 ElabEnv.NotBound => raise Fail "CoreEnv.bbind: Not bound" | |
132 | ElabEnv.Rel _ => raise Fail "CoreEnv.bbind: Rel" | |
133 | ElabEnv.Named (n, _) => pushCNamed env x n ktype NONE | |
134 | |
135 val basis = empty | |
136 val basis = bbind basis "int" | |
137 val basis = bbind basis "float" | |
138 val basis = bbind basis "string" | |
139 | |
140 end |