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 E = BrgEnvironment
21 exception TypeError of B.message
25 s: (B.context * B.term) list;
29 (* Internal functions *******************************************************)
34 let sc, st = s ^ " in the context", "the term" in
35 L.log O.specs level (L.ct_items1 sc c st t)
37 let log2 s cu u ct t =
38 let s1, s2, s3 = s ^ " in the context", "the term", "and in the context" in
39 L.log O.specs level (L.ct_items2 s1 cu s2 u s3 ct s2 t)
42 let s = Printf.sprintf "local reference not found %u" i in
43 raise (TypeError (L.items1 s))
46 let sc = "In the context" in
47 raise (TypeError (L.ct_items1 sc c st t))
49 let error3 c t1 t2 t3 =
50 let sc, st1, st2, st3 =
51 "In the context", "the term", "is of type", "but must be of type"
53 raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
55 let are_alpha_convertible f t1 t2 =
56 let rec aux f = function
57 | B.Sort (_, p1), B.Sort (_, p2)
58 | B.LRef (_, p1), B.LRef (_, p2) -> f (p1 = p2)
59 | B.GRef (_, u1), B.GRef (_, u2) -> f (U.eq u1 u2)
60 | B.Cast (_, v1, t1), B.Cast (_, v2, t2)
61 | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
62 let f r = if r then aux f (t1, t2) else f r in
64 | B.Bind (_, b1, t1), B.Bind (_, b2, t2) ->
65 let f r = if r then aux f (t1, t2) else f r in
68 and aux_bind f = function
69 | B.Abbr v1, B.Abbr v2
70 | B.Abst v1, B.Abst v2 -> aux f (v1, v2)
71 | B.Void, B.Void -> f true
74 if S.eq t1 t2 then f true else aux f (t1, t2)
80 let rec step f ?(delta=false) ?(rt=false) m x =
81 (* L.warn "entering R.step"; *)
83 | B.Sort _ -> f m None x
86 | _, _, B.Abbr v when delta ->
87 P.add ~gdelta:1 (); step f ~delta ~rt m v
88 | _, _, B.Abst w when rt ->
89 P.add ~grt:1 (); step f ~delta ~rt m w
100 step f ~delta ~rt {m with c = c} v
101 | B.Abst w when rt ->
103 step f ~delta ~rt {m with c = c} w
107 let f e = f {m with c = c} (Some (e, b)) x in
111 | B.Cast (_, _, t) ->
113 step f ~delta ~rt m t
114 | B.Appl (_, v, t) ->
115 step f ~delta ~rt {m with s = (m.c, v) :: m.s} t
116 | B.Bind (a, B.Abst w, t) ->
120 P.add ~beta:1 ~upsilon:(List.length s) ();
121 let f c = step f ~delta ~rt {m with c = c; s = s} t in
122 B.push f m.c ~c a (B.Abbr v) (* (B.Cast ([], w, v)) *)
124 | B.Bind (a, b, t) ->
125 P.add ~upsilon:(List.length m.s) ();
126 let f c = step f ~delta ~rt {m with c = c} t in
127 B.push f m.c ~c:m.c a b
130 let f r = L.unbox level; f r in
132 | B.Bind (_, B.Abst w, _) -> f m w
133 | _ -> error1 "not a function" m.c t
135 L.box level; log1 "Now scanning" m.c t;
136 step f ~delta:true ~rt:true m t
140 let a, i = match b with
141 | B.Abst _ -> B.Apix m.i :: a, succ m.i
144 let f c = f {m with c = c; i = i} in
145 B.push f m.c ~c:m.c a b
147 let rec ac_nfs f ~si r m1 a1 u m2 a2 t =
148 log2 "Now converting nfs" m1.c u m2.c t;
149 match a1, u, a2, t with
150 | _, B.Sort (_, h1), _, B.Sort (_, h2) ->
151 if h1 = h2 then f r else f false
152 | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _ ->
153 if e1 = e2 then ac_stacks f r m1 m2 else f false
154 | Some (e1, B.Abbr v1), _, Some (e2, B.Abbr v2), _ ->
160 ac f ~si true m1 v1 m2 v2
164 else if e1 < e2 then begin
166 step (ac_nfs f ~si r m1 a1 u) m2 v2
169 step (ac_nfs_rev f ~si r m2 a2 t) m1 v1
171 | _, _, Some (_, B.Abbr v2), _ ->
173 step (ac_nfs f ~si r m1 a1 u) m2 v2
174 | Some (_, B.Abbr v1), _, _, _ ->
176 step (ac_nfs_rev f ~si r m2 a2 t) m1 v1
177 | _, B.Bind (a1, (B.Abst w1 as b1), t1),
178 _, B.Bind (a2, (B.Abst w2 as b2), t2) ->
179 let g m1 m2 = ac f ~si r m1 t1 m2 t2 in
180 let g m1 = push (g m1) m2 a2 b2 in
181 let f r = if r then push g m1 a1 b1 else f false in
182 ac f ~si:false r m1 w1 m2 w2
183 | _, B.Sort _, _, B.Bind (a, b, t) when si ->
185 let f m1 m2 = ac f ~si r m1 u m2 t in
186 let f m1 = push (f m1) m2 a b in
190 and ac_nfs_rev f ~si r m2 a2 t m1 a1 u = ac_nfs f ~si r m1 a1 u m2 a2 t
192 and ac f ~si r m1 t1 m2 t2 =
193 (* L.warn "entering R.are_convertible"; *)
194 let g m1 a1 t1 = step (ac_nfs f ~si r m1 a1 t1) m2 t2 in
195 if r = false then f false else step g m1 t1
197 and ac_stacks f r m1 m2 =
198 (* L.warn "entering R.are_convertible_stacks"; *)
199 if List.length m1.s <> List.length m2.s then f false else
200 let map f r (c1, v1) (c2, v2) =
201 let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
202 ac f ~si:false r m1 v1 m2 v2
204 C.list_fold_left2 f map r m1.s m2.s
206 (* Interface functions ******************************************************)
208 let empty_machine = {
209 c = B.empty_context; s = []; i = 0
217 let assert_conversion f ?(si=false) ?(rt=false) mw u w v =
219 L.box level; log2 "Now converting" mu.c u mw.c w;
221 if r then begin L.unbox level; f () end else error3 mw.c v w u
223 let g r = if r then f r else ac f ~si true mu u mw w in
224 (* if S.eq mu mw then are_alpha_convertible g u w else *) g false
226 if rt then domain f mw u else f mw u
228 let message1 st m t =
229 L.ct_items1 "In the context" m.c st t