src/HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
author blanchet
Wed Apr 28 16:03:49 2010 +0200 (2010-04-28 ago)
changeset 36493 a3357a631b96
parent 36491 6769f8eacaac
child 36556 81dc2c20f052
permissions -rw-r--r--
reintroduced short names for HOL->FOL constants; other parts of the code rely on these
blanchet@35826
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fol_clause.ML
wenzelm@33311
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory
blanchet@36393
     3
    Author:     Jasmin Blanchette, TU Muenchen
paulson@15347
     4
wenzelm@33311
     5
Storing/printing FOL clauses and arity clauses.  Typed equality is
wenzelm@33311
     6
treated differently.
wenzelm@33311
     7
blanchet@36218
     8
FIXME: combine with sledgehammer_hol_clause!
paulson@15347
     9
*)
paulson@15347
    10
blanchet@35826
    11
signature SLEDGEHAMMER_FOL_CLAUSE =
wenzelm@24310
    12
sig
wenzelm@24310
    13
  val schematic_var_prefix: string
wenzelm@24310
    14
  val fixed_var_prefix: string
wenzelm@24310
    15
  val tvar_prefix: string
wenzelm@24310
    16
  val tfree_prefix: string
wenzelm@24310
    17
  val clause_prefix: string
wenzelm@24310
    18
  val const_prefix: string
wenzelm@24310
    19
  val tconst_prefix: string
wenzelm@24310
    20
  val class_prefix: string
wenzelm@24310
    21
  val union_all: ''a list list -> ''a list
wenzelm@24310
    22
  val const_trans_table: string Symtab.table
wenzelm@24310
    23
  val type_const_trans_table: string Symtab.table
wenzelm@24310
    24
  val ascii_of: string -> string
wenzelm@24310
    25
  val undo_ascii_of: string -> string
wenzelm@24310
    26
  val paren_pack : string list -> string
wenzelm@24310
    27
  val make_schematic_var : string * int -> string
paulson@18868
    28
  val make_fixed_var : string -> string
paulson@18868
    29
  val make_schematic_type_var : string * int -> string
wenzelm@24310
    30
  val make_fixed_type_var : string -> string
immler@30151
    31
  val make_fixed_const : bool -> string -> string
immler@30151
    32
  val make_fixed_type_const : bool -> string -> string
paulson@18868
    33
  val make_type_class : string -> string
blanchet@36169
    34
  type name = string * string
blanchet@36169
    35
  type name_pool = string Symtab.table * string Symtab.table
blanchet@36169
    36
  val empty_name_pool : bool -> name_pool option
blanchet@36169
    37
  val pool_map : ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b
blanchet@36169
    38
  val nice_name : name -> name_pool option -> string * name_pool option
wenzelm@24310
    39
  datatype kind = Axiom | Conjecture
wenzelm@24310
    40
  type axiom_name = string
wenzelm@24310
    41
  datatype fol_type =
blanchet@36169
    42
    TyVar of name |
blanchet@36169
    43
    TyFree of name |
blanchet@36169
    44
    TyConstr of name * fol_type list
blanchet@36170
    45
  val string_of_fol_type :
blanchet@36170
    46
    fol_type -> name_pool option -> string * name_pool option
wenzelm@24310
    47
  datatype type_literal = LTVar of string * string | LTFree of string * string
wenzelm@24310
    48
  exception CLAUSE of string * term
paulson@24940
    49
  val add_typs : typ list -> type_literal list
paulson@24940
    50
  val get_tvar_strs: typ list -> string list
wenzelm@24310
    51
  datatype arLit =
wenzelm@24310
    52
      TConsLit of class * string * string list
wenzelm@24310
    53
    | TVarLit of class * string
blanchet@35865
    54
  datatype arity_clause = ArityClause of
paulson@24937
    55
   {axiom_name: axiom_name, conclLit: arLit, premLits: arLit list}
blanchet@35865
    56
  datatype classrel_clause = ClassrelClause of
paulson@24937
    57
   {axiom_name: axiom_name, subclass: class, superclass: class}
blanchet@35865
    58
  val make_classrel_clauses: theory -> class list -> class list -> classrel_clause list
blanchet@35865
    59
  val make_arity_clauses_dfg: bool -> theory -> string list -> class list -> class list * arity_clause list
blanchet@35865
    60
  val make_arity_clauses: theory -> string list -> class list -> class list * arity_clause list
blanchet@36218
    61
  val add_type_sort_preds: typ -> int Symtab.table -> int Symtab.table
blanchet@36218
    62
  val add_classrel_clause_preds :
blanchet@36218
    63
    classrel_clause -> int Symtab.table -> int Symtab.table
wenzelm@24310
    64
  val class_of_arityLit: arLit -> class
blanchet@36218
    65
  val add_arity_clause_preds: arity_clause -> int Symtab.table -> int Symtab.table
blanchet@36218
    66
  val add_fol_type_funcs: fol_type -> int Symtab.table -> int Symtab.table
blanchet@36218
    67
  val add_arity_clause_funcs:
blanchet@36218
    68
    arity_clause -> int Symtab.table -> int Symtab.table
wenzelm@24310
    69
  val init_functab: int Symtab.table
mengj@19719
    70
  val dfg_sign: bool -> string -> string
paulson@24937
    71
  val dfg_of_typeLit: bool -> type_literal -> string
paulson@24937
    72
  val gen_dfg_cls: int * string * kind * string list * string list * string list -> string
wenzelm@24310
    73
  val string_of_preds: (string * Int.int) list -> string
wenzelm@24310
    74
  val string_of_funcs: (string * int) list -> string
wenzelm@24310
    75
  val string_of_symbols: string -> string -> string
mengj@19719
    76
  val string_of_start: string -> string
mengj@19719
    77
  val string_of_descrip : string -> string
wenzelm@24310
    78
  val dfg_tfree_clause : string -> string
blanchet@35865
    79
  val dfg_classrel_clause: classrel_clause -> string
blanchet@35865
    80
  val dfg_arity_clause: arity_clause -> string
wenzelm@24310
    81
  val tptp_sign: bool -> string -> string
paulson@24937
    82
  val tptp_of_typeLit : bool -> type_literal -> string
paulson@24937
    83
  val gen_tptp_cls : int * string * kind * string list * string list -> string
wenzelm@24310
    84
  val tptp_tfree_clause : string -> string
blanchet@35865
    85
  val tptp_arity_clause : arity_clause -> string
blanchet@35865
    86
  val tptp_classrel_clause : classrel_clause -> string
wenzelm@24310
    87
end
paulson@15347
    88
blanchet@35826
    89
structure Sledgehammer_FOL_Clause : SLEDGEHAMMER_FOL_CLAUSE =
paulson@15347
    90
struct
paulson@15347
    91
blanchet@36063
    92
open Sledgehammer_Util
blanchet@36063
    93
paulson@15347
    94
val schematic_var_prefix = "V_";
paulson@15347
    95
val fixed_var_prefix = "v_";
paulson@15347
    96
paulson@17230
    97
val tvar_prefix = "T_";
paulson@17230
    98
val tfree_prefix = "t_";
paulson@15347
    99
wenzelm@24310
   100
val clause_prefix = "cls_";
wenzelm@24310
   101
val arclause_prefix = "clsarity_"
paulson@17525
   102
val clrelclause_prefix = "clsrel_";
paulson@15347
   103
paulson@17230
   104
val const_prefix = "c_";
wenzelm@24310
   105
val tconst_prefix = "tc_";
wenzelm@24310
   106
val class_prefix = "class_";
paulson@15347
   107
blanchet@36218
   108
fun union_all xss = fold (union (op =)) xss []
paulson@17775
   109
blanchet@36493
   110
(* Readable names for the more common symbolic functions. Do not mess with the
blanchet@36493
   111
   last six entries of the table unless you know what you are doing. *)
paulson@15347
   112
val const_trans_table =
blanchet@35865
   113
  Symtab.make [(@{const_name "op ="}, "equal"),
blanchet@35865
   114
               (@{const_name "op &"}, "and"),
blanchet@35865
   115
               (@{const_name "op |"}, "or"),
blanchet@35865
   116
               (@{const_name "op -->"}, "implies"),
blanchet@36493
   117
               (@{const_name "op :"}, "in"),
blanchet@36493
   118
               (@{const_name fequal}, "fequal"),
blanchet@36493
   119
               (@{const_name COMBI}, "COMBI"),
blanchet@36493
   120
               (@{const_name COMBK}, "COMBK"),
blanchet@36493
   121
               (@{const_name COMBB}, "COMBB"),
blanchet@36493
   122
               (@{const_name COMBC}, "COMBC"),
blanchet@36493
   123
               (@{const_name COMBS}, "COMBS")]
paulson@15347
   124
paulson@17230
   125
val type_const_trans_table =
blanchet@36476
   126
  Symtab.make [(@{type_name "*"}, "prod"),
blanchet@36476
   127
               (@{type_name "+"}, "sum")]
paulson@15347
   128
paulson@15610
   129
(*Escaping of special characters.
paulson@15610
   130
  Alphanumeric characters are left unchanged.
paulson@15610
   131
  The character _ goes to __
paulson@15610
   132
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@24183
   133
  Other printing characters go to _nnn where nnn is the decimal ASCII code.*)
paulson@24183
   134
val A_minus_space = Char.ord #"A" - Char.ord #" ";
paulson@15610
   135
paulson@24183
   136
fun stringN_of_int 0 _ = ""
paulson@24183
   137
  | stringN_of_int k n = stringN_of_int (k-1) (n div 10) ^ Int.toString (n mod 10);
paulson@15610
   138
paulson@15347
   139
fun ascii_of_c c =
paulson@15610
   140
  if Char.isAlphaNum c then String.str c
paulson@15610
   141
  else if c = #"_" then "__"
wenzelm@24310
   142
  else if #" " <= c andalso c <= #"/"
paulson@15610
   143
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
wenzelm@24310
   144
  else if Char.isPrint c
paulson@24183
   145
       then ("_" ^ stringN_of_int 3 (Char.ord c))  (*fixed width, in case more digits follow*)
paulson@15610
   146
  else ""
paulson@15347
   147
paulson@15610
   148
val ascii_of = String.translate ascii_of_c;
paulson@15610
   149
paulson@24183
   150
(** Remove ASCII armouring from names in proof files **)
paulson@24183
   151
paulson@24183
   152
(*We don't raise error exceptions because this code can run inside the watcher.
paulson@24183
   153
  Also, the errors are "impossible" (hah!)*)
paulson@24183
   154
fun undo_ascii_aux rcs [] = String.implode(rev rcs)
paulson@24183
   155
  | undo_ascii_aux rcs [#"_"] = undo_ascii_aux (#"_"::rcs) []  (*ERROR*)
paulson@24183
   156
      (*Three types of _ escapes: __, _A to _P, _nnn*)
paulson@24183
   157
  | undo_ascii_aux rcs (#"_" :: #"_" :: cs) = undo_ascii_aux (#"_"::rcs) cs
wenzelm@24310
   158
  | undo_ascii_aux rcs (#"_" :: c :: cs) =
paulson@24183
   159
      if #"A" <= c andalso c<= #"P"  (*translation of #" " to #"/"*)
paulson@24183
   160
      then undo_ascii_aux (Char.chr(Char.ord c - A_minus_space) :: rcs) cs
wenzelm@24310
   161
      else
paulson@24183
   162
        let val digits = List.take (c::cs, 3) handle Subscript => []
wenzelm@24310
   163
        in
paulson@24183
   164
            case Int.fromString (String.implode digits) of
paulson@24183
   165
                NONE => undo_ascii_aux (c:: #"_"::rcs) cs  (*ERROR*)
paulson@24183
   166
              | SOME n => undo_ascii_aux (Char.chr n :: rcs) (List.drop (cs, 2))
paulson@24183
   167
        end
paulson@24183
   168
  | undo_ascii_aux rcs (c::cs) = undo_ascii_aux (c::rcs) cs;
paulson@24183
   169
paulson@24183
   170
val undo_ascii_of = undo_ascii_aux [] o String.explode;
paulson@15347
   171
paulson@17525
   172
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   173
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   174
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   175
paulson@21509
   176
(*TSTP format uses (...) rather than the old [...]*)
paulson@21509
   177
fun tptp_pack strings = "(" ^ space_implode " | " strings ^ ")";
paulson@17525
   178
paulson@17525
   179
paulson@16925
   180
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   181
fun trim_type_var s =
paulson@16925
   182
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   183
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   184
paulson@16903
   185
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   186
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   187
paulson@17230
   188
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   189
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   190
wenzelm@24310
   191
fun make_schematic_type_var (x,i) =
paulson@16925
   192
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   193
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   194
blanchet@36491
   195
val max_dfg_symbol_length =
blanchet@36491
   196
  if is_new_spass_version then 1000000 (* arbitrary large number *) else 63
blanchet@36378
   197
blanchet@36378
   198
(* HACK because SPASS 3.0 truncates identifiers to 63 characters. *)
blanchet@36062
   199
fun controlled_length dfg s =
blanchet@36378
   200
  if dfg andalso size s > max_dfg_symbol_length then
blanchet@36378
   201
    String.extract (s, 0, SOME (max_dfg_symbol_length div 2 - 1)) ^ "__" ^
blanchet@36378
   202
    String.extract (s, size s - max_dfg_symbol_length div 2 + 1, NONE)
blanchet@36378
   203
  else
blanchet@36378
   204
    s
paulson@23075
   205
immler@30151
   206
fun lookup_const dfg c =
wenzelm@17412
   207
    case Symtab.lookup const_trans_table c of
paulson@17230
   208
        SOME c' => c'
immler@30151
   209
      | NONE => controlled_length dfg (ascii_of c);
paulson@17230
   210
immler@30151
   211
fun lookup_type_const dfg c =
wenzelm@17412
   212
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   213
        SOME c' => c'
immler@30151
   214
      | NONE => controlled_length dfg (ascii_of c);
paulson@18411
   215
blanchet@36062
   216
(* "op =" MUST BE "equal" because it's built into ATPs. *)
blanchet@36062
   217
fun make_fixed_const _ (@{const_name "op ="}) = "equal"
blanchet@36062
   218
  | make_fixed_const dfg c = const_prefix ^ lookup_const dfg c;
paulson@18411
   219
immler@30151
   220
fun make_fixed_type_const dfg c = tconst_prefix ^ lookup_type_const dfg c;
quigley@17150
   221
wenzelm@17261
   222
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   223
quigley@17150
   224
blanchet@36169
   225
(**** name pool ****)
blanchet@36169
   226
 
blanchet@36169
   227
type name = string * string
blanchet@36169
   228
type name_pool = string Symtab.table * string Symtab.table
blanchet@36169
   229
blanchet@36222
   230
fun empty_name_pool readable_names =
blanchet@36222
   231
  if readable_names then SOME (`I Symtab.empty) else NONE
blanchet@36169
   232
blanchet@36169
   233
fun pool_map f xs =
blanchet@36170
   234
  fold_rev (fn x => fn (ys, pool) => f x pool |>> (fn y => y :: ys)) xs
blanchet@36170
   235
  o pair []
blanchet@36169
   236
blanchet@36169
   237
fun add_nice_name full_name nice_prefix j the_pool =
blanchet@36169
   238
  let
blanchet@36169
   239
    val nice_name = nice_prefix ^ (if j = 0 then "" else "_" ^ Int.toString j)
blanchet@36169
   240
  in
blanchet@36169
   241
    case Symtab.lookup (snd the_pool) nice_name of
blanchet@36169
   242
      SOME full_name' =>
blanchet@36169
   243
      if full_name = full_name' then (nice_name, the_pool)
blanchet@36169
   244
      else add_nice_name full_name nice_prefix (j + 1) the_pool
blanchet@36169
   245
    | NONE =>
blanchet@36169
   246
      (nice_name, (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@36169
   247
                   Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@36169
   248
  end
blanchet@36169
   249
blanchet@36169
   250
fun translate_first_char f s =
blanchet@36169
   251
  String.str (f (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
blanchet@36169
   252
blanchet@36222
   253
fun readable_name full_name s =
blanchet@36169
   254
  let
blanchet@36169
   255
    val s = s |> Long_Name.base_name
blanchet@36169
   256
              |> fold remove_all ["\<^sub>", "\<^bsub>", "\<^esub>", "\<^isub>"]
blanchet@36169
   257
    val s' = s |> explode |> rev |> dropwhile (curry (op =) "'")
blanchet@36169
   258
    val s' =
blanchet@36169
   259
      (s' |> rev
blanchet@36169
   260
          |> implode
blanchet@36169
   261
          |> String.translate
blanchet@36221
   262
                 (fn c => if Char.isAlphaNum c orelse c = #"_" then String.str c
blanchet@36221
   263
                          else ""))
blanchet@36169
   264
      ^ replicate_string (String.size s - length s') "_"
blanchet@36169
   265
    val s' =
blanchet@36169
   266
      if s' = "" orelse not (Char.isAlpha (String.sub (s', 0))) then "X" ^ s'
blanchet@36169
   267
      else s'
blanchet@36476
   268
    (* Avoid "equal", since it's built into ATPs; and "op" is very ambiguous
blanchet@36476
   269
       ("op &", "op |", etc.). *)
blanchet@36476
   270
    val s' = if s' = "equal" orelse s' = "op" then full_name else s'
blanchet@36169
   271
  in
blanchet@36169
   272
    case (Char.isLower (String.sub (full_name, 0)),
blanchet@36169
   273
          Char.isLower (String.sub (s', 0))) of
blanchet@36169
   274
      (true, false) => translate_first_char Char.toLower s'
blanchet@36169
   275
    | (false, true) => translate_first_char Char.toUpper s'
blanchet@36169
   276
    | _ => s'
blanchet@36169
   277
  end
blanchet@36169
   278
blanchet@36169
   279
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@36169
   280
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@36169
   281
    case Symtab.lookup (fst the_pool) full_name of
blanchet@36169
   282
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@36222
   283
    | NONE => add_nice_name full_name (readable_name full_name desired_name) 0
blanchet@36222
   284
                            the_pool
blanchet@36169
   285
              |> apsnd SOME
blanchet@36169
   286
blanchet@36169
   287
(**** Definitions and functions for FOL clauses, for conversion to TPTP or DFG
blanchet@36169
   288
      format ****)
paulson@15347
   289
paulson@21509
   290
datatype kind = Axiom | Conjecture;
paulson@23385
   291
paulson@15347
   292
type axiom_name = string;
paulson@15347
   293
paulson@15347
   294
(**** Isabelle FOL clauses ****)
paulson@15347
   295
blanchet@36168
   296
datatype fol_type =
blanchet@36169
   297
  TyVar of name |
blanchet@36169
   298
  TyFree of name |
blanchet@36169
   299
  TyConstr of name * fol_type list
mengj@18402
   300
blanchet@36170
   301
fun string_of_fol_type (TyVar sp) pool = nice_name sp pool
blanchet@36170
   302
  | string_of_fol_type (TyFree sp) pool = nice_name sp pool
blanchet@36170
   303
  | string_of_fol_type (TyConstr (sp, tys)) pool =
blanchet@36170
   304
    let
blanchet@36170
   305
      val (s, pool) = nice_name sp pool
blanchet@36170
   306
      val (ss, pool) = pool_map string_of_fol_type tys pool
blanchet@36170
   307
    in (s ^ paren_pack ss, pool) end
wenzelm@24310
   308
paulson@18798
   309
(*First string is the type class; the second is a TVar or TFfree*)
paulson@18798
   310
datatype type_literal = LTVar of string * string | LTFree of string * string;
paulson@15347
   311
paulson@17404
   312
exception CLAUSE of string * term;
paulson@15347
   313
wenzelm@24310
   314
(*Make literals for sorted type variables*)
paulson@24940
   315
fun sorts_on_typs_aux (_, [])   = []
paulson@24940
   316
  | sorts_on_typs_aux ((x,i),  s::ss) =
paulson@24940
   317
      let val sorts = sorts_on_typs_aux ((x,i), ss)
paulson@22643
   318
      in
paulson@22643
   319
          if s = "HOL.type" then sorts
paulson@24940
   320
          else if i = ~1 then LTFree(make_type_class s, make_fixed_type_var x) :: sorts
paulson@24940
   321
          else LTVar(make_type_class s, make_schematic_type_var (x,i)) :: sorts
paulson@22643
   322
      end;
quigley@17150
   323
paulson@24940
   324
fun sorts_on_typs (TFree (a,s)) = sorts_on_typs_aux ((a,~1),s)
paulson@24940
   325
  | sorts_on_typs (TVar (v,s))  = sorts_on_typs_aux (v,s);
paulson@24940
   326
paulson@18798
   327
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@22643
   328
  | pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   329
paulson@24937
   330
(*Given a list of sorted type variables, return a list of type literals.*)
blanchet@36218
   331
fun add_typs Ts = fold (union (op =)) (map sorts_on_typs Ts) []
mengj@20015
   332
paulson@29676
   333
(*The correct treatment of TFrees like 'a in lemmas (axiom clauses) is not clear.
paulson@29676
   334
  * Ignoring them leads to unsound proofs, since we do nothing to ensure that 'a
paulson@29676
   335
    in a lemma has the same sort as 'a in the conjecture.
paulson@29676
   336
  * Deleting such clauses will lead to problems with locales in other use of local results
paulson@29676
   337
    where 'a is fixed. Probably we should delete clauses unless the sorts agree.
paulson@29676
   338
  * Currently we include a class constraint in the clause, exactly as with TVars.
paulson@29676
   339
*)
paulson@29676
   340
mengj@20015
   341
(** make axiom and conjecture clauses. **)
mengj@20015
   342
mengj@20015
   343
fun get_tvar_strs [] = []
paulson@24940
   344
  | get_tvar_strs ((TVar (indx,s))::Ts) =
paulson@24940
   345
      insert (op =) (make_schematic_type_var indx) (get_tvar_strs Ts)
paulson@24940
   346
  | get_tvar_strs((TFree _)::Ts) = get_tvar_strs Ts
mengj@20015
   347
wenzelm@24310
   348
mengj@19354
   349
paulson@15347
   350
(**** Isabelle arities ****)
paulson@15347
   351
wenzelm@24310
   352
datatype arLit = TConsLit of class * string * string list
paulson@22643
   353
               | TVarLit of class * string;
wenzelm@24310
   354
blanchet@35865
   355
datatype arity_clause =
wenzelm@24310
   356
         ArityClause of {axiom_name: axiom_name,
wenzelm@24310
   357
                         conclLit: arLit,
wenzelm@24310
   358
                         premLits: arLit list};
paulson@15347
   359
paulson@15347
   360
paulson@18798
   361
fun gen_TVars 0 = []
paulson@18798
   362
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   363
paulson@18411
   364
fun pack_sort(_,[])  = []
paulson@18411
   365
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@22643
   366
  | pack_sort(tvar, cls::srt) =  (cls, tvar) :: pack_sort(tvar, srt);
wenzelm@24310
   367
paulson@18411
   368
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
immler@30151
   369
fun make_axiom_arity_clause dfg (tcons, axiom_name, (cls,args)) =
paulson@21560
   370
   let val tvars = gen_TVars (length args)
paulson@17845
   371
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   372
   in
paulson@24937
   373
      ArityClause {axiom_name = axiom_name, 
immler@30151
   374
                   conclLit = TConsLit (cls, make_fixed_type_const dfg tcons, tvars),
paulson@22643
   375
                   premLits = map TVarLit (union_all(map pack_sort tvars_srts))}
paulson@17845
   376
   end;
paulson@15347
   377
paulson@15347
   378
paulson@15347
   379
(**** Isabelle class relations ****)
paulson@15347
   380
blanchet@35865
   381
datatype classrel_clause =
wenzelm@24310
   382
         ClassrelClause of {axiom_name: axiom_name,
wenzelm@24310
   383
                            subclass: class,
wenzelm@24310
   384
                            superclass: class};
wenzelm@24310
   385
paulson@21290
   386
(*Generate all pairs (sub,super) such that sub is a proper subclass of super in theory thy.*)
paulson@21432
   387
fun class_pairs thy [] supers = []
paulson@21432
   388
  | class_pairs thy subs supers =
blanchet@36218
   389
      let
blanchet@36218
   390
        val class_less = Sorts.class_less (Sign.classes_of thy)
blanchet@36218
   391
        fun add_super sub super = class_less (sub, super) ? cons (sub, super)
blanchet@36218
   392
        fun add_supers sub = fold (add_super sub) supers
blanchet@36218
   393
      in fold add_supers subs [] end
paulson@15347
   394
blanchet@35865
   395
fun make_classrel_clause (sub,super) =
paulson@21290
   396
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of sub ^ "_" ^ ascii_of super,
wenzelm@24310
   397
                  subclass = make_type_class sub,
paulson@21290
   398
                  superclass = make_type_class super};
paulson@15347
   399
paulson@21290
   400
fun make_classrel_clauses thy subs supers =
blanchet@35865
   401
  map make_classrel_clause (class_pairs thy subs supers);
paulson@18868
   402
paulson@18868
   403
paulson@18868
   404
(** Isabelle arities **)
paulson@17845
   405
immler@30151
   406
fun arity_clause dfg _ _ (tcons, []) = []
immler@30151
   407
  | arity_clause dfg seen n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
immler@30151
   408
      arity_clause dfg seen n (tcons,ars)
immler@30151
   409
  | arity_clause dfg seen n (tcons, (ar as (class,_)) :: ars) =
paulson@21373
   410
      if class mem_string seen then (*multiple arities for the same tycon, class pair*)
immler@30151
   411
          make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class ^ "_" ^ Int.toString n, ar) ::
immler@30151
   412
          arity_clause dfg seen (n+1) (tcons,ars)
paulson@21373
   413
      else
immler@30151
   414
          make_axiom_arity_clause dfg (tcons, lookup_type_const dfg tcons ^ "_" ^ class, ar) ::
immler@30151
   415
          arity_clause dfg (class::seen) n (tcons,ars)
paulson@17845
   416
immler@30151
   417
fun multi_arity_clause dfg [] = []
blanchet@36218
   418
  | multi_arity_clause dfg ((tcons, ars) :: tc_arlists) =
blanchet@36218
   419
      arity_clause dfg [] 1 (tcons, ars) @ multi_arity_clause dfg tc_arlists
paulson@17845
   420
paulson@22643
   421
(*Generate all pairs (tycon,class,sorts) such that tycon belongs to class in theory thy
paulson@22643
   422
  provided its arguments have the corresponding sorts.*)
paulson@21373
   423
fun type_class_pairs thy tycons classes =
paulson@21373
   424
  let val alg = Sign.classes_of thy
blanchet@36218
   425
      fun domain_sorts tycon = Sorts.mg_domain alg tycon o single
blanchet@36218
   426
      fun add_class tycon class =
blanchet@36218
   427
        cons (class, domain_sorts tycon class)
blanchet@36218
   428
        handle Sorts.CLASS_ERROR _ => I
blanchet@36218
   429
      fun try_classes tycon = (tycon, fold (add_class tycon) classes [])
paulson@21373
   430
  in  map try_classes tycons  end;
paulson@21373
   431
paulson@22643
   432
(*Proving one (tycon, class) membership may require proving others, so iterate.*)
paulson@22643
   433
fun iter_type_class_pairs thy tycons [] = ([], [])
paulson@22643
   434
  | iter_type_class_pairs thy tycons classes =
paulson@22643
   435
      let val cpairs = type_class_pairs thy tycons classes
haftmann@33040
   436
          val newclasses = union_all (union_all (union_all (map (map #2 o #2) cpairs)))
haftmann@33040
   437
            |> subtract (op =) classes |> subtract (op =) HOLogic.typeS
wenzelm@24310
   438
          val (classes', cpairs') = iter_type_class_pairs thy tycons newclasses
haftmann@33042
   439
      in (union (op =) classes' classes, union (op =) cpairs' cpairs) end;
wenzelm@24310
   440
immler@30151
   441
fun make_arity_clauses_dfg dfg thy tycons classes =
wenzelm@24310
   442
  let val (classes', cpairs) = iter_type_class_pairs thy tycons classes
immler@30151
   443
  in  (classes', multi_arity_clause dfg cpairs)  end;
immler@30151
   444
val make_arity_clauses = make_arity_clauses_dfg false;
paulson@17845
   445
paulson@18868
   446
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   447
wenzelm@24310
   448
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong
paulson@18868
   449
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   450
blanchet@36218
   451
fun update_many keypairs = fold Symtab.update keypairs
paulson@18868
   452
blanchet@36218
   453
val add_type_sort_preds = update_many o map pred_of_sort o sorts_on_typs
paulson@17845
   454
blanchet@36218
   455
fun add_classrel_clause_preds (ClassrelClause {subclass, superclass, ...}) =
blanchet@36218
   456
  Symtab.update (subclass, 1) #> Symtab.update (superclass, 1)
paulson@17845
   457
paulson@22643
   458
fun class_of_arityLit (TConsLit (tclass, _, _)) = tclass
paulson@22643
   459
  | class_of_arityLit (TVarLit (tclass, _)) = tclass;
paulson@21373
   460
blanchet@36218
   461
fun add_arity_clause_preds (ArityClause {conclLit, premLits, ...}) =
blanchet@36218
   462
  let
blanchet@36218
   463
    val classes = map (make_type_class o class_of_arityLit)
blanchet@36218
   464
                      (conclLit :: premLits)
blanchet@36218
   465
  in fold (Symtab.update o rpair 1) classes end;
paulson@18868
   466
paulson@18868
   467
(*** Find occurrences of functions in clauses ***)
paulson@18868
   468
blanchet@36218
   469
fun add_fol_type_funcs (TyVar _) = I
blanchet@36218
   470
  | add_fol_type_funcs (TyFree (s, _)) = Symtab.update (s, 0)
blanchet@36218
   471
  | add_fol_type_funcs (TyConstr ((s, _), tys)) =
blanchet@36218
   472
    Symtab.update (s, length tys) #> fold add_fol_type_funcs tys
paulson@18868
   473
paulson@20038
   474
(*TFrees are recorded as constants*)
paulson@24940
   475
fun add_type_sort_funcs (TVar _, funcs) = funcs
paulson@24940
   476
  | add_type_sort_funcs (TFree (a, _), funcs) =
paulson@20038
   477
      Symtab.update (make_fixed_type_var a, 0) funcs
paulson@20038
   478
blanchet@36218
   479
fun add_arity_clause_funcs (ArityClause {conclLit,...}) funcs =
paulson@22643
   480
  let val TConsLit (_, tcons, tvars) = conclLit
paulson@18868
   481
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   482
paulson@23075
   483
(*This type can be overlooked because it is built-in...*)
paulson@23075
   484
val init_functab = Symtab.update ("tc_itself", 1) Symtab.empty;
paulson@23075
   485
paulson@18868
   486
paulson@18868
   487
(**** String-oriented operations ****)
paulson@15347
   488
wenzelm@24310
   489
fun string_of_clausename (cls_id,ax_name) =
paulson@17525
   490
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   491
wenzelm@24310
   492
fun string_of_type_clsname (cls_id,ax_name,idx) =
paulson@17525
   493
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   494
wenzelm@24310
   495
paulson@18868
   496
(**** Producing DFG files ****)
quigley@17150
   497
paulson@18863
   498
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   499
fun dfg_sign true s = s
wenzelm@24310
   500
  | dfg_sign false s = "not(" ^ s ^ ")"
paulson@18863
   501
paulson@24937
   502
fun dfg_of_typeLit pos (LTVar (s,ty))  = dfg_sign pos (s ^ "(" ^ ty ^ ")")
paulson@24937
   503
  | dfg_of_typeLit pos (LTFree (s,ty)) = dfg_sign pos (s ^ "(" ^ ty ^ ")");
wenzelm@24310
   504
paulson@18868
   505
(*Enclose the clause body by quantifiers, if necessary*)
wenzelm@24310
   506
fun dfg_forall [] body = body
paulson@18868
   507
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   508
paulson@24937
   509
fun gen_dfg_cls (cls_id, ax_name, Axiom, lits, tylits, vars) =
paulson@24937
   510
      "clause( %(axiom)\n" ^
paulson@24937
   511
      dfg_forall vars ("or(" ^ commas (tylits@lits) ^ ")") ^ ",\n" ^
paulson@24937
   512
      string_of_clausename (cls_id,ax_name) ^  ").\n\n"
paulson@24937
   513
  | gen_dfg_cls (cls_id, ax_name, Conjecture, lits, _, vars) =
paulson@24937
   514
      "clause( %(negated_conjecture)\n" ^
paulson@24937
   515
      dfg_forall vars ("or(" ^ commas lits ^ ")") ^ ",\n" ^
paulson@24937
   516
      string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   517
paulson@18798
   518
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   519
paulson@18856
   520
fun string_of_preds [] = ""
paulson@18856
   521
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   522
paulson@18856
   523
fun string_of_funcs [] = ""
paulson@18856
   524
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   525
wenzelm@24310
   526
fun string_of_symbols predstr funcstr =
paulson@17234
   527
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   528
paulson@18798
   529
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   530
wenzelm@24310
   531
fun string_of_descrip name =
wenzelm@24310
   532
  "list_of_descriptions.\nname({*" ^ name ^
paulson@18868
   533
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   534
paulson@18863
   535
fun dfg_tfree_clause tfree_lit =
paulson@21509
   536
  "clause( %(negated_conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   537
paulson@22643
   538
fun dfg_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   539
      dfg_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   540
  | dfg_of_arLit (TVarLit (c,str)) =
paulson@22643
   541
      dfg_sign false (make_type_class c ^ "(" ^ str ^ ")")
wenzelm@24310
   542
paulson@20038
   543
fun dfg_classrelLits sub sup =  "not(" ^ sub ^ "(T)), " ^ sup ^ "(T)";
paulson@17525
   544
blanchet@35865
   545
fun dfg_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   546
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   547
  axiom_name ^ ").\n\n";
wenzelm@24310
   548
paulson@21560
   549
fun string_of_ar axiom_name = arclause_prefix ^ ascii_of axiom_name;
paulson@21560
   550
paulson@24937
   551
fun dfg_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
paulson@22643
   552
  let val TConsLit (_,_,tvars) = conclLit
paulson@18868
   553
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   554
  in
paulson@24937
   555
      "clause( %(axiom)\n" ^
paulson@18868
   556
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@21560
   557
      string_of_ar axiom_name ^ ").\n\n"
paulson@18863
   558
  end;
paulson@18863
   559
quigley@17150
   560
paulson@18869
   561
(**** Produce TPTP files ****)
paulson@18868
   562
paulson@21509
   563
fun tptp_sign true s = s
paulson@21509
   564
  | tptp_sign false s = "~ " ^ s
paulson@18868
   565
blanchet@36167
   566
fun tptp_of_typeLit pos (LTVar (s, ty))  = tptp_sign pos (s ^ "(" ^ ty ^ ")")
blanchet@36167
   567
  | tptp_of_typeLit pos (LTFree (s, ty)) = tptp_sign pos (s ^ "(" ^ ty ^ ")")
blanchet@36167
   568
blanchet@36167
   569
fun tptp_cnf name kind formula =
blanchet@36167
   570
  "cnf(" ^ name ^ ", " ^ kind ^ ",\n    " ^ formula ^ ").\n"
wenzelm@24310
   571
blanchet@36167
   572
fun gen_tptp_cls (cls_id, ax_name, Axiom, lits, tylits) =
blanchet@36167
   573
      tptp_cnf (string_of_clausename (cls_id, ax_name)) "axiom"
blanchet@36167
   574
               (tptp_pack (tylits @ lits))
blanchet@36167
   575
  | gen_tptp_cls (cls_id, ax_name, Conjecture, lits, _) =
blanchet@36167
   576
      tptp_cnf (string_of_clausename (cls_id, ax_name)) "negated_conjecture"
blanchet@36167
   577
               (tptp_pack lits)
paulson@15347
   578
paulson@18863
   579
fun tptp_tfree_clause tfree_lit =
blanchet@36167
   580
    tptp_cnf "tfree_tcs" "negated_conjecture" (tptp_pack [tfree_lit])
wenzelm@24310
   581
paulson@22643
   582
fun tptp_of_arLit (TConsLit (c,t,args)) =
paulson@22643
   583
      tptp_sign true (make_type_class c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@22643
   584
  | tptp_of_arLit (TVarLit (c,str)) =
paulson@22643
   585
      tptp_sign false (make_type_class c ^ "(" ^ str ^ ")")
wenzelm@24310
   586
paulson@24937
   587
fun tptp_arity_clause (ArityClause{axiom_name,conclLit,premLits,...}) =
blanchet@36167
   588
  tptp_cnf (string_of_ar axiom_name) "axiom"
blanchet@36167
   589
           (tptp_pack (map tptp_of_arLit (conclLit :: premLits)))
paulson@15347
   590
wenzelm@24310
   591
fun tptp_classrelLits sub sup =
paulson@21509
   592
  let val tvar = "(T)"
paulson@21509
   593
  in  tptp_pack [tptp_sign false (sub^tvar), tptp_sign true (sup^tvar)]  end;
paulson@15347
   594
blanchet@35865
   595
fun tptp_classrel_clause (ClassrelClause {axiom_name,subclass,superclass,...}) =
blanchet@36167
   596
  tptp_cnf axiom_name "axiom" (tptp_classrelLits subclass superclass)
quigley@17150
   597
paulson@15347
   598
end;