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_______________________________________________________________ *)
18 module E = BrgEnvironment
19 module S = BrgSubstitution
21 exception TypeError of B.message
25 s: (B.term * int) list
28 (* Internal functions *******************************************************)
33 let sc, st = s ^ " in the context", "the term" in
34 L.log O.specs level (L.ct_items1 sc c st t)
36 let log2 s cu u ct t =
37 let s1, s2, s3 = s ^ " in the context", "the term", "and in the context" in
38 L.log O.specs level (L.ct_items2 s1 cu s2 u s3 ct s2 t)
41 let s = Printf.sprintf "local reference not found %u" i in
42 raise (TypeError (L.items1 s))
45 let sc = "In the context" in
46 raise (TypeError (L.ct_items1 sc c st t))
48 let error3 c t1 t2 t3 =
49 let sc, st1, st2, st3 =
50 "In the context", "the term", "is of type", "but must be of type"
52 raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
54 let empty_machine c = {
60 | Some (_, b) -> f e b
66 let map f (v, i) = f (v, succ i) in
71 f {m with c = (a, b) :: m.c}
74 let rec step f ?(delta=false) ?(rt=false) m x =
75 (* L.warn "entering R.step"; *)
80 | _, _, B.Abbr v when delta ->
83 | _, _, B.Abst w when rt ->
87 f m (B.GRef (B.Entry (e, b) :: a, uri))
99 f m (B.LRef (B.Entry (e, b) :: a, i))
101 let f e = S.lift_bind (f e) (succ i) (0) in
103 | B.Cast (_, _, t) ->
105 step f ~delta ~rt m t
106 | B.Appl (_, v, t) ->
107 step f ~delta ~rt {m with s = (v, 0) :: m.s} t
108 | B.Bind (a, B.Abst w, t) ->
112 P.add ~beta:1 ~upsilon:(List.length tl) ();
113 let f c s = step f ~delta ~rt {c = c; s = s} t in
114 let f c = lift_stack (f c) tl in
115 let f v = B.push f m.c a (B.Abbr v (* (B.Cast ([], w, v)) *) ) in
118 | B.Bind (a, b, t) ->
119 P.add ~upsilon:(List.length m.s) ();
120 let f s c = step f ~delta ~rt {c = c; s = s} t in
121 let f s = B.push (f s) m.c a b in
124 (* Interface functions ******************************************************)
127 let f r = L.unbox level; f r in
129 | B.Bind (_, B.Abst w, _) -> f m w
130 | _ -> error1 "not a function" m.c t
132 L.box level; log1 "Now scanning" m.c t;
133 step f ~delta:true ~rt:true m t
135 let rec ac_nfs f ~si r m1 u m2 t =
136 log2 "Now converting nfs" m1.c u m2.c t;
138 | B.Sort (_, h1), B.Sort (_, h2) ->
139 if h1 = h2 then f r else f false
140 | B.LRef (B.Entry (e1, B.Abst _) :: _, i1),
141 B.LRef (B.Entry (e2, B.Abst _) :: _, i2) ->
142 P.add ~zeta:(i1+i2-e1-e2) ();
143 if e1 = e2 then ac_stacks f ~si r m1 m2 else f false
144 | B.GRef (B.Entry (e1, B.Abst _) :: _, _),
145 B.GRef (B.Entry (e2, B.Abst _) :: _, _) ->
146 if e1 = e2 then ac_stacks f ~si r m1 m2 else f false
147 | B.GRef (B.Entry (e1, B.Abbr v1) :: _, _),
148 B.GRef (B.Entry (e2, B.Abbr v2) :: _, _) ->
154 ac f ~si true m1 v1 m2 v2
157 ac_stacks f ~si r m1 m2
158 else if e1 < e2 then begin
160 step (ac_nfs f ~si r m1 u) m2 v2
163 step (ac_nfs_rev f ~si r m2 t) m1 v1
165 | _, B.GRef (B.Entry (_, B.Abbr v2) :: _, _) ->
167 step (ac_nfs f ~si r m1 u) m2 v2
168 | B.GRef (B.Entry (_, B.Abbr v1) :: _, _), _ ->
170 step (ac_nfs_rev f ~si r m2 t) m1 v1
171 | B.Bind (a1, (B.Abst w1 as b1), t1),
172 B.Bind (a2, (B.Abst w2 as b2), t2) ->
173 let g m1 m2 = ac f ~si r m1 t1 m2 t2 in
174 let g m1 = push (g m1) m2 a2 b2 in
175 let f r = if r then push g m1 a1 b1 else f false in
176 ac f ~si r m1 w1 m2 w2
177 | B.Sort _, B.Bind (a, b, t) when si ->
179 let f m1 m2 = ac f ~si r m1 u m2 t in
180 let f m1 = push (f m1) m2 a b in
184 and ac_nfs_rev f ~si r m2 t m1 u = ac_nfs f ~si r m1 u m2 t
186 and ac f ~si r m1 t1 m2 t2 =
187 (* L.warn "entering R.are_convertible"; *)
188 let g m1 t1 = step (ac_nfs f ~si r m1 t1) m2 t2 in
189 if r = false then f false else step g m1 t1
191 and ac_stacks f ~si r m1 m2 =
192 (* L.warn "entering R.are_convertible_stacks"; *)
193 let mm1, mm2 = {m1 with s = []}, {m2 with s = []} in
194 let map f r (v1, h1) (v2, h2) =
195 let f v1 = S.lift (ac f ~si r mm1 v1 mm2) h2 (0) v2 in
198 if List.length m1.s <> List.length m2.s then
200 (* L.warn (Printf.sprintf "Different lengths: %u %u"
201 (List.length m1.s) (List.length m2.s)
206 C.list_fold_left2 f map r m1.s m2.s
208 let assert_conversion f ?(si=false) ?(rt=false) c u w v =
209 let f b = L.unbox level; f b in
210 let mw = empty_machine c in
214 | false -> error3 c v w u
216 L.box level; log2 "Now converting" c u c w;
217 ac f ~si true mu u mw w
219 if rt then domain f mw u else f mw u