src/HOL/Tools/ATP/atp_waldmeister.ML
author wenzelm
Thu, 29 Jan 2015 15:27:29 +0100
changeset 59469 fb393ecde29d
parent 58670 97c6818f4696
child 59582 0fbed69ff081
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57262
b2c629647a14 moved code around
blanchet
parents: 57217
diff changeset
     1
(*  Title:      HOL/Tools/ATP/atp_waldmeister.ML
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
     2
    Author:     Albert Steckermeier, TU Muenchen
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
     4
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
     5
General-purpose functions used by the Sledgehammer modules.
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
     6
*)
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
     7
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
     8
exception FailureMessage of string
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
     9
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    10
signature ATP_WALDMEISTER_SKOLEMIZER =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    11
sig
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    12
  val skolemize : bool -> Proof.context -> term -> (Proof.context * (term list * term))
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    13
end;
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    14
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    15
signature ATP_WALDMEISTER_TYPE_ENCODER =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    16
sig
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    17
  val encode_type : typ -> string
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    18
  val decode_type_string : string -> typ
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    19
  val encode_types : typ list -> string
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    20
  val decode_types : string -> typ list
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    21
  val encode_const : string * typ list -> string
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    22
  val decode_const : string -> string * typ list
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    23
end;
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    24
57262
b2c629647a14 moved code around
blanchet
parents: 57217
diff changeset
    25
signature ATP_WALDMEISTER =
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
    26
sig
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
    27
  type 'a atp_problem = 'a ATP_Problem.atp_problem
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
    28
  type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
    29
  type 'a atp_proof = 'a ATP_Proof.atp_proof
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
    30
  type stature = ATP_Problem_Generate.stature
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    31
  type waldmeister_info =  (string * (term list * (term option * term))) list
59469
wenzelm
parents: 58670
diff changeset
    32
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    33
  val waldmeister_skolemize_rule : string
59469
wenzelm
parents: 58670
diff changeset
    34
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    35
  val generate_waldmeister_problem : Proof.context -> term list -> term ->
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
    36
    ((string * stature) * term) list ->
59469
wenzelm
parents: 58670
diff changeset
    37
    string atp_problem * string Symtab.table * (string * term) list * int Symtab.table *
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    38
    waldmeister_info
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
    39
  val termify_waldmeister_proof : Proof.context -> string Symtab.table -> string atp_proof ->
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
    40
    (term, string) atp_step list
59469
wenzelm
parents: 58670
diff changeset
    41
  val introduce_waldmeister_skolems : waldmeister_info -> (term, string) atp_step list ->
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    42
    (term, string) atp_step list
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
    43
end;
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
    44
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    45
structure ATP_Waldmeister_Skolemizer : ATP_WALDMEISTER_SKOLEMIZER =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    46
struct
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    47
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    48
open HOLogic
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    49
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    50
fun contains_quantor (Const (@{const_name Ex}, _) $ _) = true
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    51
  | contains_quantor (Const (@{const_name All}, _) $ _) = true
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    52
  | contains_quantor (t1 $ t2) = contains_quantor t1 orelse contains_quantor t2
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    53
  | contains_quantor _ = false
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    54
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    55
fun mk_fun_for_bvar ctxt1 ctxt2 arg_trms (bound_name, ty) =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    56
  let
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    57
    val fun_type = (map type_of arg_trms) ---> ty
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    58
    val (fun_name, _) = singleton (Variable.variant_frees ctxt2 []) ("sko_" ^ bound_name,fun_type)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    59
    val (_, ctxt1_new) = Variable.add_fixes [fun_name] ctxt1
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    60
    val (_, ctxt2_new) = Variable.add_fixes [fun_name] ctxt2
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    61
  in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    62
    (Term.list_comb (Free (fun_name,fun_type), arg_trms), ctxt1_new, ctxt2_new)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    63
  end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    64
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    65
fun skolem_free ctxt1 ctxt2 vars (bound_name, ty, trm) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    66
  let
59469
wenzelm
parents: 58670
diff changeset
    67
    val (fun_trm, ctxt1_new, ctxt2_new) =
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    68
      mk_fun_for_bvar ctxt1 ctxt2 (List.rev vars) (bound_name,ty)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    69
  in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    70
    (Term.subst_bounds ([fun_trm], trm), ctxt1_new, ctxt2_new)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    71
  end
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    72
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    73
fun skolem_var ctxt (bound_name, ty, trm) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    74
  let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    75
    val (var_name, _) = singleton (Variable.variant_frees ctxt []) (bound_name, ty)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    76
    val (_, ctxt') = Variable.add_fixes [var_name] ctxt
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    77
    val var = Var ((var_name, 0), ty)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    78
  in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    79
   (Term.subst_bounds ([var], trm), ctxt', var)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    80
  end
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    81
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    82
fun skolem_bound is_free ctxt1 ctxt2 spets vars x =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    83
  if is_free then
59469
wenzelm
parents: 58670
diff changeset
    84
    let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    85
      val (trm', ctxt1', ctxt2') = skolem_free ctxt1 ctxt2 vars x
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    86
    in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    87
      (ctxt1', ctxt2',spets, trm', vars)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    88
    end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    89
  else
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    90
    let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    91
      val (trm', ctxt2', var) = skolem_var ctxt2 x
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    92
    in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    93
      (ctxt1, ctxt2', spets, trm', var :: vars)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    94
    end
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
    95
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    96
fun skolemize' pos ctxt1 ctxt2 spets vars (Const (@{const_name Not}, _) $ trm') =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    97
    let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    98
      val (ctxt1', ctxt2', spets', trm'') = skolemize' (not pos) ctxt1 ctxt2 spets vars trm'
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
    99
    in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   100
      (ctxt1', ctxt2', map mk_not spets', mk_not trm'')
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   101
    end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   102
  | skolemize' pos ctxt1 ctxt2 spets vars (trm as (Const (@{const_name HOL.eq}, t) $ a $ b)) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   103
    if t = @{typ "bool \<Rightarrow> bool \<Rightarrow> bool"} andalso contains_quantor trm then
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   104
      skolemize' pos ctxt1 ctxt2 (trm :: spets) vars (mk_conj (mk_imp (a, b), mk_imp (b, a)))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   105
    else
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   106
      (ctxt1, ctxt2, spets, trm)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   107
  | skolemize' pos ctxt1 ctxt2 spets vars (trm as (Const (name, _) $ Abs x)) =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   108
    if name = @{const_name Ex} orelse name = @{const_name All} then
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   109
      let
59469
wenzelm
parents: 58670
diff changeset
   110
        val is_free =  (name = @{const_name Ex} andalso pos)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   111
          orelse (name = @{const_name All} andalso not pos)
59469
wenzelm
parents: 58670
diff changeset
   112
        val (ctxt1', ctxt2', spets', trm', vars') =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   113
          skolem_bound is_free ctxt1 ctxt2 (if is_free then trm :: spets else spets) vars x
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   114
      in
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   115
        skolemize' pos ctxt1' ctxt2' spets' vars' trm'
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   116
      end
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   117
    else
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   118
      (ctxt1, ctxt2, spets, trm)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   119
  | skolemize' pos ctxt1 ctxt2 spets vars ((c as Const (name, _)) $ a $ b) =
59469
wenzelm
parents: 58670
diff changeset
   120
    if name = @{const_name conj} orelse name = @{const_name disj} orelse
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   121
       name = @{const_name implies} then
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   122
      let
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   123
        val pos_a = if name = @{const_name implies} then not pos else pos
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   124
        val (ctxt1', ctxt2', spets', a') = skolemize'  pos_a ctxt1 ctxt2 [] vars a
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   125
        val (ctxt1'', ctxt2'', spets'', b') = skolemize' pos ctxt1' ctxt2' [] vars b
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   126
      in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   127
        (ctxt1'', ctxt2'',
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   128
         map (fn trm => c $ a' $ trm) spets'' @ map (fn trm => c $ trm $ b) spets' @ spets,
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   129
         c $ a' $ b')
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   130
      end
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   131
    else
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   132
      (ctxt1,ctxt2,spets,c $ a $ b)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   133
  | skolemize' _ ctxt1 ctxt2 spets _ trm = (ctxt1, ctxt2, spets, trm)
59469
wenzelm
parents: 58670
diff changeset
   134
58670
97c6818f4696 Fixed bug in waldmeister skolemization
steckerm
parents: 58412
diff changeset
   135
  fun vars_of trm =
97c6818f4696 Fixed bug in waldmeister skolemization
steckerm
parents: 58412
diff changeset
   136
    rev (distinct (op =) (Term.fold_aterms (fn t as Var _ => cons t | _ => I) trm []));
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   137
59469
wenzelm
parents: 58670
diff changeset
   138
  fun skolemize positve ctxt trm =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   139
    let
58670
97c6818f4696 Fixed bug in waldmeister skolemization
steckerm
parents: 58412
diff changeset
   140
      val (ctxt1, _, spets, skolemized_trm) = skolemize' positve ctxt ctxt [] (vars_of trm) trm
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   141
    in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   142
        (ctxt1, (trm :: List.rev spets, skolemized_trm))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   143
    end
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   144
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   145
end;
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   146
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   147
structure ATP_Waldmeister_Type_Encoder : ATP_WALDMEISTER_TYPE_ENCODER =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   148
struct
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   149
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   150
val delimiter = ";"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   151
val open_paranthesis = "["
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   152
val close_parathesis = "]"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   153
val type_prefix = "Type"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   154
val tfree_prefix = "TFree"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   155
val tvar_prefix = "TVar"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   156
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   157
val identifier_character = not o member (op =) [delimiter, open_paranthesis, close_parathesis]
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   158
59469
wenzelm
parents: 58670
diff changeset
   159
fun encode_type (Type (name, types)) =
wenzelm
parents: 58670
diff changeset
   160
  type_prefix ^ open_paranthesis ^ name ^ delimiter ^
wenzelm
parents: 58670
diff changeset
   161
  (map encode_type types |> space_implode delimiter) ^ close_parathesis
wenzelm
parents: 58670
diff changeset
   162
| encode_type (TFree (name, sorts)) =
wenzelm
parents: 58670
diff changeset
   163
  tfree_prefix ^ open_paranthesis ^ name ^ delimiter ^ space_implode delimiter sorts ^
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   164
  close_parathesis
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   165
| encode_type (TVar ((name, i), sorts)) =
59469
wenzelm
parents: 58670
diff changeset
   166
  tvar_prefix ^ open_paranthesis ^ open_paranthesis ^ name ^ delimiter ^ Int.toString i ^
wenzelm
parents: 58670
diff changeset
   167
  close_parathesis ^ delimiter ^ space_implode delimiter sorts ^ close_parathesis
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   168
59469
wenzelm
parents: 58670
diff changeset
   169
fun encode_types types = space_implode delimiter (map encode_type types)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   170
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   171
fun parse_identifier x =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   172
  (Scan.many identifier_character >> implode) x
59469
wenzelm
parents: 58670
diff changeset
   173
wenzelm
parents: 58670
diff changeset
   174
fun parse_star delim scanner x =
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   175
  (Scan.optional (scanner ::: Scan.repeat ($$ delim |-- scanner)) []) x
59469
wenzelm
parents: 58670
diff changeset
   176
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   177
fun parse_type x = (Scan.this_string type_prefix |-- $$ open_paranthesis |-- parse_identifier --|
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   178
  $$ delimiter -- parse_star delimiter parse_any_type --| $$ close_parathesis >> Type) x
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   179
and parse_tfree x = (Scan.this_string tfree_prefix |-- $$ open_paranthesis |-- parse_identifier --|
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   180
  $$ delimiter -- parse_star delimiter parse_identifier --| $$ close_parathesis >> TFree) x
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   181
and parse_tvar x = (Scan.this_string tvar_prefix |-- $$ open_paranthesis |-- $$ open_paranthesis
59469
wenzelm
parents: 58670
diff changeset
   182
  |-- parse_identifier --| $$ delimiter -- (parse_identifier >> (Int.fromString #> the)) --| $$
wenzelm
parents: 58670
diff changeset
   183
  close_parathesis --| $$ delimiter -- parse_star delimiter parse_identifier --|
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   184
  $$ close_parathesis >> TVar) x
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   185
and parse_any_type x = (parse_type || parse_tfree || parse_tvar) x
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   186
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   187
fun parse_types x = parse_star delimiter parse_any_type x
59469
wenzelm
parents: 58670
diff changeset
   188
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   189
fun decode_type_string s = Scan.finite Symbol.stopper
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   190
  (Scan.error (!! (fn _ => raise FailureMessage ("unrecognized type encoding" ^
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   191
  quote s)) parse_type))  (Symbol.explode s) |> fst
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   192
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   193
fun decode_types s = Scan.finite Symbol.stopper
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   194
  (Scan.error (!! (fn _ => raise FailureMessage ("unrecognized type encoding" ^
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   195
  quote s))) parse_types) (Symbol.explode s) |> fst
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   196
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   197
fun encode_const (name,tys) = name ^ delimiter ^ encode_types tys
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   198
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   199
fun parse_const s = (parse_identifier --| $$ delimiter -- parse_types) s
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   200
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   201
fun decode_const s = Scan.finite Symbol.stopper
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   202
  (Scan.error (!! (fn _ => raise FailureMessage ("unrecognized const encoding" ^
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   203
  quote s))) parse_const) (Symbol.explode s) |> fst
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   204
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   205
end;
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   206
58405
c3c7a09a3ada Made encoded type for apply less restrictive
steckerm
parents: 58404
diff changeset
   207
structure ATP_Waldmeister (*** : ATP_WALDMEISTER  *) =
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   208
struct
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   209
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   210
open ATP_Util
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   211
open ATP_Problem
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   212
open ATP_Problem_Generate
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   213
open ATP_Proof
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   214
open ATP_Proof_Reconstruct
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   215
open ATP_Waldmeister_Skolemizer
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   216
open ATP_Waldmeister_Type_Encoder
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   217
open HOLogic
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   218
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   219
type ('a, 'b) atp_term = ('a, 'b) ATP_Problem.atp_term
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   220
type atp_connective = ATP_Problem.atp_connective
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   221
type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   222
type atp_format = ATP_Problem.atp_format
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   223
type atp_formula_role = ATP_Problem.atp_formula_role
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   224
type 'a atp_problem = 'a ATP_Problem.atp_problem
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   225
type waldmeister_info =  (string * (term list * (term option * term))) list
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   226
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   227
val const_prefix = #"c"
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   228
val var_prefix = #"V"
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   229
val free_prefix = #"v"
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   230
val conjecture_condition_name = "condition"
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   231
val waldmeister_equals = "eq"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   232
val waldmeister_true = "true"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   233
val waldmeister_false = "false"
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   234
val waldmeister_skolemize_rule = "waldmeister_skolemize"
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   235
val lam_lift_waldmeister_prefix = "lambda_wm"
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   236
val waldmeister_apply = "wm_apply"
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   237
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   238
val factsN = "Relevant facts"
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   239
val helpersN = "Helper facts"
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   240
val conjN = "Conjecture"
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   241
val conj_identifier = conjecture_prefix ^ "0"
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   242
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   243
val WM_ERROR_MSG = "Waldmeister problem generator failed: "
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   244
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   245
(*
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   246
  Some utilitary functions for translation.
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   247
*)
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   248
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   249
fun gen_ascii_tuple str = (str, ascii_of str)
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   250
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   251
fun mk_eq_true (trm as (Const (@{const_name HOL.eq}, _) $ _ $ _)) = (NONE,trm)
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   252
  | mk_eq_true trm = (SOME trm,HOLogic.mk_eq (trm, @{term True}))
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   253
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   254
val is_lambda_name = String.isPrefix lam_lifted_poly_prefix
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   255
59469
wenzelm
parents: 58670
diff changeset
   256
fun lookup table k =
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   257
  List.find (fn (key, _) => key = k) table
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   258
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   259
fun dest_list' (f $ t) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   260
  let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   261
    val (function, trms) = dest_list' f
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   262
  in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   263
    (function, t :: trms)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   264
  end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   265
  | dest_list' t = (t,[]);
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   266
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   267
fun dest_list trm = dest_list' trm ||> List.rev
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   268
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   269
fun list_update x [] = [x]
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   270
  | list_update (a,b) ((c,d) :: xs) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   271
  if a = c andalso b < d then
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   272
    (a,b) :: xs
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   273
  else
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   274
    (c,d) :: list_update (a,b) xs
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   275
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   276
(*
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   277
  Hiding partial applications in terms
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   278
*)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   279
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   280
fun map_minimal_app' info (trm :: trms) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   281
    map_minimal_app' (minimal_app' info trm) trms
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   282
  | map_minimal_app' info _ = info
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   283
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   284
and minimal_app' info (trm as _ $ _) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   285
  let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   286
    val (function, trms) = dest_list trm
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   287
    val info' = map_minimal_app' info trms
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   288
  in
59469
wenzelm
parents: 58670
diff changeset
   289
    case function of
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   290
      (Const _) => list_update (function, length trms) info' |
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   291
      (Free _) => list_update (function, length trms) info' |
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   292
      _ => info
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   293
  end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   294
  | minimal_app' info (trm as Const _) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   295
   list_update (trm, 0) info
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   296
  | minimal_app' info (trm as Free _) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   297
   list_update (trm, 0) info
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   298
  | minimal_app' info _ = info;
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   299
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   300
fun map_minimal_app trms = map_minimal_app' [] trms
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   301
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   302
fun mk_waldmeister_app function [] = function
59469
wenzelm
parents: 58670
diff changeset
   303
  | mk_waldmeister_app function (a :: args) =
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   304
    let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   305
      val funT = type_of function
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   306
      val argT = type_of a
58405
c3c7a09a3ada Made encoded type for apply less restrictive
steckerm
parents: 58404
diff changeset
   307
      val resT = dest_funT funT |> snd
c3c7a09a3ada Made encoded type for apply less restrictive
steckerm
parents: 58404
diff changeset
   308
      val newT = funT --> argT --> resT
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   309
    in
59469
wenzelm
parents: 58670
diff changeset
   310
      mk_waldmeister_app (Const (waldmeister_apply ^ "," ^
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   311
        encode_types [resT, argT], newT) $ function $ a) args
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   312
    end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   313
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   314
fun hide_partial_applications info (trm as (_ $ _)) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   315
  let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   316
    val (function, trms) = dest_list trm
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   317
    val trms' = map (hide_partial_applications info) trms
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   318
  in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   319
    case function of
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   320
    Var _ =>  mk_waldmeister_app function trms' |
59469
wenzelm
parents: 58670
diff changeset
   321
    _ =>
wenzelm
parents: 58670
diff changeset
   322
      let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   323
        val min_args = lookup info function |> the |> snd
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   324
        val args0 = List.take (trms',min_args)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   325
        val args1 = List.drop (trms',min_args)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   326
        val function' = list_comb (function,args0)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   327
      in
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   328
        mk_waldmeister_app function' args1
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   329
      end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   330
  end
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   331
  | hide_partial_applications _ t = t;
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   332
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   333
fun remove_waldmeister_app ((c as Const (name, _)) $ x $ y) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   334
  if String.isPrefix waldmeister_apply name then
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   335
    remove_waldmeister_app x $ remove_waldmeister_app y
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   336
  else
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   337
    c $ remove_waldmeister_app x $ remove_waldmeister_app y
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   338
  | remove_waldmeister_app (x $ y) = remove_waldmeister_app x $ remove_waldmeister_app y
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   339
  | remove_waldmeister_app x = x
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   340
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   341
(*
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   342
  Translation from Isabelle terms to ATP terms.
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   343
*)
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   344
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   345
fun trm_to_atp'' thy (Const (x, ty)) args =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   346
    let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   347
      val ty_args = if is_lambda_name x orelse String.isPrefix waldmeister_apply x then
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   348
        [] else Sign.const_typargs thy (x, ty)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   349
    in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   350
      [ATerm ((gen_ascii_tuple (String.str const_prefix ^ encode_const (x, ty_args)), []), args)]
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   351
    end
59469
wenzelm
parents: 58670
diff changeset
   352
  | trm_to_atp'' _ (Free (x, _)) args =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   353
    [ATerm ((gen_ascii_tuple (String.str free_prefix ^ x), []), args)]
59469
wenzelm
parents: 58670
diff changeset
   354
  | trm_to_atp'' _ (Var ((x, _), _)) args =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   355
    [ATerm ((gen_ascii_tuple (String.str var_prefix ^ x), []), args)]
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   356
  | trm_to_atp'' thy (trm1 $ trm2) args = trm_to_atp'' thy trm1 (trm_to_atp'' thy trm2 [] @ args)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   357
  | trm_to_atp'' _ _ _ = raise FailureMessage (WM_ERROR_MSG ^ "Unexpected term")
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   358
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   359
fun trm_to_atp' thy trm = trm_to_atp'' thy trm [] |> hd
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   360
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   361
fun eq_trm_to_atp thy (Const (@{const_name HOL.eq}, _) $ lhs $ rhs) =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   362
    ATerm ((("equal", "equal"), []), [trm_to_atp' thy lhs, trm_to_atp' thy rhs])
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   363
  | eq_trm_to_atp _ _ = raise FailureMessage (WM_ERROR_MSG ^ "Non-eq term")
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   364
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   365
(* Translation from ATP terms to Isabelle terms. *)
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   366
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   367
fun construct_term thy (name, args) =
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   368
  let
57217
159c18bbc879 tuned whitespaces
steckerm
parents: 57215
diff changeset
   369
    val prefix = String.sub (name, 0)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   370
    val encoded_name = String.extract(name, 1, NONE)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   371
    fun dummy_fun_type () = replicate (length args) dummyT ---> dummyT
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   372
  in
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   373
    if prefix = const_prefix then
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   374
      let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   375
        val (const_name, ty_args) = if String.isPrefix waldmeister_apply encoded_name then
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   376
          (waldmeister_apply, []) else decode_const encoded_name
59469
wenzelm
parents: 58670
diff changeset
   377
        val const_trans_name =
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   378
          if is_lambda_name const_name then
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   379
            lam_lift_waldmeister_prefix ^ (* ?? *)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   380
            String.extract(const_name, size lam_lifted_poly_prefix, NONE)
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   381
          else
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   382
            const_name
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   383
      in
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   384
        Const (const_trans_name,
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   385
          if is_lambda_name const_name orelse String.isPrefix waldmeister_apply const_name then
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   386
            dummyT
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   387
          else
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   388
            Sign.const_instance thy (const_name, ty_args))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   389
      end
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   390
    else if prefix = free_prefix then
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   391
      Free (encoded_name, dummy_fun_type ())
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   392
    else if Char.isUpper prefix then
59469
wenzelm
parents: 58670
diff changeset
   393
      Var ((name, 0), dummy_fun_type ())
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   394
      (* Use name instead of encoded_name because Waldmeister renames free variables. *)
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   395
    else if name = waldmeister_equals then
59469
wenzelm
parents: 58670
diff changeset
   396
      (case args of
58404
4be5ab4452f4 Updated fix_name function
steckerm
parents: 58403
diff changeset
   397
        [_, _] => eq_const dummyT
59469
wenzelm
parents: 58670
diff changeset
   398
      | _ => raise FailureMessage
wenzelm
parents: 58670
diff changeset
   399
        (WM_ERROR_MSG ^ "waldmeister equals needs 2 arguments but has " ^
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   400
         Int.toString (length args)))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   401
    else if name = waldmeister_true then
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   402
      @{term True}
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   403
    else if name = waldmeister_false then
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   404
      @{term False}
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   405
    else
59469
wenzelm
parents: 58670
diff changeset
   406
      raise FailureMessage
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   407
        (WM_ERROR_MSG ^ "Unknown name prefix when parsing Waldmeister proof: name = " ^ name)
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   408
  end
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   409
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   410
and atp_to_trm' thy (ATerm ((name,_), args)) =
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   411
    (case args of
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   412
      [] => construct_term thy (name, args)
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   413
     | _ => Term.list_comb (construct_term thy (name, args), map (atp_to_trm' thy) args))
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   414
     | atp_to_trm' _ _ = raise FailureMessage (WM_ERROR_MSG ^ "atp_to_trm' expects ATerm")
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   415
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   416
fun atp_to_trm thy (ATerm (("equal", _), [lhs, rhs])) =
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   417
    mk_eq (atp_to_trm' thy lhs, atp_to_trm' thy rhs)
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   418
  | atp_to_trm _ (ATerm (("$true", _), _)) = @{term True}
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   419
  | atp_to_trm _ _ = raise FailureMessage (WM_ERROR_MSG ^ "atp_to_trm expects ATerm")
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   420
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   421
fun formula_to_trm thy (AAtom aterm) = aterm |> atp_to_trm thy
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   422
  | formula_to_trm thy (AConn (ANot, [aterm])) =
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   423
    mk_not (formula_to_trm thy aterm)
59469
wenzelm
parents: 58670
diff changeset
   424
  | formula_to_trm _ _ =
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   425
    raise FailureMessage (WM_ERROR_MSG ^ "formula_to_trm expects AAtom or AConn")
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   426
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   427
(* Abstract translation *)
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   428
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   429
fun mk_formula prefix_name name atype aterm =
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   430
  Formula ((prefix_name ^ ascii_of name, name), atype, AAtom aterm, NONE, [])
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   431
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   432
fun problem_lines_of_fact thy prefix (s, (_, (_, t))) =
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   433
  mk_formula (prefix ^ "0_") s Axiom (eq_trm_to_atp thy t)
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   434
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   435
fun make_nice problem = nice_atp_problem true CNF problem
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   436
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   437
fun mk_conjecture aterm =
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   438
  let
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   439
    val formula = mk_anot (AAtom aterm)
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   440
  in
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   441
    Formula ((conj_identifier, ""), Hypothesis, formula, NONE, [])
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   442
  end
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   443
58401
b8ca69d9897b Re-added hypothesis argument to problem generation
steckerm
parents: 58200
diff changeset
   444
fun generate_waldmeister_problem ctxt hyps_t0 concl_t0 facts0 =
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   445
  let
57270
0260171a51ef reintroduce atomize in Waldmeister code
blanchet
parents: 57267
diff changeset
   446
    val thy = Proof_Context.theory_of ctxt
0260171a51ef reintroduce atomize in Waldmeister code
blanchet
parents: 57267
diff changeset
   447
0260171a51ef reintroduce atomize in Waldmeister code
blanchet
parents: 57267
diff changeset
   448
    val preproc = Object_Logic.atomize_term thy
0260171a51ef reintroduce atomize in Waldmeister code
blanchet
parents: 57267
diff changeset
   449
58401
b8ca69d9897b Re-added hypothesis argument to problem generation
steckerm
parents: 58200
diff changeset
   450
    val conditions = map preproc hyps_t0
b8ca69d9897b Re-added hypothesis argument to problem generation
steckerm
parents: 58200
diff changeset
   451
    val consequence = preproc concl_t0
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   452
    val facts = map (apsnd preproc #> apfst fst) facts0 : (string * term) list
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   453
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   454
    fun map_ctxt' _ ctxt [] ys = (ctxt, ys)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   455
      | map_ctxt' f ctxt (x :: xs) ys =
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   456
        let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   457
          val (ctxt', x') = f ctxt x
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   458
        in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   459
          map_ctxt' f ctxt' xs (x' :: ys)
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   460
        end
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   461
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   462
    fun map_ctxt f ctxt xs = map_ctxt' f ctxt xs []
59469
wenzelm
parents: 58670
diff changeset
   463
wenzelm
parents: 58670
diff changeset
   464
    fun skolemize_fact ctxt (name, trm) =
wenzelm
parents: 58670
diff changeset
   465
      let
wenzelm
parents: 58670
diff changeset
   466
        val (ctxt', (steps, trm')) = skolemize true ctxt trm
wenzelm
parents: 58670
diff changeset
   467
      in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   468
        (ctxt', (name, (steps, trm')))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   469
      end
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   470
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   471
    fun name_list' _ [] _ = []
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   472
      | name_list' prefix (x :: xs) i = (prefix ^ Int.toString i, x) :: name_list' prefix xs (i + 1)
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   473
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   474
    fun name_list prefix xs = name_list' prefix xs 0
59469
wenzelm
parents: 58670
diff changeset
   475
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   476
    (* Skolemization, hiding lambdas and translating formulas to equations *)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   477
    val (ctxt', sko_facts) = map_ctxt skolemize_fact ctxt facts
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   478
    val (ctxt'', sko_conditions) = map_ctxt (skolemize true) ctxt' conditions
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   479
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   480
    val post_skolem = do_cheaply_conceal_lambdas []
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   481
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   482
    val sko_eq_facts0 = map (apsnd (apsnd (mk_eq_true #> apsnd post_skolem))) sko_facts
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   483
    val sko_eq_conditions0 = map (apsnd (mk_eq_true #> apsnd post_skolem)) sko_conditions
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   484
      |> name_list conjecture_condition_name
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   485
    val (_, eq_conseq as (_, (non_eq_consequence0, eq_consequence0))) =
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   486
      skolemize false ctxt'' consequence |> apsnd (apsnd (mk_eq_true #> apsnd post_skolem))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   487
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   488
    val sko_eq_info =
59469
wenzelm
parents: 58670
diff changeset
   489
      (((conj_identifier, eq_conseq) :: sko_eq_conditions0)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   490
      @ map (apfst (fn name => fact_prefix ^ "0_" ^ name)) sko_eq_facts0)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   491
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   492
    (* Translation of partial function applications *)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   493
    val fun_app_info = map_minimal_app (map (snd o snd o snd) sko_eq_info)
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   494
59469
wenzelm
parents: 58670
diff changeset
   495
    fun hide_partial_apps_in_last (x, (y, (z, term))) =
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   496
      (x, (y, (z, hide_partial_applications fun_app_info term)))
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   497
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   498
    val sko_eq_facts = map hide_partial_apps_in_last sko_eq_facts0
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   499
    val sko_eq_conditions = map hide_partial_apps_in_last sko_eq_conditions0
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   500
    val eq_consequence = hide_partial_applications fun_app_info eq_consequence0
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   501
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   502
    (* Problem creation *)
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   503
    val fact_lines = map (problem_lines_of_fact thy fact_prefix) sko_eq_facts
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   504
    val condition_lines =
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   505
      map (fn (name, (_, (_, trm))) =>
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   506
        mk_formula fact_prefix name Hypothesis (eq_trm_to_atp thy trm)) sko_eq_conditions
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   507
    val axiom_lines = fact_lines @ condition_lines
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   508
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   509
    val conj_line = mk_conjecture (eq_trm_to_atp thy eq_consequence)
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   510
58412
f65f11f4854c more standard Isabelle/ML operations;
wenzelm
parents: 58411
diff changeset
   511
    val helper_lemmas_needed = exists (snd #> snd #> fst #> is_some) sko_eq_facts
f65f11f4854c more standard Isabelle/ML operations;
wenzelm
parents: 58411
diff changeset
   512
      orelse exists (snd #> snd #> fst #> is_some) sko_eq_conditions orelse
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   513
      is_some non_eq_consequence0
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   514
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   515
    val helper_lines =
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   516
      if helper_lemmas_needed then
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   517
        [(helpersN,
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   518
          @{thms waldmeister_fol}
57270
0260171a51ef reintroduce atomize in Waldmeister code
blanchet
parents: 57267
diff changeset
   519
          |> map (fn th => (("", (Global, General)), preproc (prop_of th)))
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   520
          |> map (fn ((s, _) ,t) => mk_formula helper_prefix s Axiom (eq_trm_to_atp thy t)))]
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   521
      else
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   522
        []
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   523
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   524
    val problem = (factsN, axiom_lines) :: helper_lines @ [(conjN, [conj_line])]
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   525
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   526
    val (nice_problem, pool) = make_nice problem
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   527
  in
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   528
    (nice_problem, Option.map snd pool |> the_default Symtab.empty, [], Symtab.empty, sko_eq_info)
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   529
  end
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   530
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   531
fun termify_line ctxt (name, role, u, rule, deps) =
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   532
  let
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   533
    val thy = Proof_Context.theory_of ctxt
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   534
    val t = u |> formula_to_trm thy |> remove_waldmeister_app
57699
a6cf197c1f1e also try 'metis' with 'full_types'
blanchet
parents: 57635
diff changeset
   535
      |> singleton (infer_formulas_types ctxt)
57267
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   536
      |> HOLogic.mk_Trueprop
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   537
  in
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   538
    (name, role, t, rule, deps)
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   539
  end
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   540
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   541
fun termify_waldmeister_proof ctxt pool =
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   542
  nasty_atp_proof pool
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   543
  #> map (termify_line ctxt)
8b87114357bd integrated new Waldmeister code with 'sledgehammer' command
blanchet
parents: 57262
diff changeset
   544
  #> repair_waldmeister_endgame
57215
6fc0e3d4e1e5 moved new highly experimental Waldmeister-specific code (authored by Albert Steckermeier) into Isabelle
blanchet
parents:
diff changeset
   545
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   546
fun get_skolem_info info names = case map (lookup info) names |> List.find is_some of
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   547
  SOME x => x |
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   548
  NONE => NONE
58404
4be5ab4452f4 Updated fix_name function
steckerm
parents: 58403
diff changeset
   549
59469
wenzelm
parents: 58670
diff changeset
   550
fun fix_name name =
58408
bd5e49fca1fd Removed double space
steckerm
parents: 58406
diff changeset
   551
  if String.isPrefix fact_prefix name andalso String.isSuffix "_J" name then
59469
wenzelm
parents: 58670
diff changeset
   552
    String.extract(name, size fact_prefix + 2,NONE) |> unascii_of |>
58406
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   553
    (fn x => fact_prefix ^ "0_" ^ x)
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   554
  else
539cc471186f Minor fixes in ATP_Waldmeister
steckerm
parents: 58405
diff changeset
   555
    name
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   556
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   557
fun skolemization_steps info
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   558
  (proof_step as ((waldmeister_name, isabelle_names), _, trm, rule, _)) =
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   559
  case get_skolem_info info (map fix_name isabelle_names) of
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   560
    NONE => [proof_step] |
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   561
    SOME (_, ([], _)) => [proof_step] |
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   562
    SOME (_, (step :: steps,_)) =>
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   563
      let
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   564
        val raw_trm = dest_Trueprop trm
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   565
        val is_narrowing = raw_trm = @{term "True = False"} orelse raw_trm = @{term "False = True"}
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   566
        val is_conjecture = String.isPrefix "1.0.0.0" waldmeister_name andalso not is_narrowing
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   567
      in
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   568
        if is_narrowing then
58200
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   569
          [proof_step]
d95055489fce Added translation for lambda expressions in terms.
steckerm
parents: 58142
diff changeset
   570
        else
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   571
          let
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   572
            fun mk_steps _ [] = []
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   573
              | mk_steps i (x :: xs) = (((waldmeister_name ^ "_" ^  Int.toString i),[]),
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   574
                Plain, mk_Trueprop ((is_conjecture ? mk_not) x), waldmeister_skolemize_rule,
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   575
                [(waldmeister_name ^ "_" ^  Int.toString (i-1),
59469
wenzelm
parents: 58670
diff changeset
   576
                    if i = 1 then isabelle_names else [])])
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   577
                :: mk_steps (i+1) xs
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   578
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   579
            val first_step = ((waldmeister_name ^ "_0", isabelle_names), Unknown,
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   580
                mk_Trueprop ((is_conjecture ? mk_not) step), rule, [])
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   581
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   582
            val sub_steps =  mk_steps 1 steps
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   583
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   584
            val skolem_steps = first_step :: sub_steps
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   585
            val num_of_steps = length skolem_steps
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   586
          in
59469
wenzelm
parents: 58670
diff changeset
   587
            (skolem_steps @
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   588
            [((waldmeister_name, []), Unknown, trm, waldmeister_skolemize_rule,
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   589
            [(waldmeister_name ^ "_" ^ Int.toString (num_of_steps - 1),
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   590
                if num_of_steps = 1 then isabelle_names else [])])])
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   591
          end
58142
d6a2e3567f95 Some work on the new waldmeister integration
steckerm
parents: 57699
diff changeset
   592
      end
59469
wenzelm
parents: 58670
diff changeset
   593
58403
ede6ca6a54ee Added support for partial function application
steckerm
parents: 58402
diff changeset
   594
fun introduce_waldmeister_skolems info proof_steps = proof_steps
58411
wenzelm
parents: 58408
diff changeset
   595
      |> maps (skolemization_steps info)
58402
623645fdb047 Improved equality handling in skolemization
steckerm
parents: 58401
diff changeset
   596
end;