]> matita.cs.unibo.it Git - helm.git/blob - lambda-delta/basic_rg/brgReduction.ml
made executable again
[helm.git] / 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 type machine = {
22    c: B.context;
23    s: (B.context * B.term) list;
24    i: int
25 }
26
27 (* Internal functions *******************************************************)
28
29 let level = 5
30
31 let log1 s c t =
32    let sc, st = s ^ " in the context", "the term" in
33    L.log O.specs level (L.ct_items1 sc c st t)
34
35 let log2 s cu u ct t =
36    let s1, s2, s3 = s ^ " in the context", "the term", "and in the context" in
37    L.log O.specs level (L.ct_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
38
39 let are_alpha_convertible err f t1 t2 =
40    let rec aux f = function
41       | B.Sort (_, p1), B.Sort (_, p2)
42       | B.LRef (_, p1), B.LRef (_, p2)         ->
43          if p1 = p2 then f () else err ()
44       | B.GRef (_, u1), B.GRef (_, u2)         ->
45          if U.eq u1 u2 then f () else err ()
46       | B.Cast (_, v1, t1), B.Cast (_, v2, t2)         
47       | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
48          let f () = aux f (t1, t2) in
49          aux f (v1, v2)
50       | B.Bind (b1, t1), B.Bind (b2, t2)       ->
51          let f () = aux f (t1, t2) in
52          aux_bind f (b1, b2)
53       | _                                      -> err ()
54    and aux_bind f = function
55       | B.Abbr (_, v1), B.Abbr (_, v2)
56       | B.Abst (_, v1), B.Abst (_, v2)         -> aux f (v1, v2)
57       | B.Void _, B.Void _                     -> f ()
58       | _                                      -> err ()
59    in
60    if S.eq t1 t2 then f () else aux f (t1, t2)
61
62 let get f m i =
63    B.get C.err f m.c i
64
65 (* to share *)
66 let rec step f ?(delta=false) ?(rt=false) m x = 
67 (*   L.warn "entering R.step"; *)
68    match x with
69    | B.Sort _                  -> f m None x
70    | B.GRef (_, uri)           ->
71       let f = function
72          | _, _, B.Abbr (_, v) when delta ->
73             P.add ~gdelta:1 (); step f ~delta ~rt m v
74          | _, _, B.Abst (_, w) when rt    ->
75             P.add ~grt:1 (); step f ~delta ~rt m w       
76          | _, _, B.Void _                 ->
77             assert false
78          | e, _, b                        ->
79             f m (Some (e, b)) x
80       in
81       E.get_obj f uri
82    | B.LRef (_, i)             ->
83       let f c = function
84          | B.Abbr (_, v)         ->
85             P.add ~ldelta:1 ();
86             step f ~delta ~rt {m with c = c} v
87          | B.Abst (_, w) when rt ->
88             P.add ~lrt:1 ();
89             step f ~delta ~rt {m with c = c} w
90          | B.Void _              ->
91             assert false
92          | B.Abst (a, _) as b    ->
93             let f e = f {m with c = c} (Some (e, b)) x in 
94             B.apix C.err f a
95       in 
96       get f m i
97    | B.Cast (_, _, t)          ->
98       P.add ~tau:1 ();
99       step f ~delta ~rt m t
100    | B.Appl (_, v, t)          ->
101       step f ~delta ~rt {m with s = (m.c, v) :: m.s} t   
102    | B.Bind (B.Abst (a, w), t) ->
103       begin match m.s with
104          | []          -> f m None x
105          | (c, v) :: s ->
106             P.add ~beta:1 ~upsilon:(List.length s) ();
107             let f c = step f ~delta ~rt {m with c = c; s = s} t in 
108             B.push f m.c ~c (B.abbr a v) (* (B.Cast ([], w, v)) *)
109       end
110    | B.Bind (b, t)             ->
111       P.add ~upsilon:(List.length m.s) ();
112       let f c = step f ~delta ~rt {m with c = c} t in
113       B.push f m.c ~c:m.c b
114
115 let push f m b = 
116    assert (m.s = []);
117    let b, i = match b with
118       | B.Abst (a, w) -> B.abst (B.Apix m.i :: a) w, succ m.i
119       | b             -> b, m.i
120    in
121    let f c = f {m with c = c; i = i} in
122    B.push f m.c ~c:m.c b
123
124 let rec ac_nfs err f ~si m1 a1 u m2 a2 t =
125    log2 "Now converting nfs" m1.c u m2.c t;
126    match a1, u, a2, t with
127       | _, B.Sort (_, h1), _, B.Sort (_, h2)                       ->
128          if h1 = h2 then f () else err () 
129       | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _             ->
130          if e1 = e2 then ac_stacks err f m1 m2 else err ()
131       | Some (e1, B.Abbr (_, v1)), _, Some (e2, B.Abbr (_, v2)), _ ->
132          if e1 = e2 then
133             let err () =   
134                P.add ~gdelta:2 ();
135                ac err f ~si m1 v1 m2 v2
136             in
137             ac_stacks err f m1 m2
138          else if e1 < e2 then begin 
139             P.add ~gdelta:1 ();
140             step (ac_nfs err f ~si m1 a1 u) m2 v2
141          end else begin
142             P.add ~gdelta:1 ();
143             step (ac_nfs_rev err f ~si m2 a2 t) m1 v1
144          end
145       | _, _, Some (_, B.Abbr (_, v2)), _                          ->
146          P.add ~gdelta:1 ();
147          step (ac_nfs err f ~si m1 a1 u) m2 v2      
148       | Some (_, B.Abbr (_, v1)), _, _, _                          ->
149          P.add ~gdelta:1 ();
150          step (ac_nfs_rev err f ~si m2 a2 t) m1 v1            
151       | _, B.Bind ((B.Abst (_, w1) as b1), t1), 
152         _, B.Bind ((B.Abst (_, w2) as b2), t2)                     ->
153          let f m1 m2 = ac err f ~si m1 t1 m2 t2 in
154          let f m1 = push (f m1) m2 b2 in 
155          let f () = push f m1 b1 in
156          ac err f ~si:false m1 w1 m2 w2      
157       | _, B.Sort _, _, B.Bind (b, t) when si                      ->
158          P.add ~si:1 ();
159          let f m1 m2 = ac err f ~si m1 u m2 t in
160          let f m1 = push (f m1) m2 b in
161          push f m1 b
162       | _                                                          -> err ()
163
164 and ac_nfs_rev err f ~si m2 a2 t m1 a1 u = ac_nfs err f ~si m1 a1 u m2 a2 t
165
166 and ac err f ~si m1 t1 m2 t2 =
167 (*   L.warn "entering R.are_convertible"; *)
168    let f m1 a1 t1 = step (ac_nfs err f ~si m1 a1 t1) m2 t2 in 
169    step f m1 t1
170
171 and ac_stacks err f m1 m2 =
172 (*   L.warn "entering R.are_convertible_stacks"; *)
173    if List.length m1.s <> List.length m2.s then err () else
174    let map f (c1, v1) (c2, v2) =
175       let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
176       ac err f ~si:false m1 v1 m2 v2
177    in
178    C.list_iter2 f map m1.s m2.s
179
180 (* Interface functions ******************************************************)
181
182 let empty_machine = { 
183    c = B.empty_context; s = []; i = 0
184 }
185
186 let get f m i =
187    assert (m.s = []);
188    let f c = f in
189    get f m i
190
191 let xwhd f m t =
192    L.box level; log1 "Now scanning" m.c t;
193    let f m _ t = L.unbox level; f m t in
194    step f ~delta:true ~rt:true m t
195
196 let are_convertible err f ?(si=false) mu u mw w = 
197       L.box level; log2 "Now converting" mu.c u mw.c w;
198       let f () = L.unbox level; f () in
199       let err () = ac err f ~si mu u mw w in
200 (*      if S.eq mu mw then are_alpha_convertible err f u w else *) err ()
201
202 let message1 st1 m t1 =
203    L.ct_items1 "In the context" m.c st1 t1
204
205 let message3 st1 st2 ?sm3 st3 m1 t1 t2 ?m3 t3 =
206    let sm1 = "In the context" in
207    match sm3, m3 with
208       | Some sm3, Some m3 ->
209          L.ct_items3 sm1 m1.c st1 t1 st2 t2 ~sc3:sm3 ~c3:m3.c st3 t3
210       | None, None        ->
211          L.ct_items3 sm1 m1.c st1 t1 st2 t2 st3 t3
212       | _                 -> assert false