]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgReduction.ml
Additional contribs.
[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    e: B.lenv;                 (* environment *)
24    s: (B.lenv * B.term) list; (* stack       *)
25    d: int                     (* depth       *)
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 _, c, a, b = B.get m.e i in c, a, b
71
72 (* to share *)
73 let rec step st m x = 
74 (*   L.warn "entering R.step"; *)
75    match x with
76    | B.Sort _                -> m, None, x
77    | B.GRef (_, uri)         ->
78       begin match E.get_entity uri with
79          | _, _, Y.Abbr v when st.Y.delta ->
80             P.add ~gdelta:1 (); step st m v
81          | _, _, Y.Abst w when st.Y.rt    ->
82             P.add ~grt:1 (); step st m w         
83          | a, _, Y.Abbr v                 ->
84             let e = Y.apix C.err C.start a in
85             m, Some (e, a, B.Abbr v), x   
86          | a, _, Y.Abst w                 ->
87             let e = Y.apix C.err C.start a in
88             m, Some (e, a, B.Abst w), x
89          | _, _, Y.Void                   -> assert false
90       end
91    | B.LRef (_, i)           ->
92       begin match get m i with
93          | c, _, B.Abbr v              ->
94             P.add ~ldelta:1 ();
95             step st {m with e = c} v
96          | c, _, B.Abst w when st.Y.rt ->
97             P.add ~lrt:1 ();
98             step st {m with e = c} w
99          | c, _, B.Void                ->
100             assert false
101          | c, a, (B.Abst _ as b)       ->
102             let e = Y.apix C.err C.start a in
103             {m with e = c}, Some (e, a, b), x
104       end
105    | B.Cast (_, _, t)        ->
106       P.add ~tau:1 ();
107       step st m t
108    | B.Appl (_, v, t)        ->
109       step st {m with s = (m.e, v) :: m.s} t   
110    | B.Bind (a, B.Abst w, t) ->
111       begin match m.s with
112          | []          -> m, None, x
113          | (c, v) :: s ->
114             P.add ~beta:1 ~upsilon:(List.length s) ();
115             let e = B.push m.e c a (B.abbr v) (* (B.Cast ([], w, v)) *) in 
116             step st {m with e = e; s = s} t
117       end
118    | B.Bind (a, b, t)        ->
119       P.add ~upsilon:(List.length m.s) ();
120       let e = B.push m.e m.e a b in 
121       step st {m with e = e} t
122
123 let push m a b = 
124    assert (m.s = []);
125    let a, d = match b with
126       | B.Abst _ -> Y.Apix m.d :: a, succ m.d
127       | b        -> a, m.d
128    in
129    let e = B.push m.e m.e a b in
130    {m with e = e; d = d}
131
132 let rec ac_nfs st (m1, r1, u) (m2, r2, t) =
133    log2 "Now converting nfs" m1.e u m2.e t;
134    match r1, u, r2, t with
135       | _, B.Sort (_, h1), _, B.Sort (_, h2)                   ->
136          h1 = h2  
137       | Some (e1, _, B.Abst _), _, Some (e2, _, B.Abst _), _   ->
138          if e1 = e2 then ac_stacks st m1 m2 else false
139       | Some (e1, _, B.Abbr v1), _, Some (e2, _, B.Abbr v2), _ ->
140          if e1 = e2 then
141             if ac_stacks st m1 m2 then true else begin
142                P.add ~gdelta:2 (); ac st m1 v1 m2 v2
143             end
144          else if e1 < e2 then begin 
145             P.add ~gdelta:1 ();
146             ac_nfs st (m1, r1, u) (step st m2 v2)
147          end else begin
148             P.add ~gdelta:1 ();
149             ac_nfs st (step st m1 v1) (m2, r2, t) 
150          end
151       | _, _, Some (_, _, B.Abbr v2), _                        ->
152          P.add ~gdelta:1 ();
153          ac_nfs st (m1, r1, u) (step st m2 v2)      
154       | Some (_, _, B.Abbr v1), _, _, _                        ->
155          P.add ~gdelta:1 ();
156          ac_nfs st (step st m1 v1) (m2, r2, t)             
157       | _, B.Bind (a1, (B.Abst w1 as b1), t1), 
158         _, B.Bind (a2, (B.Abst w2 as b2), t2)                  ->
159          if ac {st with Y.si = false} m1 w1 m2 w2 then
160             ac st (push m1 a1 b1) t1 (push m2 a2 b2) t2
161          else false
162       | _, B.Sort _, _, B.Bind (a, b, t) when st.Y.si          ->
163          P.add ~si:1 ();
164          ac st (push m1 a b) u (push m2 a b) t
165       | _                                                      -> false
166
167 and ac st m1 t1 m2 t2 =
168 (*   L.warn "entering R.are_convertible"; *)
169    ac_nfs st (step st m1 t1) (step st m2 t2)
170
171 and ac_stacks st m1 m2 =
172 (*   L.warn "entering R.are_convertible_stacks"; *)
173 (*   if List.length m1.s <> List.length m2.s then false else *)
174    let map (c1, v1) (c2, v2) =
175       let m1, m2 = {m1 with e = c1; s = []}, {m2 with e = c2; s = []} in
176       ac {st with Y.si = false} m1 v1 m2 v2
177    in
178    list_and map (m1.s, m2.s)
179
180 (* Interface functions ******************************************************)
181
182 let empty_kam = { 
183    e = B.empty; s = []; d = 0
184 }
185
186 let get m i =
187    assert (m.s = []);
188    let _, _, _, b = B.get m.e i in b
189
190 let xwhd st m t =
191    L.box level; log1 "Now scanning" m.e t;   
192    let m, _, t = step {st with Y.delta = true; Y.rt = true} m t in
193    L.unbox level; m, t
194
195 let are_convertible st mu u mw w = 
196    L.box level; log2 "Now converting" mu.e u mw.e w;
197    let r = ac {st with Y.delta = false; Y.rt = false} mu u mw w in   
198    L.unbox level; r
199 (*    let err _ = in 
200       if S.eq mu mw then are_alpha_convertible err f u w else err () *)
201
202 (* error reporting **********************************************************)
203
204 let pp_term m frm t = O.specs.L.pp_term m.e frm t
205
206 let pp_lenv frm m = O.specs.L.pp_lenv frm m.e
207
208 let specs = {
209    L.pp_term = pp_term; L.pp_lenv = pp_lenv
210 }