]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgReduction.ml
- common/entity: new format for kernel entities
[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 are_alpha_convertible err f t1 t2 =
41    let rec aux f = function
42       | B.Sort (_, p1), B.Sort (_, p2)
43       | B.LRef (_, p1), B.LRef (_, p2)         ->
44          if p1 = p2 then f () else err ()
45       | B.GRef (_, u1), B.GRef (_, u2)         ->
46          if U.eq u1 u2 then f () else err ()
47       | B.Cast (_, v1, t1), B.Cast (_, v2, t2)         
48       | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
49          let f _ = aux f (t1, t2) in
50          aux f (v1, v2)
51       | B.Bind (b1, t1), B.Bind (b2, t2)       ->
52          let f _ = aux f (t1, t2) in
53          aux_bind f (b1, b2)
54       | _                                      -> err ()
55    and aux_bind f = function
56       | B.Abbr (_, v1), B.Abbr (_, v2)
57       | B.Abst (_, v1), B.Abst (_, v2)         -> aux f (v1, v2)
58       | B.Void _, B.Void _                     -> f ()
59       | _                                      -> err ()
60    in
61    if S.eq t1 t2 then f () else aux f (t1, t2)
62
63 let get err f m i =
64    B.get err f m.c i
65
66 (* to share *)
67 let rec step f ?(delta=false) ?(rt=false) m x = 
68 (*   L.warn "entering R.step"; *)
69    match x with
70    | B.Sort _                  -> f m None x
71    | B.GRef (_, uri)           ->
72       let f = function
73          | _, _, Y.Abbr v when delta ->
74             P.add ~gdelta:1 (); step f ~delta ~rt m v
75          | _, _, Y.Abst w when rt    ->
76             P.add ~grt:1 (); step f ~delta ~rt m w       
77          | a, _, Y.Abbr v            ->
78             let f e = f m (Some (e, B.Abbr (a, v))) x in
79             Y.apix C.err f a        
80          | a, _, Y.Abst w            ->
81             let f e = f m (Some (e, B.Abst (a, w))) x in
82             Y.apix C.err f a        
83       in
84       E.get_entity C.err f uri
85    | B.LRef (_, i)             ->
86       let f c = function
87          | B.Abbr (_, v)         ->
88             P.add ~ldelta:1 ();
89             step f ~delta ~rt {m with c = c} v
90          | B.Abst (_, w) when rt ->
91             P.add ~lrt:1 ();
92             step f ~delta ~rt {m with c = c} w
93          | B.Void _              ->
94             assert false
95          | B.Abst (a, _) as b    ->
96             let f e = f {m with c = c} (Some (e, b)) x in 
97             Y.apix C.err f a
98       in 
99       get C.err f m i
100    | B.Cast (_, _, t)          ->
101       P.add ~tau:1 ();
102       step f ~delta ~rt m t
103    | B.Appl (_, v, t)          ->
104       step f ~delta ~rt {m with s = (m.c, v) :: m.s} t   
105    | B.Bind (B.Abst (a, w), t) ->
106       begin match m.s with
107          | []          -> f m None x
108          | (c, v) :: s ->
109             P.add ~beta:1 ~upsilon:(List.length s) ();
110             let f c = step f ~delta ~rt {m with c = c; s = s} t in 
111             B.push f m.c ~c (B.abbr a v) (* (B.Cast ([], w, v)) *)
112       end
113    | B.Bind (b, t)             ->
114       P.add ~upsilon:(List.length m.s) ();
115       let f c = step f ~delta ~rt {m with c = c} t in
116       B.push f m.c ~c:m.c b
117
118 let push f m b = 
119    assert (m.s = []);
120    let b, i = match b with
121       | B.Abst (a, w) -> B.abst (Y.Apix m.i :: a) w, succ m.i
122       | b             -> b, m.i
123    in
124    let f c = f {m with c = c; i = i} in
125    B.push f m.c ~c:m.c b
126
127 let rec ac_nfs err f ~si m1 a1 u m2 a2 t =
128    log2 "Now converting nfs" m1.c u m2.c t;
129    match a1, u, a2, t with
130       | _, B.Sort (_, h1), _, B.Sort (_, h2)                       ->
131          if h1 = h2 then f () else err () 
132       | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _             ->
133          if e1 = e2 then ac_stacks err f m1 m2 else err ()
134       | Some (e1, B.Abbr (_, v1)), _, Some (e2, B.Abbr (_, v2)), _ ->
135          if e1 = e2 then
136             let err _ = P.add ~gdelta:2 (); ac err f ~si m1 v1 m2 v2 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_kam = { 
183    c = B.empty_lenv; s = []; i = 0
184 }
185
186 let get err f m i =
187    assert (m.s = []);
188    let f c = f in
189    get err 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 x = L.unbox level; f x 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 (* error reporting **********************************************************)
203
204 let pp_term m frm t = O.specs.L.pp_term m.c frm t
205
206 let pp_lenv frm m = O.specs.L.pp_lenv frm m.c
207
208 let specs = {
209    L.pp_term = pp_term; L.pp_lenv = pp_lenv
210 }