]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgReduction.ml
- alpha conversion check added to the brg kernel (succeeds 1/4 of the times)
[helm.git] / helm / software / lambda-delta / basic_rg / brgReduction.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 module U = NUri
13 module C = Cps
14 module S = Share
15 module L = Log
16 module P = Output
17 module B = Brg
18 module O = BrgOutput
19 module E = BrgEnvironment
20
21 exception TypeError of B.message
22
23 type machine = {
24    c: B.context;
25    s: (B.context * B.term) list;
26    i: int
27 }
28
29 (* Internal functions *******************************************************)
30
31 let level = 5
32
33 let log1 s c t =
34    let sc, st = s ^ " in the context", "the term" in
35    L.log O.specs level (L.ct_items1 sc c st t)
36
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)
40
41 let error0 i = 
42    let s = Printf.sprintf "local reference not found %u" i in
43    raise (TypeError (L.items1 s))
44
45 let error1 st c t =
46    let sc = "In the context" in
47    raise (TypeError (L.ct_items1 sc c st t))
48
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"
52    in
53    raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
54
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
63          aux f (v1, v2)
64       | B.Bind (_, b1, t1), B.Bind (_, b2, t2) ->
65          let f r = if r then aux f (t1, t2) else f r in
66          aux_bind f (b1, b2)
67       | B.Sort _ as t1, B.Bind (_, _, t2)      -> aux f (t1, t2)
68       | _                                      -> f false
69    and aux_bind f = function
70       | B.Abbr v1, B.Abbr v2
71       | B.Abst v1, B.Abst v2                   -> aux f (v1, v2)
72       | B.Void, B.Void                         -> f true
73       | _                                      -> f false
74    in
75    if S.eq t1 t2 then f true else aux f (t1, t2)
76
77 let get f m i =
78    B.get error0 f m.c i
79
80 (* to share *)
81 let rec step f ?(delta=false) ?(rt=false) m x = 
82 (*   L.warn "entering R.step"; *)
83    match x with
84    | B.Sort _                -> f m None x
85    | B.GRef (a, uri)         ->
86       let f = function
87          | _, _, B.Abbr v when delta ->
88             P.add ~gdelta:1 ();
89             step f ~delta ~rt m v
90          | _, _, B.Abst w when rt   ->
91             P.add ~grt:1 ();
92             step f ~delta ~rt m w        
93          | e, _, b                   ->
94             f m (Some (e, b)) x
95       in
96       E.get_obj f uri
97    | B.LRef (a, i)           ->
98       let f c a = function
99          | B.Abbr v          ->
100             P.add ~ldelta:1 ();
101             step f ~delta ~rt {m with c = c} v
102          | B.Abst w when rt ->
103             P.add ~lrt:1 ();
104             step f ~delta ~rt {m with c = c} w
105          | B.Void            ->
106             f {m with c = c} None x
107          | b                 ->
108             let f e = f {m with c = c} (Some (e, b)) x in 
109             B.apix C.err f a
110       in 
111       get f m i
112    | B.Cast (_, _, t)        ->
113       P.add ~tau:1 ();
114       step f ~delta ~rt m t
115    | B.Appl (_, v, t)        ->
116       step f ~delta ~rt {m with s = (m.c, v) :: m.s} t   
117    | B.Bind (a, B.Abst w, t) ->
118       begin match m.s with
119          | []          -> f m None x
120          | (c, v) :: s ->
121             P.add ~beta:1 ~upsilon:(List.length s) ();
122             let f c = step f ~delta ~rt {m with c = c; s = s} t in 
123             B.push f m.c ~c a (B.Abbr v) (* (B.Cast ([], w, v)) *)
124       end
125    | B.Bind (a, b, t)        ->
126       P.add ~upsilon:(List.length m.s) ();
127       let f c = step f ~delta ~rt {m with c = c} t in
128       B.push f m.c ~c:m.c a b
129
130 let domain f m t =
131    let f r = L.unbox level; f r in
132    let f m _ = function
133       | B.Bind (_, B.Abst w, _) -> f m w
134       | _                       -> error1 "not a function" m.c t
135    in
136    L.box level; log1 "Now scanning" m.c t;
137    step f ~delta:true ~rt:true m t
138
139 let push f m a b = 
140    assert (m.s = []);
141    let a, i = match b with
142       | B.Abst _ -> B.Apix m.i :: a, succ m.i
143       | _        -> a, m.i
144    in
145    let f c = f {m with c = c; i = i} in
146    B.push f m.c ~c:m.c a b
147
148 let rec ac_nfs f ~si r m1 a1 u m2 a2 t =
149    log2 "Now converting nfs" m1.c u m2.c t;
150    match a1, u, a2, t with
151       | _, B.Sort (_, h1), 
152         _, B.Sort (_, h2)                                ->
153          if h1 = h2 then f r else f false 
154       | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _   ->
155          if e1 = e2 then ac_stacks f ~si r m1 m2 else f false
156       | Some (e1, B.Abbr v1), _, Some (e2, B.Abbr v2), _ ->
157          if e1 = e2 then
158             let f r = 
159                if r then f r 
160                else begin  
161                   P.add ~gdelta:2 ();
162                   ac f ~si true m1 v1 m2 v2
163                end
164             in
165             ac_stacks f ~si r m1 m2
166          else if e1 < e2 then begin 
167             P.add ~gdelta:1 ();
168             step (ac_nfs f ~si r m1 a1 u) m2 v2
169          end else begin
170             P.add ~gdelta:1 ();
171             step (ac_nfs_rev f ~si r m2 a2 t) m1 v1
172          end
173       | _, _, Some (_, B.Abbr v2), _                     ->
174          P.add ~gdelta:1 ();
175          step (ac_nfs f ~si r m1 a1 u) m2 v2      
176       | Some (_, B.Abbr v1), _, _, _                     ->
177          P.add ~gdelta:1 ();
178          step (ac_nfs_rev f ~si r m2 a2 t) m1 v1            
179       | _, B.Bind (a1, (B.Abst w1 as b1), t1), 
180         _, B.Bind (a2, (B.Abst w2 as b2), t2)            ->
181          let g m1 m2 = ac f ~si r m1 t1 m2 t2 in
182          let g m1 = push (g m1) m2 a2 b2 in 
183          let f r = if r then push g m1 a1 b1 else f false in
184          ac f ~si r m1 w1 m2 w2      
185       | _, B.Sort _, _, B.Bind (a, b, t) when si         ->
186          P.add ~si:1 ();
187          let f m1 m2 = ac f ~si r m1 u m2 t in
188          let f m1 = push (f m1) m2 a b in
189          push f m1 a b
190       | _                                                -> f false
191
192 and ac_nfs_rev f ~si r m2 a2 t m1 a1 u = ac_nfs f ~si r m1 a1 u m2 a2 t
193
194 and ac f ~si r m1 t1 m2 t2 =
195 (*   L.warn "entering R.are_convertible"; *)
196    let g m1 a1 t1 = step (ac_nfs f ~si r m1 a1 t1) m2 t2 in 
197    if r = false then f false else step g m1 t1
198
199 and ac_stacks f ~si r m1 m2 =
200 (*   L.warn "entering R.are_convertible_stacks"; *)
201    if List.length m1.s <> List.length m2.s then f false else
202    let map f r (c1, v1) (c2, v2) =
203       let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
204       ac f ~si r m1 v1 m2 v2
205    in
206    C.list_fold_left2 f map r m1.s m2.s
207
208 (* Interface functions ******************************************************)
209
210 let empty_machine = { 
211    c = B.empty_context; s = []; i = 0
212 }
213
214 let get f m i =
215    assert (m.s = []);
216    let f c = f in
217    get f m i
218
219 let assert_conversion f ?(si=false) ?(rt=false) mw u w v = 
220    let f mu u =
221       L.box level; log2 "Now converting" mu.c u mw.c w;
222       let f r = 
223          if r then begin L.unbox level; f () end else error3 mw.c v w u
224       in
225       let g r = if r then f r else ac f ~si true mu u mw w in
226       if S.eq mu mw then are_alpha_convertible g u w else g false
227    in
228    if rt then domain f mw u else f mw u
229
230 let message1 st m t =
231    L.ct_items1 "In the context" m.c st t