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_______________________________________________________________ *)
19 module ZE = BagEnvironment
20 module ZS = BagSubstitution
30 | LRef_ of int * Z.term option
32 | Bind_ of Z.attrs * int * Z.term * Z.term
38 (* Internal functions *******************************************************)
40 let term_of_whdr = function
42 | LRef_ (i, _) -> Z.LRef i
43 | GRef_ (_, uri, _) -> Z.GRef uri
44 | Bind_ (a, l, w, t) -> Z.bind_abst a l w t
49 let sc, st = s ^ " in the environment", "the term" in
50 L.log ZO.specs level (L.et_items1 sc c st t)
52 let log2 s cu u ct t =
53 let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
54 L.log ZO.specs level (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
56 let empty_machine = {i = 0; c = Z.empty_lenv; s = []}
58 let inc m = {m with i = succ m.i}
60 let unwind_to_term f m t =
61 let map f t (a, l, b) = f (Z.Bind (a, l, b, t)) in
62 let f mc = C.list_fold_left f map t mc in
65 let unwind_stack f m =
66 let map f v = unwind_to_term f m v in
71 let f c = Z.get C.err f c i in
74 let push msg f c m a l w =
76 let f w = Z.push msg f c a l (Z.Abst w) in
81 (* L.warn "entering R.whd"; *)
83 | Z.Sort h -> f m (Sort_ h)
85 let f entry = f m (GRef_ entry) in
89 | Z.Void -> f m (LRef_ (i, None))
90 | Z.Abst t -> f m (LRef_ (i, Some t))
91 | Z.Abbr t -> whd f c m t
94 | Z.Cast (_, t) -> whd f c m t
95 | Z.Appl (v, t) -> whd f c {m with s = v :: m.s} t
96 | Z.Bind (a, l, Z.Abst w, t) ->
98 | [] -> f m (Bind_ (a, l, w, t))
100 let nl = J.new_location () in
101 let f mc = ZS.subst (whd f c {m with c = mc; s = tl}) nl l t in
102 Z.push "!" f m.c a nl (Z.Abbr (Z.Cast (w, v)))
104 | Z.Bind (a, l, b, t) ->
105 let nl = J.new_location () in
106 let f mc = ZS.subst (whd f c {m with c = mc}) nl l t in
107 Z.push "!" f m.c a nl b
109 (* Interface functions ******************************************************)
111 let rec ho_whd f c m x =
112 (* L.warn "entering R.ho_whd"; *)
114 | Sort_ h -> f (Sort h)
115 | Bind_ (_, _, w, _) ->
116 let f w = f (Abst w) in unwind_to_term f m w
117 | LRef_ (_, Some w) -> ho_whd f c m w
118 | GRef_ (_, _, E.Abst (_, w)) -> ho_whd f c m w
119 | GRef_ (_, _, E.Abbr v) -> ho_whd f c m v
120 | LRef_ (_, None) -> assert false
121 | GRef_ (_, _, E.Void) -> assert false
126 let f r = L.unbox level; f r in
127 L.box level; log1 "Now scanning" c t;
128 ho_whd f c empty_machine t
130 let rec are_convertible f ~si a c m1 t1 m2 t2 =
131 (* L.warn "entering R.are_convertible"; *)
132 let rec aux m1 r1 m2 r2 =
133 (* L.warn "entering R.are_convertible_aux"; *)
134 let u, t = term_of_whdr r1, term_of_whdr r2 in
135 log2 "Now really converting" c u c t;
137 | Sort_ h1, Sort_ h2 ->
138 if h1 = h2 then f a else f false
139 | LRef_ (i1, _), LRef_ (i2, _) ->
140 if i1 = i2 then are_convertible_stacks f ~si a c m1 m2 else f false
141 | GRef_ ((E.Apix a1 :: _), _, E.Abst _),
142 GRef_ ((E.Apix a2 :: _), _, E.Abst _) ->
143 if a1 = a2 then are_convertible_stacks f ~si a c m1 m2 else f false
144 | GRef_ ((E.Apix a1 :: _), _, E.Abbr v1),
145 GRef_ ((E.Apix a2 :: _), _, E.Abbr v2) ->
148 if a then f a else are_convertible f ~si true c m1 v1 m2 v2
150 are_convertible_stacks f ~si a c m1 m2
152 if a1 < a2 then whd (aux m1 r1) c m2 v2 else
153 whd (aux_rev m2 r2) c m1 v1
154 | _, GRef_ (_, _, E.Abbr v2) ->
155 whd (aux m1 r1) c m2 v2
156 | GRef_ (_, _, E.Abbr v1), _ ->
157 whd (aux_rev m2 r2) c m1 v1
158 | Bind_ (a1, l1, w1, t1), Bind_ (a2, l2, w2, t2) ->
159 let l = J.new_location () in
161 let m1, m2 = inc m1, inc m2 in
162 let f t1 = ZS.subst (are_convertible f ~si a c m1 t1 m2) l l2 t2 in
165 let f r = if r then push "!" h c m1 a1 l w1 else f false in
166 are_convertible f ~si a c m1 w1 m2 w2
167 (* we detect the AUT-QE reduction rule for type/prop inclusion *)
168 | Sort_ _, Bind_ (a2, l2, w2, t2) when si ->
169 let m1, m2 = inc m1, inc m2 in
170 let f c = are_convertible f ~si a c m1 (term_of_whdr r1) m2 t2 in
171 push "nsi" f c m2 a2 l2 w2
173 and aux_rev m2 r2 m1 r1 = aux m1 r1 m2 r2 in
174 let g m1 r1 = whd (aux m1 r1) c m2 t2 in
175 if a = false then f false else whd g c m1 t1
177 and are_convertible_stacks f ~si a c m1 m2 =
178 (* L.warn "entering R.are_convertible_stacks"; *)
179 let mm1, mm2 = {m1 with s = []}, {m2 with s = []} in
180 let map f a v1 v2 = are_convertible f ~si a c mm1 v1 mm2 v2 in
181 if List.length m1.s <> List.length m2.s then
183 (* L.warn (Printf.sprintf "Different lengths: %u %u"
184 (List.length m1.s) (List.length m2.s)
189 C.list_fold_left2 f map a m1.s m2.s
191 let are_convertible f ?(si=false) c u t =
192 let f b = L.unbox level; f b in
193 L.box level; log2 "Now converting" c u c t;
194 are_convertible f ~si true c empty_machine u empty_machine t