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