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_______________________________________________________________ *)
23 module BE = BrgEnvironment
26 e: B.lenv; (* environment *)
27 s: (B.lenv * B.term) list; (* stack *)
31 (* Internal functions *******************************************************)
36 let sc, st = s ^ " in the environment", "the term" in
37 L.log BO.specs level (L.et_items1 sc c st t)
39 let log2 s cu u ct t =
40 let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
41 L.log BO.specs level (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
43 let rec list_and map = function
44 | hd1 :: tl1, hd2 :: tl2 ->
45 if map hd1 hd2 then list_and map (tl1, tl2) else false
49 let are_alpha_convertible err f t1 t2 =
50 let rec aux f = function
51 | B.Sort (_, p1), B.Sort (_, p2)
52 | B.LRef (_, p1), B.LRef (_, p2) ->
53 if p1 = p2 then f () else err ()
54 | B.GRef (_, u1), B.GRef (_, u2) ->
55 if U.eq u1 u2 then f () else err ()
56 | B.Cast (_, v1, t1), B.Cast (_, v2, t2)
57 | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
58 let f _ = aux f (t1, t2) in
60 | B.Bind (_, b1, t1), B.Bind (_, b2, t2) ->
61 let f _ = aux f (t1, t2) in
64 and aux_bind f = function
65 | B.Abbr v1, B.Abbr v2 -> aux f (v1, v2)
66 | B.Abst (n1, v1), B.Abst (n2, v2) when n1 = n2 -> aux f (v1, v2)
67 | B.Void, B.Void -> f ()
70 if W.eq t1 t2 then f () else aux f (t1, t2)
73 let _, c, a, b = B.get m.e i in c, a, b
77 (* L.warn "entering R.step"; *)
79 | B.Sort _ -> m, None, x
81 begin match BE.get_entity uri with
82 | _, _, E.Abbr v when st.S.delta ->
83 O.add ~gdelta:1 (); step st m v
84 | _, _, E.Abst (_, w) when st.S.rt ->
85 O.add ~grt:1 (); step st m w
87 let e = E.apix C.err C.start a in
88 m, Some (e, a, B.Abbr v), x
89 | a, _, E.Abst (n, w) ->
90 let e = E.apix C.err C.start a in
91 m, Some (e, a, B.Abst (n, w)), x
92 | _, _, E.Void -> assert false
95 begin match get m i with
98 step st {m with e = c} v
99 | c, _, B.Abst (_, w) when st.S.rt ->
101 step st {m with e = c} w
104 | c, a, (B.Abst _ as b) ->
105 let e = E.apix C.err C.start a in
106 {m with e = c}, Some (e, a, b), x
108 | B.Cast (_, _, t) ->
111 | B.Appl (_, v, t) ->
112 step st {m with s = (m.e, v) :: m.s} t
113 | B.Bind (a, B.Abst (n, w), t) ->
117 if N.is_zero n then Q.add_nonzero st.S.cc a;
118 O.add ~beta:1 ~theta:(List.length s) ();
119 let e = B.push m.e c a (B.abbr v) (* (B.Cast ([], w, v)) *) in
120 step st {m with e = e; s = s} t
122 | B.Bind (a, b, t) ->
123 O.add ~theta:(List.length m.s) ();
124 let e = B.push m.e m.e a b in
125 step st {m with e = e} t
129 let a, d = match b with
130 | B.Abst _ -> E.Apix m.d :: a, succ m.d
133 let e = B.push m.e m.e a b in
134 {m with e = e; d = d}
136 let rec ac_nfs st (m1, r1, u) (m2, r2, t) =
137 log2 "Now converting nfs" m1.e u m2.e t;
138 match r1, u, r2, t with
139 | _, B.Sort (_, h1), _, B.Sort (_, h2) ->
141 | Some (e1, _, B.Abst _), _, Some (e2, _, B.Abst _), _ ->
142 if e1 = e2 then ac_stacks st m1 m2 else false
143 | Some (e1, _, B.Abbr v1), _, Some (e2, _, B.Abbr v2), _ ->
145 if ac_stacks st m1 m2 then true else begin
146 O.add ~gdelta:2 (); ac st m1 v1 m2 v2
148 else if e1 < e2 then begin
150 ac_nfs st (m1, r1, u) (step st m2 v2)
153 ac_nfs st (step st m1 v1) (m2, r2, t)
155 | _, _, Some (_, _, B.Abbr v2), _ ->
157 ac_nfs st (m1, r1, u) (step st m2 v2)
158 | Some (_, _, B.Abbr v1), _, _, _ ->
160 ac_nfs st (step st m1 v1) (m2, r2, t)
161 | _, B.Bind (a1, (B.Abst (n1, w1) as b1), t1),
162 _, B.Bind (a2, (B.Abst (n2, w2) as b2), t2) ->
163 if n1 = n2 then () else Q.add_equal st.S.cc a1 a2;
164 if ac {st with S.si = false} m1 w1 m2 w2 then
165 ac st (push m1 a1 b1) t1 (push m2 a2 b2) t2
167 | _, B.Sort _, _, B.Bind (a, (B.Abst (n, _) as b), t) ->
168 if N.is_zero n then () else Q.add_zero st.S.cc a;
170 ac st (push m1 a b) u (push m2 a b) t
173 and ac st m1 t1 m2 t2 =
174 (* L.warn "entering R.are_convertible"; *)
175 ac_nfs st (step st m1 t1) (step st m2 t2)
177 and ac_stacks st m1 m2 =
178 (* L.warn "entering R.are_convertible_stacks"; *)
179 if List.length m1.s <> List.length m2.s then false else
180 let map (c1, v1) (c2, v2) =
181 let m1, m2 = {m1 with e = c1; s = []}, {m2 with e = c2; s = []} in
182 ac {st with S.si = false} m1 v1 m2 v2
184 list_and map (m1.s, m2.s)
186 (* Interface functions ******************************************************)
189 e = B.empty; s = []; d = 0
194 let _, _, _, b = B.get m.e i in b
197 L.box level; log1 "Now scanning" m.e t;
198 let m, _, t = step {st with S.delta = true; S.rt = true} m t in
201 let are_convertible st mu u mw w =
202 L.box level; log2 "Now converting" mu.e u mw.e w;
203 let r = ac {st with S.delta = st.S.expand; S.rt = false} mu u mw w in
206 if W.eq mu mw then are_alpha_convertible err f u w else err () *)
208 (* error reporting **********************************************************)
210 let pp_term m frm t = BO.specs.L.pp_term m.e frm t
212 let pp_lenv frm m = BO.specs.L.pp_lenv frm m.e
215 L.pp_term = pp_term; L.pp_lenv = pp_lenv