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