]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_rg/brgReduction.ml
- we removed a flag from the kernel status
[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 W  = Share
14 module L  = Log
15 module G  = Options
16 module H  = Hierarchy
17 module N  = Level
18 module E  = Entity
19 module O  = Output
20 module S  = Status
21 module B  = Brg
22 module BO = BrgOutput
23 module BE = BrgEnvironment
24
25 type rtm = {
26    e: B.lenv;                 (* environment              *)
27    s: (B.lenv * B.term) list; (* stack                    *)
28    l: int;                    (* level                    *)
29    d: int;                    (* inferred type iterations *)
30    n: int option;             (* expected type iterations *)
31 }
32
33 type message = (rtm, B.term) L.message
34
35 (* Internal functions *******************************************************)
36
37 let level = 4
38
39 let sublevel = succ level
40
41 let log1 st s c t =
42    let s1, s2 = s ^ " in the environment", "the term" in
43    L.log st BO.specs level (L.et_items1 s1 c s2 t)
44
45 let log2 st 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 st 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 tsteps m = match m.n with
87    | Some n when n > m.d -> n - m.d
88    | _                   -> 0
89
90 let get m i =
91    let _, c, a, b = B.get m.e i in c, a, b
92
93 (* to share *)
94 let rec step st m x = 
95    if !G.trace >= sublevel then 
96    log1 st.S.lenv (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;
97    match x with
98    | B.Sort (a, h)                ->
99       if assert_tstep m false then
100          step st (tstep m) (B.Sort (a, H.apply h))      
101       else m, x, None
102    | B.GRef (_, uri)              ->
103       begin match BE.get_entity uri with
104          | _, _, _, E.Abbr v ->
105             if m.n = None || !G.expand then begin
106                if !G.summary then O.add ~gdelta:1 ();
107                step st m v
108             end else
109                m, x, Some v
110          | _, _, _, E.Abst w ->
111             if assert_tstep m true then begin
112                if !G.summary then O.add ~grt:1 (); 
113                step st (tstep m) w
114             end else
115              m, x, None   
116          | _, _, _, E.Void   ->
117             assert false
118       end
119    | B.LRef (_, i)                ->
120       begin match get m i with
121          | c, _, B.Abbr v      ->
122             if !G.summary then O.add ~ldelta:1 ();
123             step st {m with e = c} v
124          | c, a, B.Abst (_, w) ->
125             if assert_tstep m true then begin
126                if !G.summary then O.add ~lrt:1 ();
127                step st {(tstep m) with e = c} w
128             end else
129                m, B.LRef (a, i), None
130          | _, _, B.Void        ->
131             assert false
132       end
133    | B.Cast (_, u, t)             ->
134       if assert_tstep m false then begin
135          if !G.summary then O.add ~e:1 ();
136          step st (tstep m) u
137       end else begin
138          if !G.summary then O.add ~epsilon:1 ();
139          step st m t
140       end
141    | B.Appl (_, v, t)             ->
142       step st {m with s = (m.e, v) :: m.s} t   
143    | B.Bind (a, B.Abst (n, w), t) ->
144 (*      if not !G.cc && st.S.si && N.to_string st.S.lenv n = "0" then begin
145          if !G.summary then O.add ~upsilon:1 ();
146          let e = B.push m.e m.e a B.Void in 
147          step st {m with e = e} t
148       end else *) begin match m.s with
149          | []          ->
150             let i = tsteps m in
151             if i = 0 then m, x, None else
152             let n = N.minus st.S.lenv n i in
153             m, B.Bind (a, B.Abst (n, w), t), None
154          | (c, v) :: s ->
155             if !G.cc && not (N.assert_not_zero st.S.lenv n) then assert false;
156             if !G.summary then O.add ~beta:1 ~theta:(List.length s) ();
157             let v = if assert_tstep m false then B.Cast (E.empty_node, w, v) else v in
158             let e = B.push m.e c a (B.abbr v) in
159             step st {m with e = e; s = s} t
160       end
161    | B.Bind (a, b, t)        ->
162       if !G.summary then O.add ~theta:(List.length m.s) ();
163       let e = B.push m.e m.e a b in 
164       step st {m with e = e} t
165
166 let reset m ?(e=m.e) n =
167    {m with e = e; n = n; s = []; d = 0} 
168
169 let assert_iterations m1 m2 = match m1.n, m2.n with
170       | Some n1, Some n2 -> n1 - m1.d = n2 - m2.d
171       | _                -> false 
172
173 let push m a b = 
174    let a, l = match b with
175       | B.Abst _ -> {a with E.n_apix = m.l}, succ m.l
176       | _        -> a, m.l
177    in
178    let e = B.push m.e m.e a b in
179    {m with e = e; l = l}
180
181 let rec ac_nfs st (m1, t1, r1) (m2, t2, r2) =
182    if !G.trace >= level then log2 st.S.lenv "Now converting nfs" m1.e t1 m2.e t2;
183    match t1, r1, t2, r2 with
184       | B.Sort (_, h1), _, B.Sort (_, h2), _         ->
185          h1 = h2
186       | B.LRef ({E.n_apix = e1}, _), _, 
187         B.LRef ({E.n_apix = e2}, _), _               ->
188          if e1 = e2 then ac_stacks st m1 m2 else false
189       | B.GRef (_, u1), None, B.GRef (_, u2), None   ->
190          if U.eq u1 u2 && assert_iterations m1 m2 then ac_stacks st m1 m2 else false
191       | B.GRef ({E.n_apix = e1}, u1), Some v1, 
192         B.GRef ({E.n_apix = e2}, u2), Some v2        ->
193          if e1 < e2 then begin 
194             if !G.summary then O.add ~gdelta:1 ();
195             ac_nfs st (m1, t1, r1) (step st m2 v2)
196          end else if e2 < e1 then begin
197             if !G.summary then O.add ~gdelta:1 ();
198             ac_nfs st (step st m1 v1) (m2, t2, r2) 
199          end else if U.eq u1 u2 && assert_iterations m1 m2 && ac_stacks st m1 m2 then true
200          else begin
201             if !G.summary then O.add ~gdelta:2 ();
202             ac st m1 v1 m2 v2
203          end 
204       | _, _, B.GRef _, Some v2                      ->
205          if !G.summary then O.add ~gdelta:1 ();
206          ac_nfs st (m1, t1, r1) (step st m2 v2)
207       | B.GRef _, Some v1, _, _                      ->
208          if !G.summary then O.add ~gdelta:1 ();
209          ac_nfs st (step st m1 v1) (m2, t2, r2)
210       | B.Bind (a1, (B.Abst (n1, w1) as b1), t1), _, 
211         B.Bind (a2, (B.Abst (n2, w2) as b2), t2), _  ->
212          if !G.cc && not (N.assert_equal st.S.lenv n1 n2) then false else
213          if ac {st with S.si = false} (reset m1 zero) w1 (reset m2 zero) w2 then
214             ac st (push m1 a1 b1) t1 (push m2 a2 b2) t2
215          else false
216       | B.Sort _, _, B.Bind (a, B.Abst (n, _), t), _ ->
217          if st.S.si then
218             if !G.cc && not (N.assert_zero st.S.lenv n) then false else begin
219             if !G.summary then O.add ~upsilon:1 ();
220             ac st (push m1 a B.Void) t1 (push m2 a B.Void) t end
221          else false
222       | _                                            -> false
223
224 and ac st m1 t1 m2 t2 =
225 (*   L.warn "entering R.are_convertible"; *)
226    ac_nfs st (step st m1 t1) (step st m2 t2)
227
228 and ac_stacks st m1 m2 =
229 (*   L.warn "entering R.are_convertible_stacks"; *)
230    if List.length m1.s <> List.length m2.s then false else
231    let map (c1, v1) (c2, v2) =
232       let m1, m2 = reset m1 ~e:c1 zero, reset m2 ~e:c2 zero in
233       ac {st with S.si = false} m1 v1 m2 v2
234    in
235    list_and map (m1.s, m2.s)
236
237 (* Interface functions ******************************************************)
238
239 let empty_rtm = { 
240    e = B.empty; s = []; l = 0; d = 0; n = None
241 }
242
243 let get m i =
244    assert (m.s = []);
245    let _, _, _, b = B.get m.e i in b
246
247 let xwhd st m n t =
248    if !G.trace >= level then log1 st.S.lenv "Now scanning" m.e t;   
249    let m, t, _ = step st (reset m n) t in
250    m, t
251
252 let are_convertible st m1 n1 t1 m2 n2 t2 = 
253    if !G.trace >= level then log2 st.S.lenv "Now converting" m1.e t1 m2.e t2;
254    let r = ac st (reset m1 n1) t1 (reset m2 n2) t2 in
255    r
256 (*    let err _ = in 
257       if W.eq mu mw then are_alpha_convertible err f u w else err () *)
258
259 (* error reporting **********************************************************)
260
261 let pp_term st m och t = BO.specs.L.pp_term st m.e och t
262
263 let pp_lenv st och m = BO.specs.L.pp_lenv st och m.e
264
265 let specs = {
266    L.pp_term = pp_term; L.pp_lenv = pp_lenv
267 }