]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/acic_procedural/procedural2.ml
Procedural: higher-order unification needs a lot of hints !!
[helm.git] / helm / software / components / acic_procedural / procedural2.ml
index dbd70428c7cba8195bc408b4fea6e46889fe3d5a..777ccd5f5101b797f2cb92a48e53cb0c83bc2c7d 100644 (file)
@@ -138,7 +138,7 @@ try
       | {A.annsynthesized = st; A.annexpected = Some et} -> Some (st, et)
       | {A.annsynthesized = st; A.annexpected = None}    -> Some (st, st)
    with Not_found -> None
-with Invalid_argument _ -> failwith "A2P.get_inner_types"
+with Invalid_argument _ -> failwith "P2.get_inner_types"
 
 let get_entry st id =
    let rec aux = function
@@ -167,23 +167,52 @@ let get_sub_names head l =
 
 let get_type msg st t = H.get_type msg st.context (H.cic t) 
 
+let get_uri_of_head = function
+   | C.AConst (_, u, _)
+   | C.AAppl (_, C.AConst (_, u, _) :: _)              -> Some (u, 0, 0)
+   | C.AMutInd (_, u, i, _)
+   | C.AAppl (_, C.AMutInd (_, u, i, _) :: _)          -> Some (u, succ i, 0)
+   | C.AMutConstruct (_, u, i, j, _)
+   | C.AAppl (_, C.AMutConstruct (_, u, i, j, _) :: _) -> Some (u, succ i, j)
+   | _                                                 -> None
+
+let get_uri_of_apply = function
+   | T.Exact (t, _)
+   | T.Apply (t, _) -> get_uri_of_head t
+   | _              -> None
+
+let is_reflexivity st step =
+   match get_uri_of_apply step with
+      | None             -> false
+      | Some (uri, i, j) -> st.defaults && Obj.is_eq_URI uri && i = 1 && j = 1
+
 (* proof construction *******************************************************)
 
 let anonymous_premise = C.Name "UNNAMED"
 
-let mk_exp_args hd tl classes synth =
+let mk_exp_args hd tl classes synth qs =
+   let exp = ref 0 in
    let meta id = C.AImplicit (id, None) in
    let map v (cl, b) =
-      if I.overlaps synth cl && b then v else meta ""
+      if I.overlaps synth cl
+         then if b then v, v else meta "", v
+         else meta "", meta ""
+   in
+   let rec rev a = function
+      | []       -> a
+      | hd :: tl -> 
+         if snd hd <> meta "" then incr exp;
+         rev (snd hd :: a) tl 
    in
-   let rec aux = function
-      | [] -> b, []
+   let rec aux = function
+      | []       -> []
       | hd :: tl -> 
-         if hd = meta "" then aux true tl else b, List.rev (hd :: tl)
+         if fst hd = meta "" then aux tl else rev [] (hd :: tl)
    in
    let args = T.list_rev_map2 map tl classes in
-   let b, args = aux false args in
-   if args = [] then b, hd else b, C.AAppl ("", hd :: args)
+   let args = aux args in
+   let part = !exp < List.length tl in
+   if args = [] then part, hd, qs else part, C.AAppl ("", hd :: args), qs
 
 let mk_convert st ?name sty ety note =
    let ppterm t = 
@@ -204,7 +233,10 @@ let mk_convert st ?name sty ety note =
       | 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, note)] 
          end
@@ -220,6 +252,8 @@ let get_intro = function
    | C.Name s    -> Some s
 
 let mk_preamble st what script = match script with
+   | step :: script when is_reflexivity st step ->
+      convert st what @ T.Reflexivity (T.note_of_step step) :: script
    | T.Exact _ :: _ -> script
    | _              -> convert st what @ script   
 
@@ -227,14 +261,14 @@ let mk_arg st = function
    | C.ARel (_, _, i, name) as what -> convert st ~name:(name, i) what
    | _                              -> []
 
-let mk_fwd_rewrite st dtext name tl direction v t ity =
+let mk_fwd_rewrite st dtext name tl direction v t ity ety =
    let compare premise = function
       | None   -> true
       | Some s -> s = premise
    in
    assert (List.length tl = 6);
    let what, where, predicate = List.nth tl 5, List.nth tl 3, List.nth tl 2 in
-   let e = Cn.mk_pattern 1 predicate in
+   let e = Cn.mk_pattern 1 ety predicate in
    if (Cn.does_not_occur e) then st, [] else 
    match where with
       | C.ARel (_, _, i, premise) as w ->
@@ -255,10 +289,10 @@ let mk_fwd_rewrite st dtext name tl direction v t ity =
         end
       | _                         -> assert false
 
-let mk_rewrite st dtext where qs tl direction t = 
+let mk_rewrite st dtext where qs tl direction t ety 
    assert (List.length tl = 5);
    let predicate = List.nth tl 2 in
-   let e = Cn.mk_pattern 1 predicate in
+   let e = Cn.mk_pattern 1 ety predicate in
    let script = [T.Branch (qs, "")] in
    if (Cn.does_not_occur e) then script else
    T.Rewrite (direction, where, None, e, dtext) :: script
@@ -278,21 +312,26 @@ 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 (_, _, iv, iw, _), _), _ when
+           H.alpha_equivalence ~flatten:true st.context (H.cic v) (H.cic iv) &&
+           H.alpha_equivalence ~flatten:true st.context (H.cic w) (H.cic iw)
+                                                  ->
+           st, C.Def (H.cic v, H.cic w), [T.Intros (Some 1, [intro], dtext)]
+        | _, Some (ity, ety)                      ->
            let st, rqv = match v with
                | C.AAppl (_, hd :: tl) when is_fwd_rewrite_right st hd tl ->
-                 mk_fwd_rewrite st dtext intro tl true v t ity
+                 mk_fwd_rewrite st dtext intro tl true v t ity ety
               | C.AAppl (_, hd :: tl) when is_fwd_rewrite_left st hd tl  ->
-                 mk_fwd_rewrite st dtext intro tl false v t ity
+                 mk_fwd_rewrite st dtext intro tl false v t ity ety
               | v                                                        ->
-                 assert (Ut.is_sober st.context (H.cic ity));
-                 let ity = H.acic_bc st.context ity in
+                 assert (Ut.is_sober st.context (H.cic ety));
+                 let ety = H.acic_bc st.context ety in
                  let qs = [proc_proof (next st) v; [T.Id ""]] in
-                 st, [T.Branch (qs, ""); T.Cut (intro, ity, dtext)]
+                 st, [T.Branch (qs, ""); T.Cut (intro, ety, 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
@@ -335,40 +374,52 @@ and proc_appl st what hd tl =
       let parsno, argsno = List.length classes, List.length tl in
       let decurry = parsno - argsno in
       let diff = goal_arity - decurry in
-      if diff < 0 then failwith (Printf.sprintf "NOT TOTAL: %i %s |--- %s" diff (Pp.ppcontext st.context) (Pp.ppterm (H.cic hd)));
+      if diff < 0 then 
+         let text = Printf.sprintf "partial application: %i" diff in
+        prerr_endline ("Procedural 2: " ^ text);
+        [T.Exact (what, dtext ^ text)]
+      else
       let classes = Cl.adjust st.context tl ?goal classes in
       let rec mk_synth a n =
          if n < 0 then a else mk_synth (I.S.add n a) (pred n)
       in
       let synth = mk_synth I.S.empty decurry in
-      let text = "" (* Printf.sprintf "%u %s" parsno (Cl.to_string h) *) in
+      let text = if !debug
+         then Printf.sprintf "%u %s" parsno (Cl.to_string synth (classes, rc))
+        else ""
+      in
       let script = List.rev (mk_arg st hd) in
       let tactic b t n = if b then T.Apply (t, n) else T.Exact (t, n) in
       match rc with
-         | Some (i, j, uri, tyno) ->
+         | Some (i, j, uri, tyno) when decurry = 0 ->
            let classes2, tl2, _, where = split2_last classes tl in
            let script2 = List.rev (mk_arg st where) @ script in
            let synth2 = I.S.add 1 synth in
            let names = H.get_ind_names uri tyno in
            let qs = proc_bkd_proofs (next st) synth2 names classes2 tl2 in
-            if List.length qs <> List.length names then
+            let ety = match get_inner_types st what with
+                | Some (_, ety) -> ety 
+                | None          -> 
+                  Cn.fake_annotate "" st.context (get_type "TC3" st what)
+           in
+           if List.length qs <> List.length names then
               let qs = proc_bkd_proofs (next st) synth [] classes tl in
-              let b, hd = mk_exp_args hd tl classes synth in
+              let b, hd, qs = mk_exp_args hd tl classes synth qs in
               script @ [tactic b hd (dtext ^ text); T.Branch (qs, "")]
            else if is_rewrite_right st hd then 
-              script2 @ mk_rewrite st dtext where qs tl2 false what
+              script2 @ mk_rewrite st dtext where qs tl2 false what ety
            else if is_rewrite_left st hd then 
-              script2 @ mk_rewrite st dtext where qs tl2 true what
+              script2 @ mk_rewrite st dtext where qs tl2 true what ety
            else
               let predicate = List.nth tl2 (parsno - i) in
-               let e = Cn.mk_pattern j predicate in
+               let e = Cn.mk_pattern j ety predicate in
               let using = Some hd in
               script2 @ 
               [T.Elim (where, using, e, dtext ^ text); T.Branch (qs, "")]
-        | None        ->
+        | _                                       ->
            let names = get_sub_names hd tl in
            let qs = proc_bkd_proofs (next st) synth names classes tl in
-           let b, hd = mk_exp_args hd tl classes synth in
+           let b, hd, qs = mk_exp_args hd tl classes synth qs in
            script @ [tactic b hd (dtext ^ text); T.Branch (qs, "")]
    else
       [T.Exact (what, dtext)]
@@ -385,7 +436,12 @@ and proc_case st what uri tyno u v ts =
       let ps, _ = H.get_ind_parameters st.context (H.cic v) in
       let _, rps = HEL.split_nth lpsno ps in
       let rpsno = List.length rps in 
-      let e = Cn.mk_pattern rpsno u in
+      let ety = match get_inner_types st what with
+         | Some (_, ety) -> ety 
+         | None          -> 
+           Cn.fake_annotate "" st.context (get_type "TC4" st what)
+      in
+      let e = Cn.mk_pattern rpsno ety u in
       let text = "" in
       let script = List.rev (mk_arg st v) in
       script @ [T.Cases (v, e, dtext ^ text); T.Branch (qs, "")]