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
20 exception TypeError of B.message
24 s: (B.context * B.term) 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 err () = assert false
60 let rec step f ?(delta=false) ?(rt=false) m x =
61 (* L.warn "entering R.step"; *)
63 | B.Sort _ -> f m None x
66 | _, _, B.Abbr v when delta ->
69 | _, _, B.Abst w when rt ->
80 step f ~delta ~rt {m with c = c} v
83 step f ~delta ~rt {m with c = c} w
85 f {m with c = c} None x
87 let f e = f {m with c = c} (Some (e, b)) x in
95 step f ~delta ~rt {m with s = (m.c, v) :: m.s} t
96 | B.Bind (a, B.Abst w, t) ->
100 P.add ~beta:1 ~upsilon:(List.length s) ();
101 let f c = step f ~delta ~rt {m with c = c; s = s} t in
102 B.push f m.c ~c a (B.Abbr v) (* (B.Cast ([], w, v)) *)
104 | B.Bind (a, b, t) ->
105 P.add ~upsilon:(List.length m.s) ();
106 let f c = step f ~delta ~rt {m with c = c} t in
107 B.push f m.c ~c:m.c a b
110 let f r = L.unbox level; f r in
112 | B.Bind (_, B.Abst w, _) -> f m w
113 | _ -> error1 "not a function" m.c t
115 L.box level; log1 "Now scanning" m.c t;
116 step f ~delta:true ~rt:true m t
120 let a, i = match b with
121 | B.Abst _ -> B.Apix m.i :: a, succ m.i
124 let f c = f {m with c = c; i = i} in
125 B.push f m.c ~c:m.c a b
127 let rec ac_nfs f ~si r m1 a1 u m2 a2 t =
128 log2 "Now converting nfs" m1.c u m2.c t;
129 match a1, u, a2, t with
132 if h1 = h2 then f r else f false
133 | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _ ->
134 if e1 = e2 then ac_stacks f ~si r m1 m2 else f false
135 | Some (e1, B.Abbr v1), _, Some (e2, B.Abbr v2), _ ->
141 ac f ~si true m1 v1 m2 v2
144 ac_stacks f ~si r m1 m2
145 else if e1 < e2 then begin
147 step (ac_nfs f ~si r m1 a1 u) m2 v2
150 step (ac_nfs_rev f ~si r m2 a2 t) m1 v1
152 | _, _, Some (_, B.Abbr v2), _ ->
154 step (ac_nfs f ~si r m1 a1 u) m2 v2
155 | Some (_, B.Abbr v1), _, _, _ ->
157 step (ac_nfs_rev f ~si r m2 a2 t) m1 v1
158 | _, B.Bind (a1, (B.Abst w1 as b1), t1),
159 _, B.Bind (a2, (B.Abst w2 as b2), t2) ->
160 let g m1 m2 = ac f ~si r m1 t1 m2 t2 in
161 let g m1 = push (g m1) m2 a2 b2 in
162 let f r = if r then push g m1 a1 b1 else f false in
163 ac f ~si r m1 w1 m2 w2
164 | _, B.Sort _, _, B.Bind (a, b, t) when si ->
166 let f m1 m2 = ac f ~si r m1 u m2 t in
167 let f m1 = push (f m1) m2 a b in
171 and ac_nfs_rev f ~si r m2 a2 t m1 a1 u = ac_nfs f ~si r m1 a1 u m2 a2 t
173 and ac f ~si r m1 t1 m2 t2 =
174 (* L.warn "entering R.are_convertible"; *)
175 let g m1 a1 t1 = step (ac_nfs f ~si r m1 a1 t1) m2 t2 in
176 if r = false then f false else step g m1 t1
178 and ac_stacks f ~si r m1 m2 =
179 (* L.warn "entering R.are_convertible_stacks"; *)
180 if List.length m1.s <> List.length m2.s then f false else
181 let map f r (c1, v1) (c2, v2) =
182 let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
183 ac f ~si r m1 v1 m2 v2
185 C.list_fold_left2 f map r m1.s m2.s
187 (* Interface functions ******************************************************)
189 let empty_machine = {
190 c = B.empty_context; s = []; i = 0
198 let assert_conversion f ?(si=false) ?(rt=false) mw u w v =
199 let f b = L.unbox level; f b in
203 | false -> error3 mw.c v w u
205 L.box level; log2 "Now converting" mu.c u mw.c w;
206 ac f ~si true mu u mw w
208 if rt then domain f mw u else f mw u
210 let message1 st m t =
211 L.ct_items1 "In the context" m.c st t