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