]> matita.cs.unibo.it Git - helm.git/blobdiff - components/acic_procedural/proceduralPreprocess.ml
Procedural: 2 bug fix in eta expansion + 1 bug fix in pattern generation
[helm.git] / components / acic_procedural / proceduralPreprocess.ml
index 8030c403bfcf2ab40905896f56a1bc6f6f503671..6b2b2cc90ec3eecf255ceb55a1cc50354d37b521 100644 (file)
@@ -43,7 +43,8 @@ let get_type c t =
 let is_proof c t =
    match Rd.whd ~delta:true c (get_type c (get_type c t)) with
       | C.Sort C.Prop -> true
-      | _             -> false
+      | C.Sort _      -> false
+      | _             -> assert false 
 
 let is_not_atomic = function
    | C.Sort _
@@ -78,25 +79,43 @@ let expanded_premise = "EXPANDED"
 
 let defined_premise = "DEFINED"
 
-let eta_expand tys t =
-   let n = List.length tys in
+let eta_expand tys t =
+   assert (tys <> []);
    let name i = Printf.sprintf "%s%u" expanded_premise i in 
    let lambda i ty t = C.Lambda (C.Name (name i), ty, t) in
-   let arg i n = C.Rel (n - i) in
+   let arg i = C.Rel (succ i) in
    let rec aux i f a = function
       | []       -> f, a 
-      | ty :: tl -> aux (succ i) (comp f (lambda i ty)) (arg i :: a) tl
+      | ty :: tl -> aux (succ i) (comp f (lambda i ty)) (arg i :: a) tl
    in
+   let n = List.length tys in
    let absts, args = aux 0 identity [] tys in
-   absts (C.Appl (S.lift n t :: args))
-
-let get_tys c decurry t = 
-   let tys, _ = split c (get_type c t) in
-   let tys, _ = HEL.split_nth decurry (List.tl tys) in
-   List.rev tys
+   let t = match S.lift n t with
+      | C.Appl ts -> C.Appl (ts @ args)
+      | t         -> C.Appl (t :: args)
+   in
+   g (absts t)
+
+let get_tys c decurry =
+   let rec aux n = function
+(*      | C.Appl (hd :: tl) -> aux (n + List.length tl) hd *)
+      | t                 ->
+         let tys, _ = split c (get_type c t) in
+         let _, tys = HEL.split_nth n (List.rev tys) in
+        let tys, _ = HEL.split_nth decurry tys in
+         tys
+   in
+   aux 0
 
 let eta_fix c t proof decurry =
-   if proof && decurry > 0 then eta_expand (get_tys c decurry t) t else t
+   let rec aux g c = function
+      | C.LetIn (name, v, t) ->
+         let g t = g (C.LetIn (name, v, t)) in
+        let entry = Some (name, C.Def (v, None)) in
+         aux g (entry :: c) t
+      | t                    -> eta_expand g (get_tys c decurry t) t 
+   in 
+   if proof && decurry > 0 then aux identity c t else t
 
 let rec pp_cast g ht es c t v =
    if true then pp_proof g ht es c t else find g ht t
@@ -119,7 +138,6 @@ and pp_letin g ht es c name v t =
            pp_proof g ht false c x 
          | v                               -> 
             let v = eta_fix c v proof d in
-(*         let t = eta_fix (entry :: c) t true decurry in *)
            g (C.LetIn (name, v, t)) true decurry
       in
       if true then pp_term g ht es c v else find g ht v
@@ -144,9 +162,8 @@ and pp_appl_one g ht es c t v =
               let x = C.LetIn (mame, t, C.Appl [C.Rel 1; S.lift 1 v]) in
               pp_proof g ht false c x
            | t, v                               ->
-               let _, premsno = split c (get_type c t) in
               let v = eta_fix c v proof d in
-               g (C.Appl [t; v]) true (pred premsno)
+               g (C.Appl [t; v]) true (pred decurry)
       in
       if true then pp_term g ht es c v else find g ht v
    in
@@ -159,14 +176,23 @@ and pp_appl g ht es c t = function
       let x = C.Appl (C.Appl [t; v1] :: v2 :: vs) in 
       pp_proof g ht es c x
 
+and pp_atomic g ht es c t =
+   let _, premsno = split c (get_type c t) in
+   g t true premsno
+
 and pp_proof g ht es c t = 
+  Printf.eprintf "IN: |- %s\n" (*CicPp.ppcontext c*) (CicPp.ppterm t);
+  let g t proof decurry = 
+     Printf.eprintf "OUT: %b %u |- %s\n" proof decurry (CicPp.ppterm t);
+     g t proof decurry
+  in
 (*   let g t proof decurry = add g ht t proof decurry in *)
    match t with
       | C.Cast (t, v)         -> pp_cast g ht es c t v
       | C.Lambda (name, v, t) -> pp_lambda g ht es c name v t
       | C.LetIn (name, v, t)  -> pp_letin g ht es c name v t
       | C.Appl (t :: vs)      -> pp_appl g ht es c t vs
-      | t                     -> g t true 0
+      | t                     -> pp_atomic g ht es c t
 
 and pp_term g ht es c t =
    if is_proof c t then pp_proof g ht es c t else g t false 0
@@ -180,6 +206,7 @@ let pp_obj = function
          C.Constant (name, Some bo, ty, pars, attrs)
       in
       let ht = C.CicHash.create 1 in
+      Printf.eprintf "BEGIN: %s\n" name;
       begin try pp_term g ht true [] bo
       with e -> failwith ("PPP: " ^ Printexc.to_string e) end
    | obj                                         -> obj