src/HOL/Tools/refute.ML
changeset 15334 d5a92997dc1b
parent 15333 77b2bca7fcb5
child 15335 f81e6e24351f
equal deleted inserted replaced
15333:77b2bca7fcb5 15334:d5a92997dc1b
    25 	type params
    25 	type params
    26 	type interpretation
    26 	type interpretation
    27 	type model
    27 	type model
    28 	type arguments
    28 	type arguments
    29 
    29 
    30 	exception CANNOT_INTERPRET of Term.term
       
    31 	exception MAXVARS_EXCEEDED
    30 	exception MAXVARS_EXCEEDED
    32 
    31 
    33 	val add_interpreter : string -> (theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option) -> theory -> theory
    32 	val add_interpreter : string -> (theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option) -> theory -> theory
    34 	val add_printer     : string -> (theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option) -> theory -> theory
    33 	val add_printer     : string -> (theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option) -> theory -> theory
    35 
    34 
    36 	val interpret : theory -> model -> arguments -> Term.term -> (interpretation * model * arguments)  (* exception CANNOT_INTERPRET *)
    35 	val interpret : theory -> model -> arguments -> Term.term -> (interpretation * model * arguments)
    37 
    36 
    38 	val print       : theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term
    37 	val print       : theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term
    39 	val print_model : theory -> model -> (int -> bool) -> string
    38 	val print_model : theory -> model -> (int -> bool) -> string
    40 
    39 
    41 (* ------------------------------------------------------------------------- *)
    40 (* ------------------------------------------------------------------------- *)
    64 	(* We use 'REFUTE' only for internal error conditions that should    *)
    63 	(* We use 'REFUTE' only for internal error conditions that should    *)
    65 	(* never occur in the first place (i.e. errors caused by bugs in our *)
    64 	(* never occur in the first place (i.e. errors caused by bugs in our *)
    66 	(* code).  Otherwise (e.g. to indicate invalid input data) we use    *)
    65 	(* code).  Otherwise (e.g. to indicate invalid input data) we use    *)
    67 	(* 'error'.                                                          *)
    66 	(* 'error'.                                                          *)
    68 	exception REFUTE of string * string;  (* ("in function", "cause") *)
    67 	exception REFUTE of string * string;  (* ("in function", "cause") *)
    69 
       
    70 	exception CANNOT_INTERPRET of Term.term;
       
    71 
    68 
    72 	(* should be raised by an interpreter when more variables would be *)
    69 	(* should be raised by an interpreter when more variables would be *)
    73 	(* required than allowed by 'maxvars'                              *)
    70 	(* required than allowed by 'maxvars'                              *)
    74 	exception MAXVARS_EXCEEDED;
    71 	exception MAXVARS_EXCEEDED;
    75 
    72 
   205 
   202 
   206 	structure RefuteData = TheoryDataFun(RefuteDataArgs);
   203 	structure RefuteData = TheoryDataFun(RefuteDataArgs);
   207 
   204 
   208 
   205 
   209 (* ------------------------------------------------------------------------- *)
   206 (* ------------------------------------------------------------------------- *)
   210 (* interpret: tries to interpret the term 't' using a suitable interpreter;  *)
   207 (* interpret: interprets the term 't' using a suitable interpreter; returns  *)
   211 (*            returns the interpretation and a (possibly extended) model     *)
   208 (*            the interpretation and a (possibly extended) model that keeps  *)
   212 (*            that keeps track of the interpretation of subterms             *)
   209 (*            track of the interpretation of subterms                        *)
   213 (* Note: exception 'CANNOT_INTERPRET t' is raised if the term cannot be      *)
       
   214 (*       interpreted by any interpreter                                      *)
       
   215 (* ------------------------------------------------------------------------- *)
   210 (* ------------------------------------------------------------------------- *)
   216 
   211 
   217 	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) *)
   212 	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) *)
   218 
   213 
   219 	fun interpret thy model args t =
   214 	fun interpret thy model args t =
   220 		(case get_first (fn (_, f) => f thy model args t) (#interpreters (RefuteData.get thy)) of
   215 		(case get_first (fn (_, f) => f thy model args t) (#interpreters (RefuteData.get thy)) of
   221 		  None   => raise (CANNOT_INTERPRET t)
   216 		  None   => raise REFUTE ("interpret", "unable to interpret term " ^ quote (Sign.string_of_term (sign_of thy) t))
   222 		| Some x => x);
   217 		| Some x => x);
   223 
   218 
   224 (* ------------------------------------------------------------------------- *)
   219 (* ------------------------------------------------------------------------- *)
   225 (* print: tries to convert the constant denoted by the term 't' into a term  *)
   220 (* print: tries to convert the constant denoted by the term 't' into a term  *)
   226 (*        using a suitable printer                                           *)
   221 (*        using a suitable printer                                           *)
   876 			val _      = writeln ("Ground types: "
   871 			val _      = writeln ("Ground types: "
   877 				^ (if null types then "none."
   872 				^ (if null types then "none."
   878 				   else commas (map (Sign.string_of_typ (sign_of thy)) types)))
   873 				   else commas (map (Sign.string_of_typ (sign_of thy)) types)))
   879 			(* (Term.typ * int) list -> unit *)
   874 			(* (Term.typ * int) list -> unit *)
   880 			fun find_model_loop universe =
   875 			fun find_model_loop universe =
   881 			(let
   876 			let
   882 				val init_model             = (universe, [])
   877 				val init_model             = (universe, [])
   883 				val init_args              = {maxvars = maxvars, next_idx = 1, bounds = [], wellformed = True}
   878 				val init_args              = {maxvars = maxvars, next_idx = 1, bounds = [], wellformed = True}
   884 				val _                      = immediate_output ("Translating term (sizes: " ^ commas (map (fn (_, n) => string_of_int n) universe) ^ ") ...")
   879 				val _                      = immediate_output ("Translating term (sizes: " ^ commas (map (fn (_, n) => string_of_int n) universe) ^ ") ...")
   885 				(* translate 't' and all axioms *)
   880 				(* translate 't' and all axioms *)
   886 				val ((model, args), intrs) = foldl_map (fn ((m, a), t') =>
   881 				val ((model, args), intrs) = foldl_map (fn ((m, a), t') =>
   906 					| None           => writeln "Search terminated, no larger universe within the given limits."))
   901 					| None           => writeln "Search terminated, no larger universe within the given limits."))
   907 				handle SatSolver.NOT_CONFIGURED =>
   902 				handle SatSolver.NOT_CONFIGURED =>
   908 					error ("SAT solver " ^ quote satsolver ^ " is not configured.")
   903 					error ("SAT solver " ^ quote satsolver ^ " is not configured.")
   909 			end handle MAXVARS_EXCEEDED =>
   904 			end handle MAXVARS_EXCEEDED =>
   910 				writeln ("\nSearch terminated, number of Boolean variables (" ^ string_of_int maxvars ^ " allowed) exceeded.")
   905 				writeln ("\nSearch terminated, number of Boolean variables (" ^ string_of_int maxvars ^ " allowed) exceeded.")
   911 			| CANNOT_INTERPRET t' =>
       
   912 				error ("Unable to interpret term " ^ Sign.string_of_term (sign_of thy) t'))
       
   913 			in
   906 			in
   914 				find_model_loop (first_universe types sizes minsize)
   907 				find_model_loop (first_universe types sizes minsize)
   915 			end
   908 			end
   916 		in
   909 		in
   917 			(* some parameter sanity checks *)
   910 			(* some parameter sanity checks *)
  1179 			  Type ("fun", [T1, T2]) =>
  1172 			  Type ("fun", [T1, T2]) =>
  1180 				let
  1173 				let
  1181 					(* we create 'size_of_type (interpret (... T1))' different copies *)
  1174 					(* we create 'size_of_type (interpret (... T1))' different copies *)
  1182 					(* of the interpretation for 'T2', which are then combined into a *)
  1175 					(* of the interpretation for 'T2', which are then combined into a *)
  1183 					(* single new interpretation                                      *)
  1176 					(* single new interpretation                                      *)
  1184 					val (i1, _, _) =
  1177 					val (i1, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T1))
  1185 						(interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T1))
       
  1186 						handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1187 					(* make fresh copies, with different variable indices *)
  1178 					(* make fresh copies, with different variable indices *)
  1188 					(* 'idx': next variable index                         *)
  1179 					(* 'idx': next variable index                         *)
  1189 					(* 'n'  : number of copies                            *)
  1180 					(* 'n'  : number of copies                            *)
  1190 					(* int -> int -> (int * interpretation list * prop_formula *)
  1181 					(* int -> int -> (int * interpretation list * prop_formula *)
  1191 					fun make_copies idx 0 =
  1182 					fun make_copies idx 0 =
  1192 						(idx, [], True)
  1183 						(idx, [], True)
  1193 					  | make_copies idx n =
  1184 					  | make_copies idx n =
  1194 						let
  1185 						let
  1195 							val (copy, _, new_args) =
  1186 							val (copy, _, new_args) = interpret thy (typs, []) {maxvars = maxvars, next_idx = idx, bounds = [], wellformed = True} (Free ("dummy", T2))
  1196 								(interpret thy (typs, []) {maxvars = maxvars, next_idx = idx, bounds = [], wellformed = True} (Free ("dummy", T2))
       
  1197 								handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1198 							val (idx', copies, wf') = make_copies (#next_idx new_args) (n-1)
  1187 							val (idx', copies, wf') = make_copies (#next_idx new_args) (n-1)
  1199 						in
  1188 						in
  1200 							(idx', copy :: copies, SAnd (#wellformed new_args, wf'))
  1189 							(idx', copy :: copies, SAnd (#wellformed new_args, wf'))
  1201 						end
  1190 						end
  1202 					val (next, copies, wf) = make_copies next_idx (size_of_type i1)
  1191 					val (next, copies, wf) = make_copies next_idx (size_of_type i1)
  1226 			| Bound i          =>
  1215 			| Bound i          =>
  1227 				Some (nth_elem (i, #bounds args), model, args)
  1216 				Some (nth_elem (i, #bounds args), model, args)
  1228 			| Abs (x, T, body) =>
  1217 			| Abs (x, T, body) =>
  1229 				let
  1218 				let
  1230 					(* create all constants of type 'T' *)
  1219 					(* create all constants of type 'T' *)
  1231 					val (i, _, _) =
  1220 					val (i, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1232 						(interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
       
  1233 						handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1234 					val constants = make_constants i
  1221 					val constants = make_constants i
  1235 					(* interpret the 'body' separately for each constant *)
  1222 					(* interpret the 'body' separately for each constant *)
  1236 					val ((model', args'), bodies) = foldl_map
  1223 					val ((model', args'), bodies) = foldl_map
  1237 						(fn ((m,a), c) =>
  1224 						(fn ((m,a), c) =>
  1238 							let
  1225 							let
  1386 				val (i2, m2, a2) = interpret thy m1 a1 t2
  1373 				val (i2, m2, a2) = interpret thy m1 a1 t2
  1387 			in
  1374 			in
  1388 				Some (make_equality (i1, i2), m2, a2)
  1375 				Some (make_equality (i1, i2), m2, a2)
  1389 			end
  1376 			end
  1390 		| Const ("op =", _) $ t1 =>
  1377 		| Const ("op =", _) $ t1 =>
  1391 			(Some (interpret thy model args (eta_expand t 1)) handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1378 			Some (interpret thy model args (eta_expand t 1))
  1392 		| Const ("op =", _) =>
  1379 		| Const ("op =", _) =>
  1393 			(Some (interpret thy model args (eta_expand t 2)) handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1380 			Some (interpret thy model args (eta_expand t 2))
  1394 		| Const ("op &", _) =>
  1381 		| Const ("op &", _) =>
  1395 			Some (Node [Node [TT, FF], Node [FF, FF]], model, args)
  1382 			Some (Node [Node [TT, FF], Node [FF, FF]], model, args)
  1396 		| Const ("op |", _) =>
  1383 		| Const ("op |", _) =>
  1397 			Some (Node [Node [TT, TT], Node [TT, FF]], model, args)
  1384 			Some (Node [Node [TT, TT], Node [TT, FF]], model, args)
  1398 		| Const ("op -->", _) =>
  1385 		| Const ("op -->", _) =>
  1411 			(* return an existing interpretation *)
  1398 			(* return an existing interpretation *)
  1412 			Some (intr, model, args)
  1399 			Some (intr, model, args)
  1413 		| None =>
  1400 		| None =>
  1414 			(case t of
  1401 			(case t of
  1415 			  Free (x, Type ("set", [T])) =>
  1402 			  Free (x, Type ("set", [T])) =>
  1416 				(let
  1403 				let
  1417 					val (intr, _, args') = interpret thy (typs, []) args (Free (x, T --> HOLogic.boolT))
  1404 					val (intr, _, args') = interpret thy (typs, []) args (Free (x, T --> HOLogic.boolT))
  1418 				in
  1405 				in
  1419 					Some (intr, (typs, (t, intr)::terms), args')
  1406 					Some (intr, (typs, (t, intr)::terms), args')
  1420 				end handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1407 				end
  1421 			| Var ((x,i), Type ("set", [T])) =>
  1408 			| Var ((x,i), Type ("set", [T])) =>
  1422 				(let
  1409 				let
  1423 					val (intr, _, args') = interpret thy (typs, []) args (Var ((x,i), T --> HOLogic.boolT))
  1410 					val (intr, _, args') = interpret thy (typs, []) args (Var ((x,i), T --> HOLogic.boolT))
  1424 				in
  1411 				in
  1425 					Some (intr, (typs, (t, intr)::terms), args')
  1412 					Some (intr, (typs, (t, intr)::terms), args')
  1426 				end handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1413 				end
  1427 			| Const (s, Type ("set", [T])) =>
  1414 			| Const (s, Type ("set", [T])) =>
  1428 				(let
  1415 				let
  1429 					val (intr, _, args') = interpret thy (typs, []) args (Const (s, T --> HOLogic.boolT))
  1416 					val (intr, _, args') = interpret thy (typs, []) args (Const (s, T --> HOLogic.boolT))
  1430 				in
  1417 				in
  1431 					Some (intr, (typs, (t, intr)::terms), args')
  1418 					Some (intr, (typs, (t, intr)::terms), args')
  1432 				end handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1419 				end
  1433 			(* 'Collect' == identity *)
  1420 			(* 'Collect' == identity *)
  1434 			| Const ("Collect", _) $ t1 =>
  1421 			| Const ("Collect", _) $ t1 =>
  1435 				Some (interpret thy model args t1)
  1422 				Some (interpret thy model args t1)
  1436 			| Const ("Collect", _) =>
  1423 			| Const ("Collect", _) =>
  1437 				(Some (interpret thy model args (eta_expand t 1)) handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1424 				Some (interpret thy model args (eta_expand t 1))
  1438 			(* 'op :' == application *)
  1425 			(* 'op :' == application *)
  1439 			| Const ("op :", _) $ t1 $ t2 =>
  1426 			| Const ("op :", _) $ t1 $ t2 =>
  1440 				Some (interpret thy model args (t2 $ t1))
  1427 				Some (interpret thy model args (t2 $ t1))
  1441 			| Const ("op :", _) $ t1 =>
  1428 			| Const ("op :", _) $ t1 =>
  1442 				(Some (interpret thy model args (eta_expand t 1)) handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1429 				Some (interpret thy model args (eta_expand t 1))
  1443 			| Const ("op :", _) =>
  1430 			| Const ("op :", _) =>
  1444 				(Some (interpret thy model args (eta_expand t 2)) handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1431 				Some (interpret thy model args (eta_expand t 2))
  1445 			| _ => None)
  1432 			| _ => None)
  1446 	end;
  1433 	end;
  1447 
  1434 
  1448 	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
  1435 	(* theory -> model -> arguments -> Term.term -> (interpretation * model * arguments) option *)
  1449 
  1436 
  1472 		fun size_of_dtyp typs descr typ_assoc constrs =
  1459 		fun size_of_dtyp typs descr typ_assoc constrs =
  1473 			sum (map (fn (_, ds) =>
  1460 			sum (map (fn (_, ds) =>
  1474 				product (map (fn d =>
  1461 				product (map (fn d =>
  1475 					let
  1462 					let
  1476 						val T         = typ_of_dtyp descr typ_assoc d
  1463 						val T         = typ_of_dtyp descr typ_assoc d
  1477 						val (i, _, _) =
  1464 						val (i, _, _) = interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1478 							(interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
       
  1479 							handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1480 					in
  1465 					in
  1481 						size_of_type i
  1466 						size_of_type i
  1482 					end) ds)) constrs)
  1467 					end) ds)) constrs)
  1483 		(* Term.typ -> (interpretation * model * arguments) option *)
  1468 		(* Term.typ -> (interpretation * model * arguments) option *)
  1484 		fun interpret_variable (Type (s, Ts)) =
  1469 		fun interpret_variable (Type (s, Ts)) =
  1589 												Leaf (replicate total False)
  1574 												Leaf (replicate total False)
  1590 											  | make_partial (d::ds) =
  1575 											  | make_partial (d::ds) =
  1591 												let
  1576 												let
  1592 													(* compute the "new" size of the type 'd' *)
  1577 													(* compute the "new" size of the type 'd' *)
  1593 													val T         = typ_of_dtyp descr typ_assoc d
  1578 													val T         = typ_of_dtyp descr typ_assoc d
  1594 													val (i, _, _) =
  1579 													val (i, _, _) = interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1595 														(interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
       
  1596 														handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1597 												in
  1580 												in
  1598 													(* all entries of the whole subtree are 'False' *)
  1581 													(* all entries of the whole subtree are 'False' *)
  1599 													Node (replicate (size_of_type i) (make_partial ds))
  1582 													Node (replicate (size_of_type i) (make_partial ds))
  1600 												end
  1583 												end
  1601 											(* int * DatatypeAux.dtyp list -> int * interpretation *)
  1584 											(* int * DatatypeAux.dtyp list -> int * interpretation *)
  1605 												else
  1588 												else
  1606 													raise REFUTE ("IDT_interpreter", "internal error: offset >= total")
  1589 													raise REFUTE ("IDT_interpreter", "internal error: offset >= total")
  1607 											  | make_constr (offset, d::ds) =
  1590 											  | make_constr (offset, d::ds) =
  1608 												let
  1591 												let
  1609 													(* compute the "new" and "old" size of the type 'd' *)
  1592 													(* compute the "new" and "old" size of the type 'd' *)
  1610 													val T         = typ_of_dtyp descr typ_assoc d
  1593 													val T          = typ_of_dtyp descr typ_assoc d
  1611 													val (i, _, _) =
  1594 													val (i, _, _)  = interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1612 														(interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1595 													val (i', _, _) = interpret thy (typs', []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1613 														handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
  1596 													val size       = size_of_type i
  1614 													val (i', _, _) =
  1597 													val size'      = size_of_type i'
  1615 														(interpret thy (typs', []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
       
  1616 														handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1617 													val size  = size_of_type i
       
  1618 													val size' = size_of_type i'
       
  1619 													val _ = if size<size' then
  1598 													val _ = if size<size' then
  1620 															raise REFUTE ("IDT_interpreter", "internal error: new size < old size")
  1599 															raise REFUTE ("IDT_interpreter", "internal error: new size < old size")
  1621 														else
  1600 														else
  1622 															()
  1601 															()
  1623 													val (new_offset, intrs) = foldl_map make_constr (offset, replicate size' ds)
  1602 													val (new_offset, intrs) = foldl_map make_constr (offset, replicate size' ds)
  1650 
  1629 
  1651 	fun Finite_Set_card_interpreter thy model args t =
  1630 	fun Finite_Set_card_interpreter thy model args t =
  1652 		case t of
  1631 		case t of
  1653 		  Const ("Finite_Set.card", Type ("fun", [Type ("set", [T]), Type ("nat", [])])) =>
  1632 		  Const ("Finite_Set.card", Type ("fun", [Type ("set", [T]), Type ("nat", [])])) =>
  1654 			let
  1633 			let
  1655 				val (i_nat, _, _) =
  1634 				val (i_nat, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("nat", [])))
  1656 					(interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("nat", [])))
       
  1657 						handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1658 				val size_nat      = size_of_type i_nat
  1635 				val size_nat      = size_of_type i_nat
  1659 				val (i_set, _, _) =
  1636 				val (i_set, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("set", [T])))
  1660 					(interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", Type ("set", [T])))
       
  1661 						handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1662 				val constants     = make_constants i_set
  1637 				val constants     = make_constants i_set
  1663 				(* interpretation -> int *)
  1638 				(* interpretation -> int *)
  1664 				fun number_of_elements (Node xs) =
  1639 				fun number_of_elements (Node xs) =
  1665 					foldl (fn (n, x) =>
  1640 					foldl (fn (n, x) =>
  1666 						if x=TT then n+1 else if x=FF then n else raise REFUTE ("Finite_Set_card_interpreter", "interpretation for set type does not yield a Boolean")) (0, xs)
  1641 						if x=TT then n+1 else if x=FF then n else raise REFUTE ("Finite_Set_card_interpreter", "interpretation for set type does not yield a Boolean")) (0, xs)
  1719 	in
  1694 	in
  1720 		case typeof t of
  1695 		case typeof t of
  1721 		  Some T =>
  1696 		  Some T =>
  1722 			(case T of
  1697 			(case T of
  1723 			  Type ("fun", [T1, T2]) =>
  1698 			  Type ("fun", [T1, T2]) =>
  1724 				(let
  1699 				let
  1725 					(* create all constants of type 'T1' *)
  1700 					(* create all constants of type 'T1' *)
  1726 					val (i, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T1))
  1701 					val (i, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T1))
  1727 					val constants = make_constants i
  1702 					val constants = make_constants i
  1728 					(* interpretation list *)
  1703 					(* interpretation list *)
  1729 					val results = (case intr of
  1704 					val results = (case intr of
  1741 					(* Term.term *)
  1716 					(* Term.term *)
  1742 					val HOLogic_empty_set = Const ("{}", HOLogic_setT)
  1717 					val HOLogic_empty_set = Const ("{}", HOLogic_setT)
  1743 					val HOLogic_insert    = Const ("insert", HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  1718 					val HOLogic_insert    = Const ("insert", HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  1744 				in
  1719 				in
  1745 					Some (foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc) (pairs, HOLogic_empty_set))
  1720 					Some (foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc) (pairs, HOLogic_empty_set))
  1746 				end handle CANNOT_INTERPRET _ => None)
  1721 				end
  1747 			| Type ("prop", [])      =>
  1722 			| Type ("prop", [])      =>
  1748 				(case index_from_interpretation intr of
  1723 				(case index_from_interpretation intr of
  1749 				  0 => Some (HOLogic.mk_Trueprop HOLogic.true_const)
  1724 				  0 => Some (HOLogic.mk_Trueprop HOLogic.true_const)
  1750 				| 1 => Some (HOLogic.mk_Trueprop HOLogic.false_const)
  1725 				| 1 => Some (HOLogic.mk_Trueprop HOLogic.false_const)
  1751 				| _ => raise REFUTE ("stlc_interpreter", "illegal interpretation for a propositional value"))
  1726 				| _ => raise REFUTE ("stlc_interpreter", "illegal interpretation for a propositional value"))
  1766 		  | typeof (Const (_, T)) = Some T
  1741 		  | typeof (Const (_, T)) = Some T
  1767 		  | typeof _              = None
  1742 		  | typeof _              = None
  1768 	in
  1743 	in
  1769 		case typeof t of
  1744 		case typeof t of
  1770 		  Some (Type ("set", [T])) =>
  1745 		  Some (Type ("set", [T])) =>
  1771 			(let
  1746 			let
  1772 				(* create all constants of type 'T' *)
  1747 				(* create all constants of type 'T' *)
  1773 				val (i, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1748 				val (i, _, _) = interpret thy model {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1774 				val constants = make_constants i
  1749 				val constants = make_constants i
  1775 				(* interpretation list *)
  1750 				(* interpretation list *)
  1776 				val results = (case intr of
  1751 				val results = (case intr of
  1794 				(* Term.term *)
  1769 				(* Term.term *)
  1795 				val HOLogic_empty_set = Const ("{}", HOLogic_setT)
  1770 				val HOLogic_empty_set = Const ("{}", HOLogic_setT)
  1796 				val HOLogic_insert    = Const ("insert", T --> HOLogic_setT --> HOLogic_setT)
  1771 				val HOLogic_insert    = Const ("insert", T --> HOLogic_setT --> HOLogic_setT)
  1797 			in
  1772 			in
  1798 				Some (foldl (fn (acc, elem) => HOLogic_insert $ elem $ acc) (HOLogic_empty_set, elements))
  1773 				Some (foldl (fn (acc, elem) => HOLogic_insert $ elem $ acc) (HOLogic_empty_set, elements))
  1799 			end handle CANNOT_INTERPRET _ => None)
  1774 			end
  1800 		| _ =>
  1775 		| _ =>
  1801 			None
  1776 			None
  1802 	end;
  1777 	end;
  1803 
  1778 
  1804 	(* theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option *)
  1779 	(* theory -> model -> Term.term -> interpretation -> (int -> bool) -> Term.term option *)
  1858 					fun size_of_dtyp typs descr typ_assoc xs =
  1833 					fun size_of_dtyp typs descr typ_assoc xs =
  1859 						sum (map (fn (_, ds) =>
  1834 						sum (map (fn (_, ds) =>
  1860 							product (map (fn d =>
  1835 							product (map (fn d =>
  1861 								let
  1836 								let
  1862 									val T         = typ_of_dtyp descr typ_assoc d
  1837 									val T         = typ_of_dtyp descr typ_assoc d
  1863 									val (i, _, _) =
  1838 									val (i, _, _) = interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
  1864 										(interpret thy (typs, []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", T))
       
  1865 										handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1866 					in
  1839 					in
  1867 						size_of_type i
  1840 						size_of_type i
  1868 					end) ds)) xs)
  1841 					end) ds)) xs)
  1869 					(* int -> DatatypeAux.dtyp list -> Term.term list *)
  1842 					(* int -> DatatypeAux.dtyp list -> Term.term list *)
  1870 					fun make_args n [] =
  1843 					fun make_args n [] =
  1873 						else
  1846 						else
  1874 							[]
  1847 							[]
  1875 					  | make_args n (d::ds) =
  1848 					  | make_args n (d::ds) =
  1876 						let
  1849 						let
  1877 							val dT        = typ_of_dtyp descr typ_assoc d
  1850 							val dT        = typ_of_dtyp descr typ_assoc d
  1878 							val (i, _, _) =
  1851 							val (i, _, _) = interpret thy (typs', []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", dT))
  1879 								(interpret thy (typs', []) {maxvars=0, next_idx=1, bounds=[], wellformed=True} (Free ("dummy", dT))
       
  1880 								handle CANNOT_INTERPRET _ => raise CANNOT_INTERPRET t)
       
  1881 							val size      = size_of_type i
  1852 							val size      = size_of_type i
  1882 							val consts    = make_constants i  (* we only need the (n mod size)-th element of *)
  1853 							val consts    = make_constants i  (* we only need the (n mod size)-th element of *)
  1883 								(* this list, so there might be a more efficient implementation that does not *)
  1854 								(* this list, so there might be a more efficient implementation that does not *)
  1884 								(* generate all constants                                                     *)
  1855 								(* generate all constants                                                     *)
  1885 						in
  1856 						in