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