]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/basic_rg/brgReduction.ml
basic_rg: bugfix in AST to allow attributes in global entries
[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 exception TypeError of B.message
22
23 type machine = {
24    c: B.context;
25    s: (B.context * B.term) list;
26    i: int
27 }
28
29 (* Internal functions *******************************************************)
30
31 let level = 5
32
33 let log1 s c t =
34    let sc, st = s ^ " in the context", "the term" in
35    L.log O.specs level (L.ct_items1 sc c st t)
36
37 let log2 s cu u ct t =
38    let s1, s2, s3 = s ^ " in the context", "the term", "and in the context" in
39    L.log O.specs level (L.ct_items2 s1 cu s2 u s3 ct s2 t)
40
41 let error0 i = 
42    let s = Printf.sprintf "local reference not found %u" i in
43    raise (TypeError (L.items1 s))
44
45 let error1 st c t =
46    let sc = "In the context" in
47    raise (TypeError (L.ct_items1 sc c st t))
48
49 let error3 c t1 t2 t3 =
50    let sc, st1, st2, st3 = 
51       "In the context", "the term", "is of type", "but must be of type"
52    in
53    raise (TypeError (L.ct_items3 sc c st1 t1 st2 t2 st3 t3))
54
55 let are_alpha_convertible f t1 t2 =
56    let rec aux f = function
57       | B.Sort (_, p1), B.Sort (_, p2)
58       | B.LRef (_, p1), B.LRef (_, p2)         -> f (p1 = p2)
59       | B.GRef (_, u1), B.GRef (_, u2)         -> f (U.eq u1 u2) 
60       | B.Cast (_, v1, t1), B.Cast (_, v2, t2)         
61       | B.Appl (_, v1, t1), B.Appl (_, v2, t2) ->
62          let f r = if r then aux f (t1, t2) else f r in
63          aux f (v1, v2)
64       | B.Bind (b1, t1), B.Bind (b2, t2)       ->
65          let f r = if r then aux f (t1, t2) else f r in
66          aux_bind f (b1, b2)
67       | _                                      -> f false
68    and aux_bind f = function
69       | B.Abbr (_, v1), B.Abbr (_, v2)
70       | B.Abst (_, v1), B.Abst (_, v2)         -> aux f (v1, v2)
71       | B.Void _, B.Void _                     -> f true
72       | _                                      -> f false
73    in
74    if S.eq t1 t2 then f true else aux f (t1, t2)
75
76 let get f m i =
77    B.get error0 f m.c i
78
79 (* to share *)
80 let rec step f ?(delta=false) ?(rt=false) m x = 
81 (*   L.warn "entering R.step"; *)
82    match x with
83    | B.Sort _                  -> f m None x
84    | B.GRef (_, uri)           ->
85       let f = function
86          | _, _, B.Abbr (_, v) when delta ->
87             P.add ~gdelta:1 (); step f ~delta ~rt m v
88          | _, _, B.Abst (_, w) when rt    ->
89             P.add ~grt:1 (); step f ~delta ~rt m w       
90          | _, _, B.Void _                 ->
91             assert false
92          | e, _, b                        ->
93             f m (Some (e, b)) x
94       in
95       E.get_obj f uri
96    | B.LRef (_, i)             ->
97       let f c = function
98          | B.Abbr (_, v)         ->
99             P.add ~ldelta:1 ();
100             step f ~delta ~rt {m with c = c} v
101          | B.Abst (_, w) when rt ->
102             P.add ~lrt:1 ();
103             step f ~delta ~rt {m with c = c} w
104          | B.Void _              ->
105             assert false
106          | B.Abst (a, _) as b    ->
107             let f e = f {m with c = c} (Some (e, b)) x in 
108             B.apix C.err f a
109       in 
110       get f m i
111    | B.Cast (_, _, t)          ->
112       P.add ~tau:1 ();
113       step f ~delta ~rt m t
114    | B.Appl (_, v, t)          ->
115       step f ~delta ~rt {m with s = (m.c, v) :: m.s} t   
116    | B.Bind (B.Abst (a, w), t) ->
117       begin match m.s with
118          | []          -> f m None x
119          | (c, v) :: s ->
120             P.add ~beta:1 ~upsilon:(List.length s) ();
121             let f c = step f ~delta ~rt {m with c = c; s = s} t in 
122             B.push f m.c ~c (B.abbr a v) (* (B.Cast ([], w, v)) *)
123       end
124    | B.Bind (b, t)             ->
125       P.add ~upsilon:(List.length m.s) ();
126       let f c = step f ~delta ~rt {m with c = c} t in
127       B.push f m.c ~c:m.c b
128
129 let domain f m t =
130    let f r = L.unbox level; f r in
131    let f m _ = function
132       | B.Bind (B.Abst (_, w), _) -> f m w
133       | _                         -> error1 "not a function" m.c t
134    in
135    L.box level; log1 "Now scanning" m.c t;
136    step f ~delta:true ~rt:true m t
137
138 let push f m b = 
139    assert (m.s = []);
140    let b, i = match b with
141       | B.Abst (a, w) -> B.abst (B.Apix m.i :: a) w, succ m.i
142       | b             -> b, m.i
143    in
144    let f c = f {m with c = c; i = i} in
145    B.push f m.c ~c:m.c b
146
147 let rec ac_nfs f ~si r m1 a1 u m2 a2 t =
148    log2 "Now converting nfs" m1.c u m2.c t;
149    match a1, u, a2, t with
150       | _, B.Sort (_, h1), _, B.Sort (_, h2)                       ->
151          if h1 = h2 then f r else f false 
152       | Some (e1, B.Abst _), _, Some (e2, B.Abst _), _             ->
153          if e1 = e2 then ac_stacks f r m1 m2 else f false
154       | Some (e1, B.Abbr (_, v1)), _, Some (e2, B.Abbr (_, v2)), _ ->
155          if e1 = e2 then
156             let f r = 
157                if r then f r 
158                else begin  
159                   P.add ~gdelta:2 ();
160                   ac f ~si true m1 v1 m2 v2
161                end
162             in
163             ac_stacks f r m1 m2
164          else if e1 < e2 then begin 
165             P.add ~gdelta:1 ();
166             step (ac_nfs f ~si r m1 a1 u) m2 v2
167          end else begin
168             P.add ~gdelta:1 ();
169             step (ac_nfs_rev f ~si r m2 a2 t) m1 v1
170          end
171       | _, _, Some (_, B.Abbr (_, v2)), _                          ->
172          P.add ~gdelta:1 ();
173          step (ac_nfs f ~si r m1 a1 u) m2 v2      
174       | Some (_, B.Abbr (_, v1)), _, _, _                          ->
175          P.add ~gdelta:1 ();
176          step (ac_nfs_rev f ~si r m2 a2 t) m1 v1            
177       | _, B.Bind ((B.Abst (_, w1) as b1), t1), 
178         _, B.Bind ((B.Abst (_, w2) as b2), t2)                     ->
179          let g m1 m2 = ac f ~si r m1 t1 m2 t2 in
180          let g m1 = push (g m1) m2 b2 in 
181          let f r = if r then push g m1 b1 else f false in
182          ac f ~si:false r m1 w1 m2 w2      
183       | _, B.Sort _, _, B.Bind (b, t) when si                      ->
184          P.add ~si:1 ();
185          let f m1 m2 = ac f ~si r m1 u m2 t in
186          let f m1 = push (f m1) m2 b in
187          push f m1 b
188       | _                                                          -> f false
189
190 and ac_nfs_rev f ~si r m2 a2 t m1 a1 u = ac_nfs f ~si r m1 a1 u m2 a2 t
191
192 and ac f ~si r m1 t1 m2 t2 =
193 (*   L.warn "entering R.are_convertible"; *)
194    let g m1 a1 t1 = step (ac_nfs f ~si r m1 a1 t1) m2 t2 in 
195    if r = false then f false else step g m1 t1
196
197 and ac_stacks f r m1 m2 =
198 (*   L.warn "entering R.are_convertible_stacks"; *)
199    if List.length m1.s <> List.length m2.s then f false else
200    let map f r (c1, v1) (c2, v2) =
201       let m1, m2 = {m1 with c = c1; s = []}, {m2 with c = c2; s = []} in
202       ac f ~si:false r m1 v1 m2 v2
203    in
204    C.list_fold_left2 f map r m1.s m2.s
205
206 (* Interface functions ******************************************************)
207
208 let empty_machine = { 
209    c = B.empty_context; s = []; i = 0
210 }
211
212 let get f m i =
213    assert (m.s = []);
214    let f c = f in
215    get f m i
216
217 let assert_conversion f ?(si=false) ?(rt=false) mw u w v = 
218    let f mu u =
219       L.box level; log2 "Now converting" mu.c u mw.c w;
220       let f r = 
221          if r then begin L.unbox level; f () end else error3 mw.c v w u
222       in
223       let g r = if r then f r else ac f ~si true mu u mw w in
224 (*      if S.eq mu mw then are_alpha_convertible g u w else *) g false
225    in
226    if rt then domain f mw u else f mw u
227
228 let message1 st m t =
229    L.ct_items1 "In the context" m.c st t