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