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