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