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 E = BrgEnvironment
24 s: (B.lenv * B.term) list;
28 (* Internal functions *******************************************************)
33 let sc, st = s ^ " in the environment", "the term" in
34 L.log O.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 O.specs level (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
40 let are_alpha_convertible err f t1 t2 =
41 let rec aux f = function
42 | B.Sort (_, p1), B.Sort (_, p2)
43 | B.LRef (_, p1), B.LRef (_, p2) ->
44 if p1 = p2 then f () else err ()
45 | B.GRef (_, u1), B.GRef (_, u2) ->
46 if U.eq u1 u2 then f () else err ()
47 | B.Cast (_, v1, t1), B.Cast (_, v2, t2)
48 | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
49 let f _ = aux f (t1, t2) in
51 | B.Bind (b1, t1), B.Bind (b2, t2) ->
52 let f _ = aux f (t1, t2) in
55 and aux_bind f = function
56 | B.Abbr (_, v1), B.Abbr (_, v2)
57 | B.Abst (_, v1), B.Abst (_, v2) -> aux f (v1, v2)
58 | B.Void _, B.Void _ -> f ()
61 if S.eq t1 t2 then f () else aux f (t1, t2)
67 let rec step f ?(delta=false) ?(rt=false) m x =
68 (* L.warn "entering R.step"; *)
70 | B.Sort _ -> f m None x
73 | _, _, Y.Abbr v when delta ->
74 P.add ~gdelta:1 (); step f ~delta ~rt m v
75 | _, _, Y.Abst w when rt ->
76 P.add ~grt:1 (); step f ~delta ~rt m w
78 let f e = f m (Some (e, B.Abbr (a, v))) x in
81 let f e = f m (Some (e, B.Abst (a, w))) x in
84 E.get_entity C.err f uri
89 step f ~delta ~rt {m with c = c} v
90 | B.Abst (_, w) when rt ->
92 step f ~delta ~rt {m with c = c} w
95 | B.Abst (a, _) as b ->
96 let f e = f {m with c = c} (Some (e, b)) x in
100 | B.Cast (_, _, t) ->
102 step f ~delta ~rt m t
103 | B.Appl (_, v, t) ->
104 step f ~delta ~rt {m with s = (m.c, v) :: m.s} t
105 | B.Bind (B.Abst (a, w), t) ->
109 P.add ~beta:1 ~upsilon:(List.length s) ();
110 let f c = step f ~delta ~rt {m with c = c; s = s} t in
111 B.push f m.c ~c (B.abbr a v) (* (B.Cast ([], w, v)) *)
114 P.add ~upsilon:(List.length m.s) ();
115 let f c = step f ~delta ~rt {m with c = c} t in
116 B.push f m.c ~c:m.c b
120 let b, i = match b with
121 | B.Abst (a, w) -> B.abst (Y.Apix m.i :: a) w, succ m.i
124 let f c = f {m with c = c; i = i} in
125 B.push f m.c ~c:m.c b
127 let rec ac_nfs err f ~si m1 a1 u m2 a2 t =
128 log2 "Now converting nfs" m1.c u m2.c t;
129 match a1, u, a2, t with
130 | _, B.Sort (_, h1), _, B.Sort (_, h2) ->
131 if h1 = h2 then f () else err ()
132 | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _ ->
133 if e1 = e2 then ac_stacks err f m1 m2 else err ()
134 | Some (e1, B.Abbr (_, v1)), _, Some (e2, B.Abbr (_, v2)), _ ->
136 let err _ = P.add ~gdelta:2 (); ac err f ~si m1 v1 m2 v2 in
137 ac_stacks err f m1 m2
138 else if e1 < e2 then begin
140 step (ac_nfs err f ~si m1 a1 u) m2 v2
143 step (ac_nfs_rev err f ~si m2 a2 t) m1 v1
145 | _, _, Some (_, B.Abbr (_, v2)), _ ->
147 step (ac_nfs err f ~si m1 a1 u) m2 v2
148 | Some (_, B.Abbr (_, v1)), _, _, _ ->
150 step (ac_nfs_rev err f ~si m2 a2 t) m1 v1
151 | _, B.Bind ((B.Abst (_, w1) as b1), t1),
152 _, B.Bind ((B.Abst (_, w2) as b2), t2) ->
153 let f m1 m2 = ac err f ~si m1 t1 m2 t2 in
154 let f m1 = push (f m1) m2 b2 in
155 let f _ = push f m1 b1 in
156 ac err f ~si:false m1 w1 m2 w2
157 | _, B.Sort _, _, B.Bind (b, t) when si ->
159 let f m1 m2 = ac err f ~si m1 u m2 t in
160 let f m1 = push (f m1) m2 b in
164 and ac_nfs_rev err f ~si m2 a2 t m1 a1 u = ac_nfs err f ~si m1 a1 u m2 a2 t
166 and ac err f ~si m1 t1 m2 t2 =
167 (* L.warn "entering R.are_convertible"; *)
168 let f m1 a1 t1 = step (ac_nfs err f ~si m1 a1 t1) m2 t2 in
171 and ac_stacks err f m1 m2 =
172 (* L.warn "entering R.are_convertible_stacks"; *)
173 if List.length m1.s <> List.length m2.s then err () else
174 let map f (c1, v1) (c2, v2) =
175 let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
176 ac err f ~si:false m1 v1 m2 v2
178 C.list_iter2 f map m1.s m2.s
180 (* Interface functions ******************************************************)
183 c = B.empty_lenv; s = []; i = 0
192 L.box level; log1 "Now scanning" m.c t;
193 let f m _ t = L.unbox level; f m t in
194 step f ~delta:true ~rt:true m t
196 let are_convertible err f ?(si=false) mu u mw w =
197 L.box level; log2 "Now converting" mu.c u mw.c w;
198 let f x = L.unbox level; f x in
199 let err _ = ac err f ~si 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 = O.specs.L.pp_term m.c frm t
206 let pp_lenv frm m = O.specs.L.pp_lenv frm m.c
209 L.pp_term = pp_term; L.pp_lenv = pp_lenv