src/HOL/Tools/Metis/metis_reconstruct.ML
changeset 59058 a78612c67ec0
parent 58963 26bf09b95dda
child 59498 50b60f501b05
equal deleted inserted replaced
59057:5b649fb2f2e1 59058:a78612c67ec0
   173 (*Increment the indexes of only the type variables*)
   173 (*Increment the indexes of only the type variables*)
   174 fun incr_type_indexes inc th =
   174 fun incr_type_indexes inc th =
   175   let
   175   let
   176     val tvs = Term.add_tvars (Thm.full_prop_of th) []
   176     val tvs = Term.add_tvars (Thm.full_prop_of th) []
   177     val thy = Thm.theory_of_thm th
   177     val thy = Thm.theory_of_thm th
   178     fun inc_tvar ((a, i), s) = pairself (ctyp_of thy) (TVar ((a, i), s), TVar ((a, i + inc), s))
   178     fun inc_tvar ((a, i), s) = apply2 (ctyp_of thy) (TVar ((a, i), s), TVar ((a, i + inc), s))
   179   in
   179   in
   180     Thm.instantiate (map inc_tvar tvs, []) th
   180     Thm.instantiate (map inc_tvar tvs, []) th
   181   end
   181   end
   182 
   182 
   183 (* Like RSN, but we rename apart only the type variables. Vars here typically
   183 (* Like RSN, but we rename apart only the type variables. Vars here typically
   211           |> AList.group (op =)
   211           |> AList.group (op =)
   212           |> maps (fn ((s, _), T :: Ts) => map (fn T' => (Free (s, T), Free (s, T'))) Ts)
   212           |> maps (fn ((s, _), T :: Ts) => map (fn T' => (Free (s, T), Free (s, T'))) Ts)
   213           |> rpair (Envir.empty ~1)
   213           |> rpair (Envir.empty ~1)
   214           |-> fold (Pattern.unify (Context.Proof ctxt))
   214           |-> fold (Pattern.unify (Context.Proof ctxt))
   215           |> Envir.type_env |> Vartab.dest
   215           |> Envir.type_env |> Vartab.dest
   216           |> map (fn (x, (S, T)) => pairself (ctyp_of thy) (TVar (x, S), T))
   216           |> map (fn (x, (S, T)) => apply2 (ctyp_of thy) (TVar (x, S), T))
   217       in
   217       in
   218         (* The unifier, which is invoked from "Thm.bicompose", will sometimes refuse to unify
   218         (* The unifier, which is invoked from "Thm.bicompose", will sometimes refuse to unify
   219            "?a::?'a" with "?a::?'b" or "?a::nat" and throw a "TERM" exception (with "add_ffpair" as
   219            "?a::?'a" with "?a::?'b" or "?a::nat" and throw a "TERM" exception (with "add_ffpair" as
   220            first argument). We then perform unification of the types of variables by hand and try
   220            first argument). We then perform unification of the types of variables by hand and try
   221            again. We could do this the first time around but this error occurs seldom and we don't
   221            again. We could do this the first time around but this error occurs seldom and we don't
   222            want to break existing proofs in subtle ways or slow them down. *)
   222            want to break existing proofs in subtle ways or slow them down. *)
   223         if null ps then raise TERM z
   223         if null ps then raise TERM z
   224         else res (pairself (Drule.instantiate_normalize (ps, [])) (tha, thb))
   224         else res (apply2 (Drule.instantiate_normalize (ps, [])) (tha, thb))
   225       end
   225       end
   226   end
   226   end
   227 
   227 
   228 fun s_not (@{const Not} $ t) = t
   228 fun s_not (@{const Not} $ t) = t
   229   | s_not t = HOLogic.mk_not t
   229   | s_not t = HOLogic.mk_not t
   268 (* Maps the clause  [P1,...Pn]==>False to [P1,...,P(i-1),P(i+1),...Pn] ==> ~P *)
   268 (* Maps the clause  [P1,...Pn]==>False to [P1,...,P(i-1),P(i+1),...Pn] ==> ~P *)
   269 fun select_literal ctxt = negate_head ctxt oo make_last
   269 fun select_literal ctxt = negate_head ctxt oo make_last
   270 
   270 
   271 fun resolve_inference ctxt type_enc concealed sym_tab th_pairs atom th1 th2 =
   271 fun resolve_inference ctxt type_enc concealed sym_tab th_pairs atom th1 th2 =
   272   let
   272   let
   273     val (i_th1, i_th2) = pairself (lookth th_pairs) (th1, th2)
   273     val (i_th1, i_th2) = apply2 (lookth th_pairs) (th1, th2)
   274     val _ = trace_msg ctxt (fn () =>
   274     val _ = trace_msg ctxt (fn () =>
   275         "  isa th1 (pos): " ^ Display.string_of_thm ctxt i_th1 ^ "\n\
   275         "  isa th1 (pos): " ^ Display.string_of_thm ctxt i_th1 ^ "\n\
   276         \  isa th2 (neg): " ^ Display.string_of_thm ctxt i_th2)
   276         \  isa th2 (neg): " ^ Display.string_of_thm ctxt i_th2)
   277   in
   277   in
   278     (* Trivial cases where one operand is type info *)
   278     (* Trivial cases where one operand is type info *)
   372       val _ = trace_msg ctxt (fn () => "i_tm: " ^ Syntax.string_of_term ctxt i_tm)
   372       val _ = trace_msg ctxt (fn () => "i_tm: " ^ Syntax.string_of_term ctxt i_tm)
   373       val _ = trace_msg ctxt (fn () => "located term: " ^ Syntax.string_of_term ctxt tm_subst)
   373       val _ = trace_msg ctxt (fn () => "located term: " ^ Syntax.string_of_term ctxt tm_subst)
   374       val imax = maxidx_of_term (i_tm $ tm_abs $ tm_subst)  (*ill-typed but gives right max*)
   374       val imax = maxidx_of_term (i_tm $ tm_abs $ tm_subst)  (*ill-typed but gives right max*)
   375       val subst' = Thm.incr_indexes (imax+1) (if pos then subst_em else ssubst_em)
   375       val subst' = Thm.incr_indexes (imax+1) (if pos then subst_em else ssubst_em)
   376       val _ = trace_msg ctxt (fn () => "subst' " ^ Display.string_of_thm ctxt subst')
   376       val _ = trace_msg ctxt (fn () => "subst' " ^ Display.string_of_thm ctxt subst')
   377       val eq_terms = map (pairself (cterm_of thy))
   377       val eq_terms = map (apply2 (cterm_of thy))
   378         (ListPair.zip (Misc_Legacy.term_vars (prop_of subst'), [tm_abs, tm_subst, i_tm]))
   378         (ListPair.zip (Misc_Legacy.term_vars (prop_of subst'), [tm_abs, tm_subst, i_tm]))
   379   in
   379   in
   380     cterm_instantiate eq_terms subst'
   380     cterm_instantiate eq_terms subst'
   381   end
   381   end
   382 
   382 
   520       | (Var _, _) => add_terms (t, u)
   520       | (Var _, _) => add_terms (t, u)
   521       | (_, Var _) => add_terms (u, t)
   521       | (_, Var _) => add_terms (u, t)
   522       | _ => I)
   522       | _ => I)
   523 
   523 
   524     val t_inst =
   524     val t_inst =
   525       [] |> try (unify_terms (prem, concl) #> map (pairself (cterm_of thy)))
   525       [] |> try (unify_terms (prem, concl) #> map (apply2 (cterm_of thy)))
   526          |> the_default [] (* FIXME *)
   526          |> the_default [] (* FIXME *)
   527   in
   527   in
   528     cterm_instantiate t_inst th
   528     cterm_instantiate t_inst th
   529   end
   529   end
   530 
   530 
   572                                              var_body_T :: var_binder_Ts)
   572                                              var_body_T :: var_binder_Ts)
   573           val env =
   573           val env =
   574             Envir.Envir {maxidx = Vartab.fold (Integer.max o snd o fst) tyenv 0,
   574             Envir.Envir {maxidx = Vartab.fold (Integer.max o snd o fst) tyenv 0,
   575               tenv = Vartab.empty, tyenv = tyenv}
   575               tenv = Vartab.empty, tyenv = tyenv}
   576           val ty_inst =
   576           val ty_inst =
   577             Vartab.fold (fn (x, (S, T)) => cons (pairself (ctyp_of thy) (TVar (x, S), T))) tyenv []
   577             Vartab.fold (fn (x, (S, T)) => cons (apply2 (ctyp_of thy) (TVar (x, S), T))) tyenv []
   578           val t_inst = [pairself (cterm_of thy o Envir.norm_term env) (Var var, t')]
   578           val t_inst = [apply2 (cterm_of thy o Envir.norm_term env) (Var var, t')]
   579         in
   579         in
   580           Drule.instantiate_normalize (ty_inst, t_inst) th
   580           Drule.instantiate_normalize (ty_inst, t_inst) th
   581         end
   581         end
   582       | _ => raise Fail "expected a single non-zapped, non-Metis Var")
   582       | _ => raise Fail "expected a single non-zapped, non-Metis Var")
   583   in
   583   in
   615     end
   615     end
   616 
   616 
   617 fun cluster_key ((ax_no, (cluster_no, index_no)), skolem) =
   617 fun cluster_key ((ax_no, (cluster_no, index_no)), skolem) =
   618   (ax_no, (cluster_no, (skolem, index_no)))
   618   (ax_no, (cluster_no, (skolem, index_no)))
   619 fun cluster_ord p =
   619 fun cluster_ord p =
   620   prod_ord int_ord (prod_ord int_ord (prod_ord bool_ord int_ord)) (pairself cluster_key p)
   620   prod_ord int_ord (prod_ord int_ord (prod_ord bool_ord int_ord)) (apply2 cluster_key p)
   621 
   621 
   622 val tysubst_ord =
   622 val tysubst_ord =
   623   list_ord (prod_ord Term_Ord.fast_indexname_ord (prod_ord Term_Ord.sort_ord Term_Ord.typ_ord))
   623   list_ord (prod_ord Term_Ord.fast_indexname_ord (prod_ord Term_Ord.sort_ord Term_Ord.typ_ord))
   624 
   624 
   625 structure Int_Tysubst_Table =
   625 structure Int_Tysubst_Table =
   628 
   628 
   629 structure Int_Pair_Graph =
   629 structure Int_Pair_Graph =
   630   Graph(type key = int * int val ord = prod_ord int_ord int_ord)
   630   Graph(type key = int * int val ord = prod_ord int_ord int_ord)
   631 
   631 
   632 fun shuffle_key (((axiom_no, (_, index_no)), _), _) = (axiom_no, index_no)
   632 fun shuffle_key (((axiom_no, (_, index_no)), _), _) = (axiom_no, index_no)
   633 fun shuffle_ord p = prod_ord int_ord int_ord (pairself shuffle_key p)
   633 fun shuffle_ord p = prod_ord int_ord int_ord (apply2 shuffle_key p)
   634 
   634 
   635 (* Attempts to derive the theorem "False" from a theorem of the form
   635 (* Attempts to derive the theorem "False" from a theorem of the form
   636    "P1 ==> ... ==> Pn ==> False", where the "Pi"s are to be discharged using the
   636    "P1 ==> ... ==> Pn ==> False", where the "Pi"s are to be discharged using the
   637    specified axioms. The axioms have leading "All" and "Ex" quantifiers, which
   637    specified axioms. The axioms have leading "All" and "Ex" quantifiers, which
   638    must be eliminated first. *)
   638    must be eliminated first. *)
   649             Pattern.first_order_match thy p (Vartab.empty, Vartab.empty)
   649             Pattern.first_order_match thy p (Vartab.empty, Vartab.empty)
   650           val tsubst =
   650           val tsubst =
   651             tenv |> Vartab.dest
   651             tenv |> Vartab.dest
   652                  |> filter (Meson_Clausify.is_zapped_var_name o fst o fst)
   652                  |> filter (Meson_Clausify.is_zapped_var_name o fst o fst)
   653                  |> sort (cluster_ord
   653                  |> sort (cluster_ord
   654                           o pairself (Meson_Clausify.cluster_of_zapped_var_name
   654                           o apply2 (Meson_Clausify.cluster_of_zapped_var_name
   655                                       o fst o fst))
   655                                       o fst o fst))
   656                  |> map (Meson.term_pair_of
   656                  |> map (Meson.term_pair_of #> apply2 (Envir.subst_term_types tyenv))
   657                          #> pairself (Envir.subst_term_types tyenv))
       
   658           val tysubst = tyenv |> Vartab.dest
   657           val tysubst = tyenv |> Vartab.dest
   659         in (tysubst, tsubst) end
   658         in (tysubst, tsubst) end
   660 
   659 
   661       fun subst_info_of_prem subgoal_no prem =
   660       fun subst_info_of_prem subgoal_no prem =
   662         (case prem of
   661         (case prem of
   684             clusters_in_term true t |> cluster_no > 1 ? cons (ax_no, cluster_no - 1))
   683             clusters_in_term true t |> cluster_no > 1 ? cons (ax_no, cluster_no - 1))
   685         | _ => raise TERM ("discharge_skolem_premises: Expected Var", [var]))
   684         | _ => raise TERM ("discharge_skolem_premises: Expected Var", [var]))
   686 
   685 
   687       val prems = Logic.strip_imp_prems (prop_of prems_imp_false)
   686       val prems = Logic.strip_imp_prems (prop_of prems_imp_false)
   688       val substs = prems |> map2 subst_info_of_prem (1 upto length prems)
   687       val substs = prems |> map2 subst_info_of_prem (1 upto length prems)
   689                          |> sort (int_ord o pairself fst)
   688                          |> sort (int_ord o apply2 fst)
   690       val depss = maps (map_filter deps_of_term_subst o snd o snd o snd) substs
   689       val depss = maps (map_filter deps_of_term_subst o snd o snd o snd) substs
   691       val clusters = maps (op ::) depss
   690       val clusters = maps (op ::) depss
   692       val ordered_clusters =
   691       val ordered_clusters =
   693         Int_Pair_Graph.empty
   692         Int_Pair_Graph.empty
   694         |> fold Int_Pair_Graph.default_node (map (rpair ()) clusters)
   693         |> fold Int_Pair_Graph.default_node (map (rpair ()) clusters)
   723 (* for debugging only:
   722 (* for debugging only:
   724       fun string_of_subst_info (ax_no, (subgoal_no, (tysubst, tsubst))) =
   723       fun string_of_subst_info (ax_no, (subgoal_no, (tysubst, tsubst))) =
   725         "ax: " ^ string_of_int ax_no ^ "; asm: " ^ string_of_int subgoal_no ^
   724         "ax: " ^ string_of_int ax_no ^ "; asm: " ^ string_of_int subgoal_no ^
   726         "; tysubst: " ^ @{make_string} tysubst ^ "; tsubst: {" ^
   725         "; tysubst: " ^ @{make_string} tysubst ^ "; tsubst: {" ^
   727         commas (map ((fn (s, t) => s ^ " |-> " ^ t)
   726         commas (map ((fn (s, t) => s ^ " |-> " ^ t)
   728                      o pairself (Syntax.string_of_term ctxt)) tsubst) ^ "}"
   727                      o apply2 (Syntax.string_of_term ctxt)) tsubst) ^ "}"
   729       val _ = tracing ("ORDERED CLUSTERS: " ^ @{make_string} ordered_clusters)
   728       val _ = tracing ("ORDERED CLUSTERS: " ^ @{make_string} ordered_clusters)
   730       val _ = tracing ("AXIOM COUNTS: " ^ @{make_string} ax_counts)
   729       val _ = tracing ("AXIOM COUNTS: " ^ @{make_string} ax_counts)
   731       val _ = tracing ("OUTER PARAMS: " ^ @{make_string} outer_param_names)
   730       val _ = tracing ("OUTER PARAMS: " ^ @{make_string} outer_param_names)
   732       val _ = tracing ("SUBSTS (" ^ string_of_int (length substs) ^ "):\n" ^
   731       val _ = tracing ("SUBSTS (" ^ string_of_int (length substs) ^ "):\n" ^
   733                        cat_lines (map string_of_subst_info substs))
   732                        cat_lines (map string_of_subst_info substs))