]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/basic_ag/bagReduction.ml
update in helena
[helm.git] / helm / software / helena / src / basic_ag / bagReduction.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  = Marks
16 module G  = Options
17 module E  = Entity
18 module Z  = Bag
19 module ZO = BagOutput
20 module ZE = BagEnvironment
21 module ZS = BagSubstitution
22
23 IFDEF TYPE THEN
24
25 type machine = {
26    i: int;
27    c: Z.lenv;
28    s: Z.term list
29 }
30
31 type whd_result =
32    | Sort_ of int
33    | LRef_ of P.mark * Z.term option
34    | GRef_ of Z.entity
35    | Bind_ of Z.b_attrs * P.mark * Z.term * Z.term
36
37 type ho_whd_result =
38    | Sort of int
39    | Abst of Z.term
40
41 (* Internal functions *******************************************************)
42
43 let level = 5
44
45 let term_of_whdr = function
46    | Sort_ h              -> Z.Sort h
47    | LRef_ (i, _)         -> Z.LRef i
48    | GRef_ (_, _, uri, _) -> Z.GRef uri
49    | Bind_ (a, l, w, t)   -> Z.bind_abst a l w t
50
51 let log1 st s c t =
52    let s1, s2 = s ^ " in the environment", "the term" in
53    L.log st ZO.specs (pred level) (L.et_items1 s1 c s2 t)
54
55 let log2 st s cu u ct t =
56    let s1, s2, s3 = s ^ " in the environment", "the term", "and in the environment" in
57    L.log st ZO.specs (pred level) (L.et_items2 s1 cu s2 u ~sc2:s3 ~c2:ct s2 t)
58
59 let empty_machine = {i = 0; c = Z.empty_lenv; s = []}
60
61 let inc m = {m with i = succ m.i}
62
63 let unwind_to_term f m t =
64    let map f t (y, l, b) = f (Z.Bind (y, l, b, t)) in
65    let f mc = C.list_fold_left f map t mc in
66    Z.contents f m.c
67
68 let unwind_stack f m =
69    let map f v = unwind_to_term f m v in
70    C.list_map f map m.s
71
72 let get f c m i =
73    let f _ b = f b in
74    let f c = Z.get C.err f c i in
75    Z.append f c m.c
76
77 let push msg f c m a l w = 
78    assert (m.s = []);
79    let f w = Z.push msg f c a l (Z.Abst w) in
80    unwind_to_term f m w
81
82 (* to share *)
83 let rec whd f c m x = 
84 (*   L.warn "entering R.whd"; *)
85    match x with
86    | Z.Sort h                   -> f m (Sort_ h)
87    | Z.GRef uri                 ->
88       let f entry = f m (GRef_ entry) in
89       ZE.get_entity f uri
90    | Z.LRef i                   ->
91       let f = function
92          | Z.Void   -> f m (LRef_ (i, None))
93          | Z.Abst t -> f m (LRef_ (i, Some t))
94          | Z.Abbr t -> whd f c m t
95       in
96       get f c m i
97    | Z.Cast (_, t)              -> whd f c m t
98    | Z.Appl (v, t)              -> whd f c {m with s = v :: m.s} t   
99    | Z.Bind (y, l, Z.Abst w, t) ->
100       begin match m.s with
101          | []      -> f m (Bind_ (y, l, w, t))
102          | v :: tl -> 
103             let nl = P.new_mark () in
104             let f mc = ZS.subst (whd f c {m with c = mc; s = tl}) nl l t in
105             Z.push "!" f m.c y nl (Z.Abbr (Z.Cast (w, v)))
106       end
107    | Z.Bind (y, l, b, t)        -> 
108       let nl = P.new_mark () in
109       let f mc = ZS.subst (whd f c {m with c = mc}) nl l t in
110       Z.push "!" f m.c y nl b
111
112 (* Interface functions ******************************************************)
113
114 let rec ho_whd f c m x =
115 (*   L.warn "entering R.ho_whd"; *)
116    let aux m = function
117       | Sort_ h                        -> f (Sort h)
118       | Bind_ (_, _, w, _)             -> 
119          let f w = f (Abst w) in unwind_to_term f m w
120       | LRef_ (_, Some w)              -> ho_whd f c m w
121       | GRef_ (_, _, _, E.Abst (_, w)) -> ho_whd f c m w  
122       | GRef_ (_, _, _, E.Abbr (_, v)) -> ho_whd f c m v
123       | LRef_ (_, None)                -> assert false
124       | GRef_ (_, _, _, E.Void)        -> assert false
125    in
126    whd aux c m x
127    
128 let ho_whd f st c t =
129 IFDEF TRACE THEN
130    if !G.ct >= level then log1 st "Now scanning" c t
131 ELSE () END;
132    ho_whd f c empty_machine t
133
134 let rec are_convertible f st a c m1 t1 m2 t2 =
135 (*   L.warn "entering R.are_convertible"; *)
136    let rec aux m1 r1 m2 r2 =
137 (*   L.warn "entering R.are_convertible_aux"; *)
138 IFDEF TRACE THEN
139    let u, t = term_of_whdr r1, term_of_whdr r2 in
140    if !G.ct >= level then log2 st "Now really converting" c u c t
141 ELSE () END;   
142    match r1, r2 with
143       | Sort_ k1, Sort_ k2                            ->
144          if k1 = k2 then f a else f false 
145       | LRef_ (i1, _), LRef_ (i2, _)                  ->
146          if i1 = i2 then are_convertible_stacks f st a c m1 m2 else f false
147       | GRef_ (_, {E.n_apix = a1}, _, E.Abst _), 
148         GRef_ (_, {E.n_apix = a2}, _, E.Abst _)       ->
149          if a1 = a2 then are_convertible_stacks f st a c m1 m2 else f false
150       | GRef_ (_, {E.n_apix = a1}, _, E.Abbr (_, v1)), 
151         GRef_ (_, {E.n_apix = a2}, _, E.Abbr (_, v2)) ->
152          if a1 = a2 then
153             let f a = 
154                if a then f a else are_convertible f st true c m1 v1 m2 v2
155             in
156             are_convertible_stacks f st a c m1 m2
157          else
158          if a1 < a2 then whd (aux m1 r1) c m2 v2 else
159          whd (aux_rev m2 r2) c m1 v1
160       | _, GRef_ (_, _, _, E.Abbr (_, v2))            ->
161          whd (aux m1 r1) c m2 v2
162       | GRef_ (_, _, _, E.Abbr (_, v1)), _            ->
163          whd (aux_rev m2 r2) c m1 v1      
164       | Bind_ (y1, l1, w1, t1), Bind_ (_, l2, w2, t2) ->
165            let l = P.new_mark () in
166            let h c =
167               let m1, m2 = inc m1, inc m2 in
168               let f t1 = ZS.subst (are_convertible f st a c m1 t1 m2) l l2 t2 in
169               ZS.subst f l l1 t1
170          in
171          let f r = if r then push "!" h c m1 y1 l w1 else f false in
172          are_convertible f st a c m1 w1 m2 w2
173 (* we detect the AUT-QE reduction rule for type/prop inclusion *)      
174       | Sort_ _, Bind_ (y2, l2, w2, t2) when !G.si    ->
175          let m1, m2 = inc m1, inc m2 in
176          let f c = are_convertible f st a c m1 (term_of_whdr r1) m2 t2 in
177          push "nsi" f c m2 y2 l2 w2
178       | _                                             -> f false
179    and aux_rev m2 r2 m1 r1 = aux m1 r1 m2 r2 in
180    let g m1 r1 = whd (aux m1 r1) c m2 t2 in 
181    if a = false then f false else whd g c m1 t1
182
183 and are_convertible_stacks f st a c m1 m2 =
184 (*   L.warn "entering R.are_convertible_stacks"; *)
185    let mm1, mm2 = {m1 with s = []}, {m2 with s = []} in
186    let map f a v1 v2 = are_convertible f st a c mm1 v1 mm2 v2 in
187    if List.length m1.s <> List.length m2.s then 
188       begin 
189 (*         L.warn (Printf.sprintf "Different lengths: %u %u"
190             (List.length m1.s) (List.length m2.s) 
191          ); *)
192          f false
193       end
194    else
195       C.list_fold_left2 f map a m1.s m2.s
196
197 let are_convertible f st c u t =
198 IFDEF TRACE THEN 
199    if !G.ct >= level then log2 st "Now converting" c u c t
200 ELSE () END;
201    are_convertible f st true c empty_machine u empty_machine t
202
203 END