]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/acic_procedural/procedural2.ml
- hExtlib: added debugging information for split_nth
[helm.git] / helm / software / components / acic_procedural / procedural2.ml
index f8007d3fc93b204c1fff97b5e0659d1d50245363..7ac82a0a63989326d736864adab9ca2e866d372f 100644 (file)
@@ -30,7 +30,6 @@ module TC   = CicTypeChecker
 module Un   = CicUniv
 module UM   = UriManager
 module Obj  = LibraryObjects
-module HObj = HelmLibraryObjects
 module A    = Cic2acic
 module Ut   = CicUtil
 module E    = CicEnvironment
@@ -40,6 +39,7 @@ module HEL  = HExtlib
 module DTI  = DoubleTypeInference
 module NU   = CicNotationUtil
 module L    = Librarian
+module G    = GrafiteAst
 
 module Cl   = ProceduralClassify
 module T    = ProceduralTypes
@@ -47,12 +47,14 @@ module Cn   = ProceduralConversion
 module H    = ProceduralHelpers
 
 type status = {
-   sorts : (C.id, A.sort_kind) Hashtbl.t;
-   types : (C.id, A.anntypes) Hashtbl.t;
+   sorts    : (C.id, A.sort_kind) Hashtbl.t;
+   types    : (C.id, A.anntypes) Hashtbl.t;
+   params   : G.inline_param list;
    max_depth: int option;
-   depth: int;
-   context: C.context;
-   case: int list
+   depth    : int;
+   defaults : bool;
+   context  : C.context;
+   case     : int list
 }
 
 let debug = ref false
@@ -62,8 +64,8 @@ let debug = ref false
 let split2_last l1 l2 =
 try
    let n = pred (List.length l1) in
-   let before1, after1 = HEL.split_nth n l1 in
-   let before2, after2 = HEL.split_nth n l2 in
+   let before1, after1 = HEL.split_nth "P2 1" n l1 in
+   let before2, after2 = HEL.split_nth "P2 2" n l2 in
    before1, before2, List.hd after1, List.hd after2
 with Invalid_argument _ -> failwith "A2P.split2_last"
    
@@ -109,24 +111,22 @@ try
       | Some d -> if st.depth < d then true, msg else false, "DEPTH EXCEDED: "
 with Invalid_argument _ -> failwith "A2P.test_depth"
 
-let is_rewrite_right = function
-   | C.AConst (_, uri, []) ->
-      UM.eq uri HObj.Logic.eq_ind_r_URI || Obj.is_eq_ind_r_URI uri
+let is_rewrite_right st = function
+   | C.AConst (_, uri, []) -> st.defaults && Obj.is_eq_ind_r_URI uri
    | _                     -> false
 
-let is_rewrite_left = function
-   | C.AConst (_, uri, []) ->
-      UM.eq uri HObj.Logic.eq_ind_URI || Obj.is_eq_ind_URI uri
+let is_rewrite_left st = function
+   | C.AConst (_, uri, []) -> st.defaults && Obj.is_eq_ind_URI uri
    | _                     -> false
 
-let is_fwd_rewrite_right hd tl =
-   if is_rewrite_right hd then match List.nth tl 3 with
+let is_fwd_rewrite_right st hd tl =
+   if is_rewrite_right st hd then match List.nth tl 3 with
       | C.ARel _ -> true
       | _        -> false
    else false
 
-let is_fwd_rewrite_left hd tl =
-   if is_rewrite_left hd then match List.nth tl 3 with
+let is_fwd_rewrite_left st hd tl =
+   if is_rewrite_left st hd then match List.nth tl 3 with
       | C.ARel _ -> true
       | _        -> false
    else false
@@ -140,15 +140,6 @@ try
    with Not_found -> None
 with Invalid_argument _ -> failwith "A2P.get_inner_types"
 
-let is_proof st v =
-try
-   let id = Ut.id_of_annterm v in
-   try match Hashtbl.find st.sorts id with
-      | `Prop -> true
-      | _     -> false
-   with Not_found -> H.is_proof st.context (H.cic v)
-with Invalid_argument _ -> failwith "P1.is_proof"
-
 let get_entry st id =
    let rec aux = function
       | []                                        -> assert false
@@ -195,28 +186,30 @@ let mk_exp_args hd tl classes synth =
    if args = [] then b, hd else b, C.AAppl ("", hd :: args)
 
 let mk_convert st ?name sty ety note =
+   let ppterm t = 
+      let a = ref "" in Ut.pp_term (fun s -> a := !a ^ s) [] st.context t; !a
+   in 
    let e = Cn.hole "" in
    let csty, cety = H.cic sty, H.cic ety in
-   let script = 
+   let note = 
       if !debug then
          let sname = match name with None -> "" | Some (id, _) -> id in
-         let note = Printf.sprintf "%s: %s\nSINTH: %s\nEXP: %s"
-            note sname (Pp.ppterm csty) (Pp.ppterm cety)
-        in 
-        [T.Note note]
-      else []
+         Printf.sprintf "%s: %s\nSINTH: %s\nEXP: %s"
+            note sname (ppterm csty) (ppterm cety)
+      else ""
    in
-   assert (Ut.is_sober st.context csty); 
-   assert (Ut.is_sober st.context cety);
-   if Ut.alpha_equivalence csty cety then script else 
+   if H.alpha_equivalence ~flatten:true st.context csty cety then [T.Note note] else 
    let sty, ety = H.acic_bc st.context sty, H.acic_bc st.context ety in
    match name with
-      | None         -> T.Change (sty, ety, None, e, "") :: script
+      | None         -> [T.Change (sty, ety, None, e, note)]
       | Some (id, i) -> 
          begin match get_entry st id with
-           | C.Def _  -> assert false (* T.ClearBody (id, "") :: script *)
+           | C.Def _  -> 
+              [T.Change (ety, sty, Some (id, Some id), e, note);
+               T.ClearBody (id, "")
+              ]
            | C.Decl _ -> 
-              T.Change (ety, sty, Some (id, Some id), e, "") :: script 
+              [T.Change (ety, sty, Some (id, Some id), e, note)] 
          end
 
 let convert st ?name v = 
@@ -288,21 +281,23 @@ and proc_letin st what name v w t =
    let intro = get_intro name in
    let proceed, dtext = test_depth st in
    let script = if proceed then 
-      let st, hyp, rqv = match get_inner_types st v with
-         | Some (ity, _) ->
+      let st, hyp, rqv = match get_inner_types st what, get_inner_types st v with
+         | Some (C.ALetIn _, _), _ ->
+           st, C.Def (H.cic v, H.cic w), [T.Intros (Some 1, [intro], dtext)]
+        | _, Some (ity, _)        ->
            let st, rqv = match v with
-               | C.AAppl (_, hd :: tl) when is_fwd_rewrite_right hd tl ->
+               | C.AAppl (_, hd :: tl) when is_fwd_rewrite_right st hd tl ->
                  mk_fwd_rewrite st dtext intro tl true v t ity
-              | C.AAppl (_, hd :: tl) when is_fwd_rewrite_left hd tl  ->
+              | C.AAppl (_, hd :: tl) when is_fwd_rewrite_left st hd tl  ->
                  mk_fwd_rewrite st dtext intro tl false v t ity
-              | v                                                     ->
+              | v                                                        ->
                  assert (Ut.is_sober st.context (H.cic ity));
                  let ity = H.acic_bc st.context ity in
                  let qs = [proc_proof (next st) v; [T.Id ""]] in
                  st, [T.Branch (qs, ""); T.Cut (intro, ity, dtext)]
            in
            st, C.Decl (H.cic ity), rqv
-        | None          ->
+        | _, None                 ->
            st, C.Def (H.cic v, H.cic w), [T.LetIn (intro, v, dtext)]
       in
       let entry = Some (name, hyp) in
@@ -365,9 +360,9 @@ and proc_appl st what hd tl =
               let qs = proc_bkd_proofs (next st) synth [] classes tl in
               let b, hd = mk_exp_args hd tl classes synth in
               script @ [tactic b hd (dtext ^ text); T.Branch (qs, "")]
-           else if is_rewrite_right hd then 
+           else if is_rewrite_right st hd then 
               script2 @ mk_rewrite st dtext where qs tl2 false what
-           else if is_rewrite_left hd then 
+           else if is_rewrite_left st hd then 
               script2 @ mk_rewrite st dtext where qs tl2 true what
            else
               let predicate = List.nth tl2 (parsno - i) in
@@ -393,7 +388,7 @@ and proc_case st what uri tyno u v ts =
       let qs = proc_bkd_proofs (next st) synth names classes ts in
       let lpsno, _ = H.get_ind_type uri tyno in
       let ps, _ = H.get_ind_parameters st.context (H.cic v) in
-      let _, rps = HEL.split_nth lpsno ps in
+      let _, rps = HEL.split_nth "P2 3" lpsno ps in
       let rpsno = List.length rps in 
       let e = Cn.mk_pattern rpsno u in
       let text = "" in
@@ -458,51 +453,20 @@ try
 
 with Invalid_argument s -> failwith ("A2P.proc_bkd_proofs: " ^ s)
 
-(* object costruction *******************************************************)
-
-let th_flavours = [`Theorem; `Lemma; `Remark; `Fact]
+(* initialization ***********************************************************)
 
-let def_flavours = [`Definition]
-
-let get_flavour ?flavour st v attrs =
-   let rec aux = function
-      | []               -> 
-         if is_proof st v then List.hd th_flavours else List.hd def_flavours
-      | `Flavour fl :: _ -> fl
-      | _ :: tl          -> aux tl
+let init ~ids_to_inner_sorts ~ids_to_inner_types params context =
+   let depth_map x y = match x, y with
+      | None, G.IPDepth depth -> Some depth
+      | _                     -> x
    in
-   match flavour with
-      | Some fl -> fl
-      | None    -> aux attrs
-
-let proc_obj ?flavour ?(info="") st = function
-   | C.AConstant (_, _, s, Some v, t, [], attrs)         ->
-      begin match get_flavour ?flavour st v attrs with
-         | flavour when List.mem flavour th_flavours  ->
-            let ast = proc_proof st v in
-            let steps, nodes = T.count_steps 0 ast, T.count_nodes 0 ast in
-            let text = Printf.sprintf "%s\n%s%s: %u\n%s: %u\n%s"
-              "COMMENTS" info "Tactics" steps "Final nodes" nodes "END"
-           in
-            T.Statement (flavour, Some s, t, None, "") :: ast @ [T.Qed text]
-         | flavour when List.mem flavour def_flavours ->
-            [T.Statement (flavour, Some s, t, Some v, "")]
-        | _                                  ->
-            failwith "not a theorem, definition, axiom or inductive type"
-      end
-   | C.AConstant (_, _, s, None, t, [], attrs)           ->
-      [T.Statement (`Axiom, Some s, t, None, "")]
-   | C.AInductiveDefinition (_, types, [], lpsno, attrs) ->
-      [T.Inductive (types, lpsno, "")] 
-   | _                                          ->
-      failwith "not a theorem, definition, axiom or inductive type"
-
-let init ~ids_to_inner_sorts ~ids_to_inner_types ?depth context =
    {
       sorts       = ids_to_inner_sorts;
       types       = ids_to_inner_types;
-      max_depth   = depth;
+      params      = params;
+      max_depth   = List.fold_left depth_map None params;
       depth       = 0;
+      defaults    = not (List.mem G.IPNoDefaults params);
       context     = context;
       case        = []
    }