src/HOL/Tools/refute.ML
author webertj
Thu Nov 09 18:48:45 2006 +0100 (2006-11-09)
changeset 21267 5294ecae6708
parent 21098 d0d8a48ae4e6
child 21556 e0ffb2d13f9f
permissions -rw-r--r--
interpreters for fst and snd added
webertj@14350
     1
(*  Title:      HOL/Tools/refute.ML
webertj@14350
     2
    ID:         $Id$
webertj@14350
     3
    Author:     Tjark Weber
webertj@15547
     4
    Copyright   2003-2005
webertj@14350
     5
webertj@14965
     6
Finite model generation for HOL formulas, using a SAT solver.
webertj@14350
     7
*)
webertj@14350
     8
webertj@14456
     9
(* ------------------------------------------------------------------------- *)
webertj@14456
    10
(* Declares the 'REFUTE' signature as well as a structure 'Refute'.          *)
webertj@14456
    11
(* Documentation is available in the Isabelle/Isar theory 'HOL/Refute.thy'.  *)
webertj@14350
    12
(* ------------------------------------------------------------------------- *)
webertj@14350
    13
webertj@14350
    14
signature REFUTE =
webertj@14350
    15
sig
webertj@14350
    16
webertj@14456
    17
	exception REFUTE of string * string
webertj@14456
    18
webertj@14456
    19
(* ------------------------------------------------------------------------- *)
webertj@14807
    20
(* Model/interpretation related code (translation HOL -> propositional logic *)
webertj@14456
    21
(* ------------------------------------------------------------------------- *)
webertj@14456
    22
webertj@14807
    23
	type params
webertj@14456
    24
	type interpretation
webertj@14456
    25
	type model
webertj@14456
    26
	type arguments
webertj@14456
    27
webertj@14807
    28
	exception MAXVARS_EXCEEDED
webertj@14456
    29
webertj@14807
    30
	val add_interpreter : string -> (theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option) -> theory -> theory
webertj@14807
    31
	val add_printer     : string -> (theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option) -> theory -> theory
webertj@14456
    32
webertj@15334
    33
	val interpret : theory -> model -> arguments -> Term.term -> (interpretation * model * arguments)
webertj@14807
    34
webertj@14807
    35
	val print       : theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term
webertj@14456
    36
	val print_model : theory -> model -> (int -> bool) -> string
webertj@14456
    37
webertj@14456
    38
(* ------------------------------------------------------------------------- *)
webertj@14456
    39
(* Interface                                                                 *)
webertj@14456
    40
(* ------------------------------------------------------------------------- *)
webertj@14456
    41
webertj@14456
    42
	val set_default_param  : (string * string) -> theory -> theory
webertj@14456
    43
	val get_default_param  : theory -> string -> string option
webertj@14456
    44
	val get_default_params : theory -> (string * string) list
webertj@14807
    45
	val actual_params      : theory -> (string * string) list -> params
webertj@14456
    46
webertj@14807
    47
	val find_model : theory -> params -> Term.term -> bool -> unit
webertj@14456
    48
webertj@14456
    49
	val satisfy_term   : theory -> (string * string) list -> Term.term -> unit  (* tries to find a model for a formula *)
webertj@14456
    50
	val refute_term    : theory -> (string * string) list -> Term.term -> unit  (* tries to find a model that refutes a formula *)
webertj@14456
    51
	val refute_subgoal : theory -> (string * string) list -> Thm.thm -> int -> unit
webertj@14456
    52
wenzelm@18708
    53
	val setup : theory -> theory
webertj@14456
    54
end;
webertj@14456
    55
webertj@14456
    56
structure Refute : REFUTE =
webertj@14456
    57
struct
webertj@14456
    58
webertj@14456
    59
	open PropLogic;
webertj@14456
    60
webertj@14350
    61
	(* We use 'REFUTE' only for internal error conditions that should    *)
webertj@14350
    62
	(* never occur in the first place (i.e. errors caused by bugs in our *)
webertj@14350
    63
	(* code).  Otherwise (e.g. to indicate invalid input data) we use    *)
webertj@14350
    64
	(* 'error'.                                                          *)
webertj@14456
    65
	exception REFUTE of string * string;  (* ("in function", "cause") *)
webertj@14350
    66
webertj@14807
    67
	(* should be raised by an interpreter when more variables would be *)
webertj@14807
    68
	(* required than allowed by 'maxvars'                              *)
webertj@14807
    69
	exception MAXVARS_EXCEEDED;
webertj@14350
    70
webertj@14350
    71
(* ------------------------------------------------------------------------- *)
webertj@14350
    72
(* TREES                                                                     *)
webertj@14350
    73
(* ------------------------------------------------------------------------- *)
webertj@14350
    74
webertj@14350
    75
(* ------------------------------------------------------------------------- *)
webertj@14350
    76
(* tree: implements an arbitrarily (but finitely) branching tree as a list   *)
webertj@14350
    77
(*       of (lists of ...) elements                                          *)
webertj@14350
    78
(* ------------------------------------------------------------------------- *)
webertj@14350
    79
webertj@14350
    80
	datatype 'a tree =
webertj@14350
    81
		  Leaf of 'a
webertj@14350
    82
		| Node of ('a tree) list;
webertj@14350
    83
webertj@14350
    84
	(* ('a -> 'b) -> 'a tree -> 'b tree *)
webertj@14350
    85
webertj@14350
    86
	fun tree_map f tr =
webertj@14350
    87
		case tr of
webertj@14350
    88
		  Leaf x  => Leaf (f x)
webertj@14350
    89
		| Node xs => Node (map (tree_map f) xs);
webertj@14350
    90
webertj@14350
    91
	(* ('a * 'b -> 'a) -> 'a * ('b tree) -> 'a *)
webertj@14350
    92
webertj@14350
    93
	fun tree_foldl f =
webertj@14350
    94
	let
webertj@14350
    95
		fun itl (e, Leaf x)  = f(e,x)
skalberg@15570
    96
		  | itl (e, Node xs) = Library.foldl (tree_foldl f) (e,xs)
webertj@14350
    97
	in
webertj@14350
    98
		itl
webertj@14350
    99
	end;
webertj@14350
   100
webertj@14350
   101
	(* 'a tree * 'b tree -> ('a * 'b) tree *)
webertj@14350
   102
webertj@15611
   103
	fun tree_pair (t1, t2) =
webertj@14350
   104
		case t1 of
webertj@14350
   105
		  Leaf x =>
webertj@14350
   106
			(case t2 of
webertj@14350
   107
				  Leaf y => Leaf (x,y)
webertj@14350
   108
				| Node _ => raise REFUTE ("tree_pair", "trees are of different height (second tree is higher)"))
webertj@14350
   109
		| Node xs =>
webertj@14350
   110
			(case t2 of
webertj@14807
   111
				  (* '~~' will raise an exception if the number of branches in   *)
webertj@14807
   112
				  (* both trees is different at the current node                 *)
webertj@14350
   113
				  Node ys => Node (map tree_pair (xs ~~ ys))
webertj@14350
   114
				| Leaf _  => raise REFUTE ("tree_pair", "trees are of different height (first tree is higher)"));
webertj@14350
   115
webertj@14350
   116
(* ------------------------------------------------------------------------- *)
webertj@14807
   117
(* params: parameters that control the translation into a propositional      *)
webertj@14807
   118
(*         formula/model generation                                          *)
webertj@14807
   119
(*                                                                           *)
webertj@14807
   120
(* The following parameters are supported (and required (!), except for      *)
webertj@14807
   121
(* "sizes"):                                                                 *)
webertj@14807
   122
(*                                                                           *)
webertj@14807
   123
(* Name          Type    Description                                         *)
webertj@14807
   124
(*                                                                           *)
webertj@14807
   125
(* "sizes"       (string * int) list                                         *)
webertj@14807
   126
(*                       Size of ground types (e.g. 'a=2), or depth of IDTs. *)
webertj@14807
   127
(* "minsize"     int     If >0, minimal size of each ground type/IDT depth.  *)
webertj@14807
   128
(* "maxsize"     int     If >0, maximal size of each ground type/IDT depth.  *)
webertj@14807
   129
(* "maxvars"     int     If >0, use at most 'maxvars' Boolean variables      *)
webertj@14807
   130
(*                       when transforming the term into a propositional     *)
webertj@14807
   131
(*                       formula.                                            *)
webertj@14807
   132
(* "maxtime"     int     If >0, terminate after at most 'maxtime' seconds.   *)
webertj@14807
   133
(* "satsolver"   string  SAT solver to be used.                              *)
webertj@14807
   134
(* ------------------------------------------------------------------------- *)
webertj@14807
   135
webertj@14807
   136
	type params =
webertj@14807
   137
		{
webertj@14807
   138
			sizes    : (string * int) list,
webertj@14807
   139
			minsize  : int,
webertj@14807
   140
			maxsize  : int,
webertj@14807
   141
			maxvars  : int,
webertj@14807
   142
			maxtime  : int,
webertj@14807
   143
			satsolver: string
webertj@14807
   144
		};
webertj@14807
   145
webertj@14807
   146
(* ------------------------------------------------------------------------- *)
webertj@14456
   147
(* interpretation: a term's interpretation is given by a variable of type    *)
webertj@14456
   148
(*                 'interpretation'                                          *)
webertj@14350
   149
(* ------------------------------------------------------------------------- *)
webertj@14350
   150
webertj@14456
   151
	type interpretation =
webertj@14456
   152
		prop_formula list tree;
webertj@14350
   153
webertj@14350
   154
(* ------------------------------------------------------------------------- *)
webertj@14456
   155
(* model: a model specifies the size of types and the interpretation of      *)
webertj@14456
   156
(*        terms                                                              *)
webertj@14350
   157
(* ------------------------------------------------------------------------- *)
webertj@14350
   158
webertj@14456
   159
	type model =
webertj@14456
   160
		(Term.typ * int) list * (Term.term * interpretation) list;
webertj@14350
   161
webertj@14456
   162
(* ------------------------------------------------------------------------- *)
webertj@14456
   163
(* arguments: additional arguments required during interpretation of terms   *)
webertj@14456
   164
(* ------------------------------------------------------------------------- *)
webertj@14807
   165
webertj@14456
   166
	type arguments =
webertj@14807
   167
		{
webertj@15547
   168
			maxvars   : int,   (* just passed unchanged from 'params' *)
webertj@15547
   169
			def_eq    : bool,  (* whether to use 'make_equality' or 'make_def_equality' *)
webertj@15547
   170
			(* the following may change during the translation *)
webertj@14807
   171
			next_idx  : int,
webertj@14807
   172
			bounds    : interpretation list,
webertj@14807
   173
			wellformed: prop_formula
webertj@14807
   174
		};
webertj@14456
   175
webertj@14350
   176
webertj@14456
   177
	structure RefuteDataArgs =
webertj@14456
   178
	struct
webertj@14456
   179
		val name = "HOL/refute";
webertj@14456
   180
		type T =
webertj@14456
   181
			{interpreters: (string * (theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option)) list,
webertj@14807
   182
			 printers: (string * (theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option)) list,
webertj@14456
   183
			 parameters: string Symtab.table};
webertj@14456
   184
		val empty = {interpreters = [], printers = [], parameters = Symtab.empty};
webertj@14456
   185
		val copy = I;
wenzelm@16424
   186
		val extend = I;
wenzelm@16424
   187
		fun merge _
webertj@14456
   188
			({interpreters = in1, printers = pr1, parameters = pa1},
webertj@14456
   189
			 {interpreters = in2, printers = pr2, parameters = pa2}) =
haftmann@21098
   190
			{interpreters = AList.merge (op =) (K true) (in1, in2),
haftmann@21098
   191
			 printers = AList.merge (op =) (K true) (pr1, pr2),
webertj@14456
   192
			 parameters = Symtab.merge (op=) (pa1, pa2)};
webertj@14456
   193
		fun print sg {interpreters, printers, parameters} =
webertj@14456
   194
			Pretty.writeln (Pretty.chunks
webertj@15767
   195
				[Pretty.strs ("default parameters:" :: List.concat (map (fn (name, value) => [name, "=", value]) (Symtab.dest parameters))),
webertj@14456
   196
				 Pretty.strs ("interpreters:" :: map fst interpreters),
webertj@14456
   197
				 Pretty.strs ("printers:" :: map fst printers)]);
webertj@14456
   198
	end;
webertj@14456
   199
webertj@14456
   200
	structure RefuteData = TheoryDataFun(RefuteDataArgs);
webertj@14456
   201
webertj@14350
   202
webertj@14350
   203
(* ------------------------------------------------------------------------- *)
webertj@15334
   204
(* interpret: interprets the term 't' using a suitable interpreter; returns  *)
webertj@15334
   205
(*            the interpretation and a (possibly extended) model that keeps  *)
webertj@15334
   206
(*            track of the interpretation of subterms                        *)
webertj@14350
   207
(* ------------------------------------------------------------------------- *)
webertj@14350
   208
webertj@14807
   209
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) *)
webertj@14456
   210
webertj@14456
   211
	fun interpret thy model args t =
webertj@14456
   212
		(case get_first (fn (_, f) => f thy model args t) (#interpreters (RefuteData.get thy)) of
webertj@15547
   213
		  NONE   => raise REFUTE ("interpret", "no interpreter for term " ^ quote (Sign.string_of_term (sign_of thy) t))
skalberg@15531
   214
		| SOME x => x);
webertj@14456
   215
webertj@14456
   216
(* ------------------------------------------------------------------------- *)
webertj@15547
   217
(* print: converts the constant denoted by the term 't' into a term using a  *)
webertj@15547
   218
(*        suitable printer                                                   *)
webertj@14456
   219
(* ------------------------------------------------------------------------- *)
webertj@14350
   220
webertj@14807
   221
	(* theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term *)
webertj@14456
   222
webertj@14456
   223
	fun print thy model t intr assignment =
webertj@14456
   224
		(case get_first (fn (_, f) => f thy model t intr assignment) (#printers (RefuteData.get thy)) of
webertj@15547
   225
		  NONE   => raise REFUTE ("print", "no printer for term " ^ quote (Sign.string_of_term (sign_of thy) t))
skalberg@15531
   226
		| SOME x => x);
webertj@14456
   227
webertj@14456
   228
(* ------------------------------------------------------------------------- *)
webertj@14456
   229
(* print_model: turns the model into a string, using a fixed interpretation  *)
webertj@14807
   230
(*              (given by an assignment for Boolean variables) and suitable  *)
webertj@14456
   231
(*              printers                                                     *)
webertj@14456
   232
(* ------------------------------------------------------------------------- *)
webertj@14456
   233
webertj@14807
   234
	(* theory -> model -> (int -> bool) -> string *)
webertj@14807
   235
webertj@14456
   236
	fun print_model thy model assignment =
webertj@14456
   237
	let
webertj@14456
   238
		val (typs, terms) = model
webertj@14807
   239
		val typs_msg =
webertj@14807
   240
			if null typs then
webertj@14807
   241
				"empty universe (no type variables in term)\n"
webertj@14807
   242
			else
webertj@15611
   243
				"Size of types: " ^ commas (map (fn (T, i) => Sign.string_of_typ (sign_of thy) T ^ ": " ^ string_of_int i) typs) ^ "\n"
webertj@14807
   244
		val show_consts_msg =
webertj@14807
   245
			if not (!show_consts) andalso Library.exists (is_Const o fst) terms then
webertj@14807
   246
				"set \"show_consts\" to show the interpretation of constants\n"
webertj@14807
   247
			else
webertj@14807
   248
				""
webertj@14807
   249
		val terms_msg =
webertj@14807
   250
			if null terms then
webertj@14807
   251
				"empty interpretation (no free variables in term)\n"
webertj@14807
   252
			else
webertj@15611
   253
				space_implode "\n" (List.mapPartial (fn (t, intr) =>
webertj@14807
   254
					(* print constants only if 'show_consts' is true *)
webertj@14807
   255
					if (!show_consts) orelse not (is_Const t) then
skalberg@15531
   256
						SOME (Sign.string_of_term (sign_of thy) t ^ ": " ^ Sign.string_of_term (sign_of thy) (print thy model t intr assignment))
webertj@14807
   257
					else
skalberg@15531
   258
						NONE) terms) ^ "\n"
webertj@14456
   259
	in
webertj@14807
   260
		typs_msg ^ show_consts_msg ^ terms_msg
webertj@14456
   261
	end;
webertj@14456
   262
webertj@14456
   263
webertj@14456
   264
(* ------------------------------------------------------------------------- *)
webertj@14456
   265
(* PARAMETER MANAGEMENT                                                      *)
webertj@14456
   266
(* ------------------------------------------------------------------------- *)
webertj@14456
   267
webertj@14456
   268
	(* string -> (theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option) -> theory -> theory *)
webertj@14456
   269
webertj@14456
   270
	fun add_interpreter name f thy =
webertj@14456
   271
	let
webertj@14456
   272
		val {interpreters, printers, parameters} = RefuteData.get thy
webertj@14456
   273
	in
haftmann@17314
   274
		case AList.lookup (op =) interpreters name of
skalberg@15531
   275
		  NONE   => RefuteData.put {interpreters = (name, f) :: interpreters, printers = printers, parameters = parameters} thy
skalberg@15531
   276
		| SOME _ => error ("Interpreter " ^ name ^ " already declared")
webertj@14456
   277
	end;
webertj@14456
   278
webertj@14807
   279
	(* string -> (theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option) -> theory -> theory *)
webertj@14456
   280
webertj@14456
   281
	fun add_printer name f thy =
webertj@14456
   282
	let
webertj@14456
   283
		val {interpreters, printers, parameters} = RefuteData.get thy
webertj@14456
   284
	in
haftmann@17314
   285
		case AList.lookup (op =) printers name of
skalberg@15531
   286
		  NONE   => RefuteData.put {interpreters = interpreters, printers = (name, f) :: printers, parameters = parameters} thy
skalberg@15531
   287
		| SOME _ => error ("Printer " ^ name ^ " already declared")
webertj@14456
   288
	end;
webertj@14456
   289
webertj@14456
   290
(* ------------------------------------------------------------------------- *)
webertj@14456
   291
(* set_default_param: stores the '(name, value)' pair in RefuteData's        *)
webertj@14456
   292
(*                    parameter table                                        *)
webertj@14456
   293
(* ------------------------------------------------------------------------- *)
webertj@14456
   294
webertj@14456
   295
	(* (string * string) -> theory -> theory *)
webertj@14456
   296
webertj@14456
   297
	fun set_default_param (name, value) thy =
webertj@14456
   298
	let
webertj@14456
   299
		val {interpreters, printers, parameters} = RefuteData.get thy
webertj@14456
   300
	in
wenzelm@17412
   301
		case Symtab.lookup parameters name of
skalberg@15531
   302
		  NONE   => RefuteData.put
webertj@14456
   303
			{interpreters = interpreters, printers = printers, parameters = Symtab.extend (parameters, [(name, value)])} thy
skalberg@15531
   304
		| SOME _ => RefuteData.put
wenzelm@17412
   305
			{interpreters = interpreters, printers = printers, parameters = Symtab.update (name, value) parameters} thy
webertj@14350
   306
	end;
webertj@14350
   307
webertj@14350
   308
(* ------------------------------------------------------------------------- *)
webertj@14456
   309
(* get_default_param: retrieves the value associated with 'name' from        *)
webertj@14456
   310
(*                    RefuteData's parameter table                           *)
webertj@14456
   311
(* ------------------------------------------------------------------------- *)
webertj@14456
   312
webertj@14456
   313
	(* theory -> string -> string option *)
webertj@14456
   314
wenzelm@17412
   315
	val get_default_param = Symtab.lookup o #parameters o RefuteData.get;
webertj@14456
   316
webertj@14456
   317
(* ------------------------------------------------------------------------- *)
webertj@14456
   318
(* get_default_params: returns a list of all '(name, value)' pairs that are  *)
webertj@14456
   319
(*                     stored in RefuteData's parameter table                *)
webertj@14456
   320
(* ------------------------------------------------------------------------- *)
webertj@14456
   321
webertj@14456
   322
	(* theory -> (string * string) list *)
webertj@14456
   323
wenzelm@17261
   324
	val get_default_params = Symtab.dest o #parameters o RefuteData.get;
webertj@14456
   325
webertj@14456
   326
(* ------------------------------------------------------------------------- *)
webertj@14456
   327
(* actual_params: takes a (possibly empty) list 'params' of parameters that  *)
webertj@14456
   328
(*      override the default parameters currently specified in 'thy', and    *)
webertj@14807
   329
(*      returns a record that can be passed to 'find_model'.                 *)
webertj@14456
   330
(* ------------------------------------------------------------------------- *)
webertj@14456
   331
webertj@14807
   332
	(* theory -> (string * string) list -> params *)
webertj@14456
   333
webertj@14807
   334
	fun actual_params thy override =
webertj@14456
   335
	let
webertj@14456
   336
		(* (string * string) list * string -> int *)
webertj@14456
   337
		fun read_int (parms, name) =
haftmann@17314
   338
			case AList.lookup (op =) parms name of
skalberg@15531
   339
			  SOME s => (case Int.fromString s of
webertj@14456
   340
				  SOME i => i
webertj@14456
   341
				| NONE   => error ("parameter " ^ quote name ^ " (value is " ^ quote s ^ ") must be an integer value"))
skalberg@15531
   342
			| NONE   => error ("parameter " ^ quote name ^ " must be assigned a value")
webertj@14456
   343
		(* (string * string) list * string -> string *)
webertj@14456
   344
		fun read_string (parms, name) =
haftmann@17314
   345
			case AList.lookup (op =) parms name of
skalberg@15531
   346
			  SOME s => s
skalberg@15531
   347
			| NONE   => error ("parameter " ^ quote name ^ " must be assigned a value")
webertj@14456
   348
		(* (string * string) list *)
webertj@14807
   349
		val allparams = override @ (get_default_params thy)  (* 'override' first, defaults last *)
webertj@14456
   350
		(* int *)
webertj@14456
   351
		val minsize   = read_int (allparams, "minsize")
webertj@14456
   352
		val maxsize   = read_int (allparams, "maxsize")
webertj@14456
   353
		val maxvars   = read_int (allparams, "maxvars")
webertj@20544
   354
		val maxtime   = read_int (allparams, "maxtime")
webertj@14456
   355
		(* string *)
webertj@14456
   356
		val satsolver = read_string (allparams, "satsolver")
webertj@14807
   357
		(* all remaining parameters of the form "string=int" are collected in  *)
webertj@14807
   358
		(* 'sizes'                                                             *)
webertj@14807
   359
		(* TODO: it is currently not possible to specify a size for a type     *)
webertj@14807
   360
		(*       whose name is one of the other parameters (e.g. 'maxvars')    *)
webertj@14807
   361
		(* (string * int) list *)
skalberg@15570
   362
		val sizes     = List.mapPartial
skalberg@15531
   363
			(fn (name,value) => (case Int.fromString value of SOME i => SOME (name, i) | NONE => NONE))
skalberg@15570
   364
			(List.filter (fn (name,_) => name<>"minsize" andalso name<>"maxsize" andalso name<>"maxvars" andalso name<>"maxtime" andalso name<>"satsolver")
webertj@14807
   365
				allparams)
webertj@14456
   366
	in
webertj@14807
   367
		{sizes=sizes, minsize=minsize, maxsize=maxsize, maxvars=maxvars, maxtime=maxtime, satsolver=satsolver}
webertj@14807
   368
	end;
webertj@14807
   369
webertj@14807
   370
webertj@14807
   371
(* ------------------------------------------------------------------------- *)
webertj@14807
   372
(* TRANSLATION HOL -> PROPOSITIONAL LOGIC, BOOLEAN ASSIGNMENT -> MODEL       *)
webertj@14807
   373
(* ------------------------------------------------------------------------- *)
webertj@14807
   374
webertj@14807
   375
(* ------------------------------------------------------------------------- *)
webertj@15335
   376
(* typ_of_dtyp: converts a data type ('DatatypeAux.dtyp') into a type        *)
webertj@15335
   377
(*              ('Term.typ'), given type parameters for the data type's type *)
webertj@15335
   378
(*              arguments                                                    *)
webertj@15335
   379
(* ------------------------------------------------------------------------- *)
webertj@15335
   380
webertj@15335
   381
	(* DatatypeAux.descr -> (DatatypeAux.dtyp * Term.typ) list -> DatatypeAux.dtyp -> Term.typ *)
webertj@15335
   382
webertj@15335
   383
	fun typ_of_dtyp descr typ_assoc (DatatypeAux.DtTFree a) =
webertj@15335
   384
		(* replace a 'DtTFree' variable by the associated type *)
haftmann@17314
   385
		(the o AList.lookup (op =) typ_assoc) (DatatypeAux.DtTFree a)
webertj@15547
   386
	  | typ_of_dtyp descr typ_assoc (DatatypeAux.DtType (s, ds)) =
webertj@15547
   387
		Type (s, map (typ_of_dtyp descr typ_assoc) ds)
webertj@15335
   388
	  | typ_of_dtyp descr typ_assoc (DatatypeAux.DtRec i) =
webertj@15335
   389
		let
haftmann@17314
   390
			val (s, ds, _) = (the o AList.lookup (op =) descr) i
webertj@15335
   391
		in
webertj@15335
   392
			Type (s, map (typ_of_dtyp descr typ_assoc) ds)
webertj@15547
   393
		end;
webertj@15335
   394
webertj@15335
   395
(* ------------------------------------------------------------------------- *)
webertj@14807
   396
(* collect_axioms: collects (monomorphic, universally quantified versions    *)
webertj@14807
   397
(*                 of) all HOL axioms that are relevant w.r.t 't'            *)
webertj@14807
   398
(* ------------------------------------------------------------------------- *)
webertj@14807
   399
webertj@15547
   400
	(* Note: to make the collection of axioms more easily extensible, this    *)
webertj@14807
   401
	(*       function could be based on user-supplied "axiom collectors",     *)
webertj@14807
   402
	(*       similar to 'interpret'/interpreters or 'print'/printers          *)
webertj@14807
   403
webertj@14807
   404
	(* theory -> Term.term -> Term.term list *)
webertj@14807
   405
webertj@14807
   406
	(* Which axioms are "relevant" for a particular term/type goes hand in    *)
webertj@14807
   407
	(* hand with the interpretation of that term/type by its interpreter (see *)
webertj@14807
   408
	(* way below): if the interpretation respects an axiom anyway, the axiom  *)
webertj@14807
   409
	(* does not need to be added as a constraint here.                        *)
webertj@14807
   410
webertj@14807
   411
	(* When an axiom is added as relevant, further axioms may need to be      *)
webertj@14807
   412
	(* added as well (e.g. when a constant is defined in terms of other       *)
webertj@14807
   413
	(* constants).  To avoid infinite recursion (which should not happen for  *)
webertj@14807
   414
	(* constants anyway, but it could happen for "typedef"-related axioms,    *)
webertj@14807
   415
	(* since they contain the type again), we use an accumulator 'axs' and    *)
webertj@14807
   416
	(* add a relevant axiom only if it is not in 'axs' yet.                   *)
webertj@14807
   417
webertj@14807
   418
	fun collect_axioms thy t =
webertj@14807
   419
	let
wenzelm@14984
   420
		val _ = immediate_output "Adding axioms..."
webertj@14807
   421
		(* (string * Term.term) list *)
wenzelm@16331
   422
		val axioms = Theory.all_axioms_of thy;
webertj@15547
   423
		(* string list *)
haftmann@21078
   424
		val rec_names = Symtab.fold (append o #rec_names o snd)
haftmann@21078
   425
          (DatatypePackage.get_datatypes thy) [];
webertj@15547
   426
		(* string list *)
haftmann@21056
   427
		val const_of_class_names = map Logic.const_of_class (Sign.classes thy)
webertj@14807
   428
		(* given a constant 's' of type 'T', which is a subterm of 't', where  *)
webertj@14807
   429
		(* 't' has a (possibly) more general type, the schematic type          *)
webertj@15547
   430
		(* variables in 't' are instantiated to match the type 'T' (may raise  *)
webertj@15547
   431
		(* Type.TYPE_MATCH)                                                    *)
webertj@14807
   432
		(* (string * Term.typ) * Term.term -> Term.term *)
webertj@14807
   433
		fun specialize_type ((s, T), t) =
webertj@14807
   434
		let
webertj@14807
   435
			fun find_typeSubs (Const (s', T')) =
webertj@14807
   436
				(if s=s' then
wenzelm@16935
   437
					SOME (Sign.typ_match thy (T', T) Vartab.empty) handle Type.TYPE_MATCH => NONE
webertj@14807
   438
				else
webertj@15547
   439
					NONE)
skalberg@15531
   440
			  | find_typeSubs (Free _)           = NONE
skalberg@15531
   441
			  | find_typeSubs (Var _)            = NONE
skalberg@15531
   442
			  | find_typeSubs (Bound _)          = NONE
webertj@14807
   443
			  | find_typeSubs (Abs (_, _, body)) = find_typeSubs body
skalberg@15531
   444
			  | find_typeSubs (t1 $ t2)          = (case find_typeSubs t1 of SOME x => SOME x | NONE => find_typeSubs t2)
webertj@14807
   445
			val typeSubs = (case find_typeSubs t of
skalberg@15531
   446
				  SOME x => x
webertj@15547
   447
				| NONE   => raise Type.TYPE_MATCH (* no match found - perhaps due to sort constraints *))
webertj@14807
   448
		in
wenzelm@20548
   449
			map_types
webertj@14807
   450
				(map_type_tvar
berghofe@15794
   451
					(fn v =>
berghofe@15794
   452
						case Type.lookup (typeSubs, v) of
skalberg@15531
   453
						  NONE =>
webertj@14807
   454
							(* schematic type variable not instantiated *)
webertj@14807
   455
							raise REFUTE ("collect_axioms", "term " ^ Sign.string_of_term (sign_of thy) t ^ " still has a polymorphic type (after instantiating type of " ^ quote s ^ ")")
skalberg@15531
   456
						| SOME typ =>
webertj@14807
   457
							typ))
webertj@14807
   458
					t
webertj@14807
   459
		end
webertj@15280
   460
		(* applies a type substitution 'typeSubs' for all type variables in a  *)
webertj@15280
   461
		(* term 't'                                                            *)
berghofe@15794
   462
		(* (Term.sort * Term.typ) Term.Vartab.table -> Term.term -> Term.term *)
webertj@15280
   463
		fun monomorphic_term typeSubs t =
wenzelm@20548
   464
			map_types (map_type_tvar
berghofe@15794
   465
				(fn v =>
berghofe@15794
   466
					case Type.lookup (typeSubs, v) of
skalberg@15531
   467
					  NONE =>
webertj@15280
   468
						(* schematic type variable not instantiated *)
wenzelm@18678
   469
						error ""
skalberg@15531
   470
					| SOME typ =>
webertj@15280
   471
						typ)) t
webertj@14807
   472
		(* Term.term list * Term.typ -> Term.term list *)
webertj@15547
   473
		fun collect_sort_axioms (axs, T) =
webertj@15547
   474
			let
webertj@15547
   475
				(* collect the axioms for a single 'class' (but not for its superclasses) *)
webertj@15547
   476
				(* Term.term list * string -> Term.term list *)
webertj@15547
   477
				fun collect_class_axioms (axs, class) =
webertj@15547
   478
					let
webertj@15547
   479
						(* obtain the axioms generated by the "axclass" command *)
webertj@15547
   480
						(* (string * Term.term) list *)
wenzelm@18932
   481
						val class_axioms             = List.filter (fn (s, _) => String.isPrefix (Logic.const_of_class class ^ ".axioms_") s) axioms
webertj@15547
   482
						(* replace the one schematic type variable in each axiom by the actual type 'T' *)
webertj@15547
   483
						(* (string * Term.term) list *)
webertj@15547
   484
						val monomorphic_class_axioms = map (fn (axname, ax) =>
webertj@15547
   485
							let
berghofe@15794
   486
								val (idx, S) = (case term_tvars ax of
webertj@15547
   487
									  [is] => is
webertj@15547
   488
									| _    => raise REFUTE ("collect_axioms", "class axiom " ^ axname ^ " (" ^ Sign.string_of_term (sign_of thy) ax ^ ") does not contain exactly one type variable"))
webertj@15547
   489
							in
berghofe@15794
   490
								(axname, monomorphic_term (Vartab.make [(idx, (S, T))]) ax)
webertj@15547
   491
							end) class_axioms
webertj@15547
   492
						(* Term.term list * (string * Term.term) list -> Term.term list *)
webertj@15547
   493
						fun collect_axiom (axs, (axname, ax)) =
wenzelm@20073
   494
							if member (op aconv) axs ax then
webertj@15547
   495
								axs
webertj@15547
   496
							else (
webertj@15547
   497
								immediate_output (" " ^ axname);
webertj@15547
   498
								collect_term_axioms (ax :: axs, ax)
webertj@15547
   499
							)
webertj@15547
   500
					in
skalberg@15570
   501
						Library.foldl collect_axiom (axs, monomorphic_class_axioms)
webertj@15547
   502
					end
webertj@15547
   503
				(* string list *)
webertj@15547
   504
				val sort = (case T of
webertj@15547
   505
					  TFree (_, sort) => sort
webertj@15547
   506
					| TVar (_, sort)  => sort
webertj@15547
   507
					| _               => raise REFUTE ("collect_axioms", "type " ^ Sign.string_of_typ (sign_of thy) T ^ " is not a variable"))
webertj@15547
   508
				(* obtain all superclasses of classes in 'sort' *)
webertj@15547
   509
				(* string list *)
wenzelm@19642
   510
				val superclasses = distinct (op =) (sort @ maps (Sign.super_classes thy) sort)
webertj@15547
   511
			in
skalberg@15570
   512
				Library.foldl collect_class_axioms (axs, superclasses)
webertj@15547
   513
			end
webertj@15547
   514
		(* Term.term list * Term.typ -> Term.term list *)
webertj@15547
   515
		and collect_type_axioms (axs, T) =
webertj@14807
   516
			case T of
webertj@14807
   517
			(* simple types *)
webertj@14807
   518
			  Type ("prop", [])      => axs
webertj@14807
   519
			| Type ("fun", [T1, T2]) => collect_type_axioms (collect_type_axioms (axs, T1), T2)
webertj@14807
   520
			| Type ("set", [T1])     => collect_type_axioms (axs, T1)
webertj@15547
   521
			| Type ("itself", [T1])  => collect_type_axioms (axs, T1)  (* axiomatic type classes *)
webertj@14807
   522
			| Type (s, Ts)           =>
webertj@14807
   523
				let
webertj@14807
   524
					(* look up the definition of a type, as created by "typedef" *)
webertj@14807
   525
					(* (string * Term.term) list -> (string * Term.term) option *)
webertj@14807
   526
					fun get_typedefn [] =
skalberg@15531
   527
						NONE
webertj@14807
   528
					  | get_typedefn ((axname,ax)::axms) =
webertj@14807
   529
						(let
webertj@14807
   530
							(* Term.term -> Term.typ option *)
webertj@14807
   531
							fun type_of_type_definition (Const (s', T')) =
webertj@14807
   532
								if s'="Typedef.type_definition" then
skalberg@15531
   533
									SOME T'
webertj@14807
   534
								else
skalberg@15531
   535
									NONE
skalberg@15531
   536
							  | type_of_type_definition (Free _)           = NONE
skalberg@15531
   537
							  | type_of_type_definition (Var _)            = NONE
skalberg@15531
   538
							  | type_of_type_definition (Bound _)          = NONE
webertj@14807
   539
							  | type_of_type_definition (Abs (_, _, body)) = type_of_type_definition body
skalberg@15531
   540
							  | type_of_type_definition (t1 $ t2)          = (case type_of_type_definition t1 of SOME x => SOME x | NONE => type_of_type_definition t2)
webertj@14807
   541
						in
webertj@14807
   542
							case type_of_type_definition ax of
skalberg@15531
   543
							  SOME T' =>
webertj@14807
   544
								let
webertj@14807
   545
									val T''      = (domain_type o domain_type) T'
wenzelm@16935
   546
									val typeSubs = Sign.typ_match thy (T'', T) Vartab.empty
webertj@14807
   547
								in
skalberg@15531
   548
									SOME (axname, monomorphic_term typeSubs ax)
webertj@14807
   549
								end
skalberg@15531
   550
							| NONE =>
webertj@14807
   551
								get_typedefn axms
webertj@14807
   552
						end
wenzelm@18678
   553
						handle ERROR _         => get_typedefn axms
webertj@14807
   554
						     | MATCH           => get_typedefn axms
webertj@14807
   555
						     | Type.TYPE_MATCH => get_typedefn axms)
webertj@14807
   556
				in
haftmann@19346
   557
					case DatatypePackage.get_datatype thy s of
skalberg@15531
   558
					  SOME info =>  (* inductive datatype *)
webertj@14807
   559
							(* only collect relevant type axioms for the argument types *)
skalberg@15570
   560
							Library.foldl collect_type_axioms (axs, Ts)
skalberg@15531
   561
					| NONE =>
webertj@14807
   562
						(case get_typedefn axioms of
skalberg@15531
   563
						  SOME (axname, ax) => 
wenzelm@20073
   564
							if member (op aconv) axs ax then
webertj@15547
   565
								(* only collect relevant type axioms for the argument types *)
skalberg@15570
   566
								Library.foldl collect_type_axioms (axs, Ts)
webertj@14807
   567
							else
wenzelm@14984
   568
								(immediate_output (" " ^ axname);
webertj@14807
   569
								collect_term_axioms (ax :: axs, ax))
skalberg@15531
   570
						| NONE =>
webertj@15547
   571
							(* unspecified type, perhaps introduced with 'typedecl' *)
webertj@14807
   572
							(* at least collect relevant type axioms for the argument types *)
skalberg@15570
   573
							Library.foldl collect_type_axioms (axs, Ts))
webertj@14807
   574
				end
webertj@15547
   575
			| TFree _                => collect_sort_axioms (axs, T)  (* axiomatic type classes *)
webertj@15547
   576
			| TVar _                 => collect_sort_axioms (axs, T)  (* axiomatic type classes *)
webertj@14807
   577
		(* Term.term list * Term.term -> Term.term list *)
webertj@14807
   578
		and collect_term_axioms (axs, t) =
webertj@14807
   579
			case t of
webertj@14807
   580
			(* Pure *)
webertj@14807
   581
			  Const ("all", _)                => axs
webertj@14807
   582
			| Const ("==", _)                 => axs
webertj@14807
   583
			| Const ("==>", _)                => axs
webertj@15547
   584
			| Const ("TYPE", T)               => collect_type_axioms (axs, T)  (* axiomatic type classes *)
webertj@14807
   585
			(* HOL *)
webertj@14807
   586
			| Const ("Trueprop", _)           => axs
webertj@14807
   587
			| Const ("Not", _)                => axs
webertj@14807
   588
			| Const ("True", _)               => axs  (* redundant, since 'True' is also an IDT constructor *)
webertj@14807
   589
			| Const ("False", _)              => axs  (* redundant, since 'False' is also an IDT constructor *)
webertj@14807
   590
			| Const ("arbitrary", T)          => collect_type_axioms (axs, T)
webertj@14807
   591
			| Const ("The", T)                =>
webertj@14807
   592
				let
haftmann@17314
   593
					val ax = specialize_type (("The", T), (the o AList.lookup (op =) axioms) "HOL.the_eq_trivial")
webertj@14807
   594
				in
wenzelm@20073
   595
					if member (op aconv) axs ax then
webertj@14807
   596
						collect_type_axioms (axs, T)
webertj@14807
   597
					else
wenzelm@14984
   598
						(immediate_output " HOL.the_eq_trivial";
webertj@14807
   599
						collect_term_axioms (ax :: axs, ax))
webertj@14807
   600
				end
webertj@14807
   601
			| Const ("Hilbert_Choice.Eps", T) =>
webertj@14807
   602
				let
haftmann@17314
   603
					val ax = specialize_type (("Hilbert_Choice.Eps", T),
haftmann@17314
   604
                      (the o AList.lookup (op =) axioms) "Hilbert_Choice.someI")
webertj@14807
   605
				in
wenzelm@20073
   606
					if member (op aconv) axs ax then
webertj@14807
   607
						collect_type_axioms (axs, T)
webertj@14807
   608
					else
wenzelm@14984
   609
						(immediate_output " Hilbert_Choice.someI";
webertj@14807
   610
						collect_term_axioms (ax :: axs, ax))
webertj@14807
   611
				end
webertj@14807
   612
			| Const ("All", _) $ t1           => collect_term_axioms (axs, t1)
webertj@14807
   613
			| Const ("Ex", _) $ t1            => collect_term_axioms (axs, t1)
webertj@14807
   614
			| Const ("op =", T)               => collect_type_axioms (axs, T)
webertj@14807
   615
			| Const ("op &", _)               => axs
webertj@14807
   616
			| Const ("op |", _)               => axs
webertj@14807
   617
			| Const ("op -->", _)             => axs
webertj@14807
   618
			(* sets *)
webertj@14807
   619
			| Const ("Collect", T)            => collect_type_axioms (axs, T)
webertj@14807
   620
			| Const ("op :", T)               => collect_type_axioms (axs, T)
webertj@14807
   621
			(* other optimizations *)
webertj@14807
   622
			| Const ("Finite_Set.card", T)    => collect_type_axioms (axs, T)
webertj@15571
   623
			| Const ("Finite_Set.Finites", T) => collect_type_axioms (axs, T)
haftmann@19277
   624
			| Const ("Orderings.less", T as Type ("fun", [Type ("nat", []), Type ("fun", [Type ("nat", []), Type ("bool", [])])])) => collect_type_axioms (axs, T)
haftmann@19233
   625
			| Const ("HOL.plus", T as Type ("fun", [Type ("nat", []), Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => collect_type_axioms (axs, T)
haftmann@19233
   626
			| Const ("HOL.minus", T as Type ("fun", [Type ("nat", []), Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => collect_type_axioms (axs, T)
haftmann@19233
   627
			| Const ("HOL.times", T as Type ("fun", [Type ("nat", []), Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => collect_type_axioms (axs, T)
webertj@15767
   628
			| Const ("List.op @", T)          => collect_type_axioms (axs, T)
webertj@16050
   629
			| Const ("Lfp.lfp", T)            => collect_type_axioms (axs, T)
webertj@16050
   630
			| Const ("Gfp.gfp", T)            => collect_type_axioms (axs, T)
webertj@21267
   631
			| Const ("fst", T)                => collect_type_axioms (axs, T)
webertj@21267
   632
			| Const ("snd", T)                => collect_type_axioms (axs, T)
webertj@14807
   633
			(* simply-typed lambda calculus *)
webertj@14807
   634
			| Const (s, T)                    =>
webertj@14807
   635
				let
webertj@14807
   636
					(* look up the definition of a constant, as created by "constdefs" *)
webertj@14807
   637
					(* string -> Term.typ -> (string * Term.term) list -> (string * Term.term) option *)
webertj@14807
   638
					fun get_defn [] =
skalberg@15531
   639
						NONE
webertj@15547
   640
					  | get_defn ((axname, ax)::axms) =
webertj@14807
   641
						(let
webertj@14807
   642
							val (lhs, _) = Logic.dest_equals ax  (* equations only *)
webertj@14807
   643
							val c        = head_of lhs
webertj@14807
   644
							val (s', T') = dest_Const c
webertj@14807
   645
						in
webertj@14807
   646
							if s=s' then
webertj@14807
   647
								let
wenzelm@16935
   648
									val typeSubs = Sign.typ_match thy (T', T) Vartab.empty
webertj@14807
   649
								in
skalberg@15531
   650
									SOME (axname, monomorphic_term typeSubs ax)
webertj@14807
   651
								end
webertj@14807
   652
							else
webertj@14807
   653
								get_defn axms
webertj@14807
   654
						end
wenzelm@18678
   655
						handle ERROR _         => get_defn axms
webertj@14807
   656
						     | TERM _          => get_defn axms
webertj@14807
   657
						     | Type.TYPE_MATCH => get_defn axms)
webertj@15547
   658
					(* axiomatic type classes *)
webertj@15547
   659
					(* unit -> bool *)
webertj@15547
   660
					fun is_const_of_class () =
webertj@15547
   661
						(* I'm not quite sure if checking the name 's' is sufficient, *)
webertj@15547
   662
						(* or if we should also check the type 'T'                    *)
webertj@15547
   663
						s mem const_of_class_names
webertj@15547
   664
					(* inductive data types *)
webertj@15547
   665
					(* unit -> bool *)
webertj@15547
   666
					fun is_IDT_constructor () =
webertj@15547
   667
						(case body_type T of
webertj@15547
   668
						  Type (s', _) =>
haftmann@19346
   669
							(case DatatypePackage.get_datatype_constrs thy s' of
webertj@15547
   670
							  SOME constrs =>
haftmann@19346
   671
								Library.exists (fn (cname, cty) =>
haftmann@19346
   672
								cname = s andalso Sign.typ_instance thy (T, cty))
webertj@15547
   673
									constrs
webertj@15547
   674
							| NONE =>
webertj@14807
   675
								false)
webertj@15547
   676
						| _  =>
webertj@15547
   677
							false)
webertj@15547
   678
					(* unit -> bool *)
webertj@15547
   679
					fun is_IDT_recursor () =
webertj@15547
   680
						(* I'm not quite sure if checking the name 's' is sufficient, *)
webertj@15547
   681
						(* or if we should also check the type 'T'                    *)
haftmann@21078
   682
						member (op =) rec_names s
webertj@14807
   683
				in
webertj@15547
   684
					if is_const_of_class () then
webertj@15547
   685
						(* axiomatic type classes: add "OFCLASS(?'a::c, c_class)" and *)
webertj@15547
   686
						(* the introduction rule "class.intro" as axioms              *)
webertj@15547
   687
						let
wenzelm@18932
   688
							val class   = Logic.class_of_const s
webertj@15547
   689
							val inclass = Logic.mk_inclass (TVar (("'a", 0), [class]), class)
webertj@15547
   690
							(* Term.term option *)
webertj@15547
   691
							val ofclass_ax = (SOME (specialize_type ((s, T), inclass)) handle Type.TYPE_MATCH => NONE)
haftmann@17314
   692
							val intro_ax   = (Option.map (fn t => specialize_type ((s, T), t))
haftmann@17314
   693
								(AList.lookup (op =) axioms (class ^ ".intro")) handle Type.TYPE_MATCH => NONE)
wenzelm@20073
   694
							val axs'       = (case ofclass_ax of NONE => axs | SOME ax => if member (op aconv) axs ax then
webertj@15547
   695
									(* collect relevant type axioms *)
webertj@15547
   696
									collect_type_axioms (axs, T)
webertj@15547
   697
								else
webertj@15547
   698
									(immediate_output (" " ^ Sign.string_of_term (sign_of thy) ax);
webertj@15547
   699
									collect_term_axioms (ax :: axs, ax)))
wenzelm@20073
   700
							val axs''      = (case intro_ax of NONE => axs' | SOME ax => if member (op aconv) axs' ax then
webertj@15547
   701
									(* collect relevant type axioms *)
webertj@15547
   702
									collect_type_axioms (axs', T)
webertj@15547
   703
								else
wenzelm@17274
   704
									(immediate_output (" " ^ s ^ ".intro");
webertj@15547
   705
									collect_term_axioms (ax :: axs', ax)))
webertj@15547
   706
						in
webertj@15547
   707
							axs''
webertj@15547
   708
						end
webertj@15547
   709
					else if is_IDT_constructor () then
webertj@14807
   710
						(* only collect relevant type axioms *)
webertj@14807
   711
						collect_type_axioms (axs, T)
webertj@15547
   712
					else if is_IDT_recursor () then
webertj@15125
   713
						(* only collect relevant type axioms *)
webertj@15125
   714
						collect_type_axioms (axs, T)
webertj@15547
   715
					else (
webertj@15547
   716
						case get_defn axioms of
skalberg@15531
   717
						  SOME (axname, ax) => 
wenzelm@20073
   718
							if member (op aconv) axs ax then
webertj@14807
   719
								(* collect relevant type axioms *)
webertj@14807
   720
								collect_type_axioms (axs, T)
webertj@14807
   721
							else
wenzelm@14984
   722
								(immediate_output (" " ^ axname);
webertj@14807
   723
								collect_term_axioms (ax :: axs, ax))
skalberg@15531
   724
						| NONE =>
webertj@14807
   725
							(* collect relevant type axioms *)
webertj@15547
   726
							collect_type_axioms (axs, T)
webertj@15547
   727
					)
webertj@14807
   728
				end
webertj@14807
   729
			| Free (_, T)                     => collect_type_axioms (axs, T)
webertj@14807
   730
			| Var (_, T)                      => collect_type_axioms (axs, T)
webertj@14807
   731
			| Bound i                         => axs
webertj@14807
   732
			| Abs (_, T, body)                => collect_term_axioms (collect_type_axioms (axs, T), body)
webertj@14807
   733
			| t1 $ t2                         => collect_term_axioms (collect_term_axioms (axs, t1), t2)
webertj@14807
   734
		(* universal closure over schematic variables *)
webertj@14807
   735
		(* Term.term -> Term.term *)
webertj@14807
   736
		fun close_form t =
webertj@14807
   737
		let
webertj@14807
   738
			(* (Term.indexname * Term.typ) list *)
webertj@14807
   739
			val vars = sort_wrt (fst o fst) (map dest_Var (term_vars t))
webertj@14807
   740
		in
skalberg@15570
   741
			Library.foldl
webertj@15547
   742
				(fn (t', ((x, i), T)) => (Term.all T) $ Abs (x, T, abstract_over (Var((x, i), T), t')))
webertj@14807
   743
				(t, vars)
webertj@14807
   744
		end
webertj@14807
   745
		(* Term.term list *)
webertj@14807
   746
		val result = map close_form (collect_term_axioms ([], t))
webertj@14807
   747
		val _ = writeln " ...done."
webertj@14807
   748
	in
webertj@14807
   749
		result
webertj@14456
   750
	end;
webertj@14456
   751
webertj@14456
   752
(* ------------------------------------------------------------------------- *)
webertj@14807
   753
(* ground_types: collects all ground types in a term (including argument     *)
webertj@14807
   754
(*               types of other types), suppressing duplicates.  Does not    *)
webertj@14807
   755
(*               return function types, set types, non-recursive IDTs, or    *)
webertj@14807
   756
(*               'propT'.  For IDTs, also the argument types of constructors *)
webertj@14807
   757
(*               are considered.                                             *)
webertj@14807
   758
(* ------------------------------------------------------------------------- *)
webertj@14807
   759
webertj@14807
   760
	(* theory -> Term.term -> Term.typ list *)
webertj@14807
   761
webertj@14807
   762
	fun ground_types thy t =
webertj@14807
   763
	let
webertj@14807
   764
		(* Term.typ * Term.typ list -> Term.typ list *)
webertj@14807
   765
		fun collect_types (T, acc) =
webertj@14807
   766
			if T mem acc then
webertj@14807
   767
				acc  (* prevent infinite recursion (for IDTs) *)
webertj@14807
   768
			else
webertj@14807
   769
				(case T of
webertj@14807
   770
				  Type ("fun", [T1, T2]) => collect_types (T1, collect_types (T2, acc))
webertj@14807
   771
				| Type ("prop", [])      => acc
webertj@14807
   772
				| Type ("set", [T1])     => collect_types (T1, acc)
webertj@14807
   773
				| Type (s, Ts)           =>
haftmann@19346
   774
					(case DatatypePackage.get_datatype thy s of
skalberg@15531
   775
					  SOME info =>  (* inductive datatype *)
webertj@14807
   776
						let
webertj@14807
   777
							val index               = #index info
webertj@14807
   778
							val descr               = #descr info
haftmann@17314
   779
							val (_, dtyps, constrs) = (the o AList.lookup (op =) descr) index
webertj@14807
   780
							val typ_assoc           = dtyps ~~ Ts
webertj@14807
   781
							(* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
webertj@14807
   782
							val _ = (if Library.exists (fn d =>
webertj@14807
   783
									case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
webertj@14807
   784
								then
webertj@14807
   785
									raise REFUTE ("ground_types", "datatype argument (for type " ^ Sign.string_of_typ (sign_of thy) (Type (s, Ts)) ^ ") is not a variable")
webertj@14807
   786
								else
webertj@14807
   787
									())
webertj@14807
   788
							(* if the current type is a recursive IDT (i.e. a depth is required), add it to 'acc' *)
webertj@14807
   789
							val acc' = (if Library.exists (fn (_, ds) => Library.exists DatatypeAux.is_rec_type ds) constrs then
haftmann@20854
   790
									insert (op =) T acc
webertj@14807
   791
								else
webertj@14807
   792
									acc)
webertj@14807
   793
							(* collect argument types *)
skalberg@15574
   794
							val acc_args = foldr collect_types acc' Ts
webertj@14807
   795
							(* collect constructor types *)
skalberg@15574
   796
							val acc_constrs = foldr collect_types acc_args (List.concat (map (fn (_, ds) => map (typ_of_dtyp descr typ_assoc) ds) constrs))
webertj@14807
   797
						in
webertj@14807
   798
							acc_constrs
webertj@14807
   799
						end
skalberg@15531
   800
					| NONE =>  (* not an inductive datatype, e.g. defined via "typedef" or "typedecl" *)
haftmann@20854
   801
						insert (op =) T (foldr collect_types acc Ts))
haftmann@20854
   802
				| TFree _                => insert (op =) T acc
haftmann@20854
   803
				| TVar _                 => insert (op =) T acc)
webertj@14807
   804
	in
webertj@14807
   805
		it_term_types collect_types (t, [])
webertj@14807
   806
	end;
webertj@14807
   807
webertj@14807
   808
(* ------------------------------------------------------------------------- *)
webertj@14807
   809
(* string_of_typ: (rather naive) conversion from types to strings, used to   *)
webertj@14807
   810
(*                look up the size of a type in 'sizes'.  Parameterized      *)
webertj@14807
   811
(*                types with different parameters (e.g. "'a list" vs. "bool  *)
webertj@14807
   812
(*                list") are identified.                                     *)
webertj@14807
   813
(* ------------------------------------------------------------------------- *)
webertj@14807
   814
webertj@14807
   815
	(* Term.typ -> string *)
webertj@14807
   816
webertj@14807
   817
	fun string_of_typ (Type (s, _))     = s
webertj@14807
   818
	  | string_of_typ (TFree (s, _))    = s
webertj@14807
   819
	  | string_of_typ (TVar ((s,_), _)) = s;
webertj@14807
   820
webertj@14807
   821
(* ------------------------------------------------------------------------- *)
webertj@14807
   822
(* first_universe: returns the "first" (i.e. smallest) universe by assigning *)
webertj@14807
   823
(*                 'minsize' to every type for which no size is specified in *)
webertj@14807
   824
(*                 'sizes'                                                   *)
webertj@14807
   825
(* ------------------------------------------------------------------------- *)
webertj@14807
   826
webertj@14807
   827
	(* Term.typ list -> (string * int) list -> int -> (Term.typ * int) list *)
webertj@14807
   828
webertj@14807
   829
	fun first_universe xs sizes minsize =
webertj@14807
   830
	let
webertj@14807
   831
		fun size_of_typ T =
haftmann@17314
   832
			case AList.lookup (op =) sizes (string_of_typ T) of
skalberg@15531
   833
			  SOME n => n
skalberg@15531
   834
			| NONE   => minsize
webertj@14807
   835
	in
webertj@14807
   836
		map (fn T => (T, size_of_typ T)) xs
webertj@14807
   837
	end;
webertj@14807
   838
webertj@14807
   839
(* ------------------------------------------------------------------------- *)
webertj@14807
   840
(* next_universe: enumerates all universes (i.e. assignments of sizes to     *)
webertj@14807
   841
(*                types), where the minimal size of a type is given by       *)
webertj@14807
   842
(*                'minsize', the maximal size is given by 'maxsize', and a   *)
webertj@14807
   843
(*                type may have a fixed size given in 'sizes'                *)
webertj@14456
   844
(* ------------------------------------------------------------------------- *)
webertj@14456
   845
webertj@14807
   846
	(* (Term.typ * int) list -> (string * int) list -> int -> int -> (Term.typ * int) list option *)
webertj@14456
   847
webertj@14807
   848
	fun next_universe xs sizes minsize maxsize =
webertj@14456
   849
	let
webertj@14807
   850
		(* creates the "first" list of length 'len', where the sum of all list *)
webertj@14807
   851
		(* elements is 'sum', and the length of the list is 'len'              *)
webertj@14807
   852
		(* int -> int -> int -> int list option *)
webertj@15547
   853
		fun make_first _ 0 sum =
webertj@14807
   854
			if sum=0 then
skalberg@15531
   855
				SOME []
webertj@14807
   856
			else
skalberg@15531
   857
				NONE
webertj@15547
   858
		  | make_first max len sum =
webertj@14807
   859
			if sum<=max orelse max<0 then
skalberg@15570
   860
				Option.map (fn xs' => sum :: xs') (make_first max (len-1) 0)
webertj@14807
   861
			else
skalberg@15570
   862
				Option.map (fn xs' => max :: xs') (make_first max (len-1) (sum-max))
webertj@14807
   863
		(* enumerates all int lists with a fixed length, where 0<=x<='max' for *)
webertj@14807
   864
		(* all list elements x (unless 'max'<0)                                *)
webertj@15547
   865
		(* int -> int -> int -> int list -> int list option *)
webertj@15547
   866
		fun next max len sum [] =
webertj@15547
   867
			NONE
webertj@15547
   868
		  | next max len sum [x] =
webertj@15547
   869
			(* we've reached the last list element, so there's no shift possible *)
webertj@15547
   870
			make_first max (len+1) (sum+x+1)  (* increment 'sum' by 1 *)
webertj@15547
   871
		  | next max len sum (x1::x2::xs) =
webertj@15547
   872
			if x1>0 andalso (x2<max orelse max<0) then
webertj@15547
   873
				(* we can shift *)
skalberg@15570
   874
				SOME (valOf (make_first max (len+1) (sum+x1-1)) @ (x2+1) :: xs)
webertj@15547
   875
			else
webertj@15547
   876
				(* continue search *)
webertj@15547
   877
				next max (len+1) (sum+x1) (x2::xs)
webertj@14807
   878
		(* only consider those types for which the size is not fixed *)
haftmann@17314
   879
		val mutables = List.filter (not o (AList.defined (op =) sizes) o string_of_typ o fst) xs
webertj@14807
   880
		(* subtract 'minsize' from every size (will be added again at the end) *)
webertj@14807
   881
		val diffs = map (fn (_, n) => n-minsize) mutables
webertj@14807
   882
	in
webertj@15547
   883
		case next (maxsize-minsize) 0 0 diffs of
skalberg@15531
   884
		  SOME diffs' =>
webertj@14807
   885
			(* merge with those types for which the size is fixed *)
skalberg@15531
   886
			SOME (snd (foldl_map (fn (ds, (T, _)) =>
haftmann@17314
   887
				case AList.lookup (op =) sizes (string_of_typ T) of
webertj@15547
   888
				  SOME n => (ds, (T, n))                    (* return the fixed size *)
webertj@15547
   889
				| NONE   => (tl ds, (T, minsize + hd ds)))  (* consume the head of 'ds', add 'minsize' *)
webertj@14807
   890
				(diffs', xs)))
skalberg@15531
   891
		| NONE =>
skalberg@15531
   892
			NONE
webertj@14807
   893
	end;
webertj@14807
   894
webertj@14807
   895
(* ------------------------------------------------------------------------- *)
webertj@14807
   896
(* toTrue: converts the interpretation of a Boolean value to a propositional *)
webertj@14807
   897
(*         formula that is true iff the interpretation denotes "true"        *)
webertj@14807
   898
(* ------------------------------------------------------------------------- *)
webertj@14807
   899
webertj@14807
   900
	(* interpretation -> prop_formula *)
webertj@14807
   901
webertj@15767
   902
	fun toTrue (Leaf [fm, _]) = fm
webertj@15767
   903
	  | toTrue _              = raise REFUTE ("toTrue", "interpretation does not denote a Boolean value");
webertj@14807
   904
webertj@14807
   905
(* ------------------------------------------------------------------------- *)
webertj@14807
   906
(* toFalse: converts the interpretation of a Boolean value to a              *)
webertj@14807
   907
(*          propositional formula that is true iff the interpretation        *)
webertj@14807
   908
(*          denotes "false"                                                  *)
webertj@14807
   909
(* ------------------------------------------------------------------------- *)
webertj@14807
   910
webertj@14807
   911
	(* interpretation -> prop_formula *)
webertj@14807
   912
webertj@15767
   913
	fun toFalse (Leaf [_, fm]) = fm
webertj@15767
   914
	  | toFalse _              = raise REFUTE ("toFalse", "interpretation does not denote a Boolean value");
webertj@14807
   915
webertj@14807
   916
(* ------------------------------------------------------------------------- *)
webertj@14807
   917
(* find_model: repeatedly calls 'interpret' with appropriate parameters,     *)
webertj@14807
   918
(*             applies a SAT solver, and (in case a model is found) displays *)
webertj@14807
   919
(*             the model to the user by calling 'print_model'                *)
webertj@14807
   920
(* thy       : the current theory                                            *)
webertj@14807
   921
(* {...}     : parameters that control the translation/model generation      *)
webertj@14807
   922
(* t         : term to be translated into a propositional formula            *)
webertj@14807
   923
(* negate    : if true, find a model that makes 't' false (rather than true) *)
webertj@14807
   924
(* ------------------------------------------------------------------------- *)
webertj@14807
   925
webertj@14807
   926
	(* theory -> params -> Term.term -> bool -> unit *)
webertj@14807
   927
webertj@14807
   928
	fun find_model thy {sizes, minsize, maxsize, maxvars, maxtime, satsolver} t negate =
webertj@14807
   929
	let
webertj@14807
   930
		(* unit -> unit *)
webertj@14807
   931
		fun wrapper () =
webertj@14807
   932
		let
webertj@14807
   933
			(* Term.term list *)
webertj@14807
   934
			val axioms = collect_axioms thy t
webertj@14807
   935
			(* Term.typ list *)
skalberg@15570
   936
			val types  = Library.foldl (fn (acc, t') => acc union (ground_types thy t')) ([], t :: axioms)
webertj@14807
   937
			val _      = writeln ("Ground types: "
webertj@14807
   938
				^ (if null types then "none."
webertj@14807
   939
				   else commas (map (Sign.string_of_typ (sign_of thy)) types)))
webertj@15547
   940
			(* we can only consider fragments of recursive IDTs, so we issue a  *)
webertj@15547
   941
			(* warning if the formula contains a recursive IDT                  *)
webertj@15547
   942
			(* TODO: no warning needed for /positive/ occurrences of IDTs       *)
webertj@15547
   943
			val _ = if Library.exists (fn
webertj@15547
   944
				  Type (s, _) =>
haftmann@19346
   945
					(case DatatypePackage.get_datatype thy s of
webertj@15547
   946
					  SOME info =>  (* inductive datatype *)
webertj@15547
   947
						let
webertj@15547
   948
							val index           = #index info
webertj@15547
   949
							val descr           = #descr info
haftmann@17314
   950
							val (_, _, constrs) = (the o AList.lookup (op =) descr) index
webertj@15547
   951
						in
webertj@15547
   952
							(* recursive datatype? *)
webertj@15547
   953
							Library.exists (fn (_, ds) => Library.exists DatatypeAux.is_rec_type ds) constrs
webertj@15547
   954
						end
webertj@15547
   955
					| NONE => false)
webertj@15547
   956
				| _ => false) types then
webertj@15547
   957
					warning "Term contains a recursive datatype; countermodel(s) may be spurious!"
webertj@15547
   958
				else
webertj@15547
   959
					()
webertj@14807
   960
			(* (Term.typ * int) list -> unit *)
webertj@14807
   961
			fun find_model_loop universe =
webertj@15334
   962
			let
webertj@14807
   963
				val init_model             = (universe, [])
webertj@15547
   964
				val init_args              = {maxvars = maxvars, def_eq = false, next_idx = 1, bounds = [], wellformed = True}
wenzelm@14984
   965
				val _                      = immediate_output ("Translating term (sizes: " ^ commas (map (fn (_, n) => string_of_int n) universe) ^ ") ...")
webertj@14807
   966
				(* translate 't' and all axioms *)
webertj@14807
   967
				val ((model, args), intrs) = foldl_map (fn ((m, a), t') =>
webertj@14807
   968
					let
webertj@14807
   969
						val (i, m', a') = interpret thy m a t'
webertj@14807
   970
					in
webertj@15547
   971
						(* set 'def_eq' to 'true' *)
webertj@15547
   972
						((m', {maxvars = #maxvars a', def_eq = true, next_idx = #next_idx a', bounds = #bounds a', wellformed = #wellformed a'}), i)
webertj@14807
   973
					end) ((init_model, init_args), t :: axioms)
webertj@14807
   974
				(* make 't' either true or false, and make all axioms true, and *)
webertj@14807
   975
				(* add the well-formedness side condition                       *)
webertj@14807
   976
				val fm_t  = (if negate then toFalse else toTrue) (hd intrs)
webertj@14807
   977
				val fm_ax = PropLogic.all (map toTrue (tl intrs))
webertj@14807
   978
				val fm    = PropLogic.all [#wellformed args, fm_ax, fm_t]
webertj@14456
   979
			in
wenzelm@14984
   980
				immediate_output " invoking SAT solver...";
webertj@14965
   981
				(case SatSolver.invoke_solver satsolver fm of
webertj@14965
   982
				  SatSolver.SATISFIABLE assignment =>
webertj@15547
   983
					(writeln " model found!";
webertj@15547
   984
					writeln ("*** Model found: ***\n" ^ print_model thy model (fn i => case assignment i of SOME b => b | NONE => true)))
webertj@17493
   985
				| SatSolver.UNSATISFIABLE _ =>
webertj@15547
   986
					(immediate_output " no model exists.\n";
webertj@15547
   987
					case next_universe universe sizes minsize maxsize of
webertj@15547
   988
					  SOME universe' => find_model_loop universe'
webertj@15547
   989
					| NONE           => writeln "Search terminated, no larger universe within the given limits.")
webertj@15547
   990
				| SatSolver.UNKNOWN =>
wenzelm@14984
   991
					(immediate_output " no model found.\n";
webertj@14807
   992
					case next_universe universe sizes minsize maxsize of
skalberg@15531
   993
					  SOME universe' => find_model_loop universe'
webertj@15547
   994
					| NONE           => writeln "Search terminated, no larger universe within the given limits.")
webertj@15547
   995
				) handle SatSolver.NOT_CONFIGURED =>
webertj@14965
   996
					error ("SAT solver " ^ quote satsolver ^ " is not configured.")
webertj@14807
   997
			end handle MAXVARS_EXCEEDED =>
webertj@14807
   998
				writeln ("\nSearch terminated, number of Boolean variables (" ^ string_of_int maxvars ^ " allowed) exceeded.")
webertj@14807
   999
			in
webertj@14807
  1000
				find_model_loop (first_universe types sizes minsize)
webertj@14456
  1001
			end
webertj@14807
  1002
		in
webertj@14807
  1003
			(* some parameter sanity checks *)
webertj@14807
  1004
			assert (minsize>=1) ("\"minsize\" is " ^ string_of_int minsize ^ ", must be at least 1");
webertj@14807
  1005
			assert (maxsize>=1) ("\"maxsize\" is " ^ string_of_int maxsize ^ ", must be at least 1");
webertj@14807
  1006
			assert (maxsize>=minsize) ("\"maxsize\" (=" ^ string_of_int maxsize ^ ") is less than \"minsize\" (=" ^ string_of_int minsize ^ ").");
webertj@14807
  1007
			assert (maxvars>=0) ("\"maxvars\" is " ^ string_of_int maxvars ^ ", must be at least 0");
webertj@14807
  1008
			assert (maxtime>=0) ("\"maxtime\" is " ^ string_of_int maxtime ^ ", must be at least 0");
webertj@15547
  1009
			(* enter loop with or without time limit *)
webertj@14807
  1010
			writeln ("Trying to find a model that " ^ (if negate then "refutes" else "satisfies") ^ ": "
webertj@14807
  1011
				^ Sign.string_of_term (sign_of thy) t);
webertj@15547
  1012
			if maxtime>0 then (
webertj@18760
  1013
				interrupt_timeout (Time.fromSeconds (Int.toLarge maxtime))
webertj@14807
  1014
					wrapper ()
webertj@18760
  1015
				handle Interrupt =>
webertj@14807
  1016
					writeln ("\nSearch terminated, time limit ("
webertj@14965
  1017
						^ string_of_int maxtime ^ (if maxtime=1 then " second" else " seconds")
webertj@15547
  1018
						^ ") exceeded.")
webertj@15547
  1019
			) else
webertj@14807
  1020
				wrapper ()
webertj@14807
  1021
		end;
webertj@14456
  1022
webertj@14456
  1023
webertj@14456
  1024
(* ------------------------------------------------------------------------- *)
webertj@14456
  1025
(* INTERFACE, PART 2: FINDING A MODEL                                        *)
webertj@14350
  1026
(* ------------------------------------------------------------------------- *)
webertj@14350
  1027
webertj@14350
  1028
(* ------------------------------------------------------------------------- *)
webertj@14456
  1029
(* satisfy_term: calls 'find_model' to find a model that satisfies 't'       *)
webertj@14456
  1030
(* params      : list of '(name, value)' pairs used to override default      *)
webertj@14456
  1031
(*               parameters                                                  *)
webertj@14350
  1032
(* ------------------------------------------------------------------------- *)
webertj@14350
  1033
webertj@14456
  1034
	(* theory -> (string * string) list -> Term.term -> unit *)
webertj@14350
  1035
webertj@14456
  1036
	fun satisfy_term thy params t =
webertj@14807
  1037
		find_model thy (actual_params thy params) t false;
webertj@14350
  1038
webertj@14350
  1039
(* ------------------------------------------------------------------------- *)
webertj@14456
  1040
(* refute_term: calls 'find_model' to find a model that refutes 't'          *)
webertj@14456
  1041
(* params     : list of '(name, value)' pairs used to override default       *)
webertj@14456
  1042
(*              parameters                                                   *)
webertj@14350
  1043
(* ------------------------------------------------------------------------- *)
webertj@14350
  1044
webertj@14456
  1045
	(* theory -> (string * string) list -> Term.term -> unit *)
webertj@14350
  1046
webertj@14456
  1047
	fun refute_term thy params t =
webertj@14350
  1048
	let
webertj@14807
  1049
		(* disallow schematic type variables, since we cannot properly negate  *)
webertj@14807
  1050
		(* terms containing them (their logical meaning is that there EXISTS a *)
webertj@14807
  1051
		(* type s.t. ...; to refute such a formula, we would have to show that *)
webertj@14807
  1052
		(* for ALL types, not ...)                                             *)
webertj@14456
  1053
		val _ = assert (null (term_tvars t)) "Term to be refuted contains schematic type variables"
webertj@14456
  1054
		(* existential closure over schematic variables *)
webertj@14456
  1055
		(* (Term.indexname * Term.typ) list *)
webertj@14456
  1056
		val vars = sort_wrt (fst o fst) (map dest_Var (term_vars t))
webertj@14456
  1057
		(* Term.term *)
skalberg@15570
  1058
		val ex_closure = Library.foldl
webertj@15611
  1059
			(fn (t', ((x, i), T)) => (HOLogic.exists_const T) $ Abs (x, T, abstract_over (Var ((x, i), T), t')))
webertj@14456
  1060
			(t, vars)
webertj@14456
  1061
		(* If 't' is of type 'propT' (rather than 'boolT'), applying  *)
webertj@14456
  1062
		(* 'HOLogic.exists_const' is not type-correct.  However, this *)
webertj@14807
  1063
		(* is not really a problem as long as 'find_model' still      *)
webertj@14456
  1064
		(* interprets the resulting term correctly, without checking  *)
webertj@14456
  1065
		(* its type.                                                  *)
webertj@14350
  1066
	in
webertj@14807
  1067
		find_model thy (actual_params thy params) ex_closure true
webertj@14350
  1068
	end;
webertj@14350
  1069
webertj@14350
  1070
(* ------------------------------------------------------------------------- *)
webertj@14456
  1071
(* refute_subgoal: calls 'refute_term' on a specific subgoal                 *)
webertj@14456
  1072
(* params        : list of '(name, value)' pairs used to override default    *)
webertj@14456
  1073
(*                 parameters                                                *)
webertj@14456
  1074
(* subgoal       : 0-based index specifying the subgoal number               *)
webertj@14350
  1075
(* ------------------------------------------------------------------------- *)
webertj@14350
  1076
webertj@14456
  1077
	(* theory -> (string * string) list -> Thm.thm -> int -> unit *)
webertj@14350
  1078
webertj@14456
  1079
	fun refute_subgoal thy params thm subgoal =
skalberg@15570
  1080
		refute_term thy params (List.nth (prems_of thm, subgoal));
webertj@14350
  1081
webertj@14350
  1082
webertj@14350
  1083
(* ------------------------------------------------------------------------- *)
webertj@15292
  1084
(* INTERPRETERS: Auxiliary Functions                                         *)
webertj@14350
  1085
(* ------------------------------------------------------------------------- *)
webertj@14350
  1086
webertj@14350
  1087
(* ------------------------------------------------------------------------- *)
webertj@14807
  1088
(* make_constants: returns all interpretations that have the same tree       *)
webertj@14807
  1089
(*                 structure as 'intr', but consist of unit vectors with     *)
webertj@14807
  1090
(*                 'True'/'False' only (no Boolean variables)                *)
webertj@14350
  1091
(* ------------------------------------------------------------------------- *)
webertj@14350
  1092
webertj@14807
  1093
	(* interpretation -> interpretation list *)
webertj@14350
  1094
webertj@14807
  1095
	fun make_constants intr =
webertj@14456
  1096
	let
webertj@14350
  1097
		(* returns a list with all unit vectors of length n *)
webertj@14456
  1098
		(* int -> interpretation list *)
webertj@14350
  1099
		fun unit_vectors n =
webertj@14350
  1100
		let
webertj@14350
  1101
			(* returns the k-th unit vector of length n *)
webertj@14456
  1102
			(* int * int -> interpretation *)
webertj@14350
  1103
			fun unit_vector (k,n) =
webertj@14350
  1104
				Leaf ((replicate (k-1) False) @ (True :: (replicate (n-k) False)))
webertj@14456
  1105
			(* int -> interpretation list -> interpretation list *)
webertj@14350
  1106
			fun unit_vectors_acc k vs =
webertj@14350
  1107
				if k>n then [] else (unit_vector (k,n))::(unit_vectors_acc (k+1) vs)
webertj@14350
  1108
		in
webertj@14350
  1109
			unit_vectors_acc 1 []
webertj@14350
  1110
		end
webertj@14350
  1111
		(* concatenates 'x' with every list in 'xss', returning a new list of lists *)
webertj@14350
  1112
		(* 'a -> 'a list list -> 'a list list *)
webertj@14350
  1113
		fun cons_list x xss =
webertj@14350
  1114
			map (fn xs => x::xs) xss
webertj@14350
  1115
		(* returns a list of lists, each one consisting of n (possibly identical) elements from 'xs' *)
webertj@14350
  1116
		(* int -> 'a list -> 'a list list *)
webertj@14350
  1117
		fun pick_all 1 xs =
webertj@14350
  1118
			map (fn x => [x]) xs
webertj@14350
  1119
		  | pick_all n xs =
webertj@14350
  1120
			let val rec_pick = pick_all (n-1) xs in
webertj@15611
  1121
				Library.foldl (fn (acc, x) => (cons_list x rec_pick) @ acc) ([], xs)
webertj@14350
  1122
			end
webertj@14807
  1123
	in
webertj@14807
  1124
		case intr of
webertj@14807
  1125
		  Leaf xs => unit_vectors (length xs)
webertj@14807
  1126
		| Node xs => map (fn xs' => Node xs') (pick_all (length xs) (make_constants (hd xs)))
webertj@14807
  1127
	end;
webertj@14807
  1128
webertj@14807
  1129
(* ------------------------------------------------------------------------- *)
webertj@14807
  1130
(* size_of_type: returns the number of constants in a type (i.e. 'length     *)
webertj@14807
  1131
(*               (make_constants intr)', but implemented more efficiently)   *)
webertj@14807
  1132
(* ------------------------------------------------------------------------- *)
webertj@14807
  1133
webertj@14807
  1134
	(* interpretation -> int *)
webertj@14807
  1135
webertj@14807
  1136
	fun size_of_type intr =
webertj@14807
  1137
	let
webertj@15611
  1138
		(* power (a, b) computes a^b, for a>=0, b>=0 *)
webertj@14807
  1139
		(* int * int -> int *)
webertj@15611
  1140
		fun power (a, 0) = 1
webertj@15611
  1141
		  | power (a, 1) = a
webertj@15611
  1142
		  | power (a, b) = let val ab = power(a, b div 2) in ab * ab * power(a, b mod 2) end
webertj@14807
  1143
	in
webertj@14807
  1144
		case intr of
webertj@14807
  1145
		  Leaf xs => length xs
webertj@14807
  1146
		| Node xs => power (size_of_type (hd xs), length xs)
webertj@14807
  1147
	end;
webertj@14807
  1148
webertj@14807
  1149
(* ------------------------------------------------------------------------- *)
webertj@14807
  1150
(* TT/FF: interpretations that denote "true" or "false", respectively        *)
webertj@14807
  1151
(* ------------------------------------------------------------------------- *)
webertj@14807
  1152
webertj@14807
  1153
	(* interpretation *)
webertj@14807
  1154
webertj@14807
  1155
	val TT = Leaf [True, False];
webertj@14807
  1156
webertj@14807
  1157
	val FF = Leaf [False, True];
webertj@14807
  1158
webertj@14807
  1159
(* ------------------------------------------------------------------------- *)
webertj@14807
  1160
(* make_equality: returns an interpretation that denotes (extensional)       *)
webertj@14807
  1161
(*                equality of two interpretations                            *)
webertj@15547
  1162
(* - two interpretations are 'equal' iff they are both defined and denote    *)
webertj@15547
  1163
(*   the same value                                                          *)
webertj@15547
  1164
(* - two interpretations are 'not_equal' iff they are both defined at least  *)
webertj@15547
  1165
(*   partially, and a defined part denotes different values                  *)
webertj@15547
  1166
(* - a completely undefined interpretation is neither 'equal' nor            *)
webertj@15547
  1167
(*   'not_equal' to another interpretation                                   *)
webertj@14807
  1168
(* ------------------------------------------------------------------------- *)
webertj@14807
  1169
webertj@14807
  1170
	(* We could in principle represent '=' on a type T by a particular        *)
webertj@14807
  1171
	(* interpretation.  However, the size of that interpretation is quadratic *)
webertj@14807
  1172
	(* in the size of T.  Therefore comparing the interpretations 'i1' and    *)
webertj@14807
  1173
	(* 'i2' directly is more efficient than constructing the interpretation   *)
webertj@14807
  1174
	(* for equality on T first, and "applying" this interpretation to 'i1'    *)
webertj@14807
  1175
	(* and 'i2' in the usual way (cf. 'interpretation_apply') then.           *)
webertj@14807
  1176
webertj@14807
  1177
	(* interpretation * interpretation -> interpretation *)
webertj@14807
  1178
webertj@14807
  1179
	fun make_equality (i1, i2) =
webertj@14807
  1180
	let
webertj@14807
  1181
		(* interpretation * interpretation -> prop_formula *)
webertj@14807
  1182
		fun equal (i1, i2) =
webertj@14807
  1183
			(case i1 of
webertj@14807
  1184
			  Leaf xs =>
webertj@14807
  1185
				(case i2 of
webertj@15547
  1186
				  Leaf ys => PropLogic.dot_product (xs, ys)  (* defined and equal *)
webertj@14807
  1187
				| Node _  => raise REFUTE ("make_equality", "second interpretation is higher"))
webertj@14807
  1188
			| Node xs =>
webertj@14807
  1189
				(case i2 of
webertj@14807
  1190
				  Leaf _  => raise REFUTE ("make_equality", "first interpretation is higher")
webertj@14807
  1191
				| Node ys => PropLogic.all (map equal (xs ~~ ys))))
webertj@14807
  1192
		(* interpretation * interpretation -> prop_formula *)
webertj@14807
  1193
		fun not_equal (i1, i2) =
webertj@14807
  1194
			(case i1 of
webertj@14807
  1195
			  Leaf xs =>
webertj@14807
  1196
				(case i2 of
webertj@14807
  1197
				  Leaf ys => PropLogic.all ((PropLogic.exists xs) :: (PropLogic.exists ys) ::
webertj@14807
  1198
					(map (fn (x,y) => SOr (SNot x, SNot y)) (xs ~~ ys)))  (* defined and not equal *)
webertj@14807
  1199
				| Node _  => raise REFUTE ("make_equality", "second interpretation is higher"))
webertj@14807
  1200
			| Node xs =>
webertj@14807
  1201
				(case i2 of
webertj@14807
  1202
				  Leaf _  => raise REFUTE ("make_equality", "first interpretation is higher")
webertj@14807
  1203
				| Node ys => PropLogic.exists (map not_equal (xs ~~ ys))))
webertj@14350
  1204
	in
webertj@14807
  1205
		(* a value may be undefined; therefore 'not_equal' is not just the     *)
webertj@15547
  1206
		(* negation of 'equal'                                                 *)
webertj@14807
  1207
		Leaf [equal (i1, i2), not_equal (i1, i2)]
webertj@14807
  1208
	end;
webertj@14807
  1209
webertj@15292
  1210
(* ------------------------------------------------------------------------- *)
webertj@15547
  1211
(* make_def_equality: returns an interpretation that denotes (extensional)   *)
webertj@15547
  1212
(*                    equality of two interpretations                        *)
webertj@15547
  1213
(* This function treats undefined/partially defined interpretations          *)
webertj@15547
  1214
(* different from 'make_equality': two undefined interpretations are         *)
webertj@15547
  1215
(* considered equal, while a defined interpretation is considered not equal  *)
webertj@15547
  1216
(* to an undefined interpretation.                                           *)
webertj@15547
  1217
(* ------------------------------------------------------------------------- *)
webertj@15547
  1218
webertj@15547
  1219
	(* interpretation * interpretation -> interpretation *)
webertj@15547
  1220
webertj@15547
  1221
	fun make_def_equality (i1, i2) =
webertj@15547
  1222
	let
webertj@15547
  1223
		(* interpretation * interpretation -> prop_formula *)
webertj@15547
  1224
		fun equal (i1, i2) =
webertj@15547
  1225
			(case i1 of
webertj@15547
  1226
			  Leaf xs =>
webertj@15547
  1227
				(case i2 of
webertj@15547
  1228
				  Leaf ys => SOr (PropLogic.dot_product (xs, ys),  (* defined and equal, or both undefined *)
webertj@15547
  1229
					SAnd (PropLogic.all (map SNot xs), PropLogic.all (map SNot ys)))
webertj@15547
  1230
				| Node _  => raise REFUTE ("make_def_equality", "second interpretation is higher"))
webertj@15547
  1231
			| Node xs =>
webertj@15547
  1232
				(case i2 of
webertj@15547
  1233
				  Leaf _  => raise REFUTE ("make_def_equality", "first interpretation is higher")
webertj@15547
  1234
				| Node ys => PropLogic.all (map equal (xs ~~ ys))))
webertj@15547
  1235
		(* interpretation *)
webertj@15547
  1236
		val eq = equal (i1, i2)
webertj@15547
  1237
	in
webertj@15547
  1238
		Leaf [eq, SNot eq]
webertj@15547
  1239
	end;
webertj@15547
  1240
webertj@15547
  1241
(* ------------------------------------------------------------------------- *)
webertj@15547
  1242
(* interpretation_apply: returns an interpretation that denotes the result   *)
webertj@15547
  1243
(*                       of applying the function denoted by 'i2' to the     *)
webertj@15547
  1244
(*                       argument denoted by 'i2'                            *)
webertj@15547
  1245
(* ------------------------------------------------------------------------- *)
webertj@15547
  1246
webertj@15547
  1247
	(* interpretation * interpretation -> interpretation *)
webertj@15547
  1248
webertj@15547
  1249
	fun interpretation_apply (i1, i2) =
webertj@15547
  1250
	let
webertj@15547
  1251
		(* interpretation * interpretation -> interpretation *)
webertj@15547
  1252
		fun interpretation_disjunction (tr1,tr2) =
webertj@15547
  1253
			tree_map (fn (xs,ys) => map (fn (x,y) => SOr(x,y)) (xs ~~ ys)) (tree_pair (tr1,tr2))
webertj@15547
  1254
		(* prop_formula * interpretation -> interpretation *)
webertj@15547
  1255
		fun prop_formula_times_interpretation (fm,tr) =
webertj@15547
  1256
			tree_map (map (fn x => SAnd (fm,x))) tr
webertj@15547
  1257
		(* prop_formula list * interpretation list -> interpretation *)
webertj@15547
  1258
		fun prop_formula_list_dot_product_interpretation_list ([fm],[tr]) =
webertj@15547
  1259
			prop_formula_times_interpretation (fm,tr)
webertj@15547
  1260
		  | prop_formula_list_dot_product_interpretation_list (fm::fms,tr::trees) =
webertj@15547
  1261
			interpretation_disjunction (prop_formula_times_interpretation (fm,tr), prop_formula_list_dot_product_interpretation_list (fms,trees))
webertj@15547
  1262
		  | prop_formula_list_dot_product_interpretation_list (_,_) =
webertj@15547
  1263
			raise REFUTE ("interpretation_apply", "empty list (in dot product)")
webertj@15547
  1264
		(* concatenates 'x' with every list in 'xss', returning a new list of lists *)
webertj@15547
  1265
		(* 'a -> 'a list list -> 'a list list *)
webertj@15547
  1266
		fun cons_list x xss =
webertj@15547
  1267
			map (fn xs => x::xs) xss
webertj@15547
  1268
		(* returns a list of lists, each one consisting of one element from each element of 'xss' *)
webertj@15547
  1269
		(* 'a list list -> 'a list list *)
webertj@15547
  1270
		fun pick_all [xs] =
webertj@15547
  1271
			map (fn x => [x]) xs
webertj@15547
  1272
		  | pick_all (xs::xss) =
webertj@15547
  1273
			let val rec_pick = pick_all xss in
webertj@15611
  1274
				Library.foldl (fn (acc, x) => (cons_list x rec_pick) @ acc) ([], xs)
webertj@15547
  1275
			end
webertj@15547
  1276
		  | pick_all _ =
webertj@15547
  1277
			raise REFUTE ("interpretation_apply", "empty list (in pick_all)")
webertj@15547
  1278
		(* interpretation -> prop_formula list *)
webertj@15547
  1279
		fun interpretation_to_prop_formula_list (Leaf xs) =
webertj@15547
  1280
			xs
webertj@15547
  1281
		  | interpretation_to_prop_formula_list (Node trees) =
webertj@15547
  1282
			map PropLogic.all (pick_all (map interpretation_to_prop_formula_list trees))
webertj@15547
  1283
	in
webertj@15547
  1284
		case i1 of
webertj@15547
  1285
		  Leaf _ =>
webertj@15547
  1286
			raise REFUTE ("interpretation_apply", "first interpretation is a leaf")
webertj@15547
  1287
		| Node xs =>
webertj@15547
  1288
			prop_formula_list_dot_product_interpretation_list (interpretation_to_prop_formula_list i2, xs)
webertj@15547
  1289
	end;
webertj@15547
  1290
webertj@15547
  1291
(* ------------------------------------------------------------------------- *)
webertj@15292
  1292
(* eta_expand: eta-expands a term 't' by adding 'i' lambda abstractions      *)
webertj@15292
  1293
(* ------------------------------------------------------------------------- *)
webertj@15292
  1294
webertj@15292
  1295
	(* Term.term -> int -> Term.term *)
webertj@15292
  1296
webertj@15292
  1297
	fun eta_expand t i =
webertj@15292
  1298
	let
webertj@15292
  1299
		val Ts = binder_types (fastype_of t)
webertj@15292
  1300
	in
skalberg@15574
  1301
		foldr (fn (T, t) => Abs ("<eta_expand>", T, t))
skalberg@15574
  1302
			(list_comb (t, map Bound (i-1 downto 0))) (Library.take (i, Ts))
webertj@15292
  1303
	end;
webertj@15292
  1304
webertj@15335
  1305
(* ------------------------------------------------------------------------- *)
webertj@15335
  1306
(* sum: returns the sum of a list 'xs' of integers                           *)
webertj@15335
  1307
(* ------------------------------------------------------------------------- *)
webertj@15335
  1308
webertj@15335
  1309
	(* int list -> int *)
webertj@15335
  1310
webertj@15611
  1311
	fun sum xs = foldl op+ 0 xs;
webertj@15335
  1312
webertj@15335
  1313
(* ------------------------------------------------------------------------- *)
webertj@15335
  1314
(* product: returns the product of a list 'xs' of integers                   *)
webertj@15335
  1315
(* ------------------------------------------------------------------------- *)
webertj@15335
  1316
webertj@15335
  1317
	(* int list -> int *)
webertj@15335
  1318
webertj@15611
  1319
	fun product xs = foldl op* 1 xs;
webertj@15335
  1320
webertj@15335
  1321
(* ------------------------------------------------------------------------- *)
webertj@15547
  1322
(* size_of_dtyp: the size of (an initial fragment of) an inductive data type *)
webertj@15547
  1323
(*               is the sum (over its constructors) of the product (over     *)
webertj@15547
  1324
(*               their arguments) of the size of the argument types          *)
webertj@15335
  1325
(* ------------------------------------------------------------------------- *)
webertj@15335
  1326
webertj@15335
  1327
	(* theory -> (Term.typ * int) list -> DatatypeAux.descr -> (DatatypeAux.dtyp * Term.typ) list -> (string * DatatypeAux.dtyp list) list -> int *)
webertj@15335
  1328
webertj@15335
  1329
	fun size_of_dtyp thy typ_sizes descr typ_assoc constructors =
webertj@15335
  1330
		sum (map (fn (_, dtyps) =>
webertj@15335
  1331
			product (map (fn dtyp =>
webertj@15335
  1332
				let
webertj@15335
  1333
					val T         = typ_of_dtyp descr typ_assoc dtyp
webertj@15547
  1334
					val (i, _, _) = interpret thy (typ_sizes, []) {maxvars=0, def_eq = false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@15335
  1335
				in
webertj@15335
  1336
					size_of_type i
webertj@15335
  1337
				end) dtyps)) constructors);
webertj@15335
  1338
webertj@15292
  1339
webertj@15292
  1340
(* ------------------------------------------------------------------------- *)
webertj@15292
  1341
(* INTERPRETERS: Actual Interpreters                                         *)
webertj@15292
  1342
(* ------------------------------------------------------------------------- *)
webertj@14807
  1343
webertj@14807
  1344
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@14807
  1345
webertj@14807
  1346
	(* simply typed lambda calculus: Isabelle's basic term syntax, with type  *)
webertj@14807
  1347
	(* variables, function types, and propT                                   *)
webertj@14807
  1348
webertj@14807
  1349
	fun stlc_interpreter thy model args t =
webertj@14807
  1350
	let
webertj@15547
  1351
		val (typs, terms)                                   = model
webertj@15547
  1352
		val {maxvars, def_eq, next_idx, bounds, wellformed} = args
webertj@14807
  1353
		(* Term.typ -> (interpretation * model * arguments) option *)
webertj@14807
  1354
		fun interpret_groundterm T =
webertj@14807
  1355
		let
webertj@14807
  1356
			(* unit -> (interpretation * model * arguments) option *)
webertj@14807
  1357
			fun interpret_groundtype () =
webertj@14807
  1358
			let
haftmann@17314
  1359
				val size = (if T = Term.propT then 2 else (the o AList.lookup (op =) typs) T)                    (* the model MUST specify a size for ground types *)
webertj@15547
  1360
				val next = next_idx+size
webertj@15547
  1361
				val _    = (if next-1>maxvars andalso maxvars>0 then raise MAXVARS_EXCEEDED else ())  (* check if 'maxvars' is large enough *)
webertj@14807
  1362
				(* prop_formula list *)
webertj@15547
  1363
				val fms  = map BoolVar (next_idx upto (next_idx+size-1))
webertj@14807
  1364
				(* interpretation *)
webertj@14807
  1365
				val intr = Leaf fms
webertj@14807
  1366
				(* prop_formula list -> prop_formula *)
webertj@14807
  1367
				fun one_of_two_false []      = True
webertj@14807
  1368
				  | one_of_two_false (x::xs) = SAnd (PropLogic.all (map (fn x' => SOr (SNot x, SNot x')) xs), one_of_two_false xs)
webertj@14807
  1369
				(* prop_formula *)
webertj@15547
  1370
				val wf   = one_of_two_false fms
webertj@14807
  1371
			in
webertj@14807
  1372
				(* extend the model, increase 'next_idx', add well-formedness condition *)
webertj@15547
  1373
				SOME (intr, (typs, (t, intr)::terms), {maxvars = maxvars, def_eq = def_eq, next_idx = next, bounds = bounds, wellformed = SAnd (wellformed, wf)})
webertj@14807
  1374
			end
webertj@14807
  1375
		in
webertj@14807
  1376
			case T of
webertj@14807
  1377
			  Type ("fun", [T1, T2]) =>
webertj@14807
  1378
				let
webertj@14807
  1379
					(* we create 'size_of_type (interpret (... T1))' different copies *)
webertj@14807
  1380
					(* of the interpretation for 'T2', which are then combined into a *)
webertj@14807
  1381
					(* single new interpretation                                      *)
webertj@15547
  1382
					val (i1, _, _) = interpret thy model {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T1))
webertj@14807
  1383
					(* make fresh copies, with different variable indices *)
webertj@14807
  1384
					(* 'idx': next variable index                         *)
webertj@14807
  1385
					(* 'n'  : number of copies                            *)
webertj@14807
  1386
					(* int -> int -> (int * interpretation list * prop_formula *)
webertj@14807
  1387
					fun make_copies idx 0 =
webertj@14807
  1388
						(idx, [], True)
webertj@14807
  1389
					  | make_copies idx n =
webertj@14807
  1390
						let
webertj@15547
  1391
							val (copy, _, new_args) = interpret thy (typs, []) {maxvars = maxvars, def_eq = false, next_idx = idx, bounds = [], wellformed = True} (Free ("dummy", T2))
webertj@14807
  1392
							val (idx', copies, wf') = make_copies (#next_idx new_args) (n-1)
webertj@14807
  1393
						in
webertj@14807
  1394
							(idx', copy :: copies, SAnd (#wellformed new_args, wf'))
webertj@14807
  1395
						end
webertj@14807
  1396
					val (next, copies, wf) = make_copies next_idx (size_of_type i1)
webertj@14807
  1397
					(* combine copies into a single interpretation *)
webertj@14807
  1398
					val intr = Node copies
webertj@14807
  1399
				in
webertj@14807
  1400
					(* extend the model, increase 'next_idx', add well-formedness condition *)
webertj@15547
  1401
					SOME (intr, (typs, (t, intr)::terms), {maxvars = maxvars, def_eq = def_eq, next_idx = next, bounds = bounds, wellformed = SAnd (wellformed, wf)})
webertj@14807
  1402
				end
webertj@14807
  1403
			| Type _  => interpret_groundtype ()
webertj@14807
  1404
			| TFree _ => interpret_groundtype ()
webertj@14807
  1405
			| TVar  _ => interpret_groundtype ()
webertj@14807
  1406
		end
webertj@14807
  1407
	in
haftmann@17314
  1408
		case AList.lookup (op =) terms t of
skalberg@15531
  1409
		  SOME intr =>
webertj@14807
  1410
			(* return an existing interpretation *)
skalberg@15531
  1411
			SOME (intr, model, args)
skalberg@15531
  1412
		| NONE =>
webertj@14807
  1413
			(case t of
webertj@14807
  1414
			  Const (_, T)     =>
webertj@14807
  1415
				interpret_groundterm T
webertj@14807
  1416
			| Free (_, T)      =>
webertj@14807
  1417
				interpret_groundterm T
webertj@14807
  1418
			| Var (_, T)       =>
webertj@14807
  1419
				interpret_groundterm T
webertj@14807
  1420
			| Bound i          =>
skalberg@15570
  1421
				SOME (List.nth (#bounds args, i), model, args)
webertj@14807
  1422
			| Abs (x, T, body) =>
webertj@14807
  1423
				let
webertj@14807
  1424
					(* create all constants of type 'T' *)
webertj@15547
  1425
					val (i, _, _) = interpret thy model {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@14807
  1426
					val constants = make_constants i
webertj@14807
  1427
					(* interpret the 'body' separately for each constant *)
webertj@14807
  1428
					val ((model', args'), bodies) = foldl_map
webertj@15547
  1429
						(fn ((m, a), c) =>
webertj@14807
  1430
							let
webertj@14807
  1431
								(* add 'c' to 'bounds' *)
webertj@15547
  1432
								val (i', m', a') = interpret thy m {maxvars = #maxvars a, def_eq = #def_eq a, next_idx = #next_idx a, bounds = (c :: #bounds a), wellformed = #wellformed a} body
webertj@14807
  1433
							in
webertj@14807
  1434
								(* keep the new model m' and 'next_idx' and 'wellformed', but use old 'bounds' *)
webertj@15547
  1435
								((m', {maxvars = maxvars, def_eq = def_eq, next_idx = #next_idx a', bounds = bounds, wellformed = #wellformed a'}), i')
webertj@14807
  1436
							end)
webertj@14807
  1437
						((model, args), constants)
webertj@14807
  1438
				in
skalberg@15531
  1439
					SOME (Node bodies, model', args')
webertj@14807
  1440
				end
webertj@14807
  1441
			| t1 $ t2          =>
webertj@14807
  1442
				let
webertj@14807
  1443
					(* interpret 't1' and 't2' separately *)
webertj@14807
  1444
					val (intr1, model1, args1) = interpret thy model args t1
webertj@14807
  1445
					val (intr2, model2, args2) = interpret thy model1 args1 t2
webertj@14807
  1446
				in
webertj@15547
  1447
					SOME (interpretation_apply (intr1, intr2), model2, args2)
webertj@14807
  1448
				end)
webertj@14807
  1449
	end;
webertj@14807
  1450
webertj@14807
  1451
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@14807
  1452
webertj@14807
  1453
	fun Pure_interpreter thy model args t =
webertj@14456
  1454
		case t of
webertj@14807
  1455
		  Const ("all", _) $ t1 =>  (* in the meta-logic, 'all' MUST be followed by an argument term *)
webertj@14807
  1456
			let
webertj@14807
  1457
				val (i, m, a) = interpret thy model args t1
webertj@14807
  1458
			in
webertj@14807
  1459
				case i of
webertj@14807
  1460
				  Node xs =>
webertj@14807
  1461
					let
webertj@14807
  1462
						val fmTrue  = PropLogic.all (map toTrue xs)
webertj@14807
  1463
						val fmFalse = PropLogic.exists (map toFalse xs)
webertj@14807
  1464
					in
skalberg@15531
  1465
						SOME (Leaf [fmTrue, fmFalse], m, a)
webertj@14807
  1466
					end
webertj@14807
  1467
				| _ =>
webertj@14807
  1468
					raise REFUTE ("Pure_interpreter", "\"all\" is not followed by a function")
webertj@14807
  1469
			end
webertj@14807
  1470
		| Const ("==", _) $ t1 $ t2 =>
webertj@14807
  1471
			let
webertj@14807
  1472
				val (i1, m1, a1) = interpret thy model args t1
webertj@14807
  1473
				val (i2, m2, a2) = interpret thy m1 a1 t2
webertj@14807
  1474
			in
webertj@15547
  1475
				(* we use either 'make_def_equality' or 'make_equality' *)
webertj@15547
  1476
				SOME ((if #def_eq args then make_def_equality else make_equality) (i1, i2), m2, a2)
webertj@14807
  1477
			end
webertj@14807
  1478
		| Const ("==>", _) =>  (* simpler than translating 'Const ("==>", _) $ t1 $ t2' *)
skalberg@15531
  1479
			SOME (Node [Node [TT, FF], Node [TT, TT]], model, args)
skalberg@15531
  1480
		| _ => NONE;
webertj@14807
  1481
webertj@14807
  1482
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@14807
  1483
webertj@14807
  1484
	fun HOLogic_interpreter thy model args t =
webertj@14807
  1485
	(* ------------------------------------------------------------------------- *)
webertj@14807
  1486
	(* Providing interpretations directly is more efficient than unfolding the   *)
webertj@15283
  1487
	(* logical constants.  In HOL however, logical constants can themselves be   *)
webertj@14807
  1488
	(* arguments.  "All" and "Ex" are then translated just like any other        *)
webertj@14807
  1489
	(* constant, with the relevant axiom being added by 'collect_axioms'.        *)
webertj@14807
  1490
	(* ------------------------------------------------------------------------- *)
webertj@14807
  1491
		case t of
webertj@14807
  1492
		  Const ("Trueprop", _) =>
skalberg@15531
  1493
			SOME (Node [TT, FF], model, args)
webertj@14807
  1494
		| Const ("Not", _) =>
skalberg@15531
  1495
			SOME (Node [FF, TT], model, args)
webertj@14807
  1496
		| Const ("True", _) =>  (* redundant, since 'True' is also an IDT constructor *)
skalberg@15531
  1497
			SOME (TT, model, args)
webertj@14807
  1498
		| Const ("False", _) =>  (* redundant, since 'False' is also an IDT constructor *)
skalberg@15531
  1499
			SOME (FF, model, args)
webertj@15333
  1500
		| Const ("All", _) $ t1 =>
webertj@15333
  1501
		(* if "All" occurs without an argument (i.e. as argument to a higher-order *)
webertj@15547
  1502
		(* function or predicate), it is handled by the 'stlc_interpreter' (i.e.   *)
webertj@15333
  1503
		(* by unfolding its definition)                                            *)
webertj@14350
  1504
			let
webertj@14807
  1505
				val (i, m, a) = interpret thy model args t1
webertj@14807
  1506
			in
webertj@14807
  1507
				case i of
webertj@14807
  1508
				  Node xs =>
webertj@14807
  1509
					let
webertj@14807
  1510
						val fmTrue  = PropLogic.all (map toTrue xs)
webertj@14807
  1511
						val fmFalse = PropLogic.exists (map toFalse xs)
webertj@14807
  1512
					in
skalberg@15531
  1513
						SOME (Leaf [fmTrue, fmFalse], m, a)
webertj@14807
  1514
					end
webertj@14807
  1515
				| _ =>
webertj@15292
  1516
					raise REFUTE ("HOLogic_interpreter", "\"All\" is followed by a non-function")
webertj@14807
  1517
			end
webertj@15333
  1518
		| Const ("Ex", _) $ t1 =>
webertj@15333
  1519
		(* if "Ex" occurs without an argument (i.e. as argument to a higher-order  *)
webertj@15547
  1520
		(* function or predicate), it is handled by the 'stlc_interpreter' (i.e.   *)
webertj@15333
  1521
		(* by unfolding its definition)                                            *)
webertj@14807
  1522
			let
webertj@14807
  1523
				val (i, m, a) = interpret thy model args t1
webertj@14807
  1524
			in
webertj@14807
  1525
				case i of
webertj@14807
  1526
				  Node xs =>
webertj@14807
  1527
					let
webertj@14807
  1528
						val fmTrue  = PropLogic.exists (map toTrue xs)
webertj@14807
  1529
						val fmFalse = PropLogic.all (map toFalse xs)
webertj@14807
  1530
					in
skalberg@15531
  1531
						SOME (Leaf [fmTrue, fmFalse], m, a)
webertj@14807
  1532
					end
webertj@14807
  1533
				| _ =>
webertj@15292
  1534
					raise REFUTE ("HOLogic_interpreter", "\"Ex\" is followed by a non-function")
webertj@14807
  1535
			end
webertj@14807
  1536
		| Const ("op =", _) $ t1 $ t2 =>
webertj@14807
  1537
			let
webertj@14807
  1538
				val (i1, m1, a1) = interpret thy model args t1
webertj@14807
  1539
				val (i2, m2, a2) = interpret thy m1 a1 t2
webertj@14807
  1540
			in
skalberg@15531
  1541
				SOME (make_equality (i1, i2), m2, a2)
webertj@14807
  1542
			end
webertj@14807
  1543
		| Const ("op =", _) $ t1 =>
skalberg@15531
  1544
			SOME (interpret thy model args (eta_expand t 1))
webertj@14807
  1545
		| Const ("op =", _) =>
skalberg@15531
  1546
			SOME (interpret thy model args (eta_expand t 2))
webertj@15547
  1547
		| Const ("op &", _) $ t1 $ t2 =>
webertj@15547
  1548
			(* 3-valued logic *)
webertj@15547
  1549
			let
webertj@15547
  1550
				val (i1, m1, a1) = interpret thy model args t1
webertj@15547
  1551
				val (i2, m2, a2) = interpret thy m1 a1 t2
webertj@15547
  1552
				val fmTrue       = PropLogic.SAnd (toTrue i1, toTrue i2)
webertj@15547
  1553
				val fmFalse      = PropLogic.SOr (toFalse i1, toFalse i2)
webertj@15547
  1554
			in
webertj@15547
  1555
				SOME (Leaf [fmTrue, fmFalse], m2, a2)
webertj@15547
  1556
			end
webertj@15547
  1557
		| Const ("op &", _) $ t1 =>
webertj@15547
  1558
			SOME (interpret thy model args (eta_expand t 1))
webertj@14807
  1559
		| Const ("op &", _) =>
webertj@15547
  1560
			SOME (interpret thy model args (eta_expand t 2))
webertj@15547
  1561
			(* SOME (Node [Node [TT, FF], Node [FF, FF]], model, args) *)
webertj@15547
  1562
		| Const ("op |", _) $ t1 $ t2 =>
webertj@15547
  1563
			(* 3-valued logic *)
webertj@15547
  1564
			let
webertj@15547
  1565
				val (i1, m1, a1) = interpret thy model args t1
webertj@15547
  1566
				val (i2, m2, a2) = interpret thy m1 a1 t2
webertj@15547
  1567
				val fmTrue       = PropLogic.SOr (toTrue i1, toTrue i2)
webertj@15547
  1568
				val fmFalse      = PropLogic.SAnd (toFalse i1, toFalse i2)
webertj@15547
  1569
			in
webertj@15547
  1570
				SOME (Leaf [fmTrue, fmFalse], m2, a2)
webertj@15547
  1571
			end
webertj@15547
  1572
		| Const ("op |", _) $ t1 =>
webertj@15547
  1573
			SOME (interpret thy model args (eta_expand t 1))
webertj@14807
  1574
		| Const ("op |", _) =>
webertj@15547
  1575
			SOME (interpret thy model args (eta_expand t 2))
webertj@15547
  1576
			(* SOME (Node [Node [TT, TT], Node [TT, FF]], model, args) *)
webertj@15547
  1577
		| Const ("op -->", _) $ t1 $ t2 =>
webertj@15547
  1578
			(* 3-valued logic *)
webertj@15547
  1579
			let
webertj@15547
  1580
				val (i1, m1, a1) = interpret thy model args t1
webertj@15547
  1581
				val (i2, m2, a2) = interpret thy m1 a1 t2
webertj@15547
  1582
				val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
webertj@15547
  1583
				val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
webertj@15547
  1584
			in
webertj@15547
  1585
				SOME (Leaf [fmTrue, fmFalse], m2, a2)
webertj@15547
  1586
			end
webertj@14807
  1587
		| Const ("op -->", _) =>
webertj@15547
  1588
			(* SOME (Node [Node [TT, FF], Node [TT, TT]], model, args) *)
webertj@15547
  1589
			SOME (interpret thy model args (eta_expand t 2))
skalberg@15531
  1590
		| _ => NONE;
webertj@14807
  1591
webertj@14807
  1592
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@14807
  1593
webertj@14807
  1594
	fun set_interpreter thy model args t =
webertj@14807
  1595
	(* "T set" is isomorphic to "T --> bool" *)
webertj@14807
  1596
	let
webertj@14807
  1597
		val (typs, terms) = model
webertj@14807
  1598
	in
haftmann@17314
  1599
		case AList.lookup (op =) terms t of
skalberg@15531
  1600
		  SOME intr =>
webertj@14807
  1601
			(* return an existing interpretation *)
skalberg@15531
  1602
			SOME (intr, model, args)
skalberg@15531
  1603
		| NONE =>
webertj@14807
  1604
			(case t of
webertj@14807
  1605
			  Free (x, Type ("set", [T])) =>
webertj@15334
  1606
				let
webertj@14807
  1607
					val (intr, _, args') = interpret thy (typs, []) args (Free (x, T --> HOLogic.boolT))
webertj@14807
  1608
				in
skalberg@15531
  1609
					SOME (intr, (typs, (t, intr)::terms), args')
webertj@15334
  1610
				end
webertj@15767
  1611
			| Var ((x, i), Type ("set", [T])) =>
webertj@15334
  1612
				let
webertj@14807
  1613
					val (intr, _, args') = interpret thy (typs, []) args (Var ((x,i), T --> HOLogic.boolT))
webertj@14807
  1614
				in
skalberg@15531
  1615
					SOME (intr, (typs, (t, intr)::terms), args')
webertj@15334
  1616
				end
webertj@14807
  1617
			| Const (s, Type ("set", [T])) =>
webertj@15334
  1618
				let
webertj@14807
  1619
					val (intr, _, args') = interpret thy (typs, []) args (Const (s, T --> HOLogic.boolT))
webertj@14807
  1620
				in
skalberg@15531
  1621
					SOME (intr, (typs, (t, intr)::terms), args')
webertj@15334
  1622
				end
webertj@14807
  1623
			(* 'Collect' == identity *)
webertj@14807
  1624
			| Const ("Collect", _) $ t1 =>
skalberg@15531
  1625
				SOME (interpret thy model args t1)
webertj@14807
  1626
			| Const ("Collect", _) =>
skalberg@15531
  1627
				SOME (interpret thy model args (eta_expand t 1))
webertj@14807
  1628
			(* 'op :' == application *)
webertj@14807
  1629
			| Const ("op :", _) $ t1 $ t2 =>
skalberg@15531
  1630
				SOME (interpret thy model args (t2 $ t1))
webertj@14807
  1631
			| Const ("op :", _) $ t1 =>
skalberg@15531
  1632
				SOME (interpret thy model args (eta_expand t 1))
webertj@14807
  1633
			| Const ("op :", _) =>
skalberg@15531
  1634
				SOME (interpret thy model args (eta_expand t 2))
skalberg@15531
  1635
			| _ => NONE)
webertj@14807
  1636
	end;
webertj@14807
  1637
webertj@14807
  1638
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@14807
  1639
webertj@15547
  1640
	(* interprets variables and constants whose type is an IDT; constructors of  *)
webertj@15547
  1641
	(* IDTs are properly interpreted by 'IDT_constructor_interpreter' however    *)
webertj@15547
  1642
webertj@14807
  1643
	fun IDT_interpreter thy model args t =
webertj@14807
  1644
	let
webertj@14807
  1645
		val (typs, terms) = model
webertj@14807
  1646
		(* Term.typ -> (interpretation * model * arguments) option *)
webertj@15547
  1647
		fun interpret_term (Type (s, Ts)) =
haftmann@19346
  1648
			(case DatatypePackage.get_datatype thy s of
skalberg@15531
  1649
			  SOME info =>  (* inductive datatype *)
webertj@14807
  1650
				let
webertj@14807
  1651
					(* int option -- only recursive IDTs have an associated depth *)
haftmann@17314
  1652
					val depth = AList.lookup (op =) typs (Type (s, Ts))
webertj@14807
  1653
				in
skalberg@15531
  1654
					if depth = (SOME 0) then  (* termination condition to avoid infinite recursion *)
webertj@14807
  1655
						(* return a leaf of size 0 *)
skalberg@15531
  1656
						SOME (Leaf [], model, args)
webertj@14807
  1657
					else
webertj@14807
  1658
						let
webertj@14807
  1659
							val index               = #index info
webertj@14807
  1660
							val descr               = #descr info
haftmann@17314
  1661
							val (_, dtyps, constrs) = (the o AList.lookup (op =) descr) index
webertj@14807
  1662
							val typ_assoc           = dtyps ~~ Ts
webertj@14807
  1663
							(* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
webertj@14807
  1664
							val _ = (if Library.exists (fn d =>
webertj@14807
  1665
									case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
webertj@14807
  1666
								then
webertj@14807
  1667
									raise REFUTE ("IDT_interpreter", "datatype argument (for type " ^ Sign.string_of_typ (sign_of thy) (Type (s, Ts)) ^ ") is not a variable")
webertj@14807
  1668
								else
webertj@14807
  1669
									())
webertj@14807
  1670
							(* if the model specifies a depth for the current type, decrement it to avoid infinite recursion *)
haftmann@17314
  1671
							val typs'    = case depth of NONE => typs | SOME n =>
haftmann@17314
  1672
								AList.update (op =) (Type (s, Ts), n-1) typs
webertj@14807
  1673
							(* recursively compute the size of the datatype *)
webertj@15335
  1674
							val size     = size_of_dtyp thy typs' descr typ_assoc constrs
webertj@14807
  1675
							val next_idx = #next_idx args
webertj@15547
  1676
							val next     = next_idx+size
webertj@14807
  1677
							val _        = (if next-1>(#maxvars args) andalso (#maxvars args)>0 then raise MAXVARS_EXCEEDED else ())  (* check if 'maxvars' is large enough *)
webertj@14807
  1678
							(* prop_formula list *)
webertj@15547
  1679
							val fms      = map BoolVar (next_idx upto (next_idx+size-1))
webertj@14807
  1680
							(* interpretation *)
webertj@14807
  1681
							val intr     = Leaf fms
webertj@14807
  1682
							(* prop_formula list -> prop_formula *)
webertj@14807
  1683
							fun one_of_two_false []      = True
webertj@14807
  1684
							  | one_of_two_false (x::xs) = SAnd (PropLogic.all (map (fn x' => SOr (SNot x, SNot x')) xs), one_of_two_false xs)
webertj@14807
  1685
							(* prop_formula *)
webertj@15547
  1686
							val wf       = one_of_two_false fms
webertj@14807
  1687
						in
webertj@14807
  1688
							(* extend the model, increase 'next_idx', add well-formedness condition *)
webertj@15547
  1689
							SOME (intr, (typs, (t, intr)::terms), {maxvars = #maxvars args, def_eq = #def_eq args, next_idx = next, bounds = #bounds args, wellformed = SAnd (#wellformed args, wf)})
webertj@14807
  1690
						end
webertj@14807
  1691
				end
skalberg@15531
  1692
			| NONE =>  (* not an inductive datatype *)
skalberg@15531
  1693
				NONE)
webertj@15547
  1694
		  | interpret_term _ =  (* a (free or schematic) type variable *)
skalberg@15531
  1695
			NONE
webertj@14807
  1696
	in
haftmann@17314
  1697
		case AList.lookup (op =) terms t of
skalberg@15531
  1698
		  SOME intr =>
webertj@14807
  1699
			(* return an existing interpretation *)
skalberg@15531
  1700
			SOME (intr, model, args)
skalberg@15531
  1701
		| NONE =>
webertj@14807
  1702
			(case t of
webertj@15547
  1703
			  Free (_, T)  => interpret_term T
webertj@15547
  1704
			| Var (_, T)   => interpret_term T
webertj@15547
  1705
			| Const (_, T) => interpret_term T
webertj@15547
  1706
			| _            => NONE)
webertj@15547
  1707
	end;
webertj@15547
  1708
webertj@15547
  1709
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@15547
  1710
webertj@15547
  1711
	fun IDT_constructor_interpreter thy model args t =
webertj@15547
  1712
	let
webertj@15547
  1713
		val (typs, terms) = model
webertj@15547
  1714
	in
haftmann@17314
  1715
		case AList.lookup (op =) terms t of
webertj@15547
  1716
		  SOME intr =>
webertj@15547
  1717
			(* return an existing interpretation *)
webertj@15547
  1718
			SOME (intr, model, args)
webertj@15547
  1719
		| NONE =>
webertj@15547
  1720
			(case t of
webertj@15547
  1721
			  Const (s, T) =>
webertj@15547
  1722
				(case body_type T of
webertj@15547
  1723
				  Type (s', Ts') =>
haftmann@19346
  1724
					(case DatatypePackage.get_datatype thy s' of
webertj@15547
  1725
					  SOME info =>  (* body type is an inductive datatype *)
webertj@15547
  1726
						let
webertj@15547
  1727
							val index               = #index info
webertj@15547
  1728
							val descr               = #descr info
haftmann@17314
  1729
							val (_, dtyps, constrs) = (the o AList.lookup (op =) descr) index
webertj@15547
  1730
							val typ_assoc           = dtyps ~~ Ts'
webertj@15547
  1731
							(* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
webertj@15547
  1732
							val _ = (if Library.exists (fn d =>
webertj@15547
  1733
									case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
webertj@15547
  1734
								then
webertj@15547
  1735
									raise REFUTE ("IDT_constructor_interpreter", "datatype argument (for type " ^ Sign.string_of_typ (sign_of thy) (Type (s', Ts')) ^ ") is not a variable")
webertj@15547
  1736
								else
webertj@15547
  1737
									())
webertj@15547
  1738
							(* split the constructors into those occuring before/after 'Const (s, T)' *)
webertj@15547
  1739
							val (constrs1, constrs2) = take_prefix (fn (cname, ctypes) =>
wenzelm@16935
  1740
								not (cname = s andalso Sign.typ_instance thy (T,
webertj@15547
  1741
									map (typ_of_dtyp descr typ_assoc) ctypes ---> Type (s', Ts')))) constrs
webertj@15547
  1742
						in
webertj@15547
  1743
							case constrs2 of
webertj@15547
  1744
							  [] =>
webertj@15547
  1745
								(* 'Const (s, T)' is not a constructor of this datatype *)
webertj@15547
  1746
								NONE
webertj@15547
  1747
							| (_, ctypes)::cs =>
webertj@14807
  1748
								let
webertj@15547
  1749
									(* compute the total size of the datatype (with the current depth) *)
webertj@15547
  1750
									val (i, _, _) = interpret thy (typs, []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type (s', Ts')))
webertj@15547
  1751
									val total     = size_of_type i
webertj@15547
  1752
									(* int option -- only recursive IDTs have an associated depth *)
haftmann@17314
  1753
									val depth = AList.lookup (op =) typs (Type (s', Ts'))
haftmann@17314
  1754
									val typs' = (case depth of NONE => typs | SOME n =>
haftmann@17314
  1755
										AList.update (op =) (Type (s', Ts'), n-1) typs)
webertj@15611
  1756
									(* returns an interpretation where everything is mapped to "undefined" *)
webertj@15547
  1757
									(* DatatypeAux.dtyp list -> interpretation *)
webertj@15547
  1758
									fun make_undef [] =
webertj@15547
  1759
										Leaf (replicate total False)
webertj@15547
  1760
									  | make_undef (d::ds) =
webertj@15547
  1761
										let
webertj@15547
  1762
											(* compute the current size of the type 'd' *)
webertj@15547
  1763
											val T           = typ_of_dtyp descr typ_assoc d
webertj@15547
  1764
											val (i, _, _)   = interpret thy (typs, []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@15547
  1765
											val size        = size_of_type i
webertj@15547
  1766
										in
webertj@15547
  1767
											Node (replicate size (make_undef ds))
webertj@15547
  1768
										end
webertj@15547
  1769
									(* returns the interpretation for a constructor at depth 1 *)
webertj@15547
  1770
									(* int * DatatypeAux.dtyp list -> int * interpretation *)
webertj@15547
  1771
									fun make_constr (offset, []) =
webertj@15547
  1772
										if offset<total then
webertj@15547
  1773
											(offset+1, Leaf ((replicate offset False) @ True :: (replicate (total-offset-1) False)))
webertj@14807
  1774
										else
webertj@15547
  1775
											raise REFUTE ("IDT_constructor_interpreter", "offset >= total")
webertj@15547
  1776
									  | make_constr (offset, d::ds) =
webertj@14807
  1777
										let
webertj@15547
  1778
											(* compute the current and the old size of the type 'd' *)
webertj@15547
  1779
											val T           = typ_of_dtyp descr typ_assoc d
webertj@15547
  1780
											val (i, _, _)   = interpret thy (typs, []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@15547
  1781
											val size        = size_of_type i
webertj@15547
  1782
											val (i', _, _)  = interpret thy (typs', []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@15547
  1783
											val size'       = size_of_type i'
webertj@15547
  1784
											(* sanity check *)
webertj@15547
  1785
											val _           = if size < size' then
webertj@15547
  1786
													raise REFUTE ("IDT_constructor_interpreter", "current size is less than old size")
webertj@14807
  1787
												else
webertj@15547
  1788
													()
webertj@15547
  1789
											(* int * interpretation list *)
webertj@15547
  1790
											val (new_offset, intrs) = foldl_map make_constr (offset, replicate size' ds)
webertj@15547
  1791
											(* interpretation list *)
webertj@15547
  1792
											val undefs              = replicate (size - size') (make_undef ds)
webertj@15547
  1793
										in
webertj@15611
  1794
											(* elements that exist at the previous depth are mapped to a defined *)
webertj@15611
  1795
											(* value, while new elements are mapped to "undefined" by the        *)
webertj@15611
  1796
											(* recursive constructor                                             *)
webertj@15547
  1797
											(new_offset, Node (intrs @ undefs))
webertj@15547
  1798
										end
webertj@15547
  1799
									(* extends the interpretation for a constructor (both recursive *)
webertj@15547
  1800
									(* and non-recursive) obtained at depth n (n>=1) to depth n+1   *)
webertj@15547
  1801
									(* int * DatatypeAux.dtyp list * interpretation -> int * interpretation *)
webertj@15547
  1802
									fun extend_constr (offset, [], Leaf xs) =
webertj@15547
  1803
										let
webertj@15547
  1804
											(* returns the k-th unit vector of length n *)
webertj@15547
  1805
											(* int * int -> interpretation *)
webertj@15611
  1806
											fun unit_vector (k, n) =
webertj@15547
  1807
												Leaf ((replicate (k-1) False) @ (True :: (replicate (n-k) False)))
webertj@15547
  1808
											(* int *)
webertj@15547
  1809
											val k = find_index_eq True xs
webertj@14807
  1810
										in
webertj@15547
  1811
											if k=(~1) then
webertj@15547
  1812
												(* if the element was mapped to "undefined" before, map it to   *)
webertj@15547
  1813
												(* the value given by 'offset' now (and extend the length of    *)
webertj@15547
  1814
												(* the leaf)                                                    *)
webertj@15547
  1815
												(offset+1, unit_vector (offset+1, total))
webertj@15547
  1816
											else
webertj@15547
  1817
												(* if the element was already mapped to a defined value, map it *)
webertj@15547
  1818
												(* to the same value again, just extend the length of the leaf, *)
webertj@15611
  1819
												(* do not increment the 'offset'                                *)
webertj@15547
  1820
												(offset, unit_vector (k+1, total))
webertj@15547
  1821
										end
webertj@15547
  1822
									  | extend_constr (_, [], Node _) =
webertj@15547
  1823
										raise REFUTE ("IDT_constructor_interpreter", "interpretation for constructor (with no arguments left) is a node")
webertj@15547
  1824
									  | extend_constr (offset, d::ds, Node xs) =
webertj@15547
  1825
										let
webertj@15547
  1826
											(* compute the size of the type 'd' *)
webertj@15547
  1827
											val T          = typ_of_dtyp descr typ_assoc d
webertj@15547
  1828
											val (i, _, _)  = interpret thy (typs, []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@15547
  1829
											val size       = size_of_type i
webertj@15547
  1830
											(* sanity check *)
webertj@15547
  1831
											val _          = if size < length xs then
webertj@15547
  1832
													raise REFUTE ("IDT_constructor_interpreter", "new size of type is less than old size")
webertj@15547
  1833
												else
webertj@15547
  1834
													()
webertj@15547
  1835
											(* extend the existing interpretations *)
webertj@15547
  1836
											(* int * interpretation list *)
webertj@15547
  1837
											val (new_offset, intrs) = foldl_map (fn (off, i) => extend_constr (off, ds, i)) (offset, xs)
webertj@15547
  1838
											(* new elements of the type 'd' are mapped to "undefined" *)
webertj@15547
  1839
											val undefs = replicate (size - length xs) (make_undef ds)
webertj@15547
  1840
										in
webertj@15547
  1841
											(new_offset, Node (intrs @ undefs))
webertj@15547
  1842
										end
webertj@15547
  1843
									  | extend_constr (_, d::ds, Leaf _) =
webertj@15547
  1844
										raise REFUTE ("IDT_constructor_interpreter", "interpretation for constructor (with arguments left) is a leaf")
webertj@15547
  1845
									(* returns 'true' iff the constructor has a recursive argument *)
webertj@15547
  1846
									(* DatatypeAux.dtyp list -> bool *)
webertj@15547
  1847
									fun is_rec_constr ds =
webertj@15547
  1848
										Library.exists DatatypeAux.is_rec_type ds
webertj@15611
  1849
									(* constructors before 'Const (s, T)' generate elements of the datatype *)
webertj@15611
  1850
									val offset = size_of_dtyp thy typs' descr typ_assoc constrs1
webertj@15547
  1851
								in
webertj@15547
  1852
									case depth of
webertj@15547
  1853
									  NONE =>  (* equivalent to a depth of 1 *)
webertj@15547
  1854
										SOME (snd (make_constr (offset, ctypes)), model, args)
webertj@15547
  1855
									| SOME 0 =>
webertj@15547
  1856
										raise REFUTE ("IDT_constructor_interpreter", "depth is 0")
webertj@15547
  1857
									| SOME 1 =>
webertj@15547
  1858
										SOME (snd (make_constr (offset, ctypes)), model, args)
webertj@15547
  1859
									| SOME n =>  (* n > 1 *)
webertj@15547
  1860
										let
webertj@15547
  1861
											(* interpret the constructor at depth-1 *)
webertj@15547
  1862
											val (iC, _, _) = interpret thy (typs', []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Const (s, T))
webertj@15547
  1863
											(* elements generated by the constructor at depth-1 must be added to 'offset' *)
webertj@15547
  1864
											(* interpretation -> int *)
webertj@15547
  1865
											fun number_of_defined_elements (Leaf xs) =
webertj@15547
  1866
												if find_index_eq True xs = (~1) then 0 else 1
webertj@15547
  1867
											  | number_of_defined_elements (Node xs) =
webertj@15547
  1868
												sum (map number_of_defined_elements xs)
webertj@15547
  1869
											(* int *)
webertj@15547
  1870
											val offset' = offset + number_of_defined_elements iC
webertj@15547
  1871
										in
webertj@15547
  1872
											SOME (snd (extend_constr (offset', ctypes, iC)), model, args)
webertj@14807
  1873
										end
webertj@14807
  1874
								end
webertj@15547
  1875
						end
webertj@15547
  1876
					| NONE =>  (* body type is not an inductive datatype *)
webertj@15547
  1877
						NONE)
webertj@15547
  1878
				| _ =>  (* body type is a (free or schematic) type variable *)
webertj@15547
  1879
					NONE)
webertj@15547
  1880
			| _ =>  (* term is not a constant *)
webertj@15547
  1881
				NONE)
webertj@14807
  1882
	end;
webertj@14807
  1883
webertj@14807
  1884
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@14807
  1885
webertj@15547
  1886
	(* Difficult code ahead.  Make sure you understand the 'IDT_constructor_interpreter' *)
webertj@15547
  1887
	(* and the order in which it enumerates elements of an IDT before you try to         *)
webertj@15547
  1888
	(* understand this function.                                                         *)
webertj@15547
  1889
webertj@15547
  1890
	fun IDT_recursion_interpreter thy model args t =
webertj@15547
  1891
		case strip_comb t of  (* careful: here we descend arbitrarily deep into 't', *)
webertj@15547
  1892
		                      (* possibly before any other interpreter for atomic    *)
webertj@15547
  1893
		                      (* terms has had a chance to look at 't'               *)
webertj@15547
  1894
		  (Const (s, T), params) =>
webertj@15547
  1895
			(* iterate over all datatypes in 'thy' *)
haftmann@21056
  1896
			Symtab.fold (fn (_, info) => fn result =>
webertj@15547
  1897
				case result of
webertj@15547
  1898
				  SOME _ =>
webertj@15547
  1899
					result  (* just keep 'result' *)
webertj@15547
  1900
				| NONE =>
haftmann@21056
  1901
					if member (op =) (#rec_names info) s then
webertj@15767
  1902
						(* we do have a recursion operator of the datatype given by 'info', *)
webertj@15767
  1903
						(* or of a mutually recursive datatype                              *)
webertj@15547
  1904
						let
webertj@15767
  1905
							val index              = #index info
webertj@15767
  1906
							val descr              = #descr info
haftmann@17314
  1907
							val (dtname, dtyps, _) = (the o AList.lookup (op =) descr) index
webertj@15767
  1908
							(* number of all constructors, including those of different           *)
webertj@15767
  1909
							(* (mutually recursive) datatypes within the same descriptor 'descr'  *)
webertj@15767
  1910
							val mconstrs_count     = sum (map (fn (_, (_, _, cs)) => length cs) descr)
webertj@15767
  1911
							val params_count       = length params
webertj@15767
  1912
							(* the type of a recursion operator: [T1, ..., Tn, IDT] ---> Tresult *)
webertj@15767
  1913
							val IDT = List.nth (binder_types T, mconstrs_count)
webertj@15547
  1914
						in
webertj@15767
  1915
							if (fst o dest_Type) IDT <> dtname then
webertj@15767
  1916
								(* recursion operator of a mutually recursive datatype *)
webertj@15767
  1917
								NONE
webertj@15767
  1918
							else if mconstrs_count < params_count then
webertj@15547
  1919
								(* too many actual parameters; for now we'll use the *)
webertj@15547
  1920
								(* 'stlc_interpreter' to strip off one application   *)
webertj@15547
  1921
								NONE
webertj@15767
  1922
							else if mconstrs_count > params_count then
webertj@15547
  1923
								(* too few actual parameters; we use eta expansion            *)
webertj@15547
  1924
								(* Note that the resulting expansion of lambda abstractions   *)
webertj@15547
  1925
								(* by the 'stlc_interpreter' may be rather slow (depending on *)
webertj@15547
  1926
								(* the argument types and the size of the IDT, of course).    *)
webertj@15767
  1927
								SOME (interpret thy model args (eta_expand t (mconstrs_count - params_count)))
webertj@15767
  1928
							else  (* mconstrs_count = params_count *)
webertj@15547
  1929
								let
webertj@15547
  1930
									(* interpret each parameter separately *)
webertj@15547
  1931
									val ((model', args'), p_intrs) = foldl_map (fn ((m, a), p) =>
webertj@15547
  1932
										let
webertj@15547
  1933
											val (i, m', a') = interpret thy m a p
webertj@15547
  1934
										in
webertj@15547
  1935
											((m', a'), i)
webertj@15547
  1936
										end) ((model, args), params)
webertj@15767
  1937
									val (typs, _) = model'
webertj@15547
  1938
									val typ_assoc = dtyps ~~ (snd o dest_Type) IDT
webertj@15767
  1939
									(* interpret each constructor in the descriptor (including *)
webertj@15767
  1940
									(* those of mutually recursive datatypes)                  *)
webertj@15767
  1941
									(* (int * interpretation list) list *)
webertj@15767
  1942
									val mc_intrs = map (fn (idx, (_, _, cs)) =>
webertj@15767
  1943
										let
webertj@15767
  1944
											val c_return_typ = typ_of_dtyp descr typ_assoc (DatatypeAux.DtRec idx)
webertj@15767
  1945
										in
webertj@15767
  1946
											(idx, map (fn (cname, cargs) =>
webertj@15767
  1947
												(#1 o interpret thy (typs, []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True})
webertj@15767
  1948
													(Const (cname, map (typ_of_dtyp descr typ_assoc) cargs ---> c_return_typ))) cs)
webertj@15767
  1949
										end) descr
webertj@15547
  1950
									(* the recursion operator is a function that maps every element of *)
webertj@15767
  1951
									(* the inductive datatype (and of mutually recursive types) to an  *)
webertj@15783
  1952
									(* element of some result type; an array entry of NONE means that  *)
webertj@15783
  1953
									(* the actual result has not been computed yet                     *)
webertj@15767
  1954
									(* (int * interpretation option Array.array) list *)
webertj@15767
  1955
									val INTRS = map (fn (idx, _) =>
webertj@15767
  1956
										let
webertj@15767
  1957
											val T         = typ_of_dtyp descr typ_assoc (DatatypeAux.DtRec idx)
webertj@15767
  1958
											val (i, _, _) = interpret thy (typs, []) {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
webertj@15767
  1959
											val size      = size_of_type i
webertj@15767
  1960
										in
webertj@15767
  1961
											(idx, Array.array (size, NONE))
webertj@15767
  1962
										end) descr
webertj@15767
  1963
									(* takes an interpretation, and if some leaf of this interpretation   *)
webertj@15767
  1964
									(* is the 'elem'-th element of the type, the indices of the arguments *)
webertj@15767
  1965
									(* leading to this leaf are returned                                  *)
webertj@15547
  1966
									(* interpretation -> int -> int list option *)
webertj@15547
  1967
									fun get_args (Leaf xs) elem =
webertj@15547
  1968
										if find_index_eq True xs = elem then
webertj@15547
  1969
											SOME []
webertj@15547
  1970
										else
webertj@15547
  1971
											NONE
webertj@15547
  1972
									  | get_args (Node xs) elem =
webertj@15547
  1973
										let
webertj@15547
  1974
											(* interpretation * int -> int list option *)
webertj@15547
  1975
											fun search ([], _) =
webertj@15547
  1976
												NONE
webertj@15547
  1977
											  | search (x::xs, n) =
webertj@15547
  1978
												(case get_args x elem of
webertj@15547
  1979
												  SOME result => SOME (n::result)
webertj@15547
  1980
												| NONE        => search (xs, n+1))
webertj@15547
  1981
										in
webertj@15547
  1982
											search (xs, 0)
webertj@15547
  1983
										end
webertj@15547
  1984
									(* returns the index of the constructor and indices for its      *)
webertj@15547
  1985
									(* arguments that generate the 'elem'-th element of the datatype *)
webertj@15767
  1986
									(* given by 'idx'                                                *)
webertj@15767
  1987
									(* int -> int -> int * int list *)
webertj@15767
  1988
									fun get_cargs idx elem =
webertj@15547
  1989
										let
webertj@15547
  1990
											(* int * interpretation list -> int * int list *)
webertj@15547
  1991
											fun get_cargs_rec (_, []) =
webertj@15767
  1992
												raise REFUTE ("IDT_recursion_interpreter", "no matching constructor found for element " ^ string_of_int elem ^ " in datatype " ^ Sign.string_of_typ (sign_of thy) IDT ^ " (datatype index " ^ string_of_int idx ^ ")")
webertj@15547
  1993
											  | get_cargs_rec (n, x::xs) =
webertj@15547
  1994
												(case get_args x elem of
webertj@15547
  1995
												  SOME args => (n, args)
webertj@15547
  1996
												| NONE      => get_cargs_rec (n+1, xs))
webertj@15547
  1997
										in
haftmann@17314
  1998
											get_cargs_rec (0, (the o AList.lookup (op =) mc_intrs) idx)
webertj@15547
  1999
										end
webertj@15767
  2000
									(* returns the number of constructors in datatypes that occur in *)
webertj@15767
  2001
									(* the descriptor 'descr' before the datatype given by 'idx'     *)
webertj@15767
  2002
									fun get_coffset idx =
webertj@15767
  2003
										let
webertj@15767
  2004
											fun get_coffset_acc _ [] =
webertj@15767
  2005
												raise REFUTE ("IDT_recursion_interpreter", "index " ^ string_of_int idx ^ " not found in descriptor")
webertj@15767
  2006
											  | get_coffset_acc sum ((i, (_, _, cs))::descr') =
webertj@15767
  2007
												if i=idx then
webertj@15767
  2008
													sum
webertj@15767
  2009
												else
webertj@15767
  2010
													get_coffset_acc (sum + length cs) descr'
webertj@15767
  2011
										in
webertj@15767
  2012
											get_coffset_acc 0 descr
webertj@15767
  2013
										end
webertj@15767
  2014
									(* computes one entry in INTRS, and recursively all entries needed for it, *)
webertj@15767
  2015
									(* where 'idx' gives the datatype and 'elem' the element of it             *)
webertj@15767
  2016
									(* int -> int -> interpretation *)
webertj@15767
  2017
									fun compute_array_entry idx elem =
haftmann@17314
  2018
										case Array.sub ((the o AList.lookup (op =) INTRS) idx, elem) of
webertj@15767
  2019
										  SOME result =>
webertj@15767
  2020
											(* simply return the previously computed result *)
webertj@15767
  2021
											result
webertj@15767
  2022
										| NONE =>
webertj@15547
  2023
											let
webertj@15547
  2024
												(* int * int list *)
webertj@15767
  2025
												val (c, args) = get_cargs idx elem
webertj@15547
  2026
												(* interpretation * int list -> interpretation *)
webertj@15547
  2027
												fun select_subtree (tr, []) =
webertj@15547
  2028
													tr  (* return the whole tree *)
webertj@15547
  2029
												  | select_subtree (Leaf _, _) =
webertj@15547
  2030
													raise REFUTE ("IDT_recursion_interpreter", "interpretation for parameter is a leaf; cannot select a subtree")
webertj@15547
  2031
												  | select_subtree (Node tr, x::xs) =
skalberg@15570
  2032
													select_subtree (List.nth (tr, x), xs)
webertj@15547
  2033
												(* select the correct subtree of the parameter corresponding to constructor 'c' *)
webertj@15767
  2034
												val p_intr = select_subtree (List.nth (p_intrs, get_coffset idx + c), args)
webertj@15767
  2035
												(* find the indices of the constructor's recursive arguments *)
haftmann@17314
  2036
												val (_, _, constrs) = (the o AList.lookup (op =) descr) idx
webertj@15767
  2037
												val constr_args     = (snd o List.nth) (constrs, c)
webertj@15767
  2038
												val rec_args        = List.filter (DatatypeAux.is_rec_type o fst) (constr_args ~~ args)
webertj@15767
  2039
												val rec_args'       = map (fn (dtyp, elem) => (DatatypeAux.dest_DtRec dtyp, elem)) rec_args
webertj@15547
  2040
												(* apply 'p_intr' to recursively computed results *)
webertj@15767
  2041
												val result = foldl (fn ((idx, elem), intr) =>
webertj@15767
  2042
													interpretation_apply (intr, compute_array_entry idx elem)) p_intr rec_args'
webertj@15547
  2043
												(* update 'INTRS' *)
haftmann@17314
  2044
												val _ = Array.update ((the o AList.lookup (op =) INTRS) idx, elem, SOME result)
webertj@15547
  2045
											in
webertj@15767
  2046
												result
webertj@15547
  2047
											end
webertj@15767
  2048
									(* compute all entries in INTRS for the current datatype (given by 'index') *)
webertj@15783
  2049
									(* TODO: we can use Array.modify instead once PolyML conforms to the ML standard *)
webertj@15783
  2050
									(* (int * 'a -> 'a) -> 'a array -> unit *)
webertj@15783
  2051
									fun modifyi f arr =
webertj@15783
  2052
										let
webertj@15783
  2053
											val size = Array.length arr
webertj@15783
  2054
											fun modifyi_loop i =
webertj@15783
  2055
												if i < size then (
webertj@15783
  2056
													Array.update (arr, i, f (i, Array.sub (arr, i)));
webertj@15783
  2057
													modifyi_loop (i+1)
webertj@15783
  2058
												) else
webertj@15783
  2059
													()
webertj@15783
  2060
										in
webertj@15783
  2061
											modifyi_loop 0
webertj@15783
  2062
										end
haftmann@17314
  2063
									val _ = modifyi (fn (i, _) => SOME (compute_array_entry index i)) ((the o AList.lookup (op =) INTRS) index)
webertj@15547
  2064
									(* 'a Array.array -> 'a list *)
webertj@15547
  2065
									fun toList arr =
webertj@15547
  2066
										Array.foldr op:: [] arr
webertj@15547
  2067
								in
webertj@15767
  2068
									(* return the part of 'INTRS' that corresponds to the current datatype *)
haftmann@17314
  2069
									SOME ((Node o map the o toList o the o AList.lookup (op =) INTRS) index, model', args')
webertj@15547
  2070
								end
webertj@15547
  2071
						end
webertj@15547
  2072
					else
webertj@15547
  2073
						NONE  (* not a recursion operator of this datatype *)
haftmann@21056
  2074
				) (DatatypePackage.get_datatypes thy) NONE
webertj@15547
  2075
		| _ =>  (* head of term is not a constant *)
webertj@15547
  2076
			NONE;
webertj@15547
  2077
webertj@15547
  2078
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@15547
  2079
webertj@14807
  2080
	(* only an optimization: 'card' could in principle be interpreted with    *)
webertj@14807
  2081
	(* interpreters available already (using its definition), but the code    *)
webertj@15547
  2082
	(* below is more efficient                                                *)
webertj@14807
  2083
webertj@14807
  2084
	fun Finite_Set_card_interpreter thy model args t =
webertj@14807
  2085
		case t of
webertj@14807
  2086
		  Const ("Finite_Set.card", Type ("fun", [Type ("set", [T]), Type ("nat", [])])) =>
webertj@14807
  2087
			let
webertj@15547
  2088
				val (i_nat, _, _) = interpret thy model {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("nat", [])))
webertj@14807
  2089
				val size_nat      = size_of_type i_nat
webertj@15547
  2090
				val (i_set, _, _) = interpret thy model {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("set", [T])))
webertj@14807
  2091
				val constants     = make_constants i_set
webertj@14807
  2092
				(* interpretation -> int *)
webertj@14807
  2093
				fun number_of_elements (Node xs) =
skalberg@15570
  2094
					Library.foldl (fn (n, x) =>
webertj@15547
  2095
						if x=TT then
webertj@15547
  2096
							n+1
webertj@15547
  2097
						else if x=FF then
webertj@15547
  2098
							n
webertj@15547
  2099
						else
webertj@15547
  2100
							raise REFUTE ("Finite_Set_card_interpreter", "interpretation for set type does not yield a Boolean")) (0, xs)
webertj@14807
  2101
				  | number_of_elements (Leaf _) =
webertj@14807
  2102
					raise REFUTE ("Finite_Set_card_interpreter", "interpretation for set type is a leaf")
webertj@14807
  2103
				(* takes an interpretation for a set and returns an interpretation for a 'nat' *)
webertj@14807
  2104
				(* interpretation -> interpretation *)
webertj@14807
  2105
				fun card i =
webertj@14807
  2106
					let
webertj@14807
  2107
						val n = number_of_elements i
webertj@14807
  2108
					in
webertj@14807
  2109
						if n<size_nat then
webertj@14807
  2110
							Leaf ((replicate n False) @ True :: (replicate (size_nat-n-1) False))
webertj@14456
  2111
						else
webertj@14807
  2112
							Leaf (replicate size_nat False)
webertj@14807
  2113
					end
webertj@14350
  2114
			in
skalberg@15531
  2115
				SOME (Node (map card constants), model, args)
webertj@14350
  2116
			end
webertj@14807
  2117
		| _ =>
skalberg@15531
  2118
			NONE;
webertj@14807
  2119
webertj@15547
  2120
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@15547
  2121
webertj@15571
  2122
	(* only an optimization: 'Finites' could in principle be interpreted with *)
webertj@15571
  2123
	(* interpreters available already (using its definition), but the code    *)
webertj@15571
  2124
	(* below is more efficient                                                *)
webertj@15571
  2125
webertj@15571
  2126
	fun Finite_Set_Finites_interpreter thy model args t =
webertj@15571
  2127
		case t of
webertj@15571
  2128
		  Const ("Finite_Set.Finites", Type ("set", [Type ("set", [T])])) =>
webertj@15571
  2129
			let
webertj@15571
  2130
				val (i_set, _, _) = interpret thy model {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("set", [T])))
webertj@15571
  2131
				val size_set      = size_of_type i_set
webertj@15571
  2132
			in
webertj@15571
  2133
				(* we only consider finite models anyway, hence EVERY set is in "Finites" *)
webertj@15571
  2134
				SOME (Node (replicate size_set TT), model, args)
webertj@15571
  2135
			end
webertj@15571
  2136
		| _ =>
webertj@15571
  2137
			NONE;
webertj@15571
  2138
webertj@15571
  2139
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@15571
  2140
webertj@15547
  2141
	(* only an optimization: 'op <' could in principle be interpreted with    *)
webertj@15547
  2142
	(* interpreters available already (using its definition), but the code    *)
webertj@15547
  2143
	(* below is more efficient                                                *)
webertj@15547
  2144
webertj@15547
  2145
	fun Nat_less_interpreter thy model args t =
webertj@15547
  2146
		case t of
haftmann@19277
  2147
		  Const ("Orderings.less", Type ("fun", [Type ("nat", []), Type ("fun", [Type ("nat", []), Type ("bool", [])])])) =>
webertj@15547
  2148
			let
webertj@15547
  2149
				val (i_nat, _, _) = interpret thy model {maxvars=0, def_eq=false, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("nat", [])))
webertj@15547
  2150
				val size_nat      = size_of_type i_nat
webertj@15547
  2151
				(* int -> interpretation *)
webertj@15547
  2152
				(* the 'n'-th nat is not less than the first 'n' nats, while it *)
webertj@15547
  2153
				(* is less than the remaining 'size_nat - n' nats               *)
webertj@15547
  2154
				fun less n = Node ((replicate n FF) @ (replicate (size_nat - n) TT))
webertj@15547
  2155
			in
webertj@15547
  2156
				SOME (Node (map less (1 upto size_nat)), model, args)
webertj@15547
  2157
			end
webertj@15547
  2158
		| _ =>
webertj@15547
  2159
			NONE;
webertj@15547
  2160
webertj@15547
  2161
	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
webertj@15547
  2162
haftmann@19233
  2163
	(* only an optimization: 'HOL.plus' could in principle be interpreted with*)
webertj@15547
  2164
	(* interpreters available already (using its definition), but the code    *)
webertj@15547
  2165
	(* below is more efficient                                                *)
webertj@15547
  2166
webertj@15547
  2167
	fun Nat_plus_interpreter thy model args t =
webertj@15547
  2168
		case t of
haftmann@19233
  2169
		  Const ("HOL.plus", Type ("fun", [Type ("nat", []), Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>