]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgReduction.ml
some interfaces improved
[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 type kam = {
22    c: B.lenv;
23    s: (B.lenv * 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 environment", "the term" in
33    L.log O.specs level (L.et_items1 sc c st t)
34
35 let log2 s cu u ct t =
36    let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
37    L.log O.specs level (L.et_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 err f m i =
63    B.get 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_entry C.err 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 C.err 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 _ = P.add ~gdelta:2 (); ac err f ~si m1 v1 m2 v2 in
134             ac_stacks err f m1 m2
135          else if e1 < e2 then begin 
136             P.add ~gdelta:1 ();
137             step (ac_nfs err f ~si m1 a1 u) m2 v2
138          end else begin
139             P.add ~gdelta:1 ();
140             step (ac_nfs_rev err f ~si m2 a2 t) m1 v1
141          end
142       | _, _, Some (_, B.Abbr (_, v2)), _                          ->
143          P.add ~gdelta:1 ();
144          step (ac_nfs err f ~si m1 a1 u) m2 v2      
145       | Some (_, B.Abbr (_, v1)), _, _, _                          ->
146          P.add ~gdelta:1 ();
147          step (ac_nfs_rev err f ~si m2 a2 t) m1 v1            
148       | _, B.Bind ((B.Abst (_, w1) as b1), t1), 
149         _, B.Bind ((B.Abst (_, w2) as b2), t2)                     ->
150          let f m1 m2 = ac err f ~si m1 t1 m2 t2 in
151          let f m1 = push (f m1) m2 b2 in 
152          let f _ = push f m1 b1 in
153          ac err f ~si:false m1 w1 m2 w2      
154       | _, B.Sort _, _, B.Bind (b, t) when si                      ->
155          P.add ~si:1 ();
156          let f m1 m2 = ac err f ~si m1 u m2 t in
157          let f m1 = push (f m1) m2 b in
158          push f m1 b
159       | _                                                          -> err ()
160
161 and ac_nfs_rev err f ~si m2 a2 t m1 a1 u = ac_nfs err f ~si m1 a1 u m2 a2 t
162
163 and ac err f ~si m1 t1 m2 t2 =
164 (*   L.warn "entering R.are_convertible"; *)
165    let f m1 a1 t1 = step (ac_nfs err f ~si m1 a1 t1) m2 t2 in 
166    step f m1 t1
167
168 and ac_stacks err f m1 m2 =
169 (*   L.warn "entering R.are_convertible_stacks"; *)
170    if List.length m1.s <> List.length m2.s then err () else
171    let map f (c1, v1) (c2, v2) =
172       let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
173       ac err f ~si:false m1 v1 m2 v2
174    in
175    C.list_iter2 f map m1.s m2.s
176
177 (* Interface functions ******************************************************)
178
179 let empty_kam = { 
180    c = B.empty_lenv; s = []; i = 0
181 }
182
183 let get err f m i =
184    assert (m.s = []);
185    let f c = f in
186    get err f m i
187
188 let xwhd f m t =
189    L.box level; log1 "Now scanning" m.c t;
190    let f m _ t = L.unbox level; f m t in
191    step f ~delta:true ~rt:true m t
192
193 let are_convertible err f ?(si=false) mu u mw w = 
194       L.box level; log2 "Now converting" mu.c u mw.c w;
195       let f x = L.unbox level; f x in
196       let err _ = ac err f ~si mu u mw w in
197 (*      if S.eq mu mw then are_alpha_convertible err f u w else *) err ()
198
199 (* error reporting **********************************************************)
200
201 let pp_term m frm t = O.specs.L.pp_term m.c frm t
202
203 let pp_lenv frm m = O.specs.L.pp_lenv frm m.c
204
205 let specs = {
206    L.pp_term = pp_term; L.pp_lenv = pp_lenv
207 }