2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
22 module BE = BrgEnvironment
25 e: B.lenv; (* environment *)
26 s: (B.lenv * B.term) list; (* stack *)
28 n: int option; (* expected type iterations *)
31 type message = (rtm, B.term) L.message
33 (* Internal functions *******************************************************)
37 let sublevel = succ level
40 let s1, s2 = s ^ " in the environment", "the term" in
41 L.log st BO.specs (pred level) (L.et_items1 s1 c s2 t)
43 let log2 st s cu u ct t =
44 let s1, s2, s3 = s ^ " in the environment (expected)", "the term", "and in the environment (inferred)" in
45 L.log st BO.specs (pred level) (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
47 let rec list_and map = function
48 | hd1 :: tl1, hd2 :: tl2 ->
49 if map hd1 hd2 then list_and map (tl1, tl2) else false
55 let are_alpha_convertible err f t1 t2 =
56 let rec aux f = function
57 | B.Sort (_, p1), B.Sort (_, p2)
58 | B.LRef (_, p1), B.LRef (_, p2) ->
59 if p1 = p2 then f () else err ()
60 | B.GRef (_, u1), B.GRef (_, u2) ->
61 if U.eq u1 u2 then f () else err ()
62 | B.Cast (_, v1, t1), B.Cast (_, v2, t2)
63 | B.Appl (_, _, v1, t1), B.Appl (_, _, v2, t2) ->
64 let f _ = aux f (t1, t2) in
66 | B.Bind (_, b1, t1), B.Bind (_, b2, t2) ->
67 let f _ = aux f (t1, t2) in
70 and aux_bind f = function
71 | B.Abbr v1, B.Abbr v2 -> aux f (v1, v2)
72 | B.Abst (r1, n1, v1), B.Abst (r2, n2, v2) when r1 = r2 && n1 = n2 -> aux f (v1, v2)
73 | B.Void, B.Void -> f ()
76 if S.eq t1 t2 then f () else aux f (t1, t2)
78 let assert_tstep m vo = match m.n with
82 let tstep m = match m.n with
83 | Some n -> {m with n = Some (pred n)}
86 let tsteps m = match m.n with
91 let _, c, a, b = B.get m.e i in c, a, b
95 if !G.ct >= sublevel then
96 log1 st (Printf.sprintf "entering R.step: l=%u, n=%s," m.l (match m.n with Some n -> string_of_int n | None -> "infinite")) m.e r;
99 if assert_tstep m false then
100 step st (tstep m) (B.Sort (a, H.apply h))
103 begin match BE.get_entity uri with
104 | _, a, _, E.Abbr v ->
105 m, B.gref a uri, Some v
106 | _, _, _, E.Abst w ->
107 if assert_tstep m true then begin
108 if !G.summary then O.add ~grt:1 ();
116 begin match get m i with
118 if !G.summary then O.add ~ldelta:1 ();
119 step st {m with e = c} v
120 | c, a, B.Abst (_, _, w) ->
121 if assert_tstep m true then begin
122 if !G.summary then O.add ~lrt:1 ();
123 step st {(tstep m) with e = c} w
129 | B.Cast (_, u, t) ->
130 if assert_tstep m false then begin
131 if !G.summary then O.add ~e:1 ();
134 if !G.summary then O.add ~epsilon:1 ();
137 | B.Appl (_, _, v, t) ->
138 step st {m with s = (m.e, v) :: m.s} t
139 | B.Bind (a, B.Abst (false, n, w), t) ->
141 if !G.summary then O.add ~x:i ();
142 let n = if i = 0 then n else N.minus st n i in
143 let r = B.Bind (a, B.Abst (true, n, w), t) in
145 | B.Bind (a, B.Abst (true, n, w), t) ->
146 if !G.si || N.is_not_zero st n then begin match m.s with
148 m, B.Bind (a, B.Abst (true, n, w), t), None
151 if !G.cc && not (N.assert_not_zero st n) then assert false;
153 if !G.summary then O.add ~beta:1 ~theta:(List.length s) ();
154 let v = B.Cast (E.empty_node, w, v) in
155 let e = B.push m.e c a (B.abbr v) in
156 step st {m with e = e; s = s} t
158 if !G.summary then O.add ~upsilon:1 ();
159 let e = B.push m.e m.e a B.Void in (**) (* this is wrong in general *)
160 step st {m with e = e} t
162 | B.Bind (a, b, t) ->
163 if !G.summary then O.add ~theta:(List.length m.s) ();
164 let e = B.push m.e m.e a b in
165 step st {m with e = e} t
167 let assert_iterations m1 m2 =
170 let reset m ?(e=m.e) n =
171 {m with e = e; n = n; s = []}
174 let a, l = match b with
175 | B.Abst _ -> {a with E.n_apix = m.l}, succ m.l
178 let e = B.push m.e m.e a b in
179 {m with e = e; l = l}
181 let rec ac_nfs st (m1, t1, r1) (m2, t2, r2) =
182 if !G.ct >= level then log2 st "Now converting nfs" m1.e t1 m2.e t2;
183 match t1, r1, t2, r2 with
184 | B.Sort (_, h1), _, B.Sort (_, h2), _ ->
186 | B.LRef ({E.n_apix = e1}, _), _,
187 B.LRef ({E.n_apix = e2}, _), _ ->
188 if e1 = e2 then ac_stacks st m1 m2 else false
189 | B.GRef (_, u1), None, B.GRef (_, u2), None ->
190 if U.eq u1 u2 && assert_iterations m1 m2 then ac_stacks st m1 m2 else false
191 | B.GRef ({E.n_apix = e1}, u1), Some v1,
192 B.GRef ({E.n_apix = e2}, u2), Some v2 ->
193 if e1 < e2 then begin
194 if !G.summary then O.add ~gdelta:1 ();
195 ac_nfs st (m1, t1, r1) (step st m2 v2)
196 end else if e2 < e1 then begin
197 if !G.summary then O.add ~gdelta:1 ();
198 ac_nfs st (step st m1 v1) (m2, t2, r2)
199 end else if U.eq u1 u2 && assert_iterations m1 m2 && ac_stacks st m1 m2 then true
201 if !G.summary then O.add ~gdelta:2 ();
204 | _, _, B.GRef _, Some v2 ->
205 if !G.summary then O.add ~gdelta:1 ();
206 ac_nfs st (m1, t1, r1) (step st m2 v2)
207 | B.GRef _, Some v1, _, _ ->
208 if !G.summary then O.add ~gdelta:1 ();
209 ac_nfs st (step st m1 v1) (m2, t2, r2)
210 | B.Bind (a1, (B.Abst (true, n1, w1) as b1), t1), _,
211 B.Bind (a2, (B.Abst (true, n2, w2) as b2), t2), _ ->
212 if ((!G.cc && N.assert_equal st n1 n2) || N.are_equal st n1 n2) &&
213 ac st (reset m1 zero) w1 (reset m2 zero) w2
214 then ac st (push m1 a1 b1) t1 (push m2 a2 b2) t2
216 | B.Sort _, _, B.Bind (a, B.Abst (true, n, _), t), _ ->
218 if !G.cc && not (N.assert_zero st n) then false else begin
219 if !G.summary then O.add ~upsilon:1 ();
220 ac st (push m1 a B.Void) t1 (push m2 a B.Void) t end
224 and ac st m1 t1 m2 t2 =
225 (* L.warn "entering R.are_convertible"; *)
226 ac_nfs st (step st m1 t1) (step st m2 t2)
228 and ac_stacks st m1 m2 =
229 (* L.warn "entering R.are_convertible_stacks"; *)
230 if List.length m1.s <> List.length m2.s then false else
231 let map (c1, v1) (c2, v2) =
232 let m1, m2 = reset m1 ~e:c1 zero, reset m2 ~e:c2 zero in
235 list_and map (m1.s, m2.s)
237 let rec ih_nfs st (m, t, r) =
239 | B.GRef _, Some v ->
240 if !G.summary then O.add ~gdelta:1 ();
244 and ih st m t = ih_nfs st (step st m t)
246 (* Interface functions ******************************************************)
249 e = B.empty; s = []; l = 0; n = None
254 let _, _, _, b = B.get m.e i in b
257 if !G.ct >= level then log1 st "Now scanning" m.e t;
260 let are_convertible st m1 n1 t1 m2 n2 t2 =
261 if !G.ct >= level then log2 st "Now converting" m1.e t1 m2.e t2;
262 let r = ac st (reset m1 n1) t1 (reset m2 n2) t2 in
265 if S.eq mu mw then are_alpha_convertible err f u w else err () *)
267 (* error reporting **********************************************************)
269 let pp_term st m och t = BO.specs.L.pp_term st m.e och t
271 let pp_lenv st och m = BO.specs.L.pp_lenv st och m.e
274 L.pp_term = pp_term; L.pp_lenv = pp_lenv