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