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