]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/acic_procedural/acic2Procedural.ml
basic support for imposed flavour in procedural object rendering
[helm.git] / helm / software / components / acic_procedural / acic2Procedural.ml
index 8e99a93322fa6075ca339ce36b63f2a439612592..4d5e3a668f9724267e68f2b1b3f1562e78a0b806 100644 (file)
@@ -47,17 +47,13 @@ module H    = ProceduralHelpers
 type status = {
    sorts : (C.id, A.sort_kind) Hashtbl.t;
    types : (C.id, A.anntypes) Hashtbl.t;
-   prefix: string;
    max_depth: int option;
    depth: int;
    context: C.context;
-   clears: string list;
-   clears_note: string;
-   case: int list;
-   skip_thm_and_qed : bool;
+   case: int list
 }
 
-let debug = true
+let debug = false
 
 (* helpers ******************************************************************)
 
@@ -173,9 +169,26 @@ try
       | (_, _, _, cs) -> List.map fst cs  
 with Invalid_argument _ -> failwith "A2P.get_ind_names"
 
+let string_of_atomic = function
+   | C.ARel (_, _, _, s)               -> s
+   | C.AVar (_, uri, _)                -> H.name_of_uri uri None None
+   | C.AConst (_, uri, _)              -> H.name_of_uri uri None None
+   | C.AMutInd (_, uri, i, _)          -> H.name_of_uri uri (Some i) None
+   | C.AMutConstruct (_, uri, i, j, _) -> H.name_of_uri uri (Some i) (Some j)
+   | _                                 -> ""
+
+let get_sub_names head l =
+   let s = string_of_atomic head in
+   if s = "" then [] else
+   let map (names, i) _ = 
+      let name = Printf.sprintf "%s_%u" s i in name :: names, succ i
+   in
+   let names, _ = List.fold_left map ([], 1) l in 
+   List.rev names
+
 (* proof construction *******************************************************)
 
-let used_premise = C.Name "USED"
+let anonymous_premise = C.Name "PREMISE"
 
 let mk_exp_args hd tl classes synth =
    let meta id = C.AImplicit (id, None) in
@@ -202,8 +215,8 @@ let mk_convert st ?name sty ety note =
         [T.Note note]
       else []
    in
-   assert (Ut.is_sober csty); 
-   assert (Ut.is_sober cety);
+   assert (Ut.is_sober st.context csty); 
+   assert (Ut.is_sober st.context cety);
    if Ut.alpha_equivalence csty cety then script else 
    let sty, ety = H.acic_bc st.context sty, H.acic_bc st.context ety in
    match name with
@@ -238,10 +251,7 @@ let get_intro = function
    | C.Name s    -> Some s
 
 let mk_preamble st what script =
-   let clears st script =
-      if true (* st.clears = [] *) then script else T.Clear (st.clears, st.clears_note) :: script
-   in
-   clears st (convert st what @ script)   
+   convert st what @ script   
 
 let mk_arg st = function
    | C.ARel (_, _, i, name) as what -> convert st ~name:(name, i) what
@@ -266,11 +276,14 @@ let mk_fwd_rewrite st dtext name tl direction v t ity =
         in
         if DTI.does_not_occur (succ i) (H.cic t) || compare premise name then
            {st with context = Cn.clear st.context premise}, script name
-        else
+        else begin
+           assert (Ut.is_sober st.context (H.cic ity));
+           let ity = H.acic_bc st.context ity in
            let br1 = [T.Id ""] in
            let br2 = List.rev (T.Apply (w, "assumption") :: script None) in
            let text = "non linear rewrite" in
            st, [T.Branch ([br2; br1], ""); T.Cut (name, ity, text)]
+        end
       | _                         -> assert false
 
 let mk_rewrite st dtext where qs tl direction t = 
@@ -283,17 +296,9 @@ let mk_rewrite st dtext where qs tl direction t =
    T.Rewrite (direction, where, None, e, dtext) :: script
 
 let rec proc_lambda st what name v t =
-   let dno = match get_inner_types st t with
-      | None            -> false
-      | Some (sty, ety) ->
-         let sty, ety = H.cic sty, H.cic ety in
-         DTI.does_not_occur 1 sty && DTI.does_not_occur 1 ety
-   in
-   let dno = dno && DTI.does_not_occur 1 (H.cic t) in
-   let name = match dno, name with
-      | true, _            -> C.Anonymous
-      | false, C.Anonymous -> H.mk_fresh_name st.context used_premise
-      | false, name        -> name
+   let name = match name with
+      | C.Anonymous -> H.mk_fresh_name st.context anonymous_premise
+      | name        -> name
    in
    let entry = Some (name, C.Decl (H.cic v)) in
    let intro = get_intro name in
@@ -313,8 +318,9 @@ and proc_letin st what name v w t =
               | C.AAppl (_, hd :: tl) when is_fwd_rewrite_left hd tl  ->
                  mk_fwd_rewrite st dtext intro tl false v t ity
               | 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
-                 let ity = H.acic_bc st.context ity in
                  st, [T.Branch (qs, ""); T.Cut (intro, ity, dtext)]
            in
            st, C.Decl (H.cic ity), rqv
@@ -388,7 +394,8 @@ and proc_appl st what hd tl =
               (* convert_elim st what what e @ *) script2 @ 
               [T.Elim (where, using, e, dtext ^ text); T.Branch (qs, "")]
         | None        ->
-           let qs = proc_bkd_proofs (next st) synth [] classes tl in
+           let names = get_sub_names hd tl in
+           let qs = proc_bkd_proofs (next st) synth names classes tl in
            let hd = mk_exp_args hd tl classes synth in
            script @ [T.Apply (hd, dtext ^ text); T.Branch (qs, "")]
    else
@@ -397,8 +404,9 @@ and proc_appl st what hd tl =
    mk_preamble st what script
 
 and proc_other st what =
+   let _, dtext = test_depth st in
    let text = Printf.sprintf "%s: %s" "UNEXPANDED" (string_of_head what) in
-   let script = [T.Note text] in
+   let script = [T.Apply (what, dtext ^ text)] in 
    mk_preamble st what script
 
 and proc_proof st t = 
@@ -409,9 +417,8 @@ and proc_proof st t =
          (Pp.ppterm (H.cic it)) (Pp.ppterm (H.cic et))) 
          | None          -> None, "\nNo types"
       in
-      let context, clears = Cn.get_clears st.context (H.cic t) xtypes in
-      let note = Pp.ppcontext st.context ^ note in
-      {st with context = context; clears = clears; clears_note = note; }
+      let context, _clears = Cn.get_clears st.context (H.cic t) xtypes in
+      {st with context = context}
    in
    match t with
       | C.ALambda (_, name, w, t) as what   -> proc_lambda (f st) what name w t
@@ -439,7 +446,7 @@ try
    let aux (inv, _) v =
       if I.overlaps synth inv then None else
       if I.S.is_empty inv then Some (get_note (fun st -> proc_proof st v)) else
-      Some (fun _ -> [T.Apply (v, dtext ^ "dependent")])
+      Some (get_note (fun _ -> [T.Apply (v, dtext ^ "dependent")]))
    in  
    let ps = T.list_map2_filter aux classes ts in
    let b = List.length ps > 1 in
@@ -454,33 +461,50 @@ let is_theorem pars =
    List.mem (`Flavour `Theorem) pars || List.mem (`Flavour `Fact) pars || 
    List.mem (`Flavour `Remark) pars || List.mem (`Flavour `Lemma) pars
 
+let is_definition pars =
+   List.mem (`Flavour `Definition) pars
+
 let proc_obj st = function
-   | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars ->
+   | C.AConstant (_, _, s, Some v, t, [], pars) when is_theorem pars    ->
       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 "tactics: %u\nnodes: %u" steps nodes in
-      if st.skip_thm_and_qed then ast
-      else T.Theorem (Some s, t, "") :: ast @ [T.Qed text]
-   | _                                                               ->
-      failwith "not a theorem"
+      T.Statement (`Theorem, Some s, t, None, "") :: ast @ [T.Qed text]
+   | C.AConstant (_, _, s, Some v, t, [], pars) when is_definition pars ->
+      [T.Statement (`Definition, Some s, t, Some v, "")]
+   | C.AConstant (_, _, s, None, t, [], pars)                           ->
+      [T.Statement (`Axiom, Some s, t, None, "")]
+   | _                                                                  ->
+      failwith "not a theorem, definition, axiom"
 
 (* interface functions ******************************************************)
 
-let acic2procedural ~ids_to_inner_sorts ~ids_to_inner_types ?depth
-?(skip_thm_and_qed=false) prefix aobj = 
+let procedural_of_acic_object ~ids_to_inner_sorts ~ids_to_inner_types ?depth
+   ?flavour prefix anobj = 
    let st = {
       sorts       = ids_to_inner_sorts;
       types       = ids_to_inner_types;
-      prefix      = prefix;
       max_depth   = depth;
       depth       = 0;
       context     = [];
-      clears      = [];
-      clears_note = "";
-      case        = [];
-      skip_thm_and_qed = skip_thm_and_qed;
+      case        = []
+   } in
+   HLog.debug "Procedural: level 2 transformation";
+   let steps = proc_obj st anobj in
+   HLog.debug "Procedural: grafite rendering";
+   List.rev (T.render_steps [] steps)
+
+let procedural_of_acic_term ~ids_to_inner_sorts ~ids_to_inner_types ?depth
+   prefix context annterm = 
+   let st = {
+      sorts       = ids_to_inner_sorts;
+      types       = ids_to_inner_types;
+      max_depth   = depth;
+      depth       = 0;
+      context     = context;
+      case        = []
    } in
    HLog.debug "Procedural: level 2 transformation";
-   let steps = proc_obj st aobj in
+   let steps = proc_proof st annterm in
    HLog.debug "Procedural: grafite rendering";
    List.rev (T.render_steps [] steps)