src/HOL/Tools/Sledgehammer/sledgehammer_hol_clause.ML
author blanchet
Fri Jun 11 17:10:23 2010 +0200 (2010-06-11 ago)
changeset 37399 34f080a12063
parent 36966 adc11fb3f3aa
child 37410 2bf7e6136047
permissions -rw-r--r--
proper polymorphic Skolemization of uncached facts + synchronization of caching and relevance filter
blanchet@36375
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_hol_clause.ML
wenzelm@33311
     2
    Author:     Jia Meng, NICTA
blanchet@36375
     3
    Author:     Jasmin Blanchette, TU Muenchen
mengj@17998
     4
wenzelm@24311
     5
FOL clauses translated from HOL formulae.
mengj@17998
     6
*)
mengj@17998
     7
blanchet@35826
     8
signature SLEDGEHAMMER_HOL_CLAUSE =
wenzelm@24311
     9
sig
blanchet@36170
    10
  type name = Sledgehammer_FOL_Clause.name
blanchet@36393
    11
  type name_pool = Sledgehammer_FOL_Clause.name_pool
blanchet@35865
    12
  type kind = Sledgehammer_FOL_Clause.kind
blanchet@35865
    13
  type fol_type = Sledgehammer_FOL_Clause.fol_type
blanchet@35865
    14
  type classrel_clause = Sledgehammer_FOL_Clause.classrel_clause
blanchet@35865
    15
  type arity_clause = Sledgehammer_FOL_Clause.arity_clause
wenzelm@24311
    16
  type axiom_name = string
wenzelm@24311
    17
  type polarity = bool
blanchet@35865
    18
  type hol_clause_id = int
blanchet@35865
    19
wenzelm@24311
    20
  datatype combterm =
blanchet@36170
    21
    CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    22
    CombVar of name * fol_type |
blanchet@35865
    23
    CombApp of combterm * combterm
wenzelm@24311
    24
  datatype literal = Literal of polarity * combterm
blanchet@35865
    25
  datatype hol_clause =
blanchet@35865
    26
    HOLClause of {clause_id: hol_clause_id, axiom_name: axiom_name, th: thm,
blanchet@35865
    27
                  kind: kind, literals: literal list, ctypes_sorts: typ list}
blanchet@35865
    28
blanchet@35865
    29
  val type_of_combterm : combterm -> fol_type
blanchet@35865
    30
  val strip_combterm_comb : combterm -> combterm * combterm list
blanchet@35865
    31
  val literals_of_term : theory -> term -> literal list * typ list
blanchet@37399
    32
  val kill_skolem_Eps :
blanchet@37399
    33
    int -> (string * (typ * term list * term)) list -> term
blanchet@37399
    34
    -> (string * (typ * term list * term)) list * term
blanchet@35865
    35
  exception TRIVIAL
blanchet@35865
    36
  val make_conjecture_clauses : bool -> theory -> thm list -> hol_clause list
blanchet@35865
    37
  val make_axiom_clauses : bool -> theory ->
blanchet@35865
    38
       (thm * (axiom_name * hol_clause_id)) list -> (axiom_name * hol_clause) list
blanchet@36909
    39
  val type_wrapper_name : string
blanchet@37399
    40
  val get_helper_clauses :
blanchet@37399
    41
    bool -> theory -> bool -> hol_clause list
blanchet@37399
    42
      -> (thm * (axiom_name * hol_clause_id)) list -> hol_clause list
blanchet@36235
    43
  val write_tptp_file : bool -> bool -> bool -> Path.T ->
blanchet@35865
    44
    hol_clause list * hol_clause list * hol_clause list * hol_clause list *
blanchet@36402
    45
    classrel_clause list * arity_clause list -> name_pool option * int
blanchet@36235
    46
  val write_dfg_file : bool -> bool -> Path.T ->
blanchet@35865
    47
    hol_clause list * hol_clause list * hol_clause list * hol_clause list *
blanchet@36402
    48
    classrel_clause list * arity_clause list -> name_pool option * int
wenzelm@24311
    49
end
mengj@17998
    50
blanchet@35826
    51
structure Sledgehammer_HOL_Clause : SLEDGEHAMMER_HOL_CLAUSE =
mengj@17998
    52
struct
mengj@17998
    53
blanchet@36167
    54
open Sledgehammer_Util
blanchet@35865
    55
open Sledgehammer_FOL_Clause
blanchet@35865
    56
open Sledgehammer_Fact_Preprocessor
paulson@22825
    57
wenzelm@33035
    58
fun min_arity_of const_min_arity c = the_default 0 (Symtab.lookup const_min_arity c);
paulson@22064
    59
paulson@22825
    60
(*True if the constant ever appears outside of the top-level position in literals.
paulson@22825
    61
  If false, the constant always receives all of its arguments and is used as a predicate.*)
blanchet@36235
    62
fun needs_hBOOL explicit_apply const_needs_hBOOL c =
blanchet@36235
    63
  explicit_apply orelse
wenzelm@33035
    64
    the_default false (Symtab.lookup const_needs_hBOOL c);
paulson@22064
    65
mengj@19198
    66
mengj@17998
    67
(******************************************************)
mengj@17998
    68
(* data types for typed combinator expressions        *)
mengj@17998
    69
(******************************************************)
mengj@17998
    70
mengj@17998
    71
type axiom_name = string;
mengj@17998
    72
type polarity = bool;
blanchet@35865
    73
type hol_clause_id = int;
mengj@17998
    74
blanchet@35865
    75
datatype combterm =
blanchet@36170
    76
  CombConst of name * fol_type * fol_type list (* Const and Free *) |
blanchet@36170
    77
  CombVar of name * fol_type |
blanchet@35865
    78
  CombApp of combterm * combterm
wenzelm@24311
    79
mengj@17998
    80
datatype literal = Literal of polarity * combterm;
mengj@17998
    81
blanchet@35865
    82
datatype hol_clause =
blanchet@35865
    83
  HOLClause of {clause_id: hol_clause_id, axiom_name: axiom_name, th: thm,
blanchet@35865
    84
                kind: kind, literals: literal list, ctypes_sorts: typ list};
mengj@17998
    85
mengj@17998
    86
mengj@17998
    87
(*********************************************************************)
mengj@17998
    88
(* convert a clause with type Term.term to a clause with type clause *)
mengj@17998
    89
(*********************************************************************)
mengj@17998
    90
blanchet@36170
    91
fun isFalse (Literal (pol, CombConst ((c, _), _, _))) =
blanchet@35865
    92
      (pol andalso c = "c_False") orelse (not pol andalso c = "c_True")
mengj@17998
    93
  | isFalse _ = false;
mengj@17998
    94
blanchet@36170
    95
fun isTrue (Literal (pol, CombConst ((c, _), _, _))) =
mengj@17998
    96
      (pol andalso c = "c_True") orelse
mengj@17998
    97
      (not pol andalso c = "c_False")
mengj@17998
    98
  | isTrue _ = false;
wenzelm@24311
    99
blanchet@35865
   100
fun isTaut (HOLClause {literals,...}) = exists isTrue literals;
mengj@17998
   101
immler@30151
   102
fun type_of dfg (Type (a, Ts)) =
blanchet@36169
   103
    let val (folTypes,ts) = types_of dfg Ts in
blanchet@36169
   104
      (TyConstr (`(make_fixed_type_const dfg) a, folTypes), ts)
blanchet@36169
   105
    end
blanchet@36169
   106
  | type_of _ (tp as TFree (a, _)) = (TyFree (`make_fixed_type_var a), [tp])
blanchet@36169
   107
  | type_of _ (tp as TVar (x, _)) =
blanchet@36169
   108
    (TyVar (make_schematic_type_var x, string_of_indexname x), [tp])
immler@30151
   109
and types_of dfg Ts =
immler@30151
   110
      let val (folTyps,ts) = ListPair.unzip (map (type_of dfg) Ts)
blanchet@35865
   111
      in  (folTyps, union_all ts)  end;
mengj@17998
   112
mengj@17998
   113
(* same as above, but no gathering of sort information *)
immler@30151
   114
fun simp_type_of dfg (Type (a, Ts)) =
blanchet@36169
   115
      TyConstr (`(make_fixed_type_const dfg) a, map (simp_type_of dfg) Ts)
blanchet@36169
   116
  | simp_type_of _ (TFree (a, _)) = TyFree (`make_fixed_type_var a)
blanchet@36169
   117
  | simp_type_of _ (TVar (x, _)) =
blanchet@36169
   118
    TyVar (make_schematic_type_var x, string_of_indexname x);
mengj@18440
   119
mengj@18356
   120
mengj@17998
   121
(* convert a Term.term (with combinators) into a combterm, also accummulate sort info *)
blanchet@37399
   122
fun combterm_of dfg thy (Const (c, T)) =
blanchet@37399
   123
      let
blanchet@37399
   124
        val (tp, ts) = type_of dfg T
blanchet@37399
   125
        val tvar_list =
blanchet@37399
   126
          (if String.isPrefix skolem_Eps_pseudo_theory c then
blanchet@37399
   127
             [] |> Term.add_tvarsT T |> map TVar
blanchet@37399
   128
           else
blanchet@37399
   129
             (c, T) |> Sign.const_typargs thy)
blanchet@37399
   130
          |> map (simp_type_of dfg)
blanchet@37399
   131
        val c' = CombConst (`(make_fixed_const dfg) c, tp, tvar_list)
paulson@22078
   132
      in  (c',ts)  end
blanchet@37399
   133
  | combterm_of dfg _ (Free(v, T)) =
blanchet@37399
   134
      let val (tp,ts) = type_of dfg T
blanchet@36170
   135
          val v' = CombConst (`make_fixed_var v, tp, [])
paulson@22078
   136
      in  (v',ts)  end
blanchet@37399
   137
  | combterm_of dfg _ (Var(v, T)) =
blanchet@37399
   138
      let val (tp,ts) = type_of dfg T
blanchet@36170
   139
          val v' = CombVar ((make_schematic_var v, string_of_indexname v), tp)
paulson@22078
   140
      in  (v',ts)  end
immler@30151
   141
  | combterm_of dfg thy (P $ Q) =
immler@30151
   142
      let val (P',tsP) = combterm_of dfg thy P
immler@30151
   143
          val (Q',tsQ) = combterm_of dfg thy Q
haftmann@33042
   144
      in  (CombApp(P',Q'), union (op =) tsP tsQ)  end
blanchet@37399
   145
  | combterm_of _ _ (t as Abs _) = raise CLAUSE ("HOL clause", t);
mengj@17998
   146
blanchet@35865
   147
fun predicate_of dfg thy ((@{const Not} $ P), polarity) = predicate_of dfg thy (P, not polarity)
immler@30151
   148
  | predicate_of dfg thy (t,polarity) = (combterm_of dfg thy (Envir.eta_contract t), polarity);
mengj@17998
   149
blanchet@35865
   150
fun literals_of_term1 dfg thy args (@{const Trueprop} $ P) = literals_of_term1 dfg thy args P
blanchet@35865
   151
  | literals_of_term1 dfg thy args (@{const "op |"} $ P $ Q) =
immler@30151
   152
      literals_of_term1 dfg thy (literals_of_term1 dfg thy args P) Q
immler@30151
   153
  | literals_of_term1 dfg thy (lits,ts) P =
immler@30151
   154
      let val ((pred,ts'),pol) = predicate_of dfg thy (P,true)
paulson@21509
   155
      in
haftmann@33042
   156
          (Literal(pol,pred)::lits, union (op =) ts ts')
paulson@21509
   157
      end;
mengj@17998
   158
immler@30151
   159
fun literals_of_term_dfg dfg thy P = literals_of_term1 dfg thy ([],[]) P;
immler@30151
   160
val literals_of_term = literals_of_term_dfg false;
mengj@17998
   161
blanchet@37399
   162
fun skolem_name i j num_T_args =
blanchet@37399
   163
  skolem_prefix ^ (space_implode "_" (map Int.toString [i, j, num_T_args])) ^
blanchet@37399
   164
  skolem_infix ^ "g"
blanchet@37399
   165
blanchet@37399
   166
val flip_type =
blanchet@37399
   167
  map_atyps (fn TFree (s, S) => TVar ((s, 0), S)
blanchet@37399
   168
              | TVar ((s, 0), S) => TFree (s, S)
blanchet@37399
   169
              | T as TVar (_, S) => raise TYPE ("nonzero TVar", [T], [])
blanchet@37399
   170
              | T => T)
blanchet@37399
   171
val flip_term =
blanchet@37399
   172
  map_aterms (fn Free (s, T) => Var ((s, 0), T)
blanchet@37399
   173
               | Var ((s, 0), T) => Free (s, T)
blanchet@37399
   174
               | t as Var (_, S) => raise TERM ("nonzero Var", [t])
blanchet@37399
   175
               | t => t)
blanchet@37399
   176
  #> map_types flip_type
blanchet@37399
   177
blanchet@37399
   178
fun flipped_skolem_type_and_args bound_T body =
blanchet@37399
   179
  skolem_type_and_args (flip_type bound_T) (flip_term body)
blanchet@37399
   180
  |>> flip_type ||> map flip_term
blanchet@37399
   181
blanchet@37399
   182
fun triple_aconv ((T1, ts1, t1), (T2, ts2, t2)) =
blanchet@37399
   183
  T1 = T2 andalso length ts1 = length ts2 andalso
blanchet@37399
   184
  forall (op aconv) (ts1 ~~ ts2) andalso t1 aconv t2
blanchet@37399
   185
blanchet@37399
   186
fun kill_skolem_Eps i skolem_somes t =
blanchet@37399
   187
  if exists_Const (curry (op =) @{const_name skolem_Eps} o fst) t then
blanchet@37399
   188
    let
blanchet@37399
   189
      fun aux skolem_somes
blanchet@37399
   190
              (t as (Const (@{const_name skolem_Eps}, eps_T) $ t2)) =
blanchet@37399
   191
          let
blanchet@37399
   192
            val bound_T = range_type eps_T
blanchet@37399
   193
            val (T, args) = flipped_skolem_type_and_args bound_T t2
blanchet@37399
   194
            val (skolem_somes, s) =
blanchet@37399
   195
              if i = ~1 then
blanchet@37399
   196
                (skolem_somes, @{const_name undefined})
blanchet@37399
   197
              else case AList.find triple_aconv skolem_somes (T, args, t) of
blanchet@37399
   198
                s :: _ => (skolem_somes, s)
blanchet@37399
   199
              | _ =>
blanchet@37399
   200
                let
blanchet@37399
   201
                  val s = skolem_Eps_pseudo_theory ^ "." ^
blanchet@37399
   202
                          skolem_name i (length skolem_somes)
blanchet@37399
   203
                                        (length (Term.add_tvarsT T []))
blanchet@37399
   204
                in ((s, (T, args, t)) :: skolem_somes, s) end
blanchet@37399
   205
          in (skolem_somes, list_comb (Const (s, T), args)) end
blanchet@37399
   206
        | aux skolem_somes (t1 $ t2) =
blanchet@37399
   207
          let
blanchet@37399
   208
            val (skolem_somes, t1) = aux skolem_somes t1
blanchet@37399
   209
            val (skolem_somes, t2) = aux skolem_somes t2
blanchet@37399
   210
          in (skolem_somes, t1 $ t2) end
blanchet@37399
   211
        | aux skolem_somes (Abs (s, T, t')) =
blanchet@37399
   212
          let val (skolem_somes, t') = aux skolem_somes t' in
blanchet@37399
   213
            (skolem_somes, Abs (s, T, t'))
blanchet@37399
   214
          end
blanchet@37399
   215
        | aux skolem_somes t = (skolem_somes, t)
blanchet@37399
   216
    in aux skolem_somes t end
blanchet@37399
   217
  else
blanchet@37399
   218
    (skolem_somes, t)
blanchet@37399
   219
blanchet@35865
   220
(* Trivial problem, which resolution cannot handle (empty clause) *)
blanchet@35865
   221
exception TRIVIAL;
wenzelm@28835
   222
mengj@17998
   223
(* making axiom and conjecture clauses *)
blanchet@37399
   224
fun make_clause dfg thy (clause_id, axiom_name, kind, th) skolem_somes =
blanchet@37399
   225
  let
blanchet@37399
   226
    val (skolem_somes, t) =
blanchet@37399
   227
      th |> prop_of |> kill_skolem_Eps clause_id skolem_somes
blanchet@37399
   228
    val (lits, ctypes_sorts) = literals_of_term_dfg dfg thy t
blanchet@37399
   229
  in
blanchet@37399
   230
    if forall isFalse lits then
blanchet@37399
   231
      raise TRIVIAL
blanchet@37399
   232
    else
blanchet@37399
   233
      (skolem_somes,
blanchet@37399
   234
       HOLClause {clause_id = clause_id, axiom_name = axiom_name, th = th,
blanchet@37399
   235
                  kind = kind, literals = lits, ctypes_sorts = ctypes_sorts})
blanchet@36218
   236
  end
mengj@19354
   237
blanchet@37399
   238
fun add_axiom_clause dfg thy (th, (name, id)) (skolem_somes, clss) =
blanchet@37399
   239
  let
blanchet@37399
   240
    val (skolem_somes, cls) =
blanchet@37399
   241
      make_clause dfg thy (id, name, Axiom, th) skolem_somes
blanchet@37399
   242
  in (skolem_somes, clss |> not (isTaut cls) ? cons (name, cls)) end
blanchet@37399
   243
blanchet@36218
   244
fun make_axiom_clauses dfg thy clauses =
blanchet@37399
   245
  ([], []) |> fold (add_axiom_clause dfg thy) clauses |> snd
mengj@19354
   246
blanchet@37399
   247
fun make_conjecture_clauses dfg thy =
blanchet@37399
   248
  let
blanchet@37399
   249
    fun aux _ _ [] = []
blanchet@37399
   250
      | aux n skolem_somes (th :: ths) =
blanchet@37399
   251
        let
blanchet@37399
   252
          val (skolem_somes, cls) =
blanchet@37399
   253
            make_clause dfg thy (n, "conjecture", Conjecture, th) skolem_somes
blanchet@37399
   254
        in cls :: aux (n + 1) skolem_somes ths end
blanchet@37399
   255
  in aux 0 [] end
mengj@17998
   256
mengj@17998
   257
(**********************************************************************)
mengj@17998
   258
(* convert clause into ATP specific formats:                          *)
mengj@17998
   259
(* TPTP used by Vampire and E                                         *)
mengj@19720
   260
(* DFG used by SPASS                                                  *)
mengj@17998
   261
(**********************************************************************)
mengj@17998
   262
paulson@22078
   263
(*Result of a function type; no need to check that the argument type matches.*)
blanchet@36909
   264
fun result_type (TyConstr (_, [_, tp2])) = tp2
blanchet@36909
   265
  | result_type _ = raise Fail "non-function type"
paulson@22078
   266
wenzelm@32994
   267
fun type_of_combterm (CombConst (_, tp, _)) = tp
wenzelm@32994
   268
  | type_of_combterm (CombVar (_, tp)) = tp
wenzelm@32994
   269
  | type_of_combterm (CombApp (t1, _)) = result_type (type_of_combterm t1);
mengj@17998
   270
paulson@22064
   271
(*gets the head of a combinator application, along with the list of arguments*)
blanchet@35865
   272
fun strip_combterm_comb u =
paulson@22078
   273
    let fun stripc (CombApp(t,u), ts) = stripc (t, u::ts)
paulson@22064
   274
        |   stripc  x =  x
paulson@22064
   275
    in  stripc(u,[])  end;
paulson@22064
   276
blanchet@36909
   277
val type_wrapper_name = "ti"
paulson@22851
   278
blanchet@36235
   279
fun head_needs_hBOOL explicit_apply const_needs_hBOOL
blanchet@36235
   280
                     (CombConst ((c, _), _, _)) =
blanchet@36235
   281
    needs_hBOOL explicit_apply const_needs_hBOOL c
blanchet@36235
   282
  | head_needs_hBOOL _ _ _ = true
paulson@22851
   283
blanchet@36170
   284
fun wrap_type full_types (s, ty) pool =
blanchet@36170
   285
  if full_types then
blanchet@36170
   286
    let val (s', pool) = string_of_fol_type ty pool in
blanchet@36909
   287
      (type_wrapper_name ^ paren_pack [s, s'], pool)
blanchet@36170
   288
    end
blanchet@36170
   289
  else
blanchet@36170
   290
    (s, pool)
blanchet@36170
   291
blanchet@36235
   292
fun wrap_type_if full_types explicit_apply cnh (head, s, tp) =
blanchet@36235
   293
  if head_needs_hBOOL explicit_apply cnh head then
blanchet@36235
   294
    wrap_type full_types (s, tp)
blanchet@36235
   295
  else
blanchet@36235
   296
    pair s
wenzelm@24311
   297
blanchet@35865
   298
fun apply ss = "hAPP" ^ paren_pack ss;
paulson@22851
   299
paulson@22064
   300
fun rev_apply (v, []) = v
paulson@22851
   301
  | rev_apply (v, arg::args) = apply [rev_apply (v, args), arg];
paulson@22064
   302
paulson@22064
   303
fun string_apply (v, args) = rev_apply (v, rev args);
paulson@22064
   304
blanchet@36170
   305
(* Apply an operator to the argument strings, using either the "apply" operator
blanchet@36170
   306
   or direct function application. *)
blanchet@36170
   307
fun string_of_application full_types cma (CombConst ((s, s'), _, tvars), args)
blanchet@36170
   308
                          pool =
blanchet@36170
   309
    let
blanchet@36170
   310
      val s = if s = "equal" then "c_fequal" else s
blanchet@36170
   311
      val nargs = min_arity_of cma s
blanchet@36170
   312
      val args1 = List.take (args, nargs)
blanchet@36170
   313
        handle Subscript =>
blanchet@36170
   314
               raise Fail (quote s ^ " has arity " ^ Int.toString nargs ^
blanchet@36170
   315
                           " but is applied to " ^ commas (map quote args))
blanchet@36170
   316
      val args2 = List.drop (args, nargs)
blanchet@36170
   317
      val (targs, pool) = if full_types then ([], pool)
blanchet@36170
   318
                          else pool_map string_of_fol_type tvars pool
blanchet@36170
   319
      val (s, pool) = nice_name (s, s') pool
blanchet@36170
   320
    in (string_apply (s ^ paren_pack (args1 @ targs), args2), pool) end
blanchet@36170
   321
  | string_of_application _ _ (CombVar (name, _), args) pool =
blanchet@36170
   322
    nice_name name pool |>> (fn s => string_apply (s, args))
wenzelm@24311
   323
blanchet@36235
   324
fun string_of_combterm (params as (full_types, explicit_apply, cma, cnh)) t
blanchet@36235
   325
                       pool =
blanchet@36170
   326
  let
blanchet@36170
   327
    val (head, args) = strip_combterm_comb t
blanchet@36170
   328
    val (ss, pool) = pool_map (string_of_combterm params) args pool
blanchet@36170
   329
    val (s, pool) = string_of_application full_types cma (head, ss) pool
blanchet@36235
   330
  in
blanchet@36235
   331
    wrap_type_if full_types explicit_apply cnh (head, s, type_of_combterm t)
blanchet@36235
   332
                 pool
blanchet@36235
   333
  end
mengj@18356
   334
paulson@22064
   335
(*Boolean-valued terms are here converted to literals.*)
blanchet@36170
   336
fun boolify params c =
blanchet@36170
   337
  string_of_combterm params c #>> prefix "hBOOL" o paren_pack o single
paulson@22064
   338
blanchet@36235
   339
fun string_of_predicate (params as (_, explicit_apply, _, cnh)) t =
paulson@22064
   340
  case t of
blanchet@36170
   341
    (CombApp (CombApp (CombConst (("equal", _), _, _), t1), t2)) =>
blanchet@36170
   342
    (* DFG only: new TPTP prefers infix equality *)
blanchet@36170
   343
    pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   344
    #>> prefix "equal" o paren_pack
blanchet@36170
   345
  | _ =>
blanchet@36170
   346
    case #1 (strip_combterm_comb t) of
blanchet@36170
   347
      CombConst ((s, _), _, _) =>
blanchet@36235
   348
      (if needs_hBOOL explicit_apply cnh s then boolify else string_of_combterm)
blanchet@36235
   349
          params t
blanchet@36170
   350
    | _ => boolify params t
mengj@18356
   351
mengj@17998
   352
blanchet@36170
   353
(*** TPTP format ***)
mengj@19720
   354
blanchet@36170
   355
fun tptp_of_equality params pos (t1, t2) =
blanchet@36170
   356
  pool_map (string_of_combterm params) [t1, t2]
blanchet@36170
   357
  #>> space_implode (if pos then " = " else " != ")
wenzelm@24311
   358
blanchet@36170
   359
fun tptp_literal params
blanchet@36170
   360
        (Literal (pos, CombApp (CombApp (CombConst (("equal", _), _, _), t1),
blanchet@36170
   361
                                         t2))) =
blanchet@36170
   362
    tptp_of_equality params pos (t1, t2)
blanchet@36170
   363
  | tptp_literal params (Literal (pos, pred)) =
blanchet@36170
   364
    string_of_predicate params pred #>> tptp_sign pos
blanchet@36170
   365
 
blanchet@36170
   366
(* Given a clause, returns its literals paired with a list of literals
blanchet@36170
   367
   concerning TFrees; the latter should only occur in conjecture clauses. *)
blanchet@36556
   368
fun tptp_type_literals params pos (HOLClause {literals, ctypes_sorts, ...})
blanchet@36556
   369
                       pool =
blanchet@36556
   370
  let
blanchet@36556
   371
    val (lits, pool) = pool_map (tptp_literal params) literals pool
blanchet@36556
   372
    val (tylits, pool) = pool_map (tptp_of_type_literal pos)
blanchet@36966
   373
                                  (type_literals_for_types ctypes_sorts) pool
blanchet@36556
   374
  in ((lits, tylits), pool) end
wenzelm@24311
   375
blanchet@36170
   376
fun tptp_clause params (cls as HOLClause {axiom_name, clause_id, kind, ...})
blanchet@36170
   377
                pool =
blanchet@36170
   378
let
blanchet@36170
   379
    val ((lits, tylits), pool) =
blanchet@36170
   380
      tptp_type_literals params (kind = Conjecture) cls pool
paulson@24937
   381
  in
blanchet@36170
   382
    ((gen_tptp_cls (clause_id, axiom_name, kind, lits, tylits), tylits), pool)
blanchet@36170
   383
  end
mengj@17998
   384
mengj@17998
   385
blanchet@36170
   386
(*** DFG format ***)
paulson@21561
   387
blanchet@36170
   388
fun dfg_literal params (Literal (pos, pred)) =
blanchet@36170
   389
  string_of_predicate params pred #>> dfg_sign pos
mengj@19720
   390
blanchet@36170
   391
fun dfg_type_literals params pos (HOLClause {literals, ctypes_sorts, ...}) =
blanchet@36170
   392
  pool_map (dfg_literal params) literals
blanchet@36966
   393
  #>> rpair (map (dfg_of_type_literal pos)
blanchet@36966
   394
                 (type_literals_for_types ctypes_sorts))
mengj@19720
   395
blanchet@36170
   396
fun get_uvars (CombConst _) vars pool = (vars, pool)
blanchet@36170
   397
  | get_uvars (CombVar (name, _)) vars pool =
blanchet@36170
   398
    nice_name name pool |>> (fn var => var :: vars)
blanchet@36170
   399
  | get_uvars (CombApp (P, Q)) vars pool =
blanchet@36170
   400
    let val (vars, pool) = get_uvars P vars pool in get_uvars Q vars pool end
mengj@19720
   401
blanchet@36170
   402
fun get_uvars_l (Literal (_, c)) = get_uvars c [];
mengj@19720
   403
blanchet@36170
   404
fun dfg_vars (HOLClause {literals, ...}) =
blanchet@36170
   405
  pool_map get_uvars_l literals #>> union_all
wenzelm@24311
   406
blanchet@36170
   407
fun dfg_clause params (cls as HOLClause {axiom_name, clause_id, kind,
blanchet@36170
   408
                                         ctypes_sorts, ...}) pool =
blanchet@36170
   409
  let
blanchet@36170
   410
    val ((lits, tylits), pool) =
blanchet@36170
   411
      dfg_type_literals params (kind = Conjecture) cls pool
blanchet@36170
   412
    val (vars, pool) = dfg_vars cls pool
blanchet@36170
   413
    val tvars = get_tvar_strs ctypes_sorts
paulson@24937
   414
  in
blanchet@36170
   415
    ((gen_dfg_cls (clause_id, axiom_name, kind, lits, tylits, tvars @ vars),
blanchet@36170
   416
      tylits), pool)
blanchet@36170
   417
  end
paulson@24937
   418
mengj@19720
   419
paulson@22064
   420
(** For DFG format: accumulate function and predicate declarations **)
mengj@19720
   421
blanchet@36218
   422
fun add_types tvars = fold add_fol_type_funcs tvars
mengj@19720
   423
blanchet@36235
   424
fun add_decls (full_types, explicit_apply, cma, cnh)
blanchet@36565
   425
              (CombConst ((c, _), ctp, tvars)) (funcs, preds) =
blanchet@36565
   426
      (if c = "equal" then
blanchet@36565
   427
         (add_types tvars funcs, preds)
blanchet@36565
   428
       else
blanchet@36565
   429
         let val arity = min_arity_of cma c
blanchet@36565
   430
             val ntys = if not full_types then length tvars else 0
blanchet@36565
   431
             val addit = Symtab.update(c, arity + ntys)
blanchet@36565
   432
         in
blanchet@36565
   433
             if needs_hBOOL explicit_apply cnh c then
blanchet@36565
   434
               (add_types tvars (addit funcs), preds)
blanchet@36565
   435
             else
blanchet@36565
   436
               (add_types tvars funcs, addit preds)
blanchet@36565
   437
         end) |>> full_types ? add_fol_type_funcs ctp
blanchet@36218
   438
  | add_decls _ (CombVar (_, ctp)) (funcs, preds) =
blanchet@36218
   439
      (add_fol_type_funcs ctp funcs, preds)
blanchet@36218
   440
  | add_decls params (CombApp (P, Q)) decls =
blanchet@36218
   441
      decls |> add_decls params P |> add_decls params Q
mengj@19720
   442
blanchet@36218
   443
fun add_literal_decls params (Literal (_, c)) = add_decls params c
mengj@19720
   444
blanchet@36218
   445
fun add_clause_decls params (HOLClause {literals, ...}) decls =
blanchet@36218
   446
    fold (add_literal_decls params) literals decls
wenzelm@27187
   447
    handle Symtab.DUP a => error ("function " ^ a ^ " has multiple arities")
mengj@19720
   448
nipkow@31800
   449
fun decls_of_clauses params clauses arity_clauses =
blanchet@36237
   450
  let val functab =
blanchet@36909
   451
        init_functab |> fold Symtab.update [(type_wrapper_name, 2), ("hAPP", 2),
blanchet@36237
   452
                                            ("tc_bool", 0)]
blanchet@36218
   453
      val predtab = Symtab.empty |> Symtab.update ("hBOOL", 1)
blanchet@36218
   454
      val (functab, predtab) =
blanchet@36218
   455
        (functab, predtab) |> fold (add_clause_decls params) clauses
blanchet@36218
   456
                           |>> fold add_arity_clause_funcs arity_clauses
blanchet@36218
   457
  in (Symtab.dest functab, Symtab.dest predtab) end
mengj@19720
   458
blanchet@36218
   459
fun add_clause_preds (HOLClause {ctypes_sorts, ...}) preds =
blanchet@36218
   460
  fold add_type_sort_preds ctypes_sorts preds
wenzelm@27187
   461
  handle Symtab.DUP a => error ("predicate " ^ a ^ " has multiple arities")
paulson@21398
   462
paulson@21398
   463
(*Higher-order clauses have only the predicates hBOOL and type classes.*)
wenzelm@24311
   464
fun preds_of_clauses clauses clsrel_clauses arity_clauses =
blanchet@36218
   465
  Symtab.empty
blanchet@36218
   466
  |> fold add_clause_preds clauses
blanchet@36218
   467
  |> fold add_arity_clause_preds arity_clauses
blanchet@36218
   468
  |> fold add_classrel_clause_preds clsrel_clauses
blanchet@36218
   469
  |> Symtab.dest
mengj@18440
   470
mengj@18440
   471
(**********************************************************************)
mengj@19198
   472
(* write clauses to files                                             *)
mengj@19198
   473
(**********************************************************************)
mengj@19198
   474
paulson@21573
   475
val init_counters =
blanchet@35865
   476
  Symtab.make [("c_COMBI", 0), ("c_COMBK", 0), ("c_COMBB", 0), ("c_COMBC", 0),
blanchet@35865
   477
               ("c_COMBS", 0)];
wenzelm@24311
   478
blanchet@36218
   479
fun count_combterm (CombConst ((c, _), _, _)) =
blanchet@36218
   480
    Symtab.map_entry c (Integer.add 1)
blanchet@36218
   481
  | count_combterm (CombVar _) = I
blanchet@36218
   482
  | count_combterm (CombApp (t1, t2)) = count_combterm t1 #> count_combterm t2
paulson@21573
   483
blanchet@36218
   484
fun count_literal (Literal (_, t)) = count_combterm t
paulson@21573
   485
blanchet@36218
   486
fun count_clause (HOLClause {literals, ...}) = fold count_literal literals
paulson@21573
   487
blanchet@36228
   488
fun cnf_helper_thms thy = cnf_rules_pairs thy o map (`Thm.get_name_hint)
mengj@20644
   489
blanchet@37399
   490
fun get_helper_clauses dfg thy isFO conjectures axcls =
blanchet@35865
   491
  if isFO then
blanchet@35865
   492
    []
paulson@23386
   493
  else
immler@31752
   494
    let
blanchet@37399
   495
        val axclauses = map snd (make_axiom_clauses dfg thy axcls)
blanchet@37399
   496
        val ct = init_counters
blanchet@37399
   497
                 |> fold (fold count_clause) [conjectures, axclauses]
wenzelm@33035
   498
        fun needed c = the (Symtab.lookup ct c) > 0
wenzelm@24311
   499
        val IK = if needed "c_COMBI" orelse needed "c_COMBK"
blanchet@35865
   500
                 then cnf_helper_thms thy [@{thm COMBI_def}, @{thm COMBK_def}]
wenzelm@24311
   501
                 else []
wenzelm@24311
   502
        val BC = if needed "c_COMBB" orelse needed "c_COMBC"
blanchet@35865
   503
                 then cnf_helper_thms thy [@{thm COMBB_def}, @{thm COMBC_def}]
paulson@21135
   504
                 else []
blanchet@35865
   505
        val S = if needed "c_COMBS" then cnf_helper_thms thy [@{thm COMBS_def}]
wenzelm@24311
   506
                else []
blanchet@35865
   507
        val other = cnf_helper_thms thy [@{thm fequal_imp_equal},
blanchet@35865
   508
                                         @{thm equal_imp_fequal}]
blanchet@37399
   509
    in map snd (make_axiom_clauses dfg thy (other @ IK @ BC @ S)) end
mengj@20791
   510
paulson@22064
   511
(*Find the minimal arity of each function mentioned in the term. Also, note which uses
paulson@22064
   512
  are not at top level, to see if hBOOL is needed.*)
immler@30150
   513
fun count_constants_term toplev t (const_min_arity, const_needs_hBOOL) =
blanchet@35865
   514
  let val (head, args) = strip_combterm_comb t
paulson@22064
   515
      val n = length args
blanchet@36233
   516
      val (const_min_arity, const_needs_hBOOL) =
blanchet@36233
   517
        (const_min_arity, const_needs_hBOOL)
blanchet@36233
   518
        |> fold (count_constants_term false) args
paulson@22064
   519
  in
paulson@22064
   520
      case head of
blanchet@36170
   521
          CombConst ((a, _),_,_) => (*predicate or function version of "equal"?*)
wenzelm@24311
   522
            let val a = if a="equal" andalso not toplev then "c_fequal" else a
wenzelm@24311
   523
            in
blanchet@36233
   524
              (const_min_arity |> Symtab.map_default (a, n) (Integer.min n),
blanchet@36233
   525
               const_needs_hBOOL |> not toplev ? Symtab.update (a, true))
wenzelm@24311
   526
            end
wenzelm@32994
   527
        | _ => (const_min_arity, const_needs_hBOOL)
paulson@22064
   528
  end;
paulson@22064
   529
paulson@22064
   530
(*A literal is a top-level term*)
immler@30150
   531
fun count_constants_lit (Literal (_,t)) (const_min_arity, const_needs_hBOOL) =
immler@30150
   532
  count_constants_term true t (const_min_arity, const_needs_hBOOL);
paulson@22064
   533
blanchet@35865
   534
fun count_constants_clause (HOLClause {literals, ...})
blanchet@35865
   535
                           (const_min_arity, const_needs_hBOOL) =
immler@30150
   536
  fold count_constants_lit literals (const_min_arity, const_needs_hBOOL);
paulson@22064
   537
blanchet@36235
   538
fun display_arity explicit_apply const_needs_hBOOL (c,n) =
blanchet@35865
   539
  trace_msg (fn () => "Constant: " ^ c ^
blanchet@35826
   540
                " arity:\t" ^ Int.toString n ^
blanchet@36235
   541
                (if needs_hBOOL explicit_apply const_needs_hBOOL c then
blanchet@36235
   542
                   " needs hBOOL"
blanchet@36235
   543
                 else
blanchet@36235
   544
                   ""))
paulson@22064
   545
blanchet@36235
   546
fun count_constants explicit_apply
blanchet@36235
   547
                    (conjectures, _, extra_clauses, helper_clauses, _, _) =
blanchet@36235
   548
  if not explicit_apply then
immler@30150
   549
     let val (const_min_arity, const_needs_hBOOL) =
immler@30150
   550
          fold count_constants_clause conjectures (Symtab.empty, Symtab.empty)
immler@31865
   551
       |> fold count_constants_clause extra_clauses
immler@30149
   552
       |> fold count_constants_clause helper_clauses
blanchet@36481
   553
     val _ = app (display_arity explicit_apply const_needs_hBOOL)
blanchet@36481
   554
                 (Symtab.dest (const_min_arity))
immler@30150
   555
     in (const_min_arity, const_needs_hBOOL) end
immler@30150
   556
  else (Symtab.empty, Symtab.empty);
paulson@22064
   557
blanchet@36218
   558
fun header () =
blanchet@36218
   559
  "% This file was generated by Isabelle (most likely Sledgehammer)\n" ^
blanchet@36218
   560
  "% " ^ timestamp () ^ "\n"
blanchet@36218
   561
blanchet@35865
   562
(* TPTP format *)
immler@31749
   563
blanchet@36235
   564
fun write_tptp_file readable_names full_types explicit_apply file clauses =
immler@31409
   565
  let
blanchet@36167
   566
    fun section _ [] = []
blanchet@36393
   567
      | section name ss =
blanchet@36393
   568
        "\n% " ^ name ^ plural_s (length ss) ^ " (" ^ Int.toString (length ss) ^
blanchet@36393
   569
        ")\n" :: ss
blanchet@36222
   570
    val pool = empty_name_pool readable_names
immler@31865
   571
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   572
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   573
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   574
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   575
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   576
      pool_map (tptp_clause params) conjectures pool |>> ListPair.unzip
blanchet@36218
   577
    val tfree_clss = map tptp_tfree_clause (fold (union (op =)) tfree_litss [])
blanchet@36170
   578
    val (ax_clss, pool) = pool_map (apfst fst oo tptp_clause params) axclauses
blanchet@36170
   579
                                   pool
blanchet@36170
   580
    val classrel_clss = map tptp_classrel_clause classrel_clauses
blanchet@36170
   581
    val arity_clss = map tptp_arity_clause arity_clauses
blanchet@36170
   582
    val (helper_clss, pool) = pool_map (apfst fst oo tptp_clause params)
blanchet@36170
   583
                                       helper_clauses pool
blanchet@36402
   584
    val conjecture_offset =
blanchet@36402
   585
      length ax_clss + length classrel_clss + length arity_clss
blanchet@36402
   586
      + length helper_clss
blanchet@36393
   587
    val _ =
blanchet@36393
   588
      File.write_list file
blanchet@36393
   589
          (header () ::
blanchet@36393
   590
           section "Relevant fact" ax_clss @
blanchet@36402
   591
           section "Class relationship" classrel_clss @
blanchet@36402
   592
           section "Arity declaration" arity_clss @
blanchet@36393
   593
           section "Helper fact" helper_clss @
blanchet@36393
   594
           section "Conjecture" conjecture_clss @
blanchet@36402
   595
           section "Type variable" tfree_clss)
blanchet@36402
   596
  in (pool, conjecture_offset) end
mengj@19720
   597
blanchet@35865
   598
(* DFG format *)
mengj@19720
   599
blanchet@36966
   600
fun dfg_tfree_predicate s = (first_field "(" s |> the |> fst, 1)
blanchet@36966
   601
blanchet@36235
   602
fun write_dfg_file full_types explicit_apply file clauses =
immler@31409
   603
  let
blanchet@36219
   604
    (* Some of the helper functions below are not name-pool-aware. However,
blanchet@36219
   605
       there's no point in adding name pool support if the DFG format is on its
blanchet@36219
   606
       way out anyway. *)
blanchet@36219
   607
    val pool = NONE
immler@31865
   608
    val (conjectures, axclauses, _, helper_clauses,
immler@31865
   609
      classrel_clauses, arity_clauses) = clauses
blanchet@36235
   610
    val (cma, cnh) = count_constants explicit_apply clauses
blanchet@36235
   611
    val params = (full_types, explicit_apply, cma, cnh)
blanchet@36170
   612
    val ((conjecture_clss, tfree_litss), pool) =
blanchet@36170
   613
      pool_map (dfg_clause params) conjectures pool |>> ListPair.unzip
blanchet@36966
   614
    val tfree_lits = union_all tfree_litss
blanchet@36966
   615
    val problem_name = Path.implode (Path.base file)
blanchet@36170
   616
    val (axstrs, pool) = pool_map (apfst fst oo dfg_clause params) axclauses pool
blanchet@36966
   617
    val tfree_clss = map dfg_tfree_clause tfree_lits
blanchet@36966
   618
    val tfree_preds = map dfg_tfree_predicate tfree_lits
blanchet@36170
   619
    val (helper_clauses_strs, pool) =
blanchet@36170
   620
      pool_map (apfst fst oo dfg_clause params) helper_clauses pool
blanchet@36218
   621
    val (funcs, cl_preds) = decls_of_clauses params (helper_clauses @ conjectures @ axclauses) arity_clauses
blanchet@36966
   622
    val ty_preds = preds_of_clauses axclauses classrel_clauses arity_clauses
blanchet@36966
   623
    val preds = tfree_preds @ cl_preds @ ty_preds
blanchet@36402
   624
    val conjecture_offset =
blanchet@36402
   625
      length axclauses + length classrel_clauses + length arity_clauses
blanchet@36402
   626
      + length helper_clauses
blanchet@36393
   627
    val _ =
blanchet@36393
   628
      File.write_list file
blanchet@36393
   629
          (header () ::
blanchet@36393
   630
           string_of_start problem_name ::
blanchet@36393
   631
           string_of_descrip problem_name ::
blanchet@36393
   632
           string_of_symbols (string_of_funcs funcs)
blanchet@36966
   633
                             (string_of_preds preds) ::
blanchet@36393
   634
           "list_of_clauses(axioms, cnf).\n" ::
blanchet@36393
   635
           axstrs @
blanchet@36393
   636
           map dfg_classrel_clause classrel_clauses @
blanchet@36393
   637
           map dfg_arity_clause arity_clauses @
blanchet@36393
   638
           helper_clauses_strs @
blanchet@36393
   639
           ["end_of_list.\n\nlist_of_clauses(conjectures, cnf).\n"] @
blanchet@36402
   640
           conjecture_clss @
blanchet@36393
   641
           tfree_clss @
blanchet@36393
   642
           ["end_of_list.\n\n",
blanchet@36393
   643
            "end_problem.\n"])
blanchet@36402
   644
  in (pool, conjecture_offset) end
mengj@19720
   645
wenzelm@33311
   646
end;