src/HOL/Tools/ATP/atp_reconstruct.ML
changeset 43094 269300fb83d0
parent 43093 40e50afbc203
child 43095 ccf1c09dea82
equal deleted inserted replaced
43093:40e50afbc203 43094:269300fb83d0
     6 Proof reconstruction from ATP proofs.
     6 Proof reconstruction from ATP proofs.
     7 *)
     7 *)
     8 
     8 
     9 signature ATP_RECONSTRUCT =
     9 signature ATP_RECONSTRUCT =
    10 sig
    10 sig
       
    11   type 'a fo_term = 'a ATP_Problem.fo_term
    11   type 'a proof = 'a ATP_Proof.proof
    12   type 'a proof = 'a ATP_Proof.proof
    12   type locality = ATP_Translate.locality
    13   type locality = ATP_Translate.locality
    13   type type_system = ATP_Translate.type_system
    14   type type_system = ATP_Translate.type_system
    14 
    15 
    15   datatype reconstructor =
    16   datatype reconstructor =
    40     Proof.context -> type_system -> int list list -> int
    41     Proof.context -> type_system -> int list list -> int
    41     -> (string * locality) list vector -> 'a proof -> string list option
    42     -> (string * locality) list vector -> 'a proof -> string list option
    42   val uses_typed_helpers : int list -> 'a proof -> bool
    43   val uses_typed_helpers : int list -> 'a proof -> bool
    43   val reconstructor_name : reconstructor -> string
    44   val reconstructor_name : reconstructor -> string
    44   val one_line_proof_text : one_line_params -> string
    45   val one_line_proof_text : one_line_params -> string
       
    46   val term_from_atp :
       
    47     theory -> bool -> int Symtab.table -> (string * sort) list -> typ option
       
    48     -> string fo_term -> term
    45   val isar_proof_text :
    49   val isar_proof_text :
    46     Proof.context -> bool -> isar_params -> one_line_params -> string
    50     Proof.context -> bool -> isar_params -> one_line_params -> string
    47   val proof_text :
    51   val proof_text :
    48     Proof.context -> bool -> isar_params -> one_line_params -> string
    52     Proof.context -> bool -> isar_params -> one_line_params -> string
    49 end;
    53 end;
   391 fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
   395 fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
   392 fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
   396 fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
   393   | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
   397   | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
   394   | add_type_constraint _ _ = I
   398   | add_type_constraint _ _ = I
   395 
   399 
   396 fun repair_tptp_variable_name f s =
   400 fun repair_variable_name f s =
   397   let
   401   let
   398     fun subscript_name s n = s ^ nat_subscript n
   402     fun subscript_name s n = s ^ nat_subscript n
   399     val s = String.map f s
   403     val s = String.map f s
   400   in
   404   in
   401     case space_explode "_" s of
   405     case space_explode "_" s of
   410     | _ => s
   414     | _ => s
   411   end
   415   end
   412   
   416   
   413 (* First-order translation. No types are known for variables. "HOLogic.typeT"
   417 (* First-order translation. No types are known for variables. "HOLogic.typeT"
   414    should allow them to be inferred. *)
   418    should allow them to be inferred. *)
   415 fun term_from_atp thy sym_tab tfrees =
   419 fun term_from_atp thy textual sym_tab tfrees =
   416   let
   420   let
       
   421     (* see also "mk_var" in "Metis_Reconstruct" *)
       
   422     val var_index = if textual then 0 else 1
   417     fun do_term extra_us opt_T u =
   423     fun do_term extra_us opt_T u =
   418       case u of
   424       case u of
   419         ATerm (a, us) =>
   425         ATerm (a, us) =>
   420         if String.isPrefix simple_type_prefix a then
   426         if String.isPrefix simple_type_prefix a then
   421           @{const True} (* ignore TPTP type information *)
   427           @{const True} (* ignore TPTP type information *)
   422         else if a = tptp_equal then
   428         else if a = tptp_equal then
   423           let val ts = map (do_term [] NONE) us in
   429           let val ts = map (do_term [] NONE) us in
   424             if length ts = 2 andalso hd ts aconv List.last ts then
   430             if textual andalso length ts = 2 andalso
       
   431               hd ts aconv List.last ts then
   425               (* Vampire is keen on producing these. *)
   432               (* Vampire is keen on producing these. *)
   426               @{const True}
   433               @{const True}
   427             else
   434             else
   428               list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
   435               list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
   429           end
   436           end
   471             val t =
   478             val t =
   472               case strip_prefix_and_unascii fixed_var_prefix a of
   479               case strip_prefix_and_unascii fixed_var_prefix a of
   473                 SOME b => Free (b, T)
   480                 SOME b => Free (b, T)
   474               | NONE =>
   481               | NONE =>
   475                 case strip_prefix_and_unascii schematic_var_prefix a of
   482                 case strip_prefix_and_unascii schematic_var_prefix a of
   476                   SOME b => Var ((b, 0), T)
   483                   SOME b => Var ((b, var_index), T)
   477                 | NONE =>
   484                 | NONE =>
   478                   if is_tptp_variable a then
   485                   Var ((repair_variable_name Char.toLower a, var_index), T)
   479                     Var ((repair_tptp_variable_name Char.toLower a, 0), T)
       
   480                   else
       
   481                     (* Skolem constants? *)
       
   482                     Var ((repair_tptp_variable_name Char.toUpper a, 0), T)
       
   483           in list_comb (t, ts) end
   486           in list_comb (t, ts) end
   484   in do_term [] end
   487   in do_term [] end
   485 
   488 
   486 fun term_from_atom thy sym_tab tfrees pos (u as ATerm (s, _)) =
   489 fun term_from_atom thy textual sym_tab tfrees pos (u as ATerm (s, _)) =
   487   if String.isPrefix class_prefix s then
   490   if String.isPrefix class_prefix s then
   488     add_type_constraint pos (type_constraint_from_term tfrees u)
   491     add_type_constraint pos (type_constraint_from_term tfrees u)
   489     #> pair @{const True}
   492     #> pair @{const True}
   490   else
   493   else
   491     pair (term_from_atp thy sym_tab tfrees (SOME @{typ bool}) u)
   494     pair (term_from_atp thy textual sym_tab tfrees (SOME @{typ bool}) u)
   492 
   495 
   493 val combinator_table =
   496 val combinator_table =
   494   [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def_raw}),
   497   [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def_raw}),
   495    (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def_raw}),
   498    (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def_raw}),
   496    (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def_raw}),
   499    (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def_raw}),
   531                   |> map Var
   534                   |> map Var
   532   in fold_rev quant_of vars t end
   535   in fold_rev quant_of vars t end
   533 
   536 
   534 (* Interpret an ATP formula as a HOL term, extracting sort constraints as they
   537 (* Interpret an ATP formula as a HOL term, extracting sort constraints as they
   535    appear in the formula. *)
   538    appear in the formula. *)
   536 fun prop_from_formula thy sym_tab tfrees phi =
   539 fun prop_from_formula thy textual sym_tab tfrees phi =
   537   let
   540   let
   538     fun do_formula pos phi =
   541     fun do_formula pos phi =
   539       case phi of
   542       case phi of
   540         AQuant (_, [], phi) => do_formula pos phi
   543         AQuant (_, [], phi) => do_formula pos phi
   541       | AQuant (q, (s, _) :: xs, phi') =>
   544       | AQuant (q, (s, _) :: xs, phi') =>
   542         do_formula pos (AQuant (q, xs, phi'))
   545         do_formula pos (AQuant (q, xs, phi'))
   543         (* FIXME: TFF *)
   546         (* FIXME: TFF *)
   544         #>> quantify_over_var (case q of
   547         #>> quantify_over_var (case q of
   545                                  AForall => forall_of
   548                                  AForall => forall_of
   546                                | AExists => exists_of)
   549                                | AExists => exists_of)
   547                               (repair_tptp_variable_name Char.toLower s)
   550                               (repair_variable_name Char.toLower s)
   548       | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
   551       | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
   549       | AConn (c, [phi1, phi2]) =>
   552       | AConn (c, [phi1, phi2]) =>
   550         do_formula (pos |> c = AImplies ? not) phi1
   553         do_formula (pos |> c = AImplies ? not) phi1
   551         ##>> do_formula pos phi2
   554         ##>> do_formula pos phi2
   552         #>> (case c of
   555         #>> (case c of
   555              | AImplies => s_imp
   558              | AImplies => s_imp
   556              | AIf => s_imp o swap
   559              | AIf => s_imp o swap
   557              | AIff => s_iff
   560              | AIff => s_iff
   558              | ANotIff => s_not o s_iff
   561              | ANotIff => s_not o s_iff
   559              | _ => raise Fail "unexpected connective")
   562              | _ => raise Fail "unexpected connective")
   560       | AAtom tm => term_from_atom thy sym_tab tfrees pos tm
   563       | AAtom tm => term_from_atom thy textual sym_tab tfrees pos tm
   561       | _ => raise FORMULA [phi]
   564       | _ => raise FORMULA [phi]
   562   in repair_tvar_sorts (do_formula true phi Vartab.empty) end
   565   in repair_tvar_sorts (do_formula true phi Vartab.empty) end
   563 
   566 
   564 fun check_formula ctxt =
   567 fun check_formula ctxt =
   565   Type.constraint HOLogic.boolT
   568   Type.constraint HOLogic.boolT
   572   | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
   575   | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
   573 
   576 
   574 fun decode_line sym_tab tfrees (Definition (name, phi1, phi2)) ctxt =
   577 fun decode_line sym_tab tfrees (Definition (name, phi1, phi2)) ctxt =
   575     let
   578     let
   576       val thy = Proof_Context.theory_of ctxt
   579       val thy = Proof_Context.theory_of ctxt
   577       val t1 = prop_from_formula thy sym_tab tfrees phi1
   580       val t1 = prop_from_formula thy true sym_tab tfrees phi1
   578       val vars = snd (strip_comb t1)
   581       val vars = snd (strip_comb t1)
   579       val frees = map unvarify_term vars
   582       val frees = map unvarify_term vars
   580       val unvarify_args = subst_atomic (vars ~~ frees)
   583       val unvarify_args = subst_atomic (vars ~~ frees)
   581       val t2 = prop_from_formula thy sym_tab tfrees phi2
   584       val t2 = prop_from_formula thy true sym_tab tfrees phi2
   582       val (t1, t2) =
   585       val (t1, t2) =
   583         HOLogic.eq_const HOLogic.typeT $ t1 $ t2
   586         HOLogic.eq_const HOLogic.typeT $ t1 $ t2
   584         |> unvarify_args |> uncombine_term thy |> check_formula ctxt
   587         |> unvarify_args |> uncombine_term thy |> check_formula ctxt
   585         |> HOLogic.dest_eq
   588         |> HOLogic.dest_eq
   586     in
   589     in
   588        fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
   591        fold Variable.declare_term (maps OldTerm.term_frees [t1, t2]) ctxt)
   589     end
   592     end
   590   | decode_line sym_tab tfrees (Inference (name, u, deps)) ctxt =
   593   | decode_line sym_tab tfrees (Inference (name, u, deps)) ctxt =
   591     let
   594     let
   592       val thy = Proof_Context.theory_of ctxt
   595       val thy = Proof_Context.theory_of ctxt
   593       val t = u |> prop_from_formula thy sym_tab tfrees
   596       val t = u |> prop_from_formula thy true sym_tab tfrees
   594                 |> uncombine_term thy |> check_formula ctxt
   597                 |> uncombine_term thy |> check_formula ctxt
   595     in
   598     in
   596       (Inference (name, t, deps),
   599       (Inference (name, t, deps),
   597        fold Variable.declare_term (OldTerm.term_frees t) ctxt)
   600        fold Variable.declare_term (OldTerm.term_frees t) ctxt)
   598     end
   601     end