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_______________________________________________________________ *)
20 module BE = BrgEnvironment
23 e: B.lenv; (* environment *)
24 s: (B.lenv * B.term) list; (* stack *)
28 (* Internal functions *******************************************************)
33 let sc, st = s ^ " in the environment", "the term" in
34 L.log BO.specs level (L.et_items1 sc c st t)
36 let log2 s cu u ct t =
37 let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
38 L.log BO.specs level (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
40 let rec list_and map = function
41 | hd1 :: tl1, hd2 :: tl2 ->
42 if map hd1 hd2 then list_and map (tl1, tl2) else false
46 let are_alpha_convertible err f t1 t2 =
47 let rec aux f = function
48 | B.Sort (_, p1), B.Sort (_, p2)
49 | B.LRef (_, p1), B.LRef (_, p2) ->
50 if p1 = p2 then f () else err ()
51 | B.GRef (_, u1), B.GRef (_, u2) ->
52 if U.eq u1 u2 then f () else err ()
53 | B.Cast (_, v1, t1), B.Cast (_, v2, t2)
54 | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
55 let f _ = aux f (t1, t2) in
57 | B.Bind (_, b1, t1), B.Bind (_, b2, t2) ->
58 let f _ = aux f (t1, t2) in
61 and aux_bind f = function
62 | B.Abbr v1, B.Abbr v2
63 | B.Abst v1, B.Abst v2 -> aux f (v1, v2)
64 | B.Void, B.Void -> f ()
67 if S.eq t1 t2 then f () else aux f (t1, t2)
70 let _, c, a, b = B.get m.e i in c, a, b
74 (* L.warn "entering R.step"; *)
76 | B.Sort _ -> m, None, x
78 begin match BE.get_entity uri with
79 | _, _, E.Abbr v when st.E.delta ->
80 O.add ~gdelta:1 (); step st m v
81 | _, _, E.Abst w when st.E.rt ->
82 O.add ~grt:1 (); step st m w
84 let e = E.apix C.err C.start a in
85 m, Some (e, a, B.Abbr v), x
87 let e = E.apix C.err C.start a in
88 m, Some (e, a, B.Abst w), x
89 | _, _, E.Void -> assert false
92 begin match get m i with
95 step st {m with e = c} v
96 | c, _, B.Abst w when st.E.rt ->
98 step st {m with e = c} w
101 | c, a, (B.Abst _ as b) ->
102 let e = E.apix C.err C.start a in
103 {m with e = c}, Some (e, a, b), x
105 | B.Cast (_, _, t) ->
108 | B.Appl (_, v, t) ->
109 step st {m with s = (m.e, v) :: m.s} t
110 | B.Bind (a, B.Abst w, t) ->
114 O.add ~beta:1 ~upsilon:(List.length s) ();
115 let e = B.push m.e c a (B.abbr v) (* (B.Cast ([], w, v)) *) in
116 step st {m with e = e; s = s} t
118 | B.Bind (a, b, t) ->
119 O.add ~upsilon:(List.length m.s) ();
120 let e = B.push m.e m.e a b in
121 step st {m with e = e} t
125 let a, d = match b with
126 | B.Abst _ -> E.Apix m.d :: a, succ m.d
129 let e = B.push m.e m.e a b in
130 {m with e = e; d = d}
132 let rec ac_nfs st (m1, r1, u) (m2, r2, t) =
133 log2 "Now converting nfs" m1.e u m2.e t;
134 match r1, u, r2, t with
135 | _, B.Sort (_, h1), _, B.Sort (_, h2) ->
137 | Some (e1, _, B.Abst _), _, Some (e2, _, B.Abst _), _ ->
138 if e1 = e2 then ac_stacks st m1 m2 else false
139 | Some (e1, _, B.Abbr v1), _, Some (e2, _, B.Abbr v2), _ ->
141 if ac_stacks st m1 m2 then true else begin
142 O.add ~gdelta:2 (); ac st m1 v1 m2 v2
144 else if e1 < e2 then begin
146 ac_nfs st (m1, r1, u) (step st m2 v2)
149 ac_nfs st (step st m1 v1) (m2, r2, t)
151 | _, _, Some (_, _, B.Abbr v2), _ ->
153 ac_nfs st (m1, r1, u) (step st m2 v2)
154 | Some (_, _, B.Abbr v1), _, _, _ ->
156 ac_nfs st (step st m1 v1) (m2, r2, t)
157 | _, B.Bind (a1, (B.Abst w1 as b1), t1),
158 _, B.Bind (a2, (B.Abst w2 as b2), t2) ->
159 if ac {st with E.si = false} m1 w1 m2 w2 then
160 ac st (push m1 a1 b1) t1 (push m2 a2 b2) t2
162 | _, B.Sort _, _, B.Bind (a, b, t) when st.E.si ->
164 ac st (push m1 a b) u (push m2 a b) t
167 and ac st m1 t1 m2 t2 =
168 (* L.warn "entering R.are_convertible"; *)
169 ac_nfs st (step st m1 t1) (step st m2 t2)
171 and ac_stacks st m1 m2 =
172 (* L.warn "entering R.are_convertible_stacks"; *)
173 if List.length m1.s <> List.length m2.s then false else
174 let map (c1, v1) (c2, v2) =
175 let m1, m2 = {m1 with e = c1; s = []}, {m2 with e = c2; s = []} in
176 ac {st with E.si = false} m1 v1 m2 v2
178 list_and map (m1.s, m2.s)
180 (* Interface functions ******************************************************)
183 e = B.empty; s = []; d = 0
188 let _, _, _, b = B.get m.e i in b
191 L.box level; log1 "Now scanning" m.e t;
192 let m, _, t = step {st with E.delta = true; E.rt = true} m t in
195 let are_convertible st mu u mw w =
196 L.box level; log2 "Now converting" mu.e u mw.e w;
197 let r = ac {st with E.delta = st.E.expand; E.rt = false} mu u mw w in
200 if S.eq mu mw then are_alpha_convertible err f u w else err () *)
202 (* error reporting **********************************************************)
204 let pp_term m frm t = BO.specs.L.pp_term m.e frm t
206 let pp_lenv frm m = BO.specs.L.pp_lenv frm m.e
209 L.pp_term = pp_term; L.pp_lenv = pp_lenv