src/HOL/Tools/res_clause.ML
author mengj
Thu May 25 08:08:38 2006 +0200 (2006-05-25)
changeset 19719 837025cc6317
parent 19642 ea7162f84677
child 20015 1ffcf4802802
permissions -rw-r--r--
Changed input interface of dfg_write_file.
paulson@15347
     1
(*  Author: Jia Meng, Cambridge University Computer Laboratory
paulson@15347
     2
    ID: $Id$
paulson@15347
     3
    Copyright 2004 University of Cambridge
paulson@15347
     4
paulson@15347
     5
ML data structure for storing/printing FOL clauses and arity clauses.
paulson@15347
     6
Typed equality is treated differently.
paulson@15347
     7
*)
paulson@15347
     8
paulson@15347
     9
signature RES_CLAUSE =
paulson@15347
    10
  sig
paulson@18868
    11
  exception CLAUSE of string * term
paulson@18868
    12
  type clause and arityClause and classrelClause
paulson@18868
    13
  type fol_type
paulson@18868
    14
  type typ_var
paulson@18868
    15
  type type_literal
paulson@18868
    16
  val add_typs_aux : (typ_var * string list) list -> type_literal list * type_literal list
paulson@18868
    17
  val arity_clause_thy: theory -> arityClause list 
paulson@18868
    18
  val ascii_of : string -> string
paulson@18868
    19
  val bracket_pack : string list -> string
paulson@18868
    20
  val check_var_pairs: ''a * ''b -> (''a * ''b) list -> int
paulson@18868
    21
  val classrel_clauses_thy: theory -> classrelClause list 
paulson@18868
    22
  val clause_eq : clause * clause -> bool
paulson@18868
    23
  val clause_prefix : string 
paulson@18868
    24
  val clause2tptp : clause -> string * string list
paulson@18868
    25
  val const_prefix : string
mengj@19719
    26
  val dfg_write_file:  thm list -> string -> ((thm * (string * int)) list * classrelClause list * arityClause list) -> unit
paulson@18868
    27
  val fixed_var_prefix : string
paulson@18868
    28
  val gen_tptp_cls : int * string * string * string -> string
paulson@18868
    29
  val gen_tptp_type_cls : int * string * string * string * int -> string
paulson@18868
    30
  val get_axiomName : clause ->  string
paulson@18868
    31
  val hash_clause : clause -> int
paulson@18868
    32
  val init : theory -> unit
paulson@18868
    33
  val isMeta : string -> bool
paulson@18868
    34
  val isTaut : clause -> bool
paulson@17404
    35
  val keep_types : bool ref
paulson@18868
    36
  val list_ord : ('a * 'b -> order) -> 'a list * 'b list -> order
mengj@19443
    37
  val make_axiom_clause : thm -> string * int -> clause option
mengj@19443
    38
  val make_conjecture_clauses : thm list -> clause list
paulson@18868
    39
  val make_fixed_const : string -> string		
paulson@18868
    40
  val make_fixed_type_const : string -> string   
paulson@18868
    41
  val make_fixed_type_var : string -> string
paulson@18868
    42
  val make_fixed_var : string -> string
paulson@18868
    43
  val make_schematic_type_var : string * int -> string
paulson@18868
    44
  val make_schematic_var : string * int -> string
paulson@18868
    45
  val make_type_class : string -> string
paulson@18868
    46
  val mk_fol_type: string * string * fol_type list -> fol_type
paulson@18868
    47
  val mk_typ_var_sort : Term.typ -> typ_var * sort
paulson@18868
    48
  val paren_pack : string list -> string
paulson@18868
    49
  val schematic_var_prefix : string
paulson@18868
    50
  val special_equal : bool ref
paulson@18868
    51
  val string_of_fol_type : fol_type -> string
paulson@18868
    52
  val tconst_prefix : string 
paulson@18868
    53
  val tfree_prefix : string
paulson@17404
    54
  val tptp_arity_clause : arityClause -> string
paulson@17404
    55
  val tptp_classrelClause : classrelClause -> string
paulson@18868
    56
  val tptp_of_typeLit : type_literal -> string
paulson@18863
    57
  val tptp_tfree_clause : string -> string
mengj@19443
    58
  val tptp_write_file: thm list -> string -> ((thm * (string * int)) list * classrelClause list * arityClause list) -> unit
paulson@17404
    59
  val tvar_prefix : string
paulson@18868
    60
  val types_eq: fol_type list * fol_type list -> (string*string) list * (string*string) list -> bool * ((string*string) list * (string*string) list)
paulson@18868
    61
  val types_ord : fol_type list * fol_type list -> order
mengj@17908
    62
  val union_all : ''a list list -> ''a list
paulson@18863
    63
  val writeln_strs: TextIO.outstream -> TextIO.vector list -> unit
mengj@19719
    64
  val dfg_sign: bool -> string -> string
mengj@19719
    65
  val dfg_of_typeLit: type_literal -> string
mengj@19719
    66
  val get_tvar_strs: (typ_var * sort) list -> string list
mengj@19719
    67
  val gen_dfg_cls: int * string * string * string * string list -> string
mengj@19719
    68
  val add_foltype_funcs: fol_type * int Symtab.table -> int Symtab.table
mengj@19719
    69
  val add_arityClause_funcs: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    70
  val add_arityClause_preds: arityClause * int Symtab.table -> int Symtab.table
mengj@19719
    71
  val add_classrelClause_preds : classrelClause * int Symtab.table -> int Symtab.table
mengj@19719
    72
  val dfg_tfree_clause : string -> string
mengj@19719
    73
  val string_of_start: string -> string
mengj@19719
    74
  val string_of_descrip : string -> string
mengj@19719
    75
  val string_of_symbols: string -> string -> string
mengj@19719
    76
  val string_of_funcs: (string * int) list -> string
mengj@19719
    77
  val string_of_preds: (string * Int.int) list -> string
mengj@19719
    78
  val dfg_classrelClause: classrelClause -> string
mengj@19719
    79
  val dfg_arity_clause: arityClause -> string
mengj@19719
    80
end;
paulson@15347
    81
paulson@18420
    82
structure ResClause : RES_CLAUSE =
paulson@15347
    83
struct
paulson@15347
    84
paulson@15347
    85
(* Added for typed equality *)
paulson@15347
    86
val special_equal = ref false; (* by default,equality does not carry type information *)
paulson@15347
    87
val eq_typ_wrapper = "typeinfo"; (* default string *)
paulson@15347
    88
paulson@15347
    89
paulson@15347
    90
val schematic_var_prefix = "V_";
paulson@15347
    91
val fixed_var_prefix = "v_";
paulson@15347
    92
paulson@17230
    93
val tvar_prefix = "T_";
paulson@17230
    94
val tfree_prefix = "t_";
paulson@15347
    95
paulson@15347
    96
val clause_prefix = "cls_"; 
paulson@17525
    97
val arclause_prefix = "clsarity_" 
paulson@17525
    98
val clrelclause_prefix = "clsrel_";
paulson@15347
    99
paulson@17230
   100
val const_prefix = "c_";
paulson@17230
   101
val tconst_prefix = "tc_"; 
paulson@16199
   102
val class_prefix = "class_"; 
paulson@15347
   103
paulson@17775
   104
fun union_all xss = foldl (op union) [] xss;
paulson@17775
   105
paulson@17775
   106
(*Provide readable names for the more common symbolic functions*)
paulson@15347
   107
val const_trans_table =
paulson@15347
   108
      Symtab.make [("op =", "equal"),
haftmann@19277
   109
	  	   ("Orderings.less_eq", "lessequals"),
haftmann@19277
   110
		   ("Orderings.less", "less"),
paulson@15347
   111
		   ("op &", "and"),
paulson@15347
   112
		   ("op |", "or"),
haftmann@19233
   113
		   ("HOL.plus", "plus"),
haftmann@19233
   114
		   ("HOL.minus", "minus"),
haftmann@19233
   115
		   ("HOL.times", "times"),
paulson@18676
   116
		   ("Divides.op div", "div"),
paulson@18676
   117
		   ("HOL.divide", "divide"),
paulson@15347
   118
		   ("op -->", "implies"),
paulson@17375
   119
		   ("{}", "emptyset"),
paulson@15347
   120
		   ("op :", "in"),
paulson@15347
   121
		   ("op Un", "union"),
paulson@18390
   122
		   ("op Int", "inter"),
paulson@18390
   123
		   ("List.op @", "append")];
paulson@15347
   124
paulson@17230
   125
val type_const_trans_table =
paulson@18411
   126
      Symtab.make [("*", "prod"),
paulson@18411
   127
	  	   ("+", "sum"),
paulson@18411
   128
		   ("~=>", "map")];
paulson@15347
   129
paulson@15610
   130
(*Escaping of special characters.
paulson@15610
   131
  Alphanumeric characters are left unchanged.
paulson@15610
   132
  The character _ goes to __
paulson@15610
   133
  Characters in the range ASCII space to / go to _A to _P, respectively.
paulson@15610
   134
  Other printing characters go to _NNN where NNN is the decimal ASCII code.*)
paulson@15610
   135
local
paulson@15610
   136
paulson@15610
   137
val A_minus_space = Char.ord #"A" - Char.ord #" ";
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 "__"
paulson@15610
   142
  else if #" " <= c andalso c <= #"/" 
paulson@15610
   143
       then "_" ^ String.str (Char.chr (Char.ord c + A_minus_space))
paulson@15610
   144
  else if Char.isPrint c then ("_" ^ Int.toString (Char.ord c))
paulson@15610
   145
  else ""
paulson@15347
   146
paulson@15610
   147
in
paulson@15610
   148
paulson@15610
   149
val ascii_of = String.translate ascii_of_c;
paulson@15610
   150
paulson@15610
   151
end;
paulson@15347
   152
paulson@17525
   153
(* convert a list of strings into one single string; surrounded by brackets *)
paulson@18218
   154
fun paren_pack [] = ""   (*empty argument list*)
paulson@18218
   155
  | paren_pack strings = "(" ^ commas strings ^ ")";
paulson@17525
   156
paulson@17525
   157
fun bracket_pack strings = "[" ^ commas strings ^ "]";
paulson@17525
   158
paulson@17525
   159
paulson@16925
   160
(*Remove the initial ' character from a type variable, if it is present*)
paulson@16925
   161
fun trim_type_var s =
paulson@16925
   162
  if s <> "" andalso String.sub(s,0) = #"'" then String.extract(s,1,NONE)
paulson@16925
   163
  else error ("trim_type: Malformed type variable encountered: " ^ s);
paulson@16925
   164
paulson@16903
   165
fun ascii_of_indexname (v,0) = ascii_of v
paulson@17525
   166
  | ascii_of_indexname (v,i) = ascii_of v ^ "_" ^ Int.toString i;
paulson@15347
   167
paulson@17230
   168
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of_indexname v);
paulson@15347
   169
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
   170
paulson@16925
   171
fun make_schematic_type_var (x,i) = 
paulson@16925
   172
      tvar_prefix ^ (ascii_of_indexname (trim_type_var x,i));
paulson@16925
   173
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of (trim_type_var x));
paulson@15347
   174
paulson@18411
   175
fun lookup_const c =
wenzelm@17412
   176
    case Symtab.lookup const_trans_table c of
paulson@17230
   177
        SOME c' => c'
paulson@18411
   178
      | NONE => ascii_of c;
paulson@17230
   179
paulson@18411
   180
fun lookup_type_const c = 
wenzelm@17412
   181
    case Symtab.lookup type_const_trans_table c of
paulson@17230
   182
        SOME c' => c'
paulson@18411
   183
      | NONE => ascii_of c;
paulson@18411
   184
paulson@18411
   185
fun make_fixed_const "op =" = "equal"   (*MUST BE "equal" because it's built-in to ATPs*)
paulson@18411
   186
  | make_fixed_const c      = const_prefix ^ lookup_const c;
paulson@18411
   187
paulson@18411
   188
fun make_fixed_type_const c = tconst_prefix ^ lookup_type_const c;
quigley@17150
   189
wenzelm@17261
   190
fun make_type_class clas = class_prefix ^ ascii_of clas;
quigley@17150
   191
quigley@17150
   192
paulson@18798
   193
(***** definitions and functions for FOL clauses, for conversion to TPTP or DFG format. *****)
paulson@15347
   194
paulson@17230
   195
val keep_types = ref true;
paulson@15347
   196
paulson@15347
   197
datatype kind = Axiom | Hypothesis | Conjecture;
paulson@15347
   198
fun name_of_kind Axiom = "axiom"
paulson@15347
   199
  | name_of_kind Hypothesis = "hypothesis"
paulson@15347
   200
  | name_of_kind Conjecture = "conjecture";
paulson@15347
   201
paulson@15347
   202
type clause_id = int;
paulson@15347
   203
type axiom_name = string;
paulson@15347
   204
paulson@15347
   205
paulson@15347
   206
type polarity = bool;
paulson@15347
   207
paulson@18869
   208
(* "tag" is used for vampire specific syntax FIXME REMOVE *)
paulson@15347
   209
type tag = bool; 
paulson@15347
   210
paulson@15347
   211
paulson@15347
   212
(**** Isabelle FOL clauses ****)
paulson@15347
   213
paulson@15347
   214
val tagged = ref false;
paulson@15347
   215
paulson@15347
   216
type pred_name = string;
mengj@18402
   217
mengj@18402
   218
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
mengj@18402
   219
paulson@18798
   220
(*FIXME: give the constructors more sensible names*)
mengj@18402
   221
datatype fol_type = AtomV of string
mengj@18402
   222
		  | AtomF of string
mengj@18402
   223
		  | Comp of string * fol_type list;
mengj@18402
   224
mengj@18402
   225
fun string_of_fol_type (AtomV x) = x
mengj@18402
   226
  | string_of_fol_type (AtomF x) = x
mengj@18402
   227
  | string_of_fol_type (Comp(tcon,tps)) = 
paulson@18856
   228
      tcon ^ (paren_pack (map string_of_fol_type tps));
paulson@18856
   229
      
mengj@18439
   230
fun mk_fol_type ("Var",x,_) = AtomV(x)
mengj@18439
   231
  | mk_fol_type ("Fixed",x,_) = AtomF(x)
mengj@18439
   232
  | mk_fol_type ("Comp",con,args) = Comp(con,args)
mengj@18439
   233
mengj@18439
   234
paulson@18798
   235
(*First string is the type class; the second is a TVar or TFfree*)
paulson@18798
   236
datatype type_literal = LTVar of string * string | LTFree of string * string;
paulson@15347
   237
paulson@18856
   238
datatype fol_term = UVar of string * fol_type
paulson@18856
   239
                 | Fun of string * fol_type list * fol_term list;
paulson@18856
   240
datatype predicate = Predicate of pred_name * fol_type list * fol_term list;
paulson@15347
   241
paulson@15347
   242
datatype literal = Literal of polarity * predicate * tag;
paulson@15347
   243
mengj@17999
   244
fun mk_typ_var_sort (TFree(a,s)) = (FOLTFree a,s)
mengj@17999
   245
  | mk_typ_var_sort (TVar(v,s)) = (FOLTVar v,s);
mengj@17999
   246
mengj@17999
   247
paulson@18856
   248
(*A clause has first-order literals and other, type-related literals*)
paulson@15347
   249
datatype clause = 
paulson@15347
   250
	 Clause of {clause_id: clause_id,
paulson@15347
   251
		    axiom_name: axiom_name,
paulson@19447
   252
		    th: thm,
paulson@15347
   253
		    kind: kind,
paulson@15347
   254
		    literals: literal list,
paulson@18869
   255
		    types_sorts: (typ_var * sort) list};
paulson@18869
   256
paulson@18869
   257
fun get_axiomName (Clause cls) = #axiom_name cls;
paulson@15347
   258
paulson@17404
   259
exception CLAUSE of string * term;
paulson@15347
   260
paulson@18856
   261
fun isFalse (Literal (pol,Predicate(pname,_,[]),_)) =
paulson@18856
   262
      (pol andalso pname = "c_False") orelse
paulson@18856
   263
      (not pol andalso pname = "c_True")
paulson@17317
   264
  | isFalse _ = false;
paulson@17317
   265
paulson@18856
   266
fun isTrue (Literal (pol,Predicate(pname,_,[]),_)) =
paulson@18856
   267
      (pol andalso pname = "c_True") orelse
paulson@18856
   268
      (not pol andalso pname = "c_False")
paulson@17404
   269
  | isTrue _ = false;
paulson@17404
   270
  
paulson@17404
   271
fun isTaut (Clause {literals,...}) = exists isTrue literals;  
paulson@17404
   272
paulson@19447
   273
fun make_clause (clause_id, axiom_name, th, kind, literals, types_sorts) =
paulson@17317
   274
  if forall isFalse literals 
paulson@17234
   275
  then error "Problem too trivial for resolution (empty clause)"
paulson@17234
   276
  else
paulson@19447
   277
     Clause {clause_id = clause_id, axiom_name = axiom_name, 
paulson@19447
   278
             th = th, kind = kind, 
paulson@18869
   279
             literals = literals, types_sorts = types_sorts};
paulson@17317
   280
paulson@17317
   281
paulson@18218
   282
(*Declarations of the current theory--to allow suppressing types.*)
paulson@18218
   283
val const_typargs = ref (Library.K [] : (string*typ -> typ list));
paulson@17317
   284
paulson@18218
   285
fun num_typargs(s,T) = if !keep_types then length (!const_typargs (s,T)) else 0;
paulson@16925
   286
paulson@16925
   287
(*Initialize the type suppression mechanism with the current theory before
paulson@16925
   288
    producing any clauses!*)
paulson@18218
   289
fun init thy = (const_typargs := Sign.const_typargs thy);
paulson@16925
   290
    
quigley@17150
   291
mengj@18402
   292
(*Flatten a type to a fol_type while accumulating sort constraints on the TFrees and
paulson@16925
   293
  TVars it contains.*)    
paulson@18218
   294
fun type_of (Type (a, Ts)) = 
paulson@18798
   295
      let val (folTyps, ts) = types_of Ts 
paulson@18218
   296
	  val t = make_fixed_type_const a
paulson@18798
   297
      in (Comp(t,folTyps), ts) end
paulson@18798
   298
  | type_of (TFree (a,s)) = (AtomF(make_fixed_type_var a), [(FOLTFree a, s)]) 
paulson@18798
   299
  | type_of (TVar (v, s)) = (AtomV(make_schematic_type_var v), [(FOLTVar v, s)])
paulson@18218
   300
and types_of Ts =
paulson@18798
   301
      let val (folTyps,ts) = ListPair.unzip (map type_of Ts)
paulson@18798
   302
      in (folTyps, union_all ts) end;
paulson@15390
   303
mengj@18439
   304
paulson@18218
   305
fun const_types_of (c,T) = types_of (!const_typargs (c,T));
paulson@16925
   306
paulson@16903
   307
(* Any variables created via the METAHYPS tactical should be treated as
paulson@16903
   308
   universal vars, although it is represented as "Free(...)" by Isabelle *)
paulson@16903
   309
val isMeta = String.isPrefix "METAHYP1_"
quigley@17150
   310
paulson@18798
   311
fun pred_name_type (Const(c,T)) = (make_fixed_const c, const_types_of (c,T))
paulson@15390
   312
  | pred_name_type (Free(x,T))  = 
paulson@17404
   313
      if isMeta x then raise CLAUSE("Predicate Not First Order 1", Free(x,T)) 
paulson@18798
   314
      else (make_fixed_var x, ([],[]))
paulson@17404
   315
  | pred_name_type (v as Var _) = raise CLAUSE("Predicate Not First Order 2", v)
paulson@17404
   316
  | pred_name_type t        = raise CLAUSE("Predicate input unexpected", t);
paulson@15347
   317
paulson@15615
   318
paulson@18218
   319
(* For typed equality *)
paulson@15615
   320
(* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
paulson@15615
   321
(* Find type of equality arg *)
paulson@15615
   322
fun eq_arg_type (Type("fun",[T,_])) = 
paulson@15615
   323
    let val (folT,_) = type_of T;
paulson@17230
   324
    in  folT  end;
paulson@15347
   325
paulson@18798
   326
fun fun_name_type (Const(c,T)) args = (make_fixed_const c, const_types_of (c,T))
paulson@18798
   327
  | fun_name_type (Free(x,T)) args  = 
paulson@18798
   328
       if isMeta x then raise CLAUSE("Function Not First Order", Free(x,T))
paulson@18798
   329
       else (make_fixed_var x, ([],[]))
paulson@17404
   330
  | fun_name_type f args = raise CLAUSE("Function Not First Order 1", f);
quigley@17150
   331
paulson@18856
   332
(*Convert a term to a fol_term while accumulating sort constraints on the TFrees and
paulson@18856
   333
  TVars it contains.*)    
paulson@15347
   334
fun term_of (Var(ind_nm,T)) = 
paulson@18798
   335
      let val (folType,ts) = type_of T
paulson@18798
   336
      in (UVar(make_schematic_var ind_nm, folType), ts) end
paulson@15347
   337
  | term_of (Free(x,T)) = 
paulson@18798
   338
      let val (folType, ts) = type_of T
paulson@17230
   339
      in
paulson@18798
   340
	  if isMeta x then (UVar(make_schematic_var(x,0),folType), ts)
paulson@18798
   341
	  else (Fun(make_fixed_var x, [folType], []), ts)
paulson@17230
   342
      end
paulson@18218
   343
  | term_of app = 
paulson@17230
   344
      let val (f,args) = strip_comb app
paulson@18798
   345
	  val (funName,(contys,ts1)) = fun_name_type f args
paulson@18798
   346
	  val (args',ts2) = terms_of args
paulson@17230
   347
      in
paulson@18868
   348
	  (Fun(funName,contys,args'), union_all (ts1::ts2))
paulson@17230
   349
      end
paulson@18798
   350
and terms_of ts = ListPair.unzip (map term_of ts)
paulson@15390
   351
paulson@18856
   352
(*Create a predicate value, again accumulating sort constraints.*)    
paulson@17404
   353
fun pred_of (Const("op =", typ), args) =
paulson@17404
   354
      let val arg_typ = eq_arg_type typ 
paulson@18798
   355
	  val (args',ts) = terms_of args
paulson@17404
   356
	  val equal_name = make_fixed_const "op ="
paulson@17404
   357
      in
paulson@18218
   358
	  (Predicate(equal_name,[arg_typ],args'),
paulson@18856
   359
	   union_all ts)
paulson@17404
   360
      end
paulson@17404
   361
  | pred_of (pred,args) = 
paulson@18856
   362
      let val (pname, (predType,ts1)) = pred_name_type pred
paulson@18798
   363
	  val (args',ts2) = terms_of args
paulson@17404
   364
      in
paulson@18856
   365
	  (Predicate(pname,predType,args'), union_all (ts1::ts2))
paulson@17404
   366
      end;
paulson@15347
   367
paulson@17404
   368
(*Treatment of literals, possibly negated or tagged*)
paulson@17404
   369
fun predicate_of ((Const("Not",_) $ P), polarity, tag) =
paulson@17404
   370
      predicate_of (P, not polarity, tag)
paulson@17404
   371
  | predicate_of ((Const("HOL.tag",_) $ P), polarity, tag) =
paulson@17404
   372
      predicate_of (P, polarity, true)
paulson@17404
   373
  | predicate_of (term,polarity,tag) =
paulson@17404
   374
        (pred_of (strip_comb term), polarity, tag);
paulson@15347
   375
paulson@17888
   376
fun literals_of_term1 args (Const("Trueprop",_) $ P) = literals_of_term1 args P
paulson@18856
   377
  | literals_of_term1 args (Const("op |",_) $ P $ Q) = 
paulson@18856
   378
      literals_of_term1 (literals_of_term1 args P) Q
paulson@18856
   379
  | literals_of_term1 (lits, ts) P =
paulson@18856
   380
      let val ((pred, ts'), polarity, tag) = predicate_of (P,true,false)
paulson@18856
   381
	  val lits' = Literal(polarity,pred,tag) :: lits
paulson@17234
   382
      in
paulson@18856
   383
	  (lits', ts union ts')
paulson@17234
   384
      end;
quigley@17150
   385
paulson@18856
   386
val literals_of_term = literals_of_term1 ([],[]);
quigley@17150
   387
mengj@18403
   388
mengj@18403
   389
fun list_ord _ ([],[]) = EQUAL
mengj@18403
   390
  | list_ord _ ([],_) = LESS
mengj@18403
   391
  | list_ord _ (_,[]) = GREATER
paulson@19207
   392
  | list_ord ord (x::xs, y::ys) = 
paulson@19207
   393
      (case ord(x,y) of EQUAL => list_ord ord (xs,ys)
paulson@19207
   394
	 	      | xy_ord => xy_ord);
paulson@19207
   395
		     
mengj@18403
   396
fun type_ord (AtomV(_),AtomV(_)) = EQUAL
mengj@18403
   397
  | type_ord (AtomV(_),_) = LESS
mengj@18403
   398
  | type_ord (AtomF(_),AtomV(_)) = GREATER
mengj@18403
   399
  | type_ord (AtomF(f1),AtomF(f2)) = string_ord (f1,f2)
mengj@18403
   400
  | type_ord (AtomF(_),_) = LESS
mengj@18403
   401
  | type_ord (Comp(_,_),AtomV(_)) = GREATER
mengj@18403
   402
  | type_ord (Comp(_,_),AtomF(_)) = GREATER
mengj@18403
   403
  | type_ord (Comp(con1,args1),Comp(con2,args2)) = 
paulson@19207
   404
      (case string_ord(con1,con2) of EQUAL => types_ord (args1,args2)
paulson@19207
   405
		      | con_ord => con_ord)
mengj@18403
   406
and
paulson@18920
   407
    types_ord ([],[]) = EQUAL
mengj@18403
   408
  | types_ord (tps1,tps2) = list_ord type_ord (tps1,tps2);
mengj@18403
   409
mengj@18402
   410
paulson@18920
   411
fun term_ord (UVar _, UVar _) = EQUAL
paulson@18920
   412
  | term_ord (UVar _, _) = LESS
paulson@18920
   413
  | term_ord (Fun _, UVar _) = GREATER
mengj@18403
   414
  | term_ord (Fun(f1,tps1,tms1),Fun(f2,tps2,tms2)) = 
paulson@18420
   415
     (case string_ord (f1,f2) of
paulson@18420
   416
         EQUAL => 
paulson@18420
   417
	   (case terms_ord (tms1,tms2) of EQUAL => types_ord (tps1,tps2)
paulson@18420
   418
	      | tms_ord => tms_ord)
paulson@18420
   419
       | fn_ord => fn_ord)
mengj@18403
   420
mengj@18403
   421
and
paulson@18920
   422
      terms_ord ([],[]) = EQUAL
paulson@18420
   423
    | terms_ord (tms1,tms2) = list_ord term_ord (tms1,tms2);
mengj@18403
   424
mengj@18403
   425
mengj@18403
   426
paulson@18856
   427
fun predicate_ord (Predicate(pname1,ftyps1,ftms1),Predicate(pname2,ftyps2,ftms2)) = 
paulson@18856
   428
  case string_ord (pname1,pname2) of
paulson@18856
   429
       EQUAL => (case terms_ord(ftms1,ftms2) of EQUAL => types_ord(ftyps1,ftyps2)
paulson@18856
   430
				              | ftms_ord => ftms_ord)
paulson@18856
   431
     | pname_ord => pname_ord
paulson@18856
   432
			   
mengj@18402
   433
mengj@18402
   434
fun literal_ord (Literal(false,_,_),Literal(true,_,_)) = LESS
mengj@18402
   435
  | literal_ord (Literal(true,_,_),Literal(false,_,_)) = GREATER
mengj@18402
   436
  | literal_ord (Literal(_,pred1,_),Literal(_,pred2,_)) = predicate_ord(pred1,pred2);
mengj@18402
   437
mengj@18402
   438
fun sort_lits lits = sort literal_ord lits;
mengj@18402
   439
paulson@18420
   440
mengj@18409
   441
(********** clause equivalence ******************)
mengj@18409
   442
mengj@18409
   443
fun check_var_pairs (x,y) [] = 0 
mengj@18409
   444
  | check_var_pairs (x,y) ((u,v)::w) =
mengj@18409
   445
    if (x,y) = (u,v) then 1 
mengj@18409
   446
    else
paulson@19176
   447
	if x=u orelse y=v then 2 (*conflict*)
mengj@18409
   448
	else check_var_pairs (x,y) w;
mengj@18409
   449
mengj@18409
   450
fun type_eq (AtomV(v1),AtomV(v2)) (vars,tvars) =
mengj@18409
   451
    (case check_var_pairs (v1,v2) tvars of 0 => (true,(vars,(v1,v2)::tvars))
mengj@18409
   452
					 | 1 => (true,(vars,tvars))
mengj@18409
   453
					 | 2 => (false,(vars,tvars)))
mengj@18409
   454
  | type_eq (AtomV(_),_) vtvars = (false,vtvars)
mengj@18409
   455
  | type_eq (AtomF(f1),AtomF(f2)) vtvars = (f1=f2,vtvars)
mengj@18409
   456
  | type_eq (AtomF(_),_) vtvars = (false,vtvars)
mengj@18409
   457
  | type_eq (Comp(con1,args1),Comp(con2,args2)) vtvars =
paulson@18420
   458
      let val (eq1,vtvars1) = 
paulson@18420
   459
	      if con1 = con2 then types_eq (args1,args2) vtvars
paulson@18420
   460
	      else (false,vtvars)
paulson@18420
   461
      in
paulson@18420
   462
	  (eq1,vtvars1)
paulson@18420
   463
      end
mengj@18409
   464
  | type_eq (Comp(_,_),_) vtvars = (false,vtvars)
mengj@18409
   465
paulson@19176
   466
and types_eq ([],[]) vtvars = (true,vtvars)
paulson@18420
   467
  | types_eq (tp1::tps1,tp2::tps2) vtvars =
paulson@18420
   468
      let val (eq1,vtvars1) = type_eq (tp1,tp2) vtvars
paulson@18420
   469
	  val (eq2,vtvars2) = if eq1 then types_eq (tps1,tps2) vtvars1
paulson@18420
   470
			      else (eq1,vtvars1)
paulson@18420
   471
      in
paulson@18420
   472
	  (eq2,vtvars2)
paulson@18420
   473
      end;
mengj@18409
   474
mengj@18409
   475
mengj@18409
   476
fun term_eq (UVar(v1,tp1),UVar(v2,tp2)) (vars,tvars) =
mengj@18409
   477
    (case check_var_pairs (v1,v2) vars of 0 => type_eq (tp1,tp2) (((v1,v2)::vars),tvars)
mengj@18409
   478
					| 1 => type_eq (tp1,tp2) (vars,tvars)
mengj@18409
   479
					| 2 => (false,(vars,tvars)))
paulson@18920
   480
  | term_eq (UVar _,_) vtvars = (false,vtvars)
mengj@18409
   481
  | term_eq (Fun(f1,tps1,tms1),Fun(f2,tps2,tms2)) vtvars =
paulson@18420
   482
      let val (eq1,vtvars1) = 
paulson@18420
   483
	      if f1 = f2 then terms_eq (tms1,tms2) vtvars
paulson@18420
   484
	      else (false,vtvars)
paulson@18420
   485
	  val (eq2,vtvars2) =
paulson@18420
   486
	      if eq1 then types_eq (tps1,tps2) vtvars1
paulson@18420
   487
	      else (eq1,vtvars1)
paulson@18420
   488
      in
paulson@18420
   489
	  (eq2,vtvars2)
paulson@18420
   490
      end
mengj@18409
   491
  | term_eq (Fun(_,_,_),_) vtvars = (false,vtvars)
mengj@18409
   492
paulson@19176
   493
and terms_eq ([],[]) vtvars = (true,vtvars)
paulson@18420
   494
  | terms_eq (tm1::tms1,tm2::tms2) vtvars =
paulson@18420
   495
      let val (eq1,vtvars1) = term_eq (tm1,tm2) vtvars
paulson@18420
   496
	  val (eq2,vtvars2) = if eq1 then terms_eq (tms1,tms2) vtvars1
paulson@18420
   497
				     else (eq1,vtvars1)
paulson@18420
   498
      in
paulson@18420
   499
	  (eq2,vtvars2)
paulson@18420
   500
      end;
mengj@18409
   501
					     
mengj@18409
   502
paulson@18856
   503
fun pred_eq (Predicate(pname1,tps1,tms1),Predicate(pname2,tps2,tms2)) vtvars =
mengj@18409
   504
    let val (eq1,vtvars1) = 
paulson@19176
   505
	    if pname1 = pname2 then terms_eq (tms1,tms2) vtvars
mengj@18409
   506
	    else (false,vtvars)
mengj@18409
   507
	val (eq2,vtvars2) = 
mengj@18409
   508
	    if eq1 then types_eq (tps1,tps2) vtvars1
mengj@18409
   509
	    else (eq1,vtvars1)
mengj@18409
   510
    in
mengj@18409
   511
	(eq2,vtvars2)
mengj@18409
   512
    end;
mengj@18409
   513
					      
mengj@18409
   514
mengj@18409
   515
fun lit_eq (Literal(pol1,pred1,_),Literal(pol2,pred2,_)) vtvars =
mengj@18409
   516
    if (pol1 = pol2) then pred_eq (pred1,pred2) vtvars
mengj@18409
   517
    else (false,vtvars);
mengj@18409
   518
mengj@18409
   519
fun lits_eq ([],[]) vtvars = (true,vtvars)
mengj@18409
   520
  | lits_eq (l1::ls1,l2::ls2) vtvars = 
paulson@19176
   521
      let val (eq1,vtvars1) = lit_eq (l1,l2) vtvars
paulson@19176
   522
      in
paulson@19176
   523
	  if eq1 then lits_eq (ls1,ls2) vtvars1
paulson@19176
   524
	  else (false,vtvars1)
paulson@19176
   525
      end
paulson@19176
   526
  | lits_eq _ vtvars = (false,vtvars);
mengj@18409
   527
paulson@18420
   528
(*Equality of two clauses up to variable renaming*)
paulson@18798
   529
fun clause_eq (Clause{literals=lits1,...}, Clause{literals=lits2,...}) =
paulson@19176
   530
  #1 (lits_eq (lits1,lits2) ([],[]));
mengj@18409
   531
mengj@18409
   532
paulson@18420
   533
(*** Hash function for clauses ***)
paulson@18420
   534
paulson@18420
   535
val xor_words = List.foldl Word.xorb 0w0;
paulson@18420
   536
paulson@18920
   537
fun hashw_term (UVar _, w) = w
paulson@18449
   538
  | hashw_term (Fun(f,tps,args), w) = 
paulson@18449
   539
      List.foldl hashw_term (Polyhash.hashw_string (f,w)) args;
paulson@18420
   540
  
paulson@18449
   541
fun hashw_pred (Predicate(pn,_,args), w) = 
paulson@18449
   542
    List.foldl hashw_term (Polyhash.hashw_string (pn,w)) args;
paulson@18420
   543
    
paulson@18449
   544
fun hash1_literal (Literal(true,pred,_)) = hashw_pred (pred, 0w0)
paulson@18449
   545
  | hash1_literal (Literal(false,pred,_)) = Word.notb (hashw_pred (pred, 0w0));
paulson@18420
   546
  
paulson@18798
   547
fun hash_clause (Clause{literals,...}) =
paulson@18798
   548
  Word.toIntX (xor_words (map hash1_literal literals));
mengj@18402
   549
mengj@18402
   550
paulson@18798
   551
(*Make literals for sorted type variables.  FIXME: can it use map?*) 
quigley@17150
   552
fun sorts_on_typs (_, [])   = ([]) 
paulson@16199
   553
  | sorts_on_typs (v, "HOL.type" :: s) =
paulson@18411
   554
      sorts_on_typs (v,s)                (*IGNORE sort "type"*)
paulson@18798
   555
  | sorts_on_typs ((FOLTVar indx), s::ss) =
paulson@18798
   556
      LTVar(make_type_class s, make_schematic_type_var indx) :: 
paulson@18798
   557
      sorts_on_typs ((FOLTVar indx), ss)
paulson@18798
   558
  | sorts_on_typs ((FOLTFree x), s::ss) =
paulson@18798
   559
      LTFree(make_type_class s, make_fixed_type_var x) :: 
paulson@18798
   560
      sorts_on_typs ((FOLTFree x), ss);
paulson@15347
   561
quigley@17150
   562
paulson@18798
   563
fun pred_of_sort (LTVar (s,ty)) = (s,1)
paulson@18798
   564
|   pred_of_sort (LTFree (s,ty)) = (s,1)
quigley@17150
   565
paulson@16199
   566
(*Given a list of sorted type variables, return two separate lists.
paulson@16199
   567
  The first is for TVars, the second for TFrees.*)
paulson@18856
   568
fun add_typs_aux [] = ([],[])
paulson@18856
   569
  | add_typs_aux ((FOLTVar indx,s)::tss) = 
paulson@17230
   570
      let val vs = sorts_on_typs (FOLTVar indx, s)
paulson@18856
   571
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   572
      in
paulson@18856
   573
	  (vs union vss, fss)
quigley@17150
   574
      end
paulson@18856
   575
  | add_typs_aux ((FOLTFree x,s)::tss) =
paulson@17230
   576
      let val fs = sorts_on_typs (FOLTFree x, s)
paulson@18856
   577
	  val (vss,fss) = add_typs_aux tss
quigley@17150
   578
      in
paulson@18856
   579
	  (vss, fs union fss)
quigley@17150
   580
      end;
quigley@17150
   581
mengj@17999
   582
paulson@18869
   583
(** make axiom and conjecture clauses. **)
quigley@17150
   584
quigley@17150
   585
fun get_tvar_strs [] = []
paulson@17230
   586
  | get_tvar_strs ((FOLTVar indx,s)::tss) = 
paulson@18920
   587
      (make_schematic_type_var indx) ins (get_tvar_strs tss)
paulson@18920
   588
  | get_tvar_strs((FOLTFree x,s)::tss) = get_tvar_strs tss
paulson@15347
   589
paulson@18869
   590
(* check if a clause is first-order before making a conjecture clause*)
mengj@19443
   591
fun make_conjecture_clause n thm =
mengj@19443
   592
    let val t = prop_of thm
mengj@19443
   593
	val _ = check_is_fol_term t
mengj@18199
   594
	    handle TERM("check_is_fol_term",_) => raise CLAUSE("Goal is not FOL",t)
paulson@18856
   595
	val (lits,types_sorts) = literals_of_term t
quigley@17150
   596
    in
paulson@19447
   597
	make_clause(n, "conjecture", thm, Conjecture, lits, types_sorts)
quigley@17150
   598
    end;
paulson@17845
   599
    
paulson@17845
   600
fun make_conjecture_clauses_aux _ [] = []
paulson@17888
   601
  | make_conjecture_clauses_aux n (t::ts) =
paulson@17888
   602
      make_conjecture_clause n t :: make_conjecture_clauses_aux (n+1) ts
paulson@17845
   603
paulson@17845
   604
val make_conjecture_clauses = make_conjecture_clauses_aux 0
quigley@17150
   605
paulson@18920
   606
(** Too general means, positive equality literal with a variable X as one operand,
paulson@18920
   607
  when X does not occur properly in the other operand. This rules out clearly
paulson@18920
   608
  inconsistent clauses such as V=a|V=b, though it by no means guarantees soundness. **)
paulson@18920
   609
paulson@18920
   610
fun occurs a (UVar(b,_)) = a=b
paulson@18920
   611
  | occurs a (Fun (_,_,ts)) = exists (occurs a) ts
paulson@18920
   612
paulson@18920
   613
(*Is the first operand a variable that does not properly occur in the second operand?*)
paulson@18920
   614
fun too_general_terms (UVar _, UVar _) = false
paulson@18920
   615
  | too_general_terms (Fun _, _) = false
paulson@18920
   616
  | too_general_terms (UVar (a,_), t) = not (occurs a t);
paulson@18920
   617
paulson@18920
   618
fun too_general_lit (Literal (true,Predicate("equal",_,[x,y]),_)) =
paulson@18920
   619
      too_general_terms (x,y) orelse too_general_terms(y,x)
paulson@18920
   620
  | too_general_lit _ = false;
quigley@17150
   621
mengj@18199
   622
(*before converting an axiom clause to "clause" format, check if it is FOL*)
mengj@19443
   623
fun make_axiom_clause thm (ax_name,cls_id) =
mengj@19443
   624
    let val term = prop_of thm
mengj@19443
   625
	val (lits,types_sorts) = literals_of_term term
paulson@15347
   626
    in 
paulson@19207
   627
	if not (Meson.is_fol_term term) then
paulson@19207
   628
	   (Output.debug ("Omitting " ^ ax_name ^ ": Axiom is not FOL"); 
paulson@19207
   629
	    NONE)
paulson@19207
   630
	else if forall too_general_lit lits then
paulson@18920
   631
	   (Output.debug ("Omitting " ^ ax_name ^ ": equalities are too general"); 
paulson@18920
   632
	    NONE)
paulson@19447
   633
	else SOME (make_clause(cls_id, ax_name, thm, Axiom, sort_lits lits, types_sorts))
paulson@19207
   634
    end
paulson@19207
   635
    handle CLAUSE _ => NONE;
paulson@15347
   636
paulson@15347
   637
mengj@19443
   638
fun make_axiom_clauses [] = []
mengj@19443
   639
  | make_axiom_clauses ((thm,(name,id))::thms) =
mengj@19443
   640
    case make_axiom_clause thm (name,id) of SOME cls => if isTaut cls then make_axiom_clauses thms else cls :: make_axiom_clauses thms
mengj@19443
   641
						    | NONE => make_axiom_clauses thms;
mengj@19354
   642
paulson@15347
   643
(**** Isabelle arities ****)
paulson@15347
   644
paulson@15347
   645
exception ARCLAUSE of string;
paulson@15347
   646
 
paulson@15347
   647
type class = string; 
paulson@15347
   648
type tcons = string; 
paulson@15347
   649
paulson@18868
   650
datatype arLit = TConsLit of bool * (class * tcons * string list)
paulson@18868
   651
               | TVarLit of bool * (class * string);
paulson@15347
   652
 
paulson@15347
   653
datatype arityClause =  
paulson@15347
   654
	 ArityClause of {clause_id: clause_id,
paulson@17845
   655
	  	         axiom_name: axiom_name,
paulson@15347
   656
			 kind: kind,
paulson@15347
   657
			 conclLit: arLit,
paulson@15347
   658
			 premLits: arLit list};
paulson@15347
   659
paulson@15347
   660
paulson@18798
   661
fun gen_TVars 0 = []
paulson@18798
   662
  | gen_TVars n = ("T_" ^ Int.toString n) :: gen_TVars (n-1);
paulson@15347
   663
paulson@18411
   664
fun pack_sort(_,[])  = []
paulson@18411
   665
  | pack_sort(tvar, "HOL.type"::srt) = pack_sort(tvar, srt)   (*IGNORE sort "type"*)
paulson@18411
   666
  | pack_sort(tvar, cls::srt) =  (make_type_class cls, tvar) :: pack_sort(tvar, srt);
paulson@15347
   667
    
paulson@18868
   668
fun make_TVarLit (b, (cls,str)) = TVarLit(b, (cls,str));
paulson@18868
   669
fun make_TConsLit (b, (cls,tcons,tvars)) = 
paulson@18868
   670
      TConsLit(b, (make_type_class cls, make_fixed_type_const tcons, tvars));
paulson@15347
   671
paulson@18411
   672
(*Arity of type constructor tcon :: (arg1,...,argN)res*)
paulson@18411
   673
fun make_axiom_arity_clause (tcons, n, (res,args)) =
paulson@17845
   674
   let val nargs = length args
paulson@18798
   675
       val tvars = gen_TVars nargs
paulson@17845
   676
       val tvars_srts = ListPair.zip (tvars,args)
paulson@17845
   677
       val tvars_srts' = union_all(map pack_sort tvars_srts)
paulson@17845
   678
       val false_tvars_srts' = map (pair false) tvars_srts'
paulson@17845
   679
   in
paulson@17845
   680
      ArityClause {clause_id = n, kind = Axiom, 
paulson@18411
   681
                   axiom_name = lookup_type_const tcons,
paulson@18411
   682
                   conclLit = make_TConsLit(true, (res,tcons,tvars)), 
paulson@17845
   683
                   premLits = map make_TVarLit false_tvars_srts'}
paulson@17845
   684
   end;
paulson@15347
   685
paulson@15347
   686
paulson@15347
   687
(**** Isabelle class relations ****)
paulson@15347
   688
paulson@15347
   689
datatype classrelClause = 
paulson@18868
   690
	 ClassrelClause of {axiom_name: axiom_name,
paulson@15347
   691
			    subclass: class,
paulson@18411
   692
			    superclass: class};
paulson@17845
   693
paulson@17845
   694
fun make_axiom_classrelClause n subclass superclass =
paulson@18868
   695
  ClassrelClause {axiom_name = clrelclause_prefix ^ ascii_of subclass ^ 
paulson@18868
   696
                                "_" ^ Int.toString n,
paulson@18868
   697
                  subclass = make_type_class subclass, 
paulson@18868
   698
                  superclass = make_type_class superclass};
paulson@15347
   699
paulson@17845
   700
fun classrelClauses_of_aux n sub [] = []
paulson@18411
   701
  | classrelClauses_of_aux n sub ("HOL.type"::sups) = (*Should be ignored*)
paulson@18411
   702
      classrelClauses_of_aux n sub sups
paulson@17845
   703
  | classrelClauses_of_aux n sub (sup::sups) =
paulson@18868
   704
      make_axiom_classrelClause n sub sup :: classrelClauses_of_aux (n+1) sub sups;
paulson@15347
   705
paulson@18411
   706
fun classrelClauses_of (sub,sups) = classrelClauses_of_aux 0 sub sups;
paulson@17845
   707
wenzelm@19642
   708
val classrel_clauses_thy =
wenzelm@19642
   709
  maps classrelClauses_of o Graph.dest o #classes o Sorts.rep_algebra o Sign.classes_of;
paulson@18868
   710
paulson@18868
   711
paulson@18868
   712
(** Isabelle arities **)
paulson@17845
   713
paulson@17845
   714
fun arity_clause _ (tcons, []) = []
paulson@19155
   715
  | arity_clause n (tcons, ("HOL.type",_)::ars) =  (*ignore*)
paulson@18411
   716
      arity_clause n (tcons,ars)
paulson@17845
   717
  | arity_clause n (tcons, ar::ars) =
paulson@17845
   718
      make_axiom_arity_clause (tcons,n,ar) :: 
paulson@17845
   719
      arity_clause (n+1) (tcons,ars);
paulson@17845
   720
paulson@17845
   721
fun multi_arity_clause [] = []
paulson@19155
   722
  | multi_arity_clause ((tcons,ars) :: tc_arlists) =
paulson@19155
   723
      (*Reversal ensures that older entries always get the same axiom name*)
paulson@19155
   724
      arity_clause 0 (tcons, rev ars)  @  
paulson@19155
   725
      multi_arity_clause tc_arlists 
paulson@17845
   726
paulson@17845
   727
fun arity_clause_thy thy =
wenzelm@19642
   728
  let val arities = thy |> Sign.classes_of
wenzelm@19642
   729
    |> Sorts.rep_algebra |> #arities |> Symtab.dest
wenzelm@19642
   730
    |> map (apsnd (map (fn (c, (_, Ss)) => (c, Ss))));
wenzelm@19521
   731
  in multi_arity_clause (rev arities) end;
paulson@17845
   732
paulson@17845
   733
paulson@18868
   734
(**** Find occurrences of predicates in clauses ****)
paulson@18868
   735
paulson@18868
   736
(*FIXME: multiple-arity checking doesn't work, as update_new is the wrong 
paulson@18868
   737
  function (it flags repeated declarations of a function, even with the same arity)*)
paulson@18868
   738
paulson@18868
   739
fun update_many (tab, keypairs) = foldl (uncurry Symtab.update) tab keypairs;
paulson@18868
   740
paulson@18868
   741
fun add_predicate_preds (Predicate(pname,tys,tms), preds) = 
paulson@18868
   742
  if pname = "equal" then preds (*equality is built-in and requires no declaration*)
paulson@18868
   743
  else Symtab.update (pname, length tys + length tms) preds
paulson@18868
   744
paulson@18868
   745
fun add_literal_preds (Literal(_,pred,_), preds) = add_predicate_preds (pred,preds)
paulson@18868
   746
paulson@18868
   747
fun add_type_sort_preds ((FOLTVar indx,s), preds) = 
paulson@18868
   748
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTVar indx, s)))
paulson@18868
   749
  | add_type_sort_preds ((FOLTFree x,s), preds) =
paulson@18868
   750
      update_many (preds, map pred_of_sort (sorts_on_typs (FOLTFree x, s)));
paulson@17845
   751
paulson@18868
   752
fun add_clause_preds (Clause {literals, types_sorts, ...}, preds) =
paulson@18868
   753
  foldl add_literal_preds (foldl add_type_sort_preds preds types_sorts) literals
paulson@18868
   754
  handle Symtab.DUP a => raise ERROR ("predicate " ^ a ^ " has multiple arities")
paulson@18868
   755
paulson@18868
   756
fun add_classrelClause_preds (ClassrelClause {subclass,superclass,...}, preds) =
paulson@18868
   757
  Symtab.update (subclass,1) (Symtab.update (superclass,1) preds);
paulson@17845
   758
paulson@18868
   759
fun add_arityClause_preds (ArityClause {conclLit,...}, preds) =
paulson@18868
   760
  let val TConsLit(_, (tclass, _, _)) = conclLit
paulson@18868
   761
  in  Symtab.update (tclass,1) preds  end;
paulson@18868
   762
paulson@18868
   763
fun preds_of_clauses clauses clsrel_clauses arity_clauses = 
paulson@18868
   764
  Symtab.dest
paulson@18868
   765
    (foldl add_classrelClause_preds 
paulson@18868
   766
      (foldl add_arityClause_preds
paulson@18868
   767
        (foldl add_clause_preds Symtab.empty clauses)
paulson@18868
   768
        arity_clauses)
paulson@18868
   769
      clsrel_clauses)
paulson@18798
   770
paulson@18868
   771
(*** Find occurrences of functions in clauses ***)
paulson@18868
   772
paulson@18868
   773
fun add_foltype_funcs (AtomV _, funcs) = funcs
paulson@18868
   774
  | add_foltype_funcs (AtomF a, funcs) = Symtab.update (a,0) funcs
paulson@18868
   775
  | add_foltype_funcs (Comp(a,tys), funcs) = 
paulson@18868
   776
      foldl add_foltype_funcs (Symtab.update (a, length tys) funcs) tys;
paulson@18868
   777
paulson@18868
   778
fun add_folterm_funcs (UVar _, funcs) = funcs
paulson@18868
   779
  | add_folterm_funcs (Fun(a,tys,[]), funcs) = Symtab.update (a,0) funcs
paulson@18868
   780
      (*A constant is a special case: it has no type argument even if overloaded*)
paulson@18868
   781
  | add_folterm_funcs (Fun(a,tys,tms), funcs) = 
paulson@18868
   782
      foldl add_foltype_funcs 
paulson@18868
   783
	    (foldl add_folterm_funcs (Symtab.update (a, length tys + length tms) funcs) 
paulson@18868
   784
	           tms) 
paulson@18868
   785
	    tys
paulson@18798
   786
paulson@18868
   787
fun add_predicate_funcs (Predicate(_,tys,tms), funcs) = 
paulson@18868
   788
    foldl add_foltype_funcs (foldl add_folterm_funcs funcs tms) tys;
paulson@18868
   789
paulson@18868
   790
fun add_literal_funcs (Literal(_,pred,_), funcs) = add_predicate_funcs (pred,funcs)
paulson@18868
   791
paulson@18868
   792
fun add_arityClause_funcs (ArityClause {conclLit,...}, funcs) =
paulson@18868
   793
  let val TConsLit(_, (_, tcons, tvars)) = conclLit
paulson@18868
   794
  in  Symtab.update (tcons, length tvars) funcs  end;
paulson@17845
   795
paulson@18868
   796
fun add_clause_funcs (Clause {literals, ...}, funcs) =
paulson@18868
   797
  foldl add_literal_funcs funcs literals
paulson@18868
   798
  handle Symtab.DUP a => raise ERROR ("function " ^ a ^ " has multiple arities")
paulson@18868
   799
paulson@18868
   800
fun funcs_of_clauses clauses arity_clauses = 
paulson@18868
   801
  Symtab.dest (foldl add_arityClause_funcs 
paulson@18868
   802
                     (foldl add_clause_funcs Symtab.empty clauses)
paulson@18868
   803
                     arity_clauses)
paulson@18868
   804
paulson@18868
   805
paulson@18868
   806
(**** String-oriented operations ****)
paulson@15347
   807
paulson@15347
   808
fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
paulson@15347
   809
paulson@18218
   810
(*Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed 
paulson@18218
   811
 and if we specifically ask for types to be included.   *)
paulson@15347
   812
fun string_of_equality (typ,terms) =
paulson@17230
   813
      let val [tstr1,tstr2] = map string_of_term terms
mengj@18402
   814
	  val typ' = string_of_fol_type typ
paulson@17230
   815
      in
paulson@17230
   816
	  if !keep_types andalso !special_equal 
mengj@18402
   817
	  then "equal(" ^ (wrap_eq_type typ' tstr1) ^ "," ^ 
mengj@18402
   818
		 	  (wrap_eq_type typ' tstr2) ^ ")"
paulson@17230
   819
	  else "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
paulson@17230
   820
      end
paulson@17230
   821
and string_of_term (UVar(x,_)) = x
paulson@18218
   822
  | string_of_term (Fun("equal",[typ],terms)) = string_of_equality(typ,terms)
paulson@18218
   823
  | string_of_term (Fun (name,typs,[])) = name (*Overloaded consts like 0 don't get types!*)
paulson@18218
   824
  | string_of_term (Fun (name,typs,terms)) = 
paulson@18218
   825
      let val terms_as_strings = map string_of_term terms
mengj@18402
   826
	  val typs' = if !keep_types then map string_of_fol_type typs else []
paulson@18420
   827
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
paulson@15347
   828
paulson@15347
   829
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@18218
   830
fun string_of_predicate (Predicate("equal",[typ],terms)) = string_of_equality(typ,terms)
paulson@18218
   831
  | string_of_predicate (Predicate(name,typs,terms)) = 
paulson@17230
   832
      let val terms_as_strings = map string_of_term terms
mengj@18402
   833
	  val typs' = if !keep_types then map string_of_fol_type typs else []
paulson@18420
   834
      in  name ^ (paren_pack (terms_as_strings @ typs'))  end;
paulson@17317
   835
paulson@17317
   836
fun string_of_clausename (cls_id,ax_name) = 
paulson@17525
   837
    clause_prefix ^ ascii_of ax_name ^ "_" ^ Int.toString cls_id;
paulson@17317
   838
paulson@17317
   839
fun string_of_type_clsname (cls_id,ax_name,idx) = 
paulson@17525
   840
    string_of_clausename (cls_id,ax_name) ^ "_tcs" ^ (Int.toString idx);
paulson@18863
   841
paulson@18863
   842
(*Write a list of strings to a file*)
paulson@18863
   843
fun writeln_strs os = List.app (fn s => TextIO.output (os,s));
paulson@18863
   844
quigley@17150
   845
    
paulson@18868
   846
(**** Producing DFG files ****)
quigley@17150
   847
paulson@18863
   848
(*Attach sign in DFG syntax: false means negate.*)
paulson@18863
   849
fun dfg_sign true s = s
paulson@18863
   850
  | dfg_sign false s = "not(" ^ s ^ ")"  
paulson@18863
   851
paulson@18863
   852
fun dfg_literal (Literal(pol,pred,tag)) = dfg_sign pol (string_of_predicate pred)
quigley@17150
   853
paulson@18798
   854
fun dfg_of_typeLit (LTVar (s,ty)) = "not(" ^ s ^ "(" ^ ty ^ "))"
paulson@18856
   855
  | dfg_of_typeLit (LTFree (s,ty)) = s ^ "(" ^ ty ^ ")";
quigley@17150
   856
 
paulson@18868
   857
(*Enclose the clause body by quantifiers, if necessary*)
paulson@18868
   858
fun dfg_forall [] body = body  
paulson@18868
   859
  | dfg_forall vars body = "forall([" ^ commas vars ^ "],\n" ^ body ^ ")"
quigley@17150
   860
paulson@18868
   861
fun gen_dfg_cls (cls_id, ax_name, knd, lits, vars) = 
paulson@18868
   862
    "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   863
    dfg_forall vars ("or(" ^ lits ^ ")") ^ ",\n" ^ 
paulson@18863
   864
    string_of_clausename (cls_id,ax_name) ^  ").\n\n";
quigley@17150
   865
paulson@18869
   866
fun dfg_clause_aux (Clause{literals, types_sorts, ...}) = 
paulson@18868
   867
  let val lits = map dfg_literal literals
paulson@18869
   868
      val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@17230
   869
      val tvar_lits_strs = 
paulson@18869
   870
	  if !keep_types then map dfg_of_typeLit tvar_lits else []
paulson@17230
   871
      val tfree_lits =
paulson@18869
   872
          if !keep_types then map dfg_of_typeLit tfree_lits else []
paulson@17230
   873
  in
paulson@17234
   874
      (tvar_lits_strs @ lits, tfree_lits)
paulson@17230
   875
  end; 
quigley@17150
   876
quigley@17150
   877
fun dfg_folterms (Literal(pol,pred,tag)) = 
paulson@18856
   878
  let val Predicate (_, _, folterms) = pred
paulson@18218
   879
  in  folterms  end
quigley@17150
   880
paulson@17404
   881
fun get_uvars (UVar(a,typ)) = [a] 
paulson@18868
   882
  | get_uvars (Fun (_,typ,tlist)) = union_all(map get_uvars tlist)
paulson@17404
   883
paulson@18868
   884
fun dfg_vars (Clause {literals,...}) =
paulson@18920
   885
  union_all (map get_uvars (List.concat (map dfg_folterms literals)))
quigley@17150
   886
paulson@18798
   887
fun clause2dfg (cls as Clause{axiom_name,clause_id,kind,types_sorts,...}) =
paulson@17230
   888
    let val (lits,tfree_lits) = dfg_clause_aux cls 
paulson@18798
   889
            (*"lits" includes the typing assumptions (TVars)*)
quigley@17150
   890
        val vars = dfg_vars cls
paulson@18798
   891
        val tvars = get_tvar_strs types_sorts
paulson@18798
   892
	val knd = name_of_kind kind
paulson@17234
   893
	val lits_str = commas lits
paulson@18868
   894
	val cls_str = gen_dfg_cls(clause_id, axiom_name, knd, lits_str, tvars@vars) 
paulson@18798
   895
    in (cls_str, tfree_lits) end;
quigley@17150
   896
paulson@18798
   897
fun string_of_arity (name, num) =  "(" ^ name ^ "," ^ Int.toString num ^ ")"
quigley@17150
   898
paulson@18856
   899
fun string_of_preds [] = ""
paulson@18856
   900
  | string_of_preds preds = "predicates[" ^ commas(map string_of_arity preds) ^ "].\n";
quigley@17150
   901
paulson@18856
   902
fun string_of_funcs [] = ""
paulson@18856
   903
  | string_of_funcs funcs = "functions[" ^ commas(map string_of_arity funcs) ^ "].\n" ;
quigley@17150
   904
paulson@17234
   905
fun string_of_symbols predstr funcstr = 
paulson@17234
   906
  "list_of_symbols.\n" ^ predstr  ^ funcstr  ^ "end_of_list.\n\n";
quigley@17150
   907
paulson@18798
   908
fun string_of_start name = "begin_problem(" ^ name ^ ").\n\n";
quigley@17150
   909
paulson@18863
   910
fun string_of_descrip name = 
paulson@18868
   911
  "list_of_descriptions.\nname({*" ^ name ^ 
paulson@18868
   912
  "*}).\nauthor({*Isabelle*}).\nstatus(unknown).\ndescription({*auto-generated*}).\nend_of_list.\n\n"
quigley@17150
   913
paulson@18863
   914
fun dfg_tfree_clause tfree_lit =
paulson@18863
   915
  "clause( %(conjecture)\n" ^ "or( " ^ tfree_lit ^ "),\n" ^ "tfree_tcs" ^ ").\n\n"
paulson@18863
   916
paulson@17845
   917
fun string_of_arClauseID (ArityClause {clause_id,axiom_name,...}) =
paulson@17845
   918
    arclause_prefix ^ ascii_of axiom_name ^ "_" ^ Int.toString clause_id;
quigley@17150
   919
paulson@18863
   920
fun dfg_of_arLit (TConsLit(pol,(c,t,args))) =
paulson@18863
   921
      dfg_sign pol (c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@18863
   922
  | dfg_of_arLit (TVarLit(pol,(c,str))) =
paulson@18863
   923
      dfg_sign pol (c ^ "(" ^ str ^ ")")
paulson@17525
   924
    
paulson@18863
   925
fun dfg_classrelLits sub sup = 
paulson@18863
   926
    let val tvar = "(T)"
paulson@18863
   927
    in 
paulson@18863
   928
	"not(" ^ sub ^ tvar ^ "), " ^ sup ^ tvar
paulson@18863
   929
    end;
paulson@17525
   930
paulson@18868
   931
fun dfg_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
   932
  "clause(forall([T],\nor( " ^ dfg_classrelLits subclass superclass ^ ")),\n" ^
paulson@18868
   933
  axiom_name ^ ").\n\n";
paulson@18868
   934
      
paulson@18868
   935
fun dfg_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
paulson@18868
   936
  let val arcls_id = string_of_arClauseID arcls
paulson@18868
   937
      val knd = name_of_kind kind
paulson@18868
   938
      val TConsLit(_, (_,_,tvars)) = conclLit
paulson@18868
   939
      val lits = map dfg_of_arLit (conclLit :: premLits)
paulson@18863
   940
  in
paulson@18868
   941
      "clause( %(" ^ knd ^ ")\n" ^ 
paulson@18868
   942
      dfg_forall tvars ("or( " ^ commas lits ^ ")") ^ ",\n" ^
paulson@18868
   943
      arcls_id ^ ").\n\n"
paulson@18863
   944
  end;
paulson@18863
   945
paulson@18863
   946
(* write out a subgoal in DFG format to the file "xxxx_N"*)
mengj@19719
   947
fun dfg_write_file thms filename (axclauses,classrel_clauses,arity_clauses) = 
paulson@18863
   948
  let 
paulson@19207
   949
    val _ = Output.debug ("Preparing to write the DFG file " ^ filename)
mengj@19719
   950
    val conjectures = make_conjecture_clauses thms
mengj@19719
   951
    val axclauses' = make_axiom_clauses axclauses
paulson@18868
   952
    val (dfg_clss, tfree_litss) = ListPair.unzip (map clause2dfg conjectures)
mengj@19719
   953
    val clss = conjectures @ axclauses'
paulson@18868
   954
    val funcs = funcs_of_clauses clss arity_clauses
paulson@18868
   955
    and preds = preds_of_clauses clss classrel_clauses arity_clauses
paulson@18863
   956
    and probname = Path.pack (Path.base (Path.unpack filename))
mengj@19719
   957
    val (axstrs, _) = ListPair.unzip (map clause2dfg axclauses')
paulson@18863
   958
    val tfree_clss = map dfg_tfree_clause (union_all tfree_litss) 
paulson@19155
   959
    val out = TextIO.openOut filename
paulson@18863
   960
  in
paulson@18868
   961
    TextIO.output (out, string_of_start probname); 
paulson@18868
   962
    TextIO.output (out, string_of_descrip probname); 
paulson@18868
   963
    TextIO.output (out, string_of_symbols (string_of_funcs funcs) (string_of_preds preds)); 
paulson@18868
   964
    TextIO.output (out, "list_of_clauses(axioms,cnf).\n");
paulson@18868
   965
    writeln_strs out axstrs;
paulson@18868
   966
    List.app (curry TextIO.output out o dfg_classrelClause) classrel_clauses;
paulson@18868
   967
    List.app (curry TextIO.output out o dfg_arity_clause) arity_clauses;
paulson@18868
   968
    TextIO.output (out, "end_of_list.\n\nlist_of_clauses(conjectures,cnf).\n");
paulson@18868
   969
    writeln_strs out tfree_clss;
paulson@18868
   970
    writeln_strs out dfg_clss;
paulson@18868
   971
    TextIO.output (out, "end_of_list.\n\nend_problem.\n");
paulson@18868
   972
    TextIO.closeOut out
paulson@17525
   973
  end;
quigley@17150
   974
quigley@17150
   975
paulson@18869
   976
(**** Produce TPTP files ****)
paulson@18868
   977
paulson@18868
   978
(*Attach sign in TPTP syntax: false means negate.*)
paulson@18868
   979
fun tptp_sign true s = "++" ^ s
paulson@18868
   980
  | tptp_sign false s = "--" ^ s
paulson@18868
   981
paulson@18868
   982
fun tptp_literal (Literal(pol,pred,tag)) =  (*FIXME REMOVE TAGGING*)
paulson@15347
   983
    let val pred_string = string_of_predicate pred
paulson@17230
   984
	val tagged_pol = 
paulson@17230
   985
	      if (tag andalso !tagged) then (if pol then "+++" else "---")
paulson@17230
   986
	      else (if pol then "++" else "--")
paulson@15347
   987
     in
paulson@15347
   988
	tagged_pol ^ pred_string
paulson@15347
   989
    end;
paulson@15347
   990
paulson@18798
   991
fun tptp_of_typeLit (LTVar (s,ty)) = "--" ^ s ^ "(" ^ ty ^ ")"
paulson@18798
   992
  | tptp_of_typeLit (LTFree (s,ty)) = "++" ^ s ^ "(" ^ ty ^ ")";
paulson@15347
   993
 
paulson@15347
   994
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@17317
   995
    "input_clause(" ^ string_of_clausename (cls_id,ax_name) ^ "," ^ 
paulson@18863
   996
    knd ^ "," ^ lits ^ ").\n";
paulson@15347
   997
paulson@17317
   998
fun gen_tptp_type_cls (cls_id,ax_name,knd,tfree_lit,idx) = 
paulson@17317
   999
    "input_clause(" ^ string_of_type_clsname (cls_id,ax_name,idx) ^ "," ^ 
paulson@18863
  1000
    knd ^ ",[" ^ tfree_lit ^ "]).\n";
paulson@15347
  1001
paulson@18869
  1002
fun tptp_type_lits (Clause {literals, types_sorts, ...}) = 
paulson@18869
  1003
    let val lits = map tptp_literal literals
paulson@18869
  1004
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@18869
  1005
        val tvar_lits_strs =
paulson@18869
  1006
            if !keep_types then map tptp_of_typeLit tvar_lits else []
paulson@18869
  1007
	val tfree_lits =
paulson@18869
  1008
	    if !keep_types then map tptp_of_typeLit tfree_lits else []
paulson@15347
  1009
    in
paulson@17305
  1010
	(tvar_lits_strs @ lits, tfree_lits)
paulson@15347
  1011
    end; 
paulson@15347
  1012
paulson@18869
  1013
fun clause2tptp (cls as Clause {clause_id, axiom_name, kind, ...}) =
paulson@17422
  1014
    let val (lits,tfree_lits) = tptp_type_lits cls 
paulson@17305
  1015
            (*"lits" includes the typing assumptions (TVars)*)
paulson@18869
  1016
	val knd = name_of_kind kind
paulson@18869
  1017
	val cls_str = gen_tptp_cls(clause_id, axiom_name, knd, bracket_pack lits) 
paulson@15608
  1018
    in
paulson@15608
  1019
	(cls_str,tfree_lits) 
paulson@15608
  1020
    end;
paulson@15608
  1021
paulson@18863
  1022
fun tptp_tfree_clause tfree_lit =
paulson@18863
  1023
    "input_clause(" ^ "tfree_tcs," ^ "conjecture" ^ ",[" ^ tfree_lit ^ "]).\n";
paulson@15608
  1024
paulson@17525
  1025
fun tptp_of_arLit (TConsLit(b,(c,t,args))) =
paulson@18868
  1026
      tptp_sign b (c ^ "(" ^ t ^ paren_pack args ^ ")")
paulson@17525
  1027
  | tptp_of_arLit (TVarLit(b,(c,str))) =
paulson@18868
  1028
      tptp_sign b (c ^ "(" ^ str ^ ")")
paulson@15347
  1029
    
paulson@18868
  1030
fun tptp_arity_clause (arcls as ArityClause{kind,conclLit,premLits,...}) = 
paulson@18868
  1031
  let val arcls_id = string_of_arClauseID arcls
paulson@18868
  1032
      val knd = name_of_kind kind
paulson@18868
  1033
      val lits = map tptp_of_arLit (conclLit :: premLits)
paulson@18868
  1034
  in
paulson@18868
  1035
    "input_clause(" ^ arcls_id ^ "," ^ knd ^ "," ^ bracket_pack lits ^ ").\n"
paulson@18868
  1036
  end;
paulson@15347
  1037
paulson@15347
  1038
fun tptp_classrelLits sub sup = 
paulson@15347
  1039
    let val tvar = "(T)"
paulson@15347
  1040
    in 
paulson@18411
  1041
	"[--" ^ sub ^ tvar ^ ",++" ^ sup ^ tvar ^ "]"
paulson@15347
  1042
    end;
paulson@15347
  1043
paulson@18868
  1044
fun tptp_classrelClause (ClassrelClause {axiom_name,subclass,superclass,...}) =
paulson@18868
  1045
  "input_clause(" ^ axiom_name ^ ",axiom," ^ tptp_classrelLits subclass superclass ^ ").\n" 
quigley@17150
  1046
paulson@18863
  1047
(* write out a subgoal as tptp clauses to the file "xxxx_N"*)
mengj@19443
  1048
fun tptp_write_file thms filename (axclauses,classrel_clauses,arity_clauses) =
paulson@18863
  1049
  let
paulson@19207
  1050
    val _ = Output.debug ("Preparing to write the TPTP file " ^ filename)
mengj@19443
  1051
    val clss = make_conjecture_clauses thms
mengj@19443
  1052
    val axclauses' = make_axiom_clauses axclauses
paulson@18863
  1053
    val (tptp_clss,tfree_litss) = ListPair.unzip (map clause2tptp clss)
paulson@18863
  1054
    val tfree_clss = map tptp_tfree_clause (foldl (op union_string) [] tfree_litss)
paulson@18863
  1055
    val out = TextIO.openOut filename
paulson@18863
  1056
  in
mengj@19197
  1057
    List.app (curry TextIO.output out o #1 o clause2tptp) axclauses';
paulson@18863
  1058
    writeln_strs out tfree_clss;
paulson@18863
  1059
    writeln_strs out tptp_clss;
paulson@18868
  1060
    List.app (curry TextIO.output out o tptp_classrelClause) classrel_clauses;
paulson@18868
  1061
    List.app (curry TextIO.output out o tptp_arity_clause) arity_clauses;
paulson@18863
  1062
    TextIO.closeOut out
paulson@18863
  1063
  end;
paulson@18863
  1064
mengj@19354
  1065
mengj@19354
  1066
mengj@19354
  1067
mengj@19354
  1068
paulson@15347
  1069
end;