src/HOL/Tools/Metis/metis_reconstruct.ML
changeset 43104 81d1b15aa0ae
parent 43103 35962353e36b
child 43106 6ec2a3c7b69e
equal deleted inserted replaced
43103:35962353e36b 43104:81d1b15aa0ae
   178           if String.isPrefix new_skolem_const_prefix c then
   178           if String.isPrefix new_skolem_const_prefix c then
   179             Var ((new_skolem_var_name_from_const c, 1), dummyT)
   179             Var ((new_skolem_var_name_from_const c, 1), dummyT)
   180           else
   180           else
   181             Const (c, dummyT)
   181             Const (c, dummyT)
   182         end
   182         end
   183       fun cvt (Metis_Term.Fn ("ti", [Metis_Term.Var v, _])) =
   183       fun cvt (Metis_Term.Fn (":", [Metis_Term.Var v, _])) =
   184              (case strip_prefix_and_unascii schematic_var_prefix v of
   184              (case strip_prefix_and_unascii schematic_var_prefix v of
   185                   SOME w =>  mk_var(w, dummyT)
   185                   SOME w =>  mk_var(w, dummyT)
   186                 | NONE   => mk_var(v, dummyT))
   186                 | NONE   => mk_var(v, dummyT))
   187         | cvt (Metis_Term.Fn ("ti", [Metis_Term.Fn ("=",[]), _])) =
   187         | cvt (Metis_Term.Fn (":", [Metis_Term.Fn ("=",[]), _])) =
   188             Const (@{const_name HOL.eq}, HOLogic.typeT)
   188             Const (@{const_name HOL.eq}, HOLogic.typeT)
   189         | cvt (Metis_Term.Fn ("ti", [Metis_Term.Fn (x,[]), ty])) =
   189         | cvt (Metis_Term.Fn (":", [Metis_Term.Fn (x,[]), ty])) =
   190            (case strip_prefix_and_unascii const_prefix x of
   190            (case strip_prefix_and_unascii const_prefix x of
   191                 SOME c => do_const c
   191                 SOME c => do_const c
   192               | NONE => (*Not a constant. Is it a fixed variable??*)
   192               | NONE => (*Not a constant. Is it a fixed variable??*)
   193             case strip_prefix_and_unascii fixed_var_prefix x of
   193             case strip_prefix_and_unascii fixed_var_prefix x of
   194                 SOME v => Free (v, hol_type_from_metis_term ctxt ty)
   194                 SOME v => Free (v, hol_type_from_metis_term ctxt ty)
   195               | NONE => raise Fail ("hol_term_from_metis_FT bad constant: " ^ x))
   195               | NONE => raise Fail ("hol_term_from_metis_FT bad constant: " ^ x))
   196         | cvt (Metis_Term.Fn ("ti", [Metis_Term.Fn (".", [tm1,tm2]), _])) =
   196         | cvt (Metis_Term.Fn (":", [Metis_Term.Fn (".", [tm1,tm2]), _])) =
   197             cvt tm1 $ cvt tm2
   197             cvt tm1 $ cvt tm2
   198         | cvt (Metis_Term.Fn (".",[tm1,tm2])) = (*untyped application*)
   198         | cvt (Metis_Term.Fn (".",[tm1,tm2])) = (*untyped application*)
   199             cvt tm1 $ cvt tm2
   199             cvt tm1 $ cvt tm2
   200         | cvt (Metis_Term.Fn ("{}", [arg])) = cvt arg   (*hBOOL*)
   200         | cvt (Metis_Term.Fn ("{}", [arg])) = cvt arg   (*hBOOL*)
   201         | cvt (Metis_Term.Fn ("=", [tm1,tm2])) =
   201         | cvt (Metis_Term.Fn ("=", [tm1,tm2])) =
   213                    "hol_term_from_metis_FT bad term: " ^ Metis_Term.toString t);
   213                    "hol_term_from_metis_FT bad term: " ^ Metis_Term.toString t);
   214                    hol_term_from_metis_PT ctxt t)
   214                    hol_term_from_metis_PT ctxt t)
   215   in fol_tm |> cvt end
   215   in fol_tm |> cvt end
   216 
   216 
   217 fun atp_name_from_metis s =
   217 fun atp_name_from_metis s =
   218   case AList.find (op =) metis_name_table s of
   218   case find_first (fn (_, (s', _)) => s' = s) metis_name_table of
   219     (s', ary) :: _ => (s', SOME ary)
   219     SOME ((s, _), (_, swap)) => (s, swap)
   220   | _ => (s, NONE)
   220   | _ => (s, false)
   221 fun atp_term_from_metis (Metis_Term.Fn (s, tms)) =
   221 fun atp_term_from_metis (Metis_Term.Fn (s, tms)) =
   222     ATerm (fst (atp_name_from_metis s), map atp_term_from_metis tms)
   222     let val (s, swap) = atp_name_from_metis s in
       
   223       ATerm (s, tms |> map atp_term_from_metis |> swap ? rev)
       
   224     end
   223   | atp_term_from_metis (Metis_Term.Var s) = ATerm (s, [])
   225   | atp_term_from_metis (Metis_Term.Var s) = ATerm (s, [])
   224 
   226 
   225 fun hol_term_from_metis_MX sym_tab ctxt =
   227 fun hol_term_from_metis_MX sym_tab ctxt =
   226   let val thy = Proof_Context.theory_of ctxt in
   228   let val thy = Proof_Context.theory_of ctxt in
   227     atp_term_from_metis #> term_from_atp thy false sym_tab []
   229     atp_term_from_metis #> term_from_atp thy false sym_tab []
   485       val [i_atm, i_tm] =
   487       val [i_atm, i_tm] =
   486         hol_terms_from_metis ctxt mode old_skolems sym_tab [m_tm, fr]
   488         hol_terms_from_metis ctxt mode old_skolems sym_tab [m_tm, fr]
   487       val _ = trace_msg ctxt (fn () => "sign of the literal: " ^ Bool.toString pos)
   489       val _ = trace_msg ctxt (fn () => "sign of the literal: " ^ Bool.toString pos)
   488       fun replace_item_list lx 0 (_::ls) = lx::ls
   490       fun replace_item_list lx 0 (_::ls) = lx::ls
   489         | replace_item_list lx i (l::ls) = l :: replace_item_list lx (i-1) ls
   491         | replace_item_list lx i (l::ls) = l :: replace_item_list lx (i-1) ls
       
   492       fun path_finder_fail mode tm ps t =
       
   493         raise Fail ("Cannot replay Metis proof in Isabelle:\n" ^
       
   494                     "equality_inf, path_finder_" ^ string_of_mode mode ^
       
   495                     ": path = " ^ space_implode " " (map string_of_int ps) ^
       
   496                     " isa-term: " ^ Syntax.string_of_term ctxt tm ^
       
   497                     (case t of
       
   498                        SOME t => " fol-term: " ^ Metis_Term.toString t
       
   499                      | NONE => ""))
   490       fun path_finder_FO tm [] = (tm, Bound 0)
   500       fun path_finder_FO tm [] = (tm, Bound 0)
   491         | path_finder_FO tm (p::ps) =
   501         | path_finder_FO tm (p::ps) =
   492             let val (tm1,args) = strip_comb tm
   502             let val (tm1,args) = strip_comb tm
   493                 val adjustment = get_ty_arg_size thy tm1
   503                 val adjustment = get_ty_arg_size thy tm1
   494                 val p' = if adjustment > p then p else p - adjustment
   504                 val p' = if adjustment > p then p else p - adjustment
   505                 (r, list_comb (tm1, replace_item_list t p' args))
   515                 (r, list_comb (tm1, replace_item_list t p' args))
   506             end
   516             end
   507       fun path_finder_HO tm [] = (tm, Bound 0)
   517       fun path_finder_HO tm [] = (tm, Bound 0)
   508         | path_finder_HO (t$u) (0::ps) = (fn(x,y) => (x, y$u)) (path_finder_HO t ps)
   518         | path_finder_HO (t$u) (0::ps) = (fn(x,y) => (x, y$u)) (path_finder_HO t ps)
   509         | path_finder_HO (t$u) (_::ps) = (fn(x,y) => (x, t$y)) (path_finder_HO u ps)
   519         | path_finder_HO (t$u) (_::ps) = (fn(x,y) => (x, t$y)) (path_finder_HO u ps)
   510         | path_finder_HO tm ps =
   520         | path_finder_HO tm ps = path_finder_fail HO tm ps NONE
   511           raise Fail ("Cannot replay Metis proof in Isabelle:\n" ^
       
   512                       "equality_inf, path_finder_HO: path = " ^
       
   513                       space_implode " " (map string_of_int ps) ^
       
   514                       " isa-term: " ^  Syntax.string_of_term ctxt tm)
       
   515       fun path_finder_FT tm [] _ = (tm, Bound 0)
   521       fun path_finder_FT tm [] _ = (tm, Bound 0)
   516         | path_finder_FT tm (0::ps) (Metis_Term.Fn ("ti", [t1, _])) =
   522         | path_finder_FT tm (0::ps) (Metis_Term.Fn (":", [t1, _])) =
   517             path_finder_FT tm ps t1
   523             path_finder_FT tm ps t1
   518         | path_finder_FT (t$u) (0::ps) (Metis_Term.Fn (".", [t1, _])) =
   524         | path_finder_FT (t$u) (0::ps) (Metis_Term.Fn (".", [t1, _])) =
   519             (fn(x,y) => (x, y$u)) (path_finder_FT t ps t1)
   525             (fn(x,y) => (x, y$u)) (path_finder_FT t ps t1)
   520         | path_finder_FT (t$u) (1::ps) (Metis_Term.Fn (".", [_, t2])) =
   526         | path_finder_FT (t$u) (1::ps) (Metis_Term.Fn (".", [_, t2])) =
   521             (fn(x,y) => (x, t$y)) (path_finder_FT u ps t2)
   527             (fn(x,y) => (x, t$y)) (path_finder_FT u ps t2)
   522         | path_finder_FT tm ps t =
   528         | path_finder_FT tm ps t = path_finder_fail FT tm ps (SOME t)
   523           raise Fail ("Cannot replay Metis proof in Isabelle:\n" ^
       
   524                       "equality_inf, path_finder_FT: path = " ^
       
   525                       space_implode " " (map string_of_int ps) ^
       
   526                       " isa-term: " ^  Syntax.string_of_term ctxt tm ^
       
   527                       " fol-term: " ^ Metis_Term.toString t)
       
   528       fun path_finder_MX tm [] _ = (tm, Bound 0)
   529       fun path_finder_MX tm [] _ = (tm, Bound 0)
   529         | path_finder_MX tm (p :: ps) (t as Metis_Term.Fn (s, ts)) =
   530         | path_finder_MX tm (p :: ps) (t as Metis_Term.Fn (s, ts)) =
   530           if s = metis_app_op (* FIXME ### mangled etc. *) then
   531           (* FIXME ### what if these are mangled? *) 
       
   532           if s = metis_type_tag then
       
   533             if p = 1 then path_finder_MX tm ps (nth ts 1)
       
   534             else path_finder_fail MX tm (p :: ps) (SOME t)
       
   535           else if s = metis_app_op then
   531             let
   536             let
   532               val (tm1, tm2) = dest_comb tm in
   537               val (tm1, tm2) = dest_comb tm in
   533               if p = 0 then path_finder_MX tm1 ps (hd ts) ||> (fn y => y $ tm2)
   538               if p = 0 then path_finder_MX tm1 ps (hd ts) ||> (fn y => y $ tm2)
   534               else path_finder_MX tm2 ps (nth ts 1) ||> (fn y => tm1 $ y)
   539               else path_finder_MX tm2 ps (nth ts 1) ||> (fn y => tm1 $ y)
   535             end
   540             end
   538               val (tm1, args) = strip_comb tm
   543               val (tm1, args) = strip_comb tm
   539               val adjustment = length ts - length args
   544               val adjustment = length ts - length args
   540               val p' = if adjustment > p then p else p - adjustment
   545               val p' = if adjustment > p then p else p - adjustment
   541               val tm_p = nth args p'
   546               val tm_p = nth args p'
   542                 handle Subscript =>
   547                 handle Subscript =>
   543                        raise METIS ("equality_inf",
   548                        path_finder_fail MX tm (p :: ps) (SOME t)
   544                                     string_of_int p ^ " adj " ^
       
   545                                     string_of_int adjustment ^ " term " ^
       
   546                                     Syntax.string_of_term ctxt tm)
       
   547               val _ = trace_msg ctxt (fn () =>
   549               val _ = trace_msg ctxt (fn () =>
   548                   "path_finder: " ^ string_of_int p ^ "  " ^
   550                   "path_finder: " ^ string_of_int p ^ "  " ^
   549                   Syntax.string_of_term ctxt tm_p)
   551                   Syntax.string_of_term ctxt tm_p)
   550               val (r, t) = path_finder_MX tm_p ps (nth ts p)
   552               val (r, t) = path_finder_MX tm_p ps (nth ts p)
   551             in (r, list_comb (tm1, replace_item_list t p' args)) end
   553             in (r, list_comb (tm1, replace_item_list t p' args)) end
   552         | path_finder_MX tm ps t =
   554         | path_finder_MX tm ps t = path_finder_fail MX tm ps (SOME t)
   553           raise Fail ("Cannot replay Metis proof in Isabelle:\n" ^
       
   554                       "equality_inf, path_finder_MX: path = " ^
       
   555                       space_implode " " (map string_of_int ps) ^
       
   556                       " isa-term: " ^  Syntax.string_of_term ctxt tm ^
       
   557                       " fol-term: " ^ Metis_Term.toString t)
       
   558       fun path_finder FO tm ps _ = path_finder_FO tm ps
   555       fun path_finder FO tm ps _ = path_finder_FO tm ps
   559         | path_finder HO (tm as Const(@{const_name HOL.eq},_) $ _ $ _) (p::ps) _ =
   556         | path_finder HO (tm as Const(@{const_name HOL.eq},_) $ _ $ _) (p::ps) _ =
   560              (*equality: not curried, as other predicates are*)
   557              (*equality: not curried, as other predicates are*)
   561              if p=0 then path_finder_HO tm (0::1::ps)  (*select first operand*)
   558              if p=0 then path_finder_HO tm (0::1::ps)  (*select first operand*)
   562              else path_finder_HO tm (p::ps)        (*1 selects second operand*)
   559              else path_finder_HO tm (p::ps)        (*1 selects second operand*)