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