src/HOL/Tools/res_clause.ML
author paulson
Thu Dec 09 15:49:40 2004 +0100 (2004-12-09)
changeset 15390 87f78411f7c9
parent 15347 14585bc8fa09
child 15452 e2a721567f67
permissions -rw-r--r--
Comments and other tweaks by Jia
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@15347
    11
    exception ARCLAUSE of string
paulson@15347
    12
    exception CLAUSE of string
paulson@15347
    13
    type arityClause 
paulson@15347
    14
    type classrelClause
paulson@15347
    15
    val classrelClauses_of :
paulson@15347
    16
       string * string list -> classrelClause list
paulson@15347
    17
    type clause
paulson@15347
    18
    val keep_types : bool ref
paulson@15347
    19
    val make_axiom_arity_clause :
paulson@15347
    20
       string * (string * string list list) -> arityClause
paulson@15347
    21
    val make_axiom_classrelClause :
paulson@15347
    22
       string * string Library.option -> classrelClause
paulson@15347
    23
    val make_axiom_clause : Term.term -> string * int -> clause
paulson@15347
    24
    val make_axiom_clause_thm : Thm.thm -> string * int -> clause
paulson@15347
    25
    val make_conjecture_clause : Term.term -> clause
paulson@15347
    26
    val make_conjecture_clause_thm : Thm.thm -> clause
paulson@15347
    27
    val make_hypothesis_clause : Term.term -> clause
paulson@15347
    28
    val make_hypothesis_clause_thm : Thm.thm -> clause
paulson@15347
    29
    val special_equal : bool ref
paulson@15347
    30
    val tptp_arity_clause : arityClause -> string
paulson@15347
    31
    val tptp_classrelClause : classrelClause -> string
paulson@15347
    32
    val tptp_clause : clause -> string list
paulson@15347
    33
    val tptp_clauses2str : string list -> string
paulson@15347
    34
    val typed : unit -> unit
paulson@15347
    35
    val untyped : unit -> unit
paulson@15347
    36
  end;
paulson@15347
    37
paulson@15347
    38
structure ResClause : RES_CLAUSE =
paulson@15347
    39
struct
paulson@15347
    40
paulson@15347
    41
(* Added for typed equality *)
paulson@15347
    42
val special_equal = ref false; (* by default,equality does not carry type information *)
paulson@15347
    43
val eq_typ_wrapper = "typeinfo"; (* default string *)
paulson@15347
    44
paulson@15347
    45
paulson@15347
    46
val schematic_var_prefix = "V_";
paulson@15347
    47
val fixed_var_prefix = "v_";
paulson@15347
    48
paulson@15347
    49
paulson@15347
    50
val tvar_prefix = "Typ_";
paulson@15347
    51
val tfree_prefix = "typ_";
paulson@15347
    52
paulson@15347
    53
paulson@15347
    54
val clause_prefix = "cls_"; 
paulson@15347
    55
paulson@15347
    56
val arclause_prefix = "arcls_" 
paulson@15347
    57
paulson@15347
    58
val const_prefix = "const_";
paulson@15347
    59
val tconst_prefix = "tconst_"; 
paulson@15347
    60
paulson@15347
    61
val class_prefix = "clas_"; 
paulson@15347
    62
paulson@15347
    63
paulson@15347
    64
paulson@15347
    65
(**** some useful functions ****)
paulson@15347
    66
 
paulson@15347
    67
val const_trans_table =
paulson@15347
    68
      Symtab.make [("op =", "equal"),
paulson@15347
    69
	  	   ("op <=", "lessequals"),
paulson@15347
    70
		   ("op <", "less"),
paulson@15347
    71
		   ("op &", "and"),
paulson@15347
    72
		   ("op |", "or"),
paulson@15347
    73
		   ("op -->", "implies"),
paulson@15347
    74
		   ("op :", "in"),
paulson@15347
    75
		   ("op Un", "union"),
paulson@15347
    76
		   ("op Int", "inter")];
paulson@15347
    77
paulson@15347
    78
paulson@15347
    79
paulson@15347
    80
fun ascii_of_c c =
paulson@15347
    81
    let val n = ord c
paulson@15347
    82
    in
paulson@15347
    83
	(if ((n < 48) orelse (n > 57 andalso n < 65) orelse 
paulson@15347
    84
	   (n > 90 andalso n < 97) orelse (n > 122)) then ("_asc" ^ string_of_int n ^ "_")
paulson@15347
    85
							 else c)
paulson@15347
    86
    end;
paulson@15347
    87
paulson@15347
    88
fun ascii_of s = implode(map ascii_of_c (explode s));
paulson@15347
    89
paulson@15347
    90
paulson@15347
    91
(* another version of above functions that remove chars that may not be allowed by Vampire *)
paulson@15347
    92
fun make_schematic_var v = schematic_var_prefix ^ (ascii_of v);
paulson@15347
    93
fun make_fixed_var x = fixed_var_prefix ^ (ascii_of x);
paulson@15347
    94
paulson@15347
    95
fun make_schematic_type_var v = tvar_prefix ^ (ascii_of v);
paulson@15347
    96
fun make_fixed_type_var x = tfree_prefix ^ (ascii_of x);
paulson@15347
    97
paulson@15347
    98
fun make_fixed_const c = const_prefix ^ (ascii_of c);
paulson@15347
    99
fun make_fixed_type_const c = tconst_prefix ^ (ascii_of c);
paulson@15347
   100
paulson@15347
   101
fun make_type_class clas = class_prefix ^ (ascii_of clas);
paulson@15347
   102
paulson@15347
   103
paulson@15347
   104
paulson@15347
   105
paulson@15347
   106
fun lookup_const c =
paulson@15347
   107
    case Symtab.lookup (const_trans_table,c) of
paulson@15347
   108
        Some c' => c'
paulson@15347
   109
      | None =>  make_fixed_const c;
paulson@15347
   110
paulson@15347
   111
paulson@15347
   112
paulson@15347
   113
(***** definitions and functions for FOL clauses, prepared for conversion into TPTP format or SPASS format. *****)
paulson@15347
   114
paulson@15347
   115
val keep_types = ref true; (* default is true *)
paulson@15347
   116
fun untyped () = (keep_types := false);
paulson@15347
   117
fun typed () = (keep_types := true);
paulson@15347
   118
paulson@15347
   119
paulson@15347
   120
datatype kind = Axiom | Hypothesis | Conjecture;
paulson@15347
   121
fun name_of_kind Axiom = "axiom"
paulson@15347
   122
  | name_of_kind Hypothesis = "hypothesis"
paulson@15347
   123
  | name_of_kind Conjecture = "conjecture";
paulson@15347
   124
paulson@15347
   125
type clause_id = int;
paulson@15347
   126
type axiom_name = string;
paulson@15347
   127
paulson@15347
   128
paulson@15347
   129
type polarity = bool;
paulson@15347
   130
paulson@15347
   131
type indexname = Term.indexname;
paulson@15347
   132
paulson@15347
   133
paulson@15347
   134
(* "tag" is used for vampire specific syntax  *)
paulson@15347
   135
type tag = bool; 
paulson@15347
   136
paulson@15347
   137
paulson@15347
   138
paulson@15347
   139
fun string_of_indexname (name,index) = name ^ "_" ^ (string_of_int index);
paulson@15347
   140
paulson@15347
   141
paulson@15347
   142
val id_ref = ref 0;
paulson@15347
   143
fun generate_id () = 
paulson@15347
   144
     let val id = !id_ref
paulson@15347
   145
     in
paulson@15347
   146
	 (id_ref:=id + 1; id)
paulson@15347
   147
     end;
paulson@15347
   148
paulson@15347
   149
paulson@15347
   150
paulson@15347
   151
(**** Isabelle FOL clauses ****)
paulson@15347
   152
paulson@15347
   153
(* by default it is false *)
paulson@15347
   154
val tagged = ref false;
paulson@15347
   155
paulson@15347
   156
type pred_name = string;
paulson@15347
   157
type sort = Term.sort;
paulson@15347
   158
type fol_type = string;
paulson@15347
   159
paulson@15347
   160
paulson@15347
   161
datatype type_literal = LTVar of string | LTFree of string;
paulson@15347
   162
paulson@15347
   163
paulson@15347
   164
datatype folTerm = UVar of string * fol_type| Fun of string * fol_type * folTerm list;
paulson@15347
   165
datatype predicate = Predicate of pred_name * fol_type * folTerm list;
paulson@15347
   166
paulson@15347
   167
paulson@15347
   168
datatype literal = Literal of polarity * predicate * tag;
paulson@15347
   169
paulson@15347
   170
paulson@15347
   171
datatype typ_var = FOLTVar of indexname | FOLTFree of string;
paulson@15347
   172
paulson@15347
   173
paulson@15347
   174
(* ML datatype used to repsent one single clause: disjunction of literals. *)
paulson@15347
   175
datatype clause = 
paulson@15347
   176
	 Clause of {clause_id: clause_id,
paulson@15347
   177
		    axiom_name: axiom_name,
paulson@15347
   178
		    kind: kind,
paulson@15347
   179
		    literals: literal list,
paulson@15347
   180
		    types_sorts: (typ_var * sort) list, 
paulson@15347
   181
                    tvar_type_literals: type_literal list, 
paulson@15347
   182
                    tfree_type_literals: type_literal list };
paulson@15347
   183
paulson@15347
   184
paulson@15347
   185
exception CLAUSE of string;
paulson@15347
   186
paulson@15347
   187
paulson@15347
   188
paulson@15347
   189
(*** make clauses ***)
paulson@15347
   190
paulson@15347
   191
paulson@15347
   192
fun make_clause (clause_id,axiom_name,kind,literals,types_sorts,tvar_type_literals,tfree_type_literals) =
paulson@15347
   193
     Clause {clause_id = clause_id, axiom_name = axiom_name, kind = kind, literals = literals, types_sorts = types_sorts,tvar_type_literals = tvar_type_literals,tfree_type_literals = tfree_type_literals};
paulson@15347
   194
paulson@15347
   195
paulson@15347
   196
paulson@15347
   197
fun type_of (Type (a, [])) = (make_fixed_type_const a,[]) 
paulson@15347
   198
  | type_of (Type (a, Ts)) = 
paulson@15347
   199
    let val foltyps_ts = map type_of Ts
paulson@15347
   200
        val (folTyps,ts) = ResLib.unzip foltyps_ts
paulson@15347
   201
        val ts' = ResLib.flat_noDup ts
paulson@15347
   202
    in    
paulson@15347
   203
        (((make_fixed_type_const a) ^ (ResLib.list_to_string folTyps)),ts') 
paulson@15347
   204
    end
paulson@15347
   205
  | type_of (TFree (a, s)) = (make_fixed_type_var a, [((FOLTFree a),s)])
paulson@15347
   206
  | type_of (TVar (v, s))  = (make_schematic_type_var (string_of_indexname v), [((FOLTVar v),s)]);
paulson@15390
   207
paulson@15390
   208
(* added: checkMeta: string -> bool *)
paulson@15390
   209
(* Any meta vars like ?x should be treated as universal vars,although it is represented as "Free(...)" by Isabelle *)
paulson@15390
   210
fun checkMeta s =
paulson@15390
   211
    let val chars = explode s
paulson@15390
   212
    in
paulson@15390
   213
	["M", "E", "T", "A", "H", "Y", "P", "1"] prefix chars
paulson@15390
   214
    end;
paulson@15390
   215
paulson@15347
   216
    
paulson@15347
   217
fun pred_name_type (Const(c,T)) = (lookup_const c,type_of T)
paulson@15390
   218
  | pred_name_type (Free(x,T))  = 
paulson@15390
   219
    let val is_meta = checkMeta x
paulson@15390
   220
    in
paulson@15390
   221
	if is_meta then (raise CLAUSE("Predicate Not First Order")) else
paulson@15390
   222
	(make_fixed_var x,type_of T)
paulson@15390
   223
    end
paulson@15347
   224
  | pred_name_type (Var(_,_))   = raise CLAUSE("Predicate Not First Order")
paulson@15347
   225
  | pred_name_type _          = raise CLAUSE("Predicate input unexpected");
paulson@15347
   226
paulson@15347
   227
fun fun_name_type (Const(c,T)) = (lookup_const c,type_of T)
paulson@15347
   228
  | fun_name_type (Free(x,T)) = (make_fixed_var x,type_of T)
paulson@15347
   229
  | fun_name_type _ = raise CLAUSE("Function Not First Order");
paulson@15347
   230
    
paulson@15347
   231
paulson@15347
   232
fun term_of (Var(ind_nm,T)) = 
paulson@15347
   233
    let val (folType,ts) = type_of T
paulson@15347
   234
    in
paulson@15347
   235
	(UVar(make_schematic_var(string_of_indexname ind_nm),folType),ts)
paulson@15347
   236
    end
paulson@15347
   237
  | term_of (Free(x,T)) = 
paulson@15390
   238
    let val is_meta = checkMeta x
paulson@15390
   239
	val (folType,ts) = type_of T
paulson@15347
   240
    in
paulson@15390
   241
	if is_meta then (UVar(make_schematic_var x,folType),ts)
paulson@15390
   242
	else
paulson@15390
   243
            (Fun(make_fixed_var x,folType,[]),ts)
paulson@15347
   244
    end
paulson@15347
   245
  | term_of (Const(c,T)) = 
paulson@15347
   246
    let val (folType,ts) = type_of T
paulson@15347
   247
     in
paulson@15347
   248
        (Fun(lookup_const c,folType,[]),ts)
paulson@15347
   249
    end    
paulson@15347
   250
  | term_of (app as (t $ a)) = 
paulson@15347
   251
    let val (f,args) = strip_comb app
paulson@15347
   252
        fun term_of_aux () = 
paulson@15347
   253
            let val (funName,(funType,ts1)) = fun_name_type f
paulson@15347
   254
                 val (args',ts2) = ResLib.unzip (map term_of args)
paulson@15347
   255
                 val ts3 = ResLib.flat_noDup (ts1::ts2)
paulson@15347
   256
            in
paulson@15347
   257
		(Fun(funName,funType,args'),ts3)
paulson@15347
   258
            end
paulson@15347
   259
    in
paulson@15347
   260
        case f of Const(_,_) => term_of_aux ()
paulson@15390
   261
                | Free(s,_)  => if (checkMeta s) then (raise CLAUSE("Function Not First Order")) else term_of_aux ()
paulson@15347
   262
                | _          => raise CLAUSE("Function Not First Order")
paulson@15347
   263
    end
paulson@15390
   264
  | term_of _ = raise CLAUSE("Function Not First Order"); 
paulson@15390
   265
paulson@15347
   266
paulson@15347
   267
paulson@15347
   268
paulson@15347
   269
(* For type equality *)
paulson@15347
   270
(* here "arg_typ" is the type of "="'s argument's type, not the type of the equality *)
paulson@15347
   271
(* Find type of equality arg *)
paulson@15347
   272
local
paulson@15347
   273
paulson@15347
   274
fun eq_arg_type (Type("fun",[T,_])) = 
paulson@15347
   275
    let val (folT,_) = type_of T;
paulson@15347
   276
    in
paulson@15347
   277
	folT
paulson@15347
   278
    end;
paulson@15347
   279
paulson@15347
   280
in
paulson@15347
   281
paulson@15347
   282
fun pred_of_eq ((Const ("op =", typ)),args) =
paulson@15347
   283
    let val arg_typ = eq_arg_type typ 
paulson@15347
   284
	val (args',ts) = ResLib.unzip (map term_of args)
paulson@15347
   285
	val equal_name = lookup_const "op ="
paulson@15347
   286
    in
paulson@15347
   287
	(Predicate(equal_name,arg_typ,args'),ResLib.flat_noDup ts)
paulson@15347
   288
    end;
paulson@15347
   289
paulson@15347
   290
end;
paulson@15347
   291
paulson@15347
   292
(* changed for non-equality predicate *)
paulson@15347
   293
(* The input "pred" cannot be an equality *)
paulson@15347
   294
fun pred_of_nonEq (pred,args) = 
paulson@15347
   295
    let val (predName,(predType,ts1)) = pred_name_type pred
paulson@15347
   296
	val (args',ts2) = ResLib.unzip (map term_of args)
paulson@15347
   297
	val ts3 = ResLib.flat_noDup (ts1::ts2)
paulson@15347
   298
    in
paulson@15347
   299
	(Predicate(predName,predType,args'),ts3)
paulson@15347
   300
    end;
paulson@15347
   301
paulson@15347
   302
paulson@15347
   303
(* Changed for typed equality *)
paulson@15347
   304
(* First check if the predicate is an equality or not, then call different functions for equality and non-equalities *)
paulson@15347
   305
fun predicate_of term =
paulson@15347
   306
    let val (pred,args) = strip_comb term
paulson@15347
   307
    in
paulson@15347
   308
	case pred of (Const ("op =", _)) => pred_of_eq (pred,args)
paulson@15347
   309
		   | _ => pred_of_nonEq (pred,args)
paulson@15347
   310
    end;
paulson@15347
   311
paulson@15347
   312
paulson@15347
   313
 
paulson@15347
   314
fun literals_of_term ((Const("Trueprop",_) $ P),lits_ts) = literals_of_term(P,lits_ts)
paulson@15347
   315
  | literals_of_term ((Const("op |",_) $ P $ Q),(lits,ts)) = 
paulson@15347
   316
    let val (lits',ts') = literals_of_term(P,(lits,ts))
paulson@15347
   317
    in
paulson@15347
   318
        literals_of_term(Q,(lits',ts'))
paulson@15347
   319
    end
paulson@15347
   320
  | literals_of_term ((Const("Not",_) $ P),(lits,ts)) = 
paulson@15347
   321
    let val (pred,ts') = predicate_of P
paulson@15347
   322
        val lits' = Literal(false,pred,false) :: lits
paulson@15347
   323
        val ts'' = ResLib.no_rep_app ts ts'
paulson@15347
   324
    in
paulson@15347
   325
        (lits',ts'')
paulson@15347
   326
    end
paulson@15347
   327
  | literals_of_term (P,(lits,ts)) = 
paulson@15347
   328
    let val (pred,ts') = predicate_of P
paulson@15347
   329
        val lits' = Literal(true,pred,false) :: lits
paulson@15347
   330
        val ts'' = ResLib.no_rep_app ts ts' 
paulson@15347
   331
    in
paulson@15347
   332
        (lits',ts'')
paulson@15347
   333
    end
paulson@15347
   334
  | literals_of_term _ = raise CLAUSE("Unexpected clause format");
paulson@15347
   335
     
paulson@15347
   336
paulson@15347
   337
fun literals_of_thm thm = 
paulson@15347
   338
    let val term_of_thm = prop_of thm
paulson@15347
   339
			  
paulson@15347
   340
    in
paulson@15347
   341
	literals_of_term (term_of_thm,([],[]))
paulson@15347
   342
    end;
paulson@15347
   343
    
paulson@15347
   344
 
paulson@15347
   345
fun sorts_on_typs (_, []) = []
paulson@15347
   346
  | sorts_on_typs ((FOLTVar(indx)), [s]) = [LTVar((make_type_class s) ^ "(" ^ (make_schematic_type_var(string_of_indexname indx)) ^ ")")]
paulson@15347
   347
  | sorts_on_typs ((FOLTVar(indx)), (s::ss))=  LTVar((make_type_class s) ^ "(" ^ (make_schematic_type_var(string_of_indexname indx)) ^ ")") :: (sorts_on_typs ((FOLTVar(indx)), ss))
paulson@15347
   348
  | sorts_on_typs ((FOLTFree(x)), [s]) = [LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var(x)) ^ ")")]
paulson@15347
   349
  | sorts_on_typs ((FOLTFree(x)), (s::ss)) = LTFree((make_type_class s) ^ "(" ^ (make_fixed_type_var(x)) ^ ")") :: (sorts_on_typs ((FOLTFree(x)), ss));
paulson@15347
   350
paulson@15347
   351
paulson@15347
   352
fun add_typs_aux [] = ([],[])
paulson@15347
   353
  | add_typs_aux ((FOLTVar(indx),s)::tss) = 
paulson@15347
   354
    let val vs = sorts_on_typs (FOLTVar(indx),s)
paulson@15347
   355
        val (vss,fss) = add_typs_aux tss
paulson@15347
   356
    in
paulson@15347
   357
        (ResLib.no_rep_app vs vss,fss)
paulson@15347
   358
    end
paulson@15347
   359
  | add_typs_aux ((FOLTFree(x),s)::tss) =
paulson@15347
   360
    let val fs = sorts_on_typs (FOLTFree(x),s)
paulson@15347
   361
        val (vss,fss) = add_typs_aux tss
paulson@15347
   362
    in
paulson@15347
   363
        (vss,ResLib.no_rep_app fs fss)
paulson@15347
   364
    end;
paulson@15347
   365
paulson@15347
   366
paulson@15347
   367
fun add_typs (Clause cls) = 
paulson@15347
   368
    let val ts = #types_sorts cls
paulson@15347
   369
    in
paulson@15347
   370
	add_typs_aux ts
paulson@15347
   371
    end;    
paulson@15347
   372
paulson@15347
   373
paulson@15347
   374
paulson@15347
   375
paulson@15347
   376
(** make axiom clauses, hypothesis clauses and conjecture clauses. **)
paulson@15347
   377
paulson@15347
   378
local 
paulson@15347
   379
    fun replace_dot "." = "_"
paulson@15347
   380
      | replace_dot c = c;
paulson@15347
   381
paulson@15347
   382
in
paulson@15347
   383
paulson@15347
   384
fun proper_ax_name ax_name = 
paulson@15347
   385
    let val chars = explode ax_name
paulson@15347
   386
    in
paulson@15347
   387
	implode (map replace_dot chars)
paulson@15347
   388
    end;
paulson@15347
   389
end;
paulson@15347
   390
paulson@15347
   391
fun make_axiom_clause_thm thm (name,number)=
paulson@15347
   392
    let val (lits,types_sorts) = literals_of_thm thm
paulson@15347
   393
	val cls_id = number
paulson@15347
   394
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@15347
   395
	val ax_name = proper_ax_name name
paulson@15347
   396
    in 
paulson@15347
   397
	make_clause(cls_id,ax_name,Axiom,lits,types_sorts,tvar_lits,tfree_lits)
paulson@15347
   398
    end;
paulson@15347
   399
   
paulson@15347
   400
fun make_hypothesis_clause_thm thm =
paulson@15347
   401
    let val (lits,types_sorts) = literals_of_thm thm
paulson@15347
   402
	val cls_id = generate_id()
paulson@15347
   403
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@15347
   404
    in
paulson@15347
   405
	make_clause(cls_id,"",Hypothesis,lits,types_sorts,tvar_lits,tfree_lits)
paulson@15347
   406
    end;
paulson@15347
   407
paulson@15347
   408
paulson@15347
   409
fun make_conjecture_clause_thm thm =
paulson@15347
   410
    let val (lits,types_sorts) = literals_of_thm thm
paulson@15347
   411
	val cls_id = generate_id()
paulson@15347
   412
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@15347
   413
    in
paulson@15347
   414
	make_clause(cls_id,"",Conjecture,lits,types_sorts,tvar_lits,tfree_lits)
paulson@15347
   415
    end;
paulson@15347
   416
paulson@15347
   417
paulson@15347
   418
fun make_axiom_clause term (name,number)=
paulson@15347
   419
    let val (lits,types_sorts) = literals_of_term (term,([],[]))
paulson@15347
   420
	val cls_id = number
paulson@15347
   421
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@15347
   422
	val ax_name = proper_ax_name name
paulson@15347
   423
    in 
paulson@15347
   424
	make_clause(cls_id,ax_name,Axiom,lits,types_sorts,tvar_lits,tfree_lits)
paulson@15347
   425
    end;
paulson@15347
   426
paulson@15347
   427
paulson@15347
   428
fun make_hypothesis_clause term =
paulson@15347
   429
    let val (lits,types_sorts) = literals_of_term (term,([],[]))
paulson@15347
   430
	val cls_id = generate_id()
paulson@15347
   431
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@15347
   432
    in
paulson@15347
   433
	make_clause(cls_id,"",Hypothesis,lits,types_sorts,tvar_lits,tfree_lits)
paulson@15347
   434
    end;
paulson@15347
   435
    
paulson@15347
   436
 
paulson@15347
   437
fun make_conjecture_clause term =
paulson@15347
   438
    let val (lits,types_sorts) = literals_of_term (term,([],[]))
paulson@15347
   439
	val cls_id = generate_id()
paulson@15347
   440
	val (tvar_lits,tfree_lits) = add_typs_aux types_sorts
paulson@15347
   441
    in
paulson@15347
   442
	make_clause(cls_id,"",Conjecture,lits,types_sorts,tvar_lits,tfree_lits)
paulson@15347
   443
    end;
paulson@15347
   444
 
paulson@15347
   445
paulson@15347
   446
 
paulson@15347
   447
(**** Isabelle arities ****)
paulson@15347
   448
paulson@15347
   449
exception ARCLAUSE of string;
paulson@15347
   450
 
paulson@15347
   451
paulson@15347
   452
type class = string; 
paulson@15347
   453
type tcons = string; 
paulson@15347
   454
paulson@15347
   455
paulson@15347
   456
datatype arLit = TConsLit of bool * (class * tcons * string list) | TVarLit of bool * (class * string);
paulson@15347
   457
 
paulson@15347
   458
datatype arityClause =  
paulson@15347
   459
	 ArityClause of {clause_id: clause_id,
paulson@15347
   460
			 kind: kind,
paulson@15347
   461
			 conclLit: arLit,
paulson@15347
   462
			 premLits: arLit list};
paulson@15347
   463
paulson@15347
   464
paulson@15347
   465
fun get_TVars 0 = []
paulson@15347
   466
  | get_TVars n = ("T_" ^ (string_of_int n)) :: get_TVars (n-1);
paulson@15347
   467
paulson@15347
   468
paulson@15347
   469
paulson@15347
   470
fun pack_sort(_,[])  = raise ARCLAUSE("Empty Sort Found") 
paulson@15347
   471
  | pack_sort(tvar, [cls]) = [(make_type_class cls, tvar)] 
paulson@15347
   472
  | pack_sort(tvar, cls::srt) =  (make_type_class cls,tvar) :: (pack_sort(tvar, srt));
paulson@15347
   473
    
paulson@15347
   474
    
paulson@15347
   475
fun make_TVarLit (b,(cls,str)) = TVarLit(b,(cls,str));
paulson@15347
   476
fun make_TConsLit (b,(cls,tcons,tvars)) = TConsLit(b,(make_type_class cls,make_fixed_type_const tcons,tvars));
paulson@15347
   477
paulson@15347
   478
paulson@15347
   479
fun make_arity_clause (clause_id,kind,conclLit,premLits) =
paulson@15347
   480
    ArityClause {clause_id = clause_id, kind = kind, conclLit = conclLit, premLits = premLits};
paulson@15347
   481
paulson@15347
   482
paulson@15347
   483
fun make_axiom_arity_clause (tcons,(res,args)) =
paulson@15347
   484
     let val cls_id = generate_id()
paulson@15347
   485
	 val nargs = length args
paulson@15347
   486
	 val tvars = get_TVars nargs
paulson@15347
   487
	 val conclLit = make_TConsLit(true,(res,tcons,tvars))
paulson@15347
   488
         val tvars_srts = ResLib.zip tvars args
paulson@15347
   489
	 val tvars_srts' = ResLib.flat_noDup(map pack_sort tvars_srts)
paulson@15347
   490
         val false_tvars_srts' = ResLib.pair_ins false tvars_srts'
paulson@15347
   491
	 val premLits = map make_TVarLit false_tvars_srts'
paulson@15347
   492
     in
paulson@15347
   493
	 make_arity_clause (cls_id,Axiom,conclLit,premLits)
paulson@15347
   494
     end;
paulson@15347
   495
    
paulson@15347
   496
paulson@15347
   497
paulson@15347
   498
(**** Isabelle class relations ****)
paulson@15347
   499
paulson@15347
   500
paulson@15347
   501
datatype classrelClause = 
paulson@15347
   502
	 ClassrelClause of {clause_id: clause_id,
paulson@15347
   503
			    subclass: class,
paulson@15347
   504
			    superclass: class Library.option};
paulson@15347
   505
paulson@15347
   506
fun make_classrelClause (clause_id,subclass,superclass) =
paulson@15347
   507
    ClassrelClause {clause_id = clause_id,subclass = subclass, superclass = superclass};
paulson@15347
   508
paulson@15347
   509
paulson@15347
   510
fun make_axiom_classrelClause (subclass,superclass) =
paulson@15347
   511
    let val cls_id = generate_id()
paulson@15347
   512
	val sub = make_type_class subclass
paulson@15347
   513
	val sup = case superclass of None => None 
paulson@15347
   514
				   | Some s => Some (make_type_class s)
paulson@15347
   515
    in
paulson@15347
   516
	make_classrelClause(cls_id,sub,sup)
paulson@15347
   517
    end;
paulson@15347
   518
paulson@15347
   519
paulson@15347
   520
paulson@15347
   521
fun classrelClauses_of_aux (sub,[]) = []
paulson@15347
   522
  | classrelClauses_of_aux (sub,(sup::sups)) = make_axiom_classrelClause(sub,Some sup) :: (classrelClauses_of_aux (sub,sups));
paulson@15347
   523
paulson@15347
   524
paulson@15347
   525
fun classrelClauses_of (sub,sups) = 
paulson@15347
   526
    case sups of [] => [make_axiom_classrelClause (sub,None)]
paulson@15347
   527
	       | _ => classrelClauses_of_aux (sub, sups);
paulson@15347
   528
paulson@15347
   529
paulson@15347
   530
paulson@15347
   531
(***** convert clauses to tptp format *****)
paulson@15347
   532
paulson@15347
   533
paulson@15347
   534
fun string_of_clauseID (Clause cls) = clause_prefix ^ (string_of_int (#clause_id cls));
paulson@15347
   535
paulson@15347
   536
paulson@15347
   537
fun string_of_kind (Clause cls) = name_of_kind (#kind cls);
paulson@15347
   538
paulson@15347
   539
fun string_of_axiomName (Clause cls) = #axiom_name cls;
paulson@15347
   540
paulson@15347
   541
fun string_of_term (UVar(x,_)) = x
paulson@15347
   542
  | string_of_term (Fun (name,typ,[])) = name
paulson@15347
   543
  | string_of_term (Fun (name,typ,terms)) = 
paulson@15347
   544
    let val terms' = map string_of_term terms
paulson@15347
   545
    in
paulson@15347
   546
        if (!keep_types) then name ^ (ResLib.list_to_string (typ :: terms'))
paulson@15347
   547
        else name ^ (ResLib.list_to_string terms')
paulson@15347
   548
    end;
paulson@15347
   549
paulson@15347
   550
paulson@15347
   551
paulson@15347
   552
(****!!!! Changed for typed equality !!!!****)
paulson@15347
   553
fun wrap_eq_type typ t = eq_typ_wrapper ^"(" ^ t ^ "," ^ typ ^ ")";
paulson@15347
   554
paulson@15347
   555
paulson@15347
   556
(****!!!! Changed for typed equality !!!!****)
paulson@15347
   557
(* Only need to wrap equality's arguments with "typeinfo" if the output clauses are typed && if we specifically ask for types to be included.   *)
paulson@15347
   558
fun string_of_equality (typ,terms) =
paulson@15347
   559
    let val [tstr1,tstr2] = map string_of_term terms
paulson@15347
   560
    in
paulson@15347
   561
	if ((!keep_types) andalso (!special_equal)) then 
paulson@15347
   562
	    "equal(" ^ (wrap_eq_type typ tstr1) ^ "," ^ (wrap_eq_type typ tstr2) ^ ")"
paulson@15347
   563
	else
paulson@15347
   564
	    "equal(" ^ tstr1 ^ "," ^ tstr2 ^ ")"
paulson@15347
   565
    end;
paulson@15347
   566
paulson@15347
   567
paulson@15347
   568
paulson@15347
   569
(* Changed for typed equality *)
paulson@15347
   570
(* before output the string of the predicate, check if the predicate corresponds to an equality or not. *)
paulson@15347
   571
fun string_of_predicate (Predicate("equal",typ,terms)) = string_of_equality(typ,terms)
paulson@15347
   572
  | string_of_predicate (Predicate(name,_,[])) = name 
paulson@15347
   573
  | string_of_predicate (Predicate(name,typ,terms)) = 
paulson@15347
   574
    let val terms_as_strings = map string_of_term terms
paulson@15347
   575
    in
paulson@15347
   576
        if (!keep_types) then name ^ (ResLib.list_to_string  (typ :: terms_as_strings))
paulson@15347
   577
        else name ^ (ResLib.list_to_string terms_as_strings) 
paulson@15347
   578
    end;
paulson@15347
   579
paulson@15347
   580
    
paulson@15347
   581
paulson@15347
   582
paulson@15347
   583
fun tptp_literal (Literal(pol,pred,tag)) =
paulson@15347
   584
    let val pred_string = string_of_predicate pred
paulson@15347
   585
	val tagged_pol = if (tag andalso !tagged) then (if pol then "+++" else "---")
paulson@15347
   586
                         else (if pol then "++" else "--")
paulson@15347
   587
     in
paulson@15347
   588
	tagged_pol ^ pred_string
paulson@15347
   589
    end;
paulson@15347
   590
paulson@15347
   591
paulson@15347
   592
paulson@15347
   593
fun tptp_of_typeLit (LTVar x) = "--" ^ x
paulson@15347
   594
  | tptp_of_typeLit (LTFree x) = "++" ^ x;
paulson@15347
   595
 
paulson@15347
   596
paulson@15347
   597
fun gen_tptp_cls (cls_id,ax_name,knd,lits) = 
paulson@15347
   598
    let val ax_str = (if ax_name = "" then "" else ("_" ^ ax_name))
paulson@15347
   599
    in
paulson@15347
   600
	"input_clause(" ^ cls_id ^ ax_str ^ "," ^ knd ^ "," ^ lits ^ ")."
paulson@15347
   601
    end;
paulson@15347
   602
paulson@15347
   603
paulson@15347
   604
fun gen_tptp_type_cls (cls_id,knd,tfree_lit,idx) = "input_clause(" ^ cls_id ^ "_tcs" ^ (string_of_int idx) ^ "," ^ knd ^ ",[" ^ tfree_lit ^ "]).";
paulson@15347
   605
paulson@15347
   606
paulson@15347
   607
fun tptp_clause_aux (Clause cls) = 
paulson@15347
   608
    let val lits = map tptp_literal (#literals cls)
paulson@15347
   609
	val tvar_lits_strs = if (!keep_types) then (map tptp_of_typeLit (#tvar_type_literals cls)) else []
paulson@15347
   610
	val tfree_lits = if (!keep_types) then (map tptp_of_typeLit (#tfree_type_literals cls)) else []
paulson@15347
   611
    in
paulson@15347
   612
	(tvar_lits_strs @ lits,tfree_lits)
paulson@15347
   613
    end; 
paulson@15347
   614
paulson@15347
   615
    
paulson@15347
   616
fun tptp_clause cls =
paulson@15347
   617
    let val (lits,tfree_lits) = tptp_clause_aux cls (*"lits" includes the typing assumptions (TVars)*)
paulson@15347
   618
	val cls_id = string_of_clauseID cls
paulson@15347
   619
	val ax_name = string_of_axiomName cls
paulson@15347
   620
	val knd = string_of_kind cls
paulson@15347
   621
	val lits_str = ResLib.list_to_string' lits
paulson@15347
   622
	val cls_str = gen_tptp_cls(cls_id,ax_name,knd,lits_str) 			fun typ_clss k [] = []
paulson@15347
   623
          | typ_clss k (tfree :: tfrees) = 
paulson@15347
   624
            (gen_tptp_type_cls(cls_id,knd,tfree,k)) ::  (typ_clss (k+1) tfrees)
paulson@15347
   625
    in 
paulson@15347
   626
	cls_str :: (typ_clss 0 tfree_lits)
paulson@15347
   627
    end;
paulson@15347
   628
paulson@15347
   629
paulson@15347
   630
val delim = "\n";
paulson@15347
   631
val tptp_clauses2str = ResLib.list2str_sep delim; 
paulson@15347
   632
     
paulson@15347
   633
paulson@15347
   634
fun string_of_arClauseID (ArityClause arcls) = arclause_prefix ^ string_of_int(#clause_id arcls);
paulson@15347
   635
paulson@15347
   636
paulson@15347
   637
fun string_of_arLit (TConsLit(b,(c,t,args))) =
paulson@15347
   638
    let val pol = if b then "++" else "--"
paulson@15347
   639
	val  arg_strs = (case args of [] => "" | _ => ResLib.list_to_string args)
paulson@15347
   640
    in 
paulson@15347
   641
	pol ^ c ^ "(" ^ t ^ arg_strs ^ ")"
paulson@15347
   642
    end
paulson@15347
   643
  | string_of_arLit (TVarLit(b,(c,str))) =
paulson@15347
   644
    let val pol = if b then "++" else "--"
paulson@15347
   645
    in
paulson@15347
   646
	pol ^ c ^ "(" ^ str ^ ")"
paulson@15347
   647
    end;
paulson@15347
   648
    
paulson@15347
   649
paulson@15347
   650
fun string_of_conclLit (ArityClause arcls) = string_of_arLit (#conclLit arcls);
paulson@15347
   651
     
paulson@15347
   652
paulson@15347
   653
fun strings_of_premLits (ArityClause arcls) = map string_of_arLit (#premLits arcls);
paulson@15347
   654
		
paulson@15347
   655
paulson@15347
   656
fun string_of_arKind (ArityClause arcls) = name_of_kind(#kind arcls);
paulson@15347
   657
paulson@15347
   658
fun tptp_arity_clause arcls = 
paulson@15347
   659
    let val arcls_id = string_of_arClauseID arcls
paulson@15347
   660
	val concl_lit = string_of_conclLit arcls
paulson@15347
   661
	val prems_lits = strings_of_premLits arcls
paulson@15347
   662
	val knd = string_of_arKind arcls
paulson@15347
   663
	val all_lits = concl_lit :: prems_lits
paulson@15347
   664
    in
paulson@15347
   665
	"input_clause(" ^ arcls_id ^ "," ^ knd ^ (ResLib.list_to_string' all_lits) ^ ")."
paulson@15347
   666
	
paulson@15347
   667
    end;
paulson@15347
   668
paulson@15347
   669
paulson@15347
   670
val clrelclause_prefix = "relcls_";
paulson@15347
   671
paulson@15347
   672
paulson@15347
   673
fun tptp_classrelLits sub sup = 
paulson@15347
   674
    let val tvar = "(T)"
paulson@15347
   675
    in 
paulson@15347
   676
	case sup of None => "[++" ^ sub ^ tvar ^ "]"
paulson@15347
   677
		  | (Some supcls) =>  "[++" ^ sub ^ tvar ^ ",--" ^ supcls ^ tvar ^ "]"
paulson@15347
   678
    end;
paulson@15347
   679
paulson@15347
   680
paulson@15347
   681
fun tptp_classrelClause (ClassrelClause cls) =
paulson@15347
   682
    let val relcls_id = clrelclause_prefix ^ string_of_int(#clause_id cls)
paulson@15347
   683
	val sub = #subclass cls
paulson@15347
   684
	val sup = #superclass cls
paulson@15347
   685
	val lits = tptp_classrelLits sub sup
paulson@15347
   686
    in
paulson@15347
   687
	"input_clause(" ^ relcls_id ^ ",axiom," ^ lits ^ ")."
paulson@15347
   688
    end; 
paulson@15347
   689
    
paulson@15347
   690
end;