adamc@81
|
1 (* Copyright (c) 2008, Adam Chlipala
|
adamc@81
|
2 * All rights reserved.
|
adamc@81
|
3 *
|
adamc@81
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@81
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@81
|
6 *
|
adamc@81
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@81
|
8 * this list of conditions and the following disclaimer.
|
adamc@81
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@81
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@81
|
11 * and/or other materials provided with the distribution.
|
adamc@81
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@81
|
13 * derived from this software without specific prior written permission.
|
adamc@81
|
14 *
|
adamc@81
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@81
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@81
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@81
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@81
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@81
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@81
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@81
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@81
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@81
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@81
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@81
|
26 *)
|
adamc@81
|
27
|
adamc@81
|
28 structure ElabOps :> ELAB_OPS = struct
|
adamc@81
|
29
|
adamc@81
|
30 open Elab
|
adamc@81
|
31
|
adamc@81
|
32 structure E = ElabEnv
|
adamc@81
|
33 structure U = ElabUtil
|
adamc@81
|
34
|
adamc@81
|
35 val liftConInCon = E.liftConInCon
|
adamc@81
|
36
|
adamc@81
|
37 val subConInCon =
|
adamc@81
|
38 U.Con.mapB {kind = fn k => k,
|
adamc@81
|
39 con = fn (xn, rep) => fn c =>
|
adamc@81
|
40 case c of
|
adamc@81
|
41 CRel xn' =>
|
adamc@81
|
42 (case Int.compare (xn', xn) of
|
adamc@81
|
43 EQUAL => #1 rep
|
adamc@81
|
44 | GREATER => CRel (xn' - 1)
|
adamc@81
|
45 | LESS => c)
|
adamc@81
|
46 (*| CUnif _ => raise SynUnif*)
|
adamc@81
|
47 | _ => c,
|
adamc@81
|
48 bind = fn ((xn, rep), U.Con.Rel _) => (xn+1, liftConInCon 0 rep)
|
adamc@81
|
49 | (ctx, _) => ctx}
|
adamc@81
|
50
|
adamc@81
|
51 fun subStrInSgn (m1, m2) =
|
adamc@81
|
52 U.Sgn.map {kind = fn k => k,
|
adamc@81
|
53 con = fn c as CModProj (m1', ms, x) =>
|
adamc@81
|
54 if m1 = m1' then
|
adamc@81
|
55 CModProj (m2, ms, x)
|
adamc@81
|
56 else
|
adamc@81
|
57 c
|
adamc@81
|
58 | c => c,
|
adamc@81
|
59 sgn_item = fn sgi => sgi,
|
adamc@81
|
60 sgn = fn sgn => sgn}
|
adamc@81
|
61
|
adamc@81
|
62
|
adamc@81
|
63 fun hnormCon env (cAll as (c, loc)) =
|
adamc@81
|
64 case c of
|
adamc@81
|
65 CUnif (_, _, _, ref (SOME c)) => hnormCon env c
|
adamc@81
|
66
|
adamc@81
|
67 | CNamed xn =>
|
adamc@81
|
68 (case E.lookupCNamed env xn of
|
adamc@81
|
69 (_, _, SOME c') => hnormCon env c'
|
adamc@81
|
70 | _ => cAll)
|
adamc@81
|
71
|
adamc@81
|
72 | CModProj (n, ms, x) =>
|
adamc@81
|
73 let
|
adamc@81
|
74 val (_, sgn) = E.lookupStrNamed env n
|
adamc@81
|
75 val (str, sgn) = foldl (fn (m, (str, sgn)) =>
|
adamc@81
|
76 case E.projectStr env {sgn = sgn, str = str, field = m} of
|
adamc@81
|
77 NONE => raise Fail "hnormCon: Unknown substructure"
|
adamc@81
|
78 | SOME sgn => ((StrProj (str, m), loc), sgn))
|
adamc@81
|
79 ((StrVar n, loc), sgn) ms
|
adamc@81
|
80 in
|
adamc@81
|
81 case E.projectCon env {sgn = sgn, str = str, field = x} of
|
adamc@81
|
82 NONE => raise Fail "kindof: Unknown con in structure"
|
adamc@81
|
83 | SOME (_, NONE) => cAll
|
adamc@81
|
84 | SOME (_, SOME c) => hnormCon env c
|
adamc@81
|
85 end
|
adamc@81
|
86
|
adamc@81
|
87 | CApp (c1, c2) =>
|
adamc@81
|
88 (case #1 (hnormCon env c1) of
|
adamc@81
|
89 CAbs (x, k, cb) =>
|
adamc@81
|
90 let
|
adamc@81
|
91 val sc = (hnormCon env (subConInCon (0, c2) cb))
|
adamc@81
|
92 handle SynUnif => cAll
|
adamc@81
|
93 (*val env' = E.pushCRel env x k*)
|
adamc@81
|
94 in
|
adamc@328
|
95 (*Print.eprefaces "Subst" [("x", Print.PD.string x),
|
adamc@328
|
96 ("cb", ElabPrint.p_con env' cb),
|
adamc@328
|
97 ("c2", ElabPrint.p_con env c2),
|
adamc@328
|
98 ("sc", ElabPrint.p_con env sc)];*)
|
adamc@81
|
99 sc
|
adamc@81
|
100 end
|
adamc@326
|
101 | c1' as CApp (c', i) =>
|
adamc@326
|
102 let
|
adamc@326
|
103 fun default () = (CApp ((c1', loc), hnormCon env c2), loc)
|
adamc@326
|
104 in
|
adamc@326
|
105 case #1 (hnormCon env c') of
|
adamc@326
|
106 CApp (c', f) =>
|
adamc@326
|
107 (case #1 (hnormCon env c') of
|
adamc@326
|
108 CFold ks =>
|
adamc@326
|
109 (case #1 (hnormCon env c2) of
|
adamc@326
|
110 CRecord (_, []) => hnormCon env i
|
adamc@326
|
111 | CRecord (k, (x, c) :: rest) =>
|
adamc@326
|
112 hnormCon env
|
adamc@326
|
113 (CApp ((CApp ((CApp (f, x), loc), c), loc),
|
adamc@81
|
114 (CApp ((CApp ((CApp ((CFold ks, loc), f), loc), i), loc),
|
adamc@326
|
115 (CRecord (k, rest), loc)), loc)), loc)
|
adamc@326
|
116 | CConcat ((CRecord (k, (x, c) :: rest), _), rest') =>
|
adamc@326
|
117 let
|
adamc@326
|
118 val rest'' = (CConcat ((CRecord (k, rest), loc), rest'), loc)
|
adamc@81
|
119
|
adamc@326
|
120 (*val ccc = (CApp ((CApp ((CApp (f, x), loc), c), loc),
|
adamc@326
|
121 (CApp ((CApp ((CApp ((CFold ks, loc), f), loc), i), loc),
|
adamc@326
|
122 rest''), loc)), loc)*)
|
adamc@326
|
123 in
|
adamc@326
|
124 (*eprefaces "Red to" [("ccc", p_con env ccc), ("ccc'", p_con env (hnormCon env ccc))];*)
|
adamc@326
|
125 hnormCon env
|
adamc@326
|
126 (CApp ((CApp ((CApp (f, x), loc), c), loc),
|
adamc@81
|
127 (CApp ((CApp ((CApp ((CFold ks, loc), f), loc), i), loc),
|
adamc@326
|
128 rest''), loc)), loc)
|
adamc@326
|
129 end
|
adamc@329
|
130 | _ =>
|
adamc@329
|
131 let
|
adamc@329
|
132 fun cunif () =
|
adamc@329
|
133 let
|
adamc@329
|
134 val r = ref NONE
|
adamc@329
|
135 in
|
adamc@329
|
136 (r, (CUnif (loc, (KType, loc), "_", r), loc))
|
adamc@329
|
137 end
|
adamc@329
|
138
|
adamc@329
|
139 val (nmR, nm) = cunif ()
|
adamc@329
|
140 val (vR, v) = cunif ()
|
adamc@329
|
141 val (rR, r) = cunif ()
|
adamc@329
|
142
|
adamc@329
|
143 val c = f
|
adamc@329
|
144 val c = (CApp (c, nm), loc)
|
adamc@329
|
145 val c = (CApp (c, v), loc)
|
adamc@329
|
146 val c = (CApp (c, r), loc)
|
adamc@329
|
147 fun unconstraint c =
|
adamc@329
|
148 case hnormCon env c of
|
adamc@329
|
149 (CDisjoint (_, _, c), _) => unconstraint c
|
adamc@329
|
150 | c => c
|
adamc@329
|
151 val c = unconstraint c
|
adamc@329
|
152 in
|
adamc@329
|
153 (*Print.prefaces "Consider" [("c", ElabPrint.p_con env c)];*)
|
adamc@329
|
154 case (hnormCon env i, unconstraint c) of
|
adamc@329
|
155 ((CRecord (_, []), _),
|
adamc@329
|
156 (CConcat (c1, c2'), _)) =>
|
adamc@329
|
157 (case (hnormCon env c1, hnormCon env c2') of
|
adamc@329
|
158 ((CRecord (_, [(nm', v')]), _),
|
adamc@329
|
159 (CUnif (_, _, _, rR'), _)) =>
|
adamc@329
|
160 (case (hnormCon env nm', hnormCon env v') of
|
adamc@329
|
161 ((CUnif (_, _, _, nmR'), _),
|
adamc@329
|
162 (CUnif (_, _, _, vR'), _)) =>
|
adamc@329
|
163 if nmR' = nmR andalso vR' = vR andalso rR' = rR then
|
adamc@329
|
164 hnormCon env c2
|
adamc@329
|
165 else
|
adamc@329
|
166 default ()
|
adamc@329
|
167 | _ => default ())
|
adamc@329
|
168 | _ => default ())
|
adamc@329
|
169 | _ => default ()
|
adamc@329
|
170 end)
|
adamc@326
|
171 | _ => default ())
|
adamc@326
|
172 | _ => default ()
|
adamc@326
|
173 end
|
adamc@326
|
174 | c1' => (CApp ((c1', loc), hnormCon env c2), loc))
|
adamc@81
|
175
|
adamc@81
|
176 | CConcat (c1, c2) =>
|
adamc@81
|
177 (case (hnormCon env c1, hnormCon env c2) of
|
adamc@81
|
178 ((CRecord (k, xcs1), loc), (CRecord (_, xcs2), _)) =>
|
adamc@81
|
179 (CRecord (k, xcs1 @ xcs2), loc)
|
adamc@81
|
180 | ((CRecord (_, []), _), c2') => c2'
|
adamc@81
|
181 | ((CConcat (c11, c12), loc), c2') =>
|
adamc@81
|
182 hnormCon env (CConcat (c11, (CConcat (c12, c2'), loc)), loc)
|
adamc@209
|
183 | (c1', (CRecord (_, []), _)) => c1'
|
adamc@329
|
184 | (c1', c2') => (CConcat (c1', c2'), loc))
|
adamc@81
|
185
|
adamc@207
|
186 | CProj (c, n) =>
|
adamc@207
|
187 (case hnormCon env c of
|
adamc@207
|
188 (CTuple cs, _) => hnormCon env (List.nth (cs, n - 1))
|
adamc@207
|
189 | _ => cAll)
|
adamc@207
|
190
|
adamc@81
|
191 | _ => cAll
|
adamc@81
|
192
|
adamc@81
|
193 end
|