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 ZE = BagEnvironment
21 module ZS = BagSubstitution
33 | LRef_ of P.mark * Z.term option
35 | Bind_ of Z.b_attrs * P.mark * Z.term * Z.term
41 (* Internal functions *******************************************************)
45 let term_of_whdr = function
47 | LRef_ (i, _) -> Z.LRef i
48 | GRef_ (_, _, uri, _) -> Z.GRef uri
49 | Bind_ (a, l, w, t) -> Z.bind_abst a l w t
52 let s1, s2 = s ^ " in the environment", "the term" in
53 L.log st ZO.specs (pred level) (L.et_items1 s1 c s2 t)
55 let log2 st s cu u ct t =
56 let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
57 L.log st ZO.specs (pred level) (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
59 let empty_machine = {i = 0; c = Z.empty_lenv; s = []}
61 let inc m = {m with i = succ m.i}
63 let unwind_to_term f m t =
64 let map f t (y, l, b) = f (Z.Bind (y, l, b, t)) in
65 let f mc = C.list_fold_left f map t mc in
68 let unwind_stack f m =
69 let map f v = unwind_to_term f m v in
74 let f c = Z.get C.err f c i in
77 let push msg f c m a l w =
79 let f w = Z.push msg f c a l (Z.Abst w) in
84 (* L.warn "entering R.whd"; *)
86 | Z.Sort h -> f m (Sort_ h)
88 let f entry = f m (GRef_ entry) in
92 | Z.Void -> f m (LRef_ (i, None))
93 | Z.Abst t -> f m (LRef_ (i, Some t))
94 | Z.Abbr t -> whd f c m t
97 | Z.Cast (_, t) -> whd f c m t
98 | Z.Appl (v, t) -> whd f c {m with s = v :: m.s} t
99 | Z.Bind (y, l, Z.Abst w, t) ->
101 | [] -> f m (Bind_ (y, l, w, t))
103 let nl = P.new_mark () in
104 let f mc = ZS.subst (whd f c {m with c = mc; s = tl}) nl l t in
105 Z.push "!" f m.c y nl (Z.Abbr (Z.Cast (w, v)))
107 | Z.Bind (y, l, b, t) ->
108 let nl = P.new_mark () in
109 let f mc = ZS.subst (whd f c {m with c = mc}) nl l t in
110 Z.push "!" f m.c y nl b
112 (* Interface functions ******************************************************)
114 let rec ho_whd f c m x =
115 (* L.warn "entering R.ho_whd"; *)
117 | Sort_ h -> f (Sort h)
118 | Bind_ (_, _, w, _) ->
119 let f w = f (Abst w) in unwind_to_term f m w
120 | LRef_ (_, Some w) -> ho_whd f c m w
121 | GRef_ (_, _, _, E.Abst w) -> ho_whd f c m w
122 | GRef_ (_, _, _, E.Abbr v) -> ho_whd f c m v
123 | LRef_ (_, None) -> assert false
124 | GRef_ (_, _, _, E.Void) -> assert false
128 let ho_whd f st c t =
130 if !G.ct >= level then log1 st "Now scanning" c t
132 ho_whd f c empty_machine t
134 let rec are_convertible f st a c m1 t1 m2 t2 =
135 (* L.warn "entering R.are_convertible"; *)
136 let rec aux m1 r1 m2 r2 =
137 (* L.warn "entering R.are_convertible_aux"; *)
139 let u, t = term_of_whdr r1, term_of_whdr r2 in
140 if !G.ct >= level then log2 st "Now really converting" c u c t
143 | Sort_ k1, Sort_ k2 ->
144 if k1 = k2 then f a else f false
145 | LRef_ (i1, _), LRef_ (i2, _) ->
146 if i1 = i2 then are_convertible_stacks f st a c m1 m2 else f false
147 | GRef_ (_, {E.n_apix = a1}, _, E.Abst _),
148 GRef_ (_, {E.n_apix = a2}, _, E.Abst _) ->
149 if a1 = a2 then are_convertible_stacks f st a c m1 m2 else f false
150 | GRef_ (_, {E.n_apix = a1}, _, E.Abbr v1),
151 GRef_ (_, {E.n_apix = a2}, _, E.Abbr v2) ->
154 if a then f a else are_convertible f st true c m1 v1 m2 v2
156 are_convertible_stacks f st a c m1 m2
158 if a1 < a2 then whd (aux m1 r1) c m2 v2 else
159 whd (aux_rev m2 r2) c m1 v1
160 | _, GRef_ (_, _, _, E.Abbr v2) ->
161 whd (aux m1 r1) c m2 v2
162 | GRef_ (_, _, _, E.Abbr v1), _ ->
163 whd (aux_rev m2 r2) c m1 v1
164 | Bind_ (y1, l1, w1, t1), Bind_ (_, l2, w2, t2) ->
165 let l = P.new_mark () in
167 let m1, m2 = inc m1, inc m2 in
168 let f t1 = ZS.subst (are_convertible f st a c m1 t1 m2) l l2 t2 in
171 let f r = if r then push "!" h c m1 y1 l w1 else f false in
172 are_convertible f st a c m1 w1 m2 w2
173 (* we detect the AUT-QE reduction rule for type/prop inclusion *)
174 | Sort_ _, Bind_ (y2, l2, w2, t2) when !G.si ->
175 let m1, m2 = inc m1, inc m2 in
176 let f c = are_convertible f st a c m1 (term_of_whdr r1) m2 t2 in
177 push "nsi" f c m2 y2 l2 w2
179 and aux_rev m2 r2 m1 r1 = aux m1 r1 m2 r2 in
180 let g m1 r1 = whd (aux m1 r1) c m2 t2 in
181 if a = false then f false else whd g c m1 t1
183 and are_convertible_stacks f st a c m1 m2 =
184 (* L.warn "entering R.are_convertible_stacks"; *)
185 let mm1, mm2 = {m1 with s = []}, {m2 with s = []} in
186 let map f a v1 v2 = are_convertible f st a c mm1 v1 mm2 v2 in
187 if List.length m1.s <> List.length m2.s then
189 (* L.warn (Printf.sprintf "Different lengths: %u %u"
190 (List.length m1.s) (List.length m2.s)
195 C.list_fold_left2 f map a m1.s m2.s
197 let are_convertible f st c u t =
199 if !G.ct >= level then log2 st "Now converting" c u c t
201 are_convertible f st true c empty_machine u empty_machine t