src/HOL/Library/refute.ML
changeset 55507 5f27fb2110e0
parent 55411 27de2c976d90
child 55891 d1a9b07783ab
equal deleted inserted replaced
55506:46f3e31c5a87 55507:5f27fb2110e0
   100 
   100 
   101 datatype 'a tree =
   101 datatype 'a tree =
   102     Leaf of 'a
   102     Leaf of 'a
   103   | Node of ('a tree) list;
   103   | Node of ('a tree) list;
   104 
   104 
   105 (* ('a -> 'b) -> 'a tree -> 'b tree *)
       
   106 
       
   107 fun tree_map f tr =
   105 fun tree_map f tr =
   108   case tr of
   106   case tr of
   109     Leaf x  => Leaf (f x)
   107     Leaf x  => Leaf (f x)
   110   | Node xs => Node (map (tree_map f) xs);
   108   | Node xs => Node (map (tree_map f) xs);
   111 
       
   112 (* ('a * 'b -> 'a) -> 'a * ('b tree) -> 'a *)
       
   113 
       
   114 fun tree_foldl f =
       
   115   let
       
   116     fun itl (e, Leaf x)  = f(e,x)
       
   117       | itl (e, Node xs) = Library.foldl (tree_foldl f) (e,xs)
       
   118   in
       
   119     itl
       
   120   end;
       
   121 
       
   122 (* 'a tree * 'b tree -> ('a * 'b) tree *)
       
   123 
   109 
   124 fun tree_pair (t1, t2) =
   110 fun tree_pair (t1, t2) =
   125   case t1 of
   111   case t1 of
   126     Leaf x =>
   112     Leaf x =>
   127       (case t2 of
   113       (case t2 of
   333 (*      returns a record that can be passed to 'find_model'.                 *)
   319 (*      returns a record that can be passed to 'find_model'.                 *)
   334 (* ------------------------------------------------------------------------- *)
   320 (* ------------------------------------------------------------------------- *)
   335 
   321 
   336 fun actual_params ctxt override =
   322 fun actual_params ctxt override =
   337   let
   323   let
   338     (* (string * string) list * string -> bool *)
       
   339     fun read_bool (parms, name) =
   324     fun read_bool (parms, name) =
   340       case AList.lookup (op =) parms name of
   325       case AList.lookup (op =) parms name of
   341         SOME "true" => true
   326         SOME "true" => true
   342       | SOME "false" => false
   327       | SOME "false" => false
   343       | SOME s => error ("parameter " ^ quote name ^
   328       | SOME s => error ("parameter " ^ quote name ^
   344           " (value is " ^ quote s ^ ") must be \"true\" or \"false\"")
   329           " (value is " ^ quote s ^ ") must be \"true\" or \"false\"")
   345       | NONE   => error ("parameter " ^ quote name ^
   330       | NONE   => error ("parameter " ^ quote name ^
   346           " must be assigned a value")
   331           " must be assigned a value")
   347     (* (string * string) list * string -> int *)
       
   348     fun read_int (parms, name) =
   332     fun read_int (parms, name) =
   349       case AList.lookup (op =) parms name of
   333       case AList.lookup (op =) parms name of
   350         SOME s =>
   334         SOME s =>
   351           (case Int.fromString s of
   335           (case Int.fromString s of
   352             SOME i => i
   336             SOME i => i
   353           | NONE   => error ("parameter " ^ quote name ^
   337           | NONE   => error ("parameter " ^ quote name ^
   354             " (value is " ^ quote s ^ ") must be an integer value"))
   338             " (value is " ^ quote s ^ ") must be an integer value"))
   355       | NONE => error ("parameter " ^ quote name ^
   339       | NONE => error ("parameter " ^ quote name ^
   356           " must be assigned a value")
   340           " must be assigned a value")
   357     (* (string * string) list * string -> string *)
       
   358     fun read_string (parms, name) =
   341     fun read_string (parms, name) =
   359       case AList.lookup (op =) parms name of
   342       case AList.lookup (op =) parms name of
   360         SOME s => s
   343         SOME s => s
   361       | NONE => error ("parameter " ^ quote name ^
   344       | NONE => error ("parameter " ^ quote name ^
   362         " must be assigned a value")
   345         " must be assigned a value")
   363     (* 'override' first, defaults last: *)
   346     (* 'override' first, defaults last: *)
   364     (* (string * string) list *)
       
   365     val allparams = override @ get_default_params ctxt
   347     val allparams = override @ get_default_params ctxt
   366     (* int *)
       
   367     val minsize = read_int (allparams, "minsize")
   348     val minsize = read_int (allparams, "minsize")
   368     val maxsize = read_int (allparams, "maxsize")
   349     val maxsize = read_int (allparams, "maxsize")
   369     val maxvars = read_int (allparams, "maxvars")
   350     val maxvars = read_int (allparams, "maxvars")
   370     val maxtime = read_int (allparams, "maxtime")
   351     val maxtime = read_int (allparams, "maxtime")
   371     (* string *)
       
   372     val satsolver = read_string (allparams, "satsolver")
   352     val satsolver = read_string (allparams, "satsolver")
   373     val no_assms = read_bool (allparams, "no_assms")
   353     val no_assms = read_bool (allparams, "no_assms")
   374     val expect = the_default "" (AList.lookup (op =) allparams "expect")
   354     val expect = the_default "" (AList.lookup (op =) allparams "expect")
   375     (* all remaining parameters of the form "string=int" are collected in *)
   355     (* all remaining parameters of the form "string=int" are collected in *)
   376     (* 'sizes'                                                            *)
   356     (* 'sizes'                                                            *)
   459 (* get_def: looks up the definition of a constant                            *)
   439 (* get_def: looks up the definition of a constant                            *)
   460 (* ------------------------------------------------------------------------- *)
   440 (* ------------------------------------------------------------------------- *)
   461 
   441 
   462 fun get_def thy (s, T) =
   442 fun get_def thy (s, T) =
   463   let
   443   let
   464     (* (string * Term.term) list -> (string * Term.term) option *)
       
   465     fun get_def_ax [] = NONE
   444     fun get_def_ax [] = NONE
   466       | get_def_ax ((axname, ax) :: axioms) =
   445       | get_def_ax ((axname, ax) :: axioms) =
   467           (let
   446           (let
   468             val (lhs, _) = Logic.dest_equals ax  (* equations only *)
   447             val (lhs, _) = Logic.dest_equals ax  (* equations only *)
   469             val c        = Term.head_of lhs
   448             val c        = Term.head_of lhs
   490 (* get_typedef: looks up the definition of a type, as created by "typedef"   *)
   469 (* get_typedef: looks up the definition of a type, as created by "typedef"   *)
   491 (* ------------------------------------------------------------------------- *)
   470 (* ------------------------------------------------------------------------- *)
   492 
   471 
   493 fun get_typedef thy T =
   472 fun get_typedef thy T =
   494   let
   473   let
   495     (* (string * Term.term) list -> (string * Term.term) option *)
       
   496     fun get_typedef_ax [] = NONE
   474     fun get_typedef_ax [] = NONE
   497       | get_typedef_ax ((axname, ax) :: axioms) =
   475       | get_typedef_ax ((axname, ax) :: axioms) =
   498           (let
   476           (let
   499             (* Term.term -> Term.typ option *)
       
   500             fun type_of_type_definition (Const (s', T')) =
   477             fun type_of_type_definition (Const (s', T')) =
   501                   if s'= @{const_name type_definition} then
   478                   if s'= @{const_name type_definition} then
   502                     SOME T'
   479                     SOME T'
   503                   else
   480                   else
   504                     NONE
   481                     NONE
   555 (*       the other hand, this would cause additional work for terms where *)
   532 (*       the other hand, this would cause additional work for terms where *)
   556 (*       constants are duplicated by beta-reduction (e.g. 'S c1 c2 c3').  *)
   533 (*       constants are duplicated by beta-reduction (e.g. 'S c1 c2 c3').  *)
   557 
   534 
   558 fun unfold_defs thy t =
   535 fun unfold_defs thy t =
   559   let
   536   let
   560     (* Term.term -> Term.term *)
       
   561     fun unfold_loop t =
   537     fun unfold_loop t =
   562       case t of
   538       case t of
   563       (* Pure *)
   539       (* Pure *)
   564         Const (@{const_name all}, _) => t
   540         Const (@{const_name all}, _) => t
   565       | Const (@{const_name "=="}, _) => t
   541       | Const (@{const_name "=="}, _) => t
   905 (*                look up the size of a type in 'sizes'.  Parameterized      *)
   881 (*                look up the size of a type in 'sizes'.  Parameterized      *)
   906 (*                types with different parameters (e.g. "'a list" vs. "bool  *)
   882 (*                types with different parameters (e.g. "'a list" vs. "bool  *)
   907 (*                list") are identified.                                     *)
   883 (*                list") are identified.                                     *)
   908 (* ------------------------------------------------------------------------- *)
   884 (* ------------------------------------------------------------------------- *)
   909 
   885 
   910 (* Term.typ -> string *)
       
   911 
       
   912 fun string_of_typ (Type (s, _))     = s
   886 fun string_of_typ (Type (s, _))     = s
   913   | string_of_typ (TFree (s, _))    = s
   887   | string_of_typ (TFree (s, _))    = s
   914   | string_of_typ (TVar ((s,_), _)) = s;
   888   | string_of_typ (TVar ((s,_), _)) = s;
   915 
   889 
   916 (* ------------------------------------------------------------------------- *)
   890 (* ------------------------------------------------------------------------- *)
   917 (* first_universe: returns the "first" (i.e. smallest) universe by assigning *)
   891 (* first_universe: returns the "first" (i.e. smallest) universe by assigning *)
   918 (*                 'minsize' to every type for which no size is specified in *)
   892 (*                 'minsize' to every type for which no size is specified in *)
   919 (*                 'sizes'                                                   *)
   893 (*                 'sizes'                                                   *)
   920 (* ------------------------------------------------------------------------- *)
   894 (* ------------------------------------------------------------------------- *)
   921 
       
   922 (* Term.typ list -> (string * int) list -> int -> (Term.typ * int) list *)
       
   923 
   895 
   924 fun first_universe xs sizes minsize =
   896 fun first_universe xs sizes minsize =
   925   let
   897   let
   926     fun size_of_typ T =
   898     fun size_of_typ T =
   927       case AList.lookup (op =) sizes (string_of_typ T) of
   899       case AList.lookup (op =) sizes (string_of_typ T) of
   936 (*                types), where the minimal size of a type is given by       *)
   908 (*                types), where the minimal size of a type is given by       *)
   937 (*                'minsize', the maximal size is given by 'maxsize', and a   *)
   909 (*                'minsize', the maximal size is given by 'maxsize', and a   *)
   938 (*                type may have a fixed size given in 'sizes'                *)
   910 (*                type may have a fixed size given in 'sizes'                *)
   939 (* ------------------------------------------------------------------------- *)
   911 (* ------------------------------------------------------------------------- *)
   940 
   912 
   941 (* (Term.typ * int) list -> (string * int) list -> int -> int ->
       
   942   (Term.typ * int) list option *)
       
   943 
       
   944 fun next_universe xs sizes minsize maxsize =
   913 fun next_universe xs sizes minsize maxsize =
   945   let
   914   let
   946     (* creates the "first" list of length 'len', where the sum of all list *)
   915     (* creates the "first" list of length 'len', where the sum of all list *)
   947     (* elements is 'sum', and the length of the list is 'len'              *)
   916     (* elements is 'sum', and the length of the list is 'len'              *)
   948     (* int -> int -> int -> int list option *)
       
   949     fun make_first _ 0 sum =
   917     fun make_first _ 0 sum =
   950           if sum = 0 then
   918           if sum = 0 then
   951             SOME []
   919             SOME []
   952           else
   920           else
   953             NONE
   921             NONE
   956             Option.map (fn xs' => sum :: xs') (make_first max (len-1) 0)
   924             Option.map (fn xs' => sum :: xs') (make_first max (len-1) 0)
   957           else
   925           else
   958             Option.map (fn xs' => max :: xs') (make_first max (len-1) (sum-max))
   926             Option.map (fn xs' => max :: xs') (make_first max (len-1) (sum-max))
   959     (* enumerates all int lists with a fixed length, where 0<=x<='max' for *)
   927     (* enumerates all int lists with a fixed length, where 0<=x<='max' for *)
   960     (* all list elements x (unless 'max'<0)                                *)
   928     (* all list elements x (unless 'max'<0)                                *)
   961     (* int -> int -> int -> int list -> int list option *)
       
   962     fun next _ _ _ [] =
   929     fun next _ _ _ [] =
   963           NONE
   930           NONE
   964       | next max len sum [x] =
   931       | next max len sum [x] =
   965           (* we've reached the last list element, so there's no shift possible *)
   932           (* we've reached the last list element, so there's no shift possible *)
   966           make_first max (len+1) (sum+x+1)  (* increment 'sum' by 1 *)
   933           make_first max (len+1) (sum+x+1)  (* increment 'sum' by 1 *)
   992 (* ------------------------------------------------------------------------- *)
   959 (* ------------------------------------------------------------------------- *)
   993 (* toTrue: converts the interpretation of a Boolean value to a propositional *)
   960 (* toTrue: converts the interpretation of a Boolean value to a propositional *)
   994 (*         formula that is true iff the interpretation denotes "true"        *)
   961 (*         formula that is true iff the interpretation denotes "true"        *)
   995 (* ------------------------------------------------------------------------- *)
   962 (* ------------------------------------------------------------------------- *)
   996 
   963 
   997 (* interpretation -> prop_formula *)
       
   998 
       
   999 fun toTrue (Leaf [fm, _]) = fm
   964 fun toTrue (Leaf [fm, _]) = fm
  1000   | toTrue _ = raise REFUTE ("toTrue", "interpretation does not denote a Boolean value");
   965   | toTrue _ = raise REFUTE ("toTrue", "interpretation does not denote a Boolean value");
  1001 
   966 
  1002 (* ------------------------------------------------------------------------- *)
   967 (* ------------------------------------------------------------------------- *)
  1003 (* toFalse: converts the interpretation of a Boolean value to a              *)
   968 (* toFalse: converts the interpretation of a Boolean value to a              *)
  1004 (*          propositional formula that is true iff the interpretation        *)
   969 (*          propositional formula that is true iff the interpretation        *)
  1005 (*          denotes "false"                                                  *)
   970 (*          denotes "false"                                                  *)
  1006 (* ------------------------------------------------------------------------- *)
   971 (* ------------------------------------------------------------------------- *)
  1007 
       
  1008 (* interpretation -> prop_formula *)
       
  1009 
   972 
  1010 fun toFalse (Leaf [_, fm]) = fm
   973 fun toFalse (Leaf [_, fm]) = fm
  1011   | toFalse _ = raise REFUTE ("toFalse", "interpretation does not denote a Boolean value");
   974   | toFalse _ = raise REFUTE ("toFalse", "interpretation does not denote a Boolean value");
  1012 
   975 
  1013 (* ------------------------------------------------------------------------- *)
   976 (* ------------------------------------------------------------------------- *)
  1023 fun find_model ctxt
   986 fun find_model ctxt
  1024     {sizes, minsize, maxsize, maxvars, maxtime, satsolver, no_assms, expect}
   987     {sizes, minsize, maxsize, maxvars, maxtime, satsolver, no_assms, expect}
  1025     assm_ts t negate =
   988     assm_ts t negate =
  1026   let
   989   let
  1027     val thy = Proof_Context.theory_of ctxt
   990     val thy = Proof_Context.theory_of ctxt
  1028     (* string -> string *)
       
  1029     fun check_expect outcome_code =
   991     fun check_expect outcome_code =
  1030       if expect = "" orelse outcome_code = expect then outcome_code
   992       if expect = "" orelse outcome_code = expect then outcome_code
  1031       else error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
   993       else error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
  1032     (* unit -> string *)
       
  1033     fun wrapper () =
   994     fun wrapper () =
  1034       let
   995       let
  1035         val timer = Timer.startRealTimer ()
   996         val timer = Timer.startRealTimer ()
  1036         val t =
   997         val t =
  1037           if no_assms then t
   998           if no_assms then t
  1038           else if negate then Logic.list_implies (assm_ts, t)
   999           else if negate then Logic.list_implies (assm_ts, t)
  1039           else Logic.mk_conjunction_list (t :: assm_ts)
  1000           else Logic.mk_conjunction_list (t :: assm_ts)
  1040         val u = unfold_defs thy t
  1001         val u = unfold_defs thy t
  1041         val _ = tracing ("Unfolded term: " ^ Syntax.string_of_term ctxt u)
  1002         val _ = tracing ("Unfolded term: " ^ Syntax.string_of_term ctxt u)
  1042         val axioms = collect_axioms ctxt u
  1003         val axioms = collect_axioms ctxt u
  1043         (* Term.typ list *)
       
  1044         val types = fold (union (op =) o ground_types ctxt) (u :: axioms) []
  1004         val types = fold (union (op =) o ground_types ctxt) (u :: axioms) []
  1045         val _ = tracing ("Ground types: "
  1005         val _ = tracing ("Ground types: "
  1046           ^ (if null types then "none."
  1006           ^ (if null types then "none."
  1047              else commas (map (Syntax.string_of_typ ctxt) types)))
  1007              else commas (map (Syntax.string_of_typ ctxt) types)))
  1048         (* we can only consider fragments of recursive IDTs, so we issue a  *)
  1008         (* we can only consider fragments of recursive IDTs, so we issue a  *)
  1067           if maybe_spurious then
  1027           if maybe_spurious then
  1068             warning ("Term contains a recursive datatype; "
  1028             warning ("Term contains a recursive datatype; "
  1069               ^ "countermodel(s) may be spurious!")
  1029               ^ "countermodel(s) may be spurious!")
  1070           else
  1030           else
  1071             ()
  1031             ()
  1072         (* (Term.typ * int) list -> string *)
       
  1073         fun find_model_loop universe =
  1032         fun find_model_loop universe =
  1074           let
  1033           let
  1075             val msecs_spent = Time.toMilliseconds (Timer.checkRealTimer timer)
  1034             val msecs_spent = Time.toMilliseconds (Timer.checkRealTimer timer)
  1076             val _ = maxtime = 0 orelse msecs_spent < 1000 * maxtime
  1035             val _ = maxtime = 0 orelse msecs_spent < 1000 * maxtime
  1077                     orelse raise TimeLimit.TimeOut
  1036                     orelse raise TimeLimit.TimeOut
  1269 (* ------------------------------------------------------------------------- *)
  1228 (* ------------------------------------------------------------------------- *)
  1270 
  1229 
  1271 fun make_constants ctxt model T =
  1230 fun make_constants ctxt model T =
  1272   let
  1231   let
  1273     (* returns a list with all unit vectors of length n *)
  1232     (* returns a list with all unit vectors of length n *)
  1274     (* int -> interpretation list *)
       
  1275     fun unit_vectors n =
  1233     fun unit_vectors n =
  1276       let
  1234       let
  1277         (* returns the k-th unit vector of length n *)
  1235         (* returns the k-th unit vector of length n *)
  1278         (* int * int -> interpretation *)
       
  1279         fun unit_vector (k, n) =
  1236         fun unit_vector (k, n) =
  1280           Leaf ((replicate (k-1) False) @ (True :: (replicate (n-k) False)))
  1237           Leaf ((replicate (k-1) False) @ (True :: (replicate (n-k) False)))
  1281         (* int -> interpretation list *)
       
  1282         fun unit_vectors_loop k =
  1238         fun unit_vectors_loop k =
  1283           if k>n then [] else unit_vector (k,n) :: unit_vectors_loop (k+1)
  1239           if k>n then [] else unit_vector (k,n) :: unit_vectors_loop (k+1)
  1284       in
  1240       in
  1285         unit_vectors_loop 1
  1241         unit_vectors_loop 1
  1286       end
  1242       end
  1287     (* returns a list of lists, each one consisting of n (possibly *)
  1243     (* returns a list of lists, each one consisting of n (possibly *)
  1288     (* identical) elements from 'xs'                               *)
  1244     (* identical) elements from 'xs'                               *)
  1289     (* int -> 'a list -> 'a list list *)
       
  1290     fun pick_all 1 xs = map single xs
  1245     fun pick_all 1 xs = map single xs
  1291       | pick_all n xs =
  1246       | pick_all n xs =
  1292           let val rec_pick = pick_all (n - 1) xs in
  1247           let val rec_pick = pick_all (n - 1) xs in
  1293             maps (fn x => map (cons x) rec_pick) xs
  1248             maps (fn x => map (cons x) rec_pick) xs
  1294           end
  1249           end
  1295     (* returns all constant interpretations that have the same tree *)
  1250     (* returns all constant interpretations that have the same tree *)
  1296     (* structure as the interpretation argument                     *)
  1251     (* structure as the interpretation argument                     *)
  1297     (* interpretation -> interpretation list *)
       
  1298     fun make_constants_intr (Leaf xs) = unit_vectors (length xs)
  1252     fun make_constants_intr (Leaf xs) = unit_vectors (length xs)
  1299       | make_constants_intr (Node xs) = map Node (pick_all (length xs)
  1253       | make_constants_intr (Node xs) = map Node (pick_all (length xs)
  1300           (make_constants_intr (hd xs)))
  1254           (make_constants_intr (hd xs)))
  1301     (* obtain the interpretation for a variable of type 'T' *)
  1255     (* obtain the interpretation for a variable of type 'T' *)
  1302     val (i, _, _) = interpret ctxt model {maxvars=0, def_eq=false, next_idx=1,
  1256     val (i, _, _) = interpret ctxt model {maxvars=0, def_eq=false, next_idx=1,
  1333 
  1287 
  1334 (* ------------------------------------------------------------------------- *)
  1288 (* ------------------------------------------------------------------------- *)
  1335 (* TT/FF: interpretations that denote "true" or "false", respectively        *)
  1289 (* TT/FF: interpretations that denote "true" or "false", respectively        *)
  1336 (* ------------------------------------------------------------------------- *)
  1290 (* ------------------------------------------------------------------------- *)
  1337 
  1291 
  1338 (* interpretation *)
       
  1339 
       
  1340 val TT = Leaf [True, False];
  1292 val TT = Leaf [True, False];
  1341 
  1293 
  1342 val FF = Leaf [False, True];
  1294 val FF = Leaf [False, True];
  1343 
  1295 
  1344 (* ------------------------------------------------------------------------- *)
  1296 (* ------------------------------------------------------------------------- *)
  1357 (* in the size of T.  Therefore comparing the interpretations 'i1' and    *)
  1309 (* in the size of T.  Therefore comparing the interpretations 'i1' and    *)
  1358 (* 'i2' directly is more efficient than constructing the interpretation   *)
  1310 (* 'i2' directly is more efficient than constructing the interpretation   *)
  1359 (* for equality on T first, and "applying" this interpretation to 'i1'    *)
  1311 (* for equality on T first, and "applying" this interpretation to 'i1'    *)
  1360 (* and 'i2' in the usual way (cf. 'interpretation_apply') then.           *)
  1312 (* and 'i2' in the usual way (cf. 'interpretation_apply') then.           *)
  1361 
  1313 
  1362 (* interpretation * interpretation -> interpretation *)
       
  1363 
       
  1364 fun make_equality (i1, i2) =
  1314 fun make_equality (i1, i2) =
  1365   let
  1315   let
  1366     (* interpretation * interpretation -> prop_formula *)
       
  1367     fun equal (i1, i2) =
  1316     fun equal (i1, i2) =
  1368       (case i1 of
  1317       (case i1 of
  1369         Leaf xs =>
  1318         Leaf xs =>
  1370           (case i2 of
  1319           (case i2 of
  1371             Leaf ys => Prop_Logic.dot_product (xs, ys)  (* defined and equal *)
  1320             Leaf ys => Prop_Logic.dot_product (xs, ys)  (* defined and equal *)
  1374       | Node xs =>
  1323       | Node xs =>
  1375           (case i2 of
  1324           (case i2 of
  1376             Leaf _  => raise REFUTE ("make_equality",
  1325             Leaf _  => raise REFUTE ("make_equality",
  1377             "first interpretation is higher")
  1326             "first interpretation is higher")
  1378           | Node ys => Prop_Logic.all (map equal (xs ~~ ys))))
  1327           | Node ys => Prop_Logic.all (map equal (xs ~~ ys))))
  1379     (* interpretation * interpretation -> prop_formula *)
       
  1380     fun not_equal (i1, i2) =
  1328     fun not_equal (i1, i2) =
  1381       (case i1 of
  1329       (case i1 of
  1382         Leaf xs =>
  1330         Leaf xs =>
  1383           (case i2 of
  1331           (case i2 of
  1384             (* defined and not equal *)
  1332             (* defined and not equal *)
  1405 (* different from 'make_equality': two undefined interpretations are         *)
  1353 (* different from 'make_equality': two undefined interpretations are         *)
  1406 (* considered equal, while a defined interpretation is considered not equal  *)
  1354 (* considered equal, while a defined interpretation is considered not equal  *)
  1407 (* to an undefined interpretation.                                           *)
  1355 (* to an undefined interpretation.                                           *)
  1408 (* ------------------------------------------------------------------------- *)
  1356 (* ------------------------------------------------------------------------- *)
  1409 
  1357 
  1410 (* interpretation * interpretation -> interpretation *)
       
  1411 
       
  1412 fun make_def_equality (i1, i2) =
  1358 fun make_def_equality (i1, i2) =
  1413   let
  1359   let
  1414     (* interpretation * interpretation -> prop_formula *)
       
  1415     fun equal (i1, i2) =
  1360     fun equal (i1, i2) =
  1416       (case i1 of
  1361       (case i1 of
  1417         Leaf xs =>
  1362         Leaf xs =>
  1418           (case i2 of
  1363           (case i2 of
  1419             (* defined and equal, or both undefined *)
  1364             (* defined and equal, or both undefined *)
  1424       | Node xs =>
  1369       | Node xs =>
  1425           (case i2 of
  1370           (case i2 of
  1426             Leaf _  => raise REFUTE ("make_def_equality",
  1371             Leaf _  => raise REFUTE ("make_def_equality",
  1427             "first interpretation is higher")
  1372             "first interpretation is higher")
  1428           | Node ys => Prop_Logic.all (map equal (xs ~~ ys))))
  1373           | Node ys => Prop_Logic.all (map equal (xs ~~ ys))))
  1429     (* interpretation *)
       
  1430     val eq = equal (i1, i2)
  1374     val eq = equal (i1, i2)
  1431   in
  1375   in
  1432     Leaf [eq, SNot eq]
  1376     Leaf [eq, SNot eq]
  1433   end;
  1377   end;
  1434 
  1378 
  1436 (* interpretation_apply: returns an interpretation that denotes the result   *)
  1380 (* interpretation_apply: returns an interpretation that denotes the result   *)
  1437 (*                       of applying the function denoted by 'i1' to the     *)
  1381 (*                       of applying the function denoted by 'i1' to the     *)
  1438 (*                       argument denoted by 'i2'                            *)
  1382 (*                       argument denoted by 'i2'                            *)
  1439 (* ------------------------------------------------------------------------- *)
  1383 (* ------------------------------------------------------------------------- *)
  1440 
  1384 
  1441 (* interpretation * interpretation -> interpretation *)
       
  1442 
       
  1443 fun interpretation_apply (i1, i2) =
  1385 fun interpretation_apply (i1, i2) =
  1444   let
  1386   let
  1445     (* interpretation * interpretation -> interpretation *)
       
  1446     fun interpretation_disjunction (tr1,tr2) =
  1387     fun interpretation_disjunction (tr1,tr2) =
  1447       tree_map (fn (xs,ys) => map (fn (x,y) => SOr(x,y)) (xs ~~ ys))
  1388       tree_map (fn (xs,ys) => map (fn (x,y) => SOr(x,y)) (xs ~~ ys))
  1448         (tree_pair (tr1,tr2))
  1389         (tree_pair (tr1,tr2))
  1449     (* prop_formula * interpretation -> interpretation *)
       
  1450     fun prop_formula_times_interpretation (fm,tr) =
  1390     fun prop_formula_times_interpretation (fm,tr) =
  1451       tree_map (map (fn x => SAnd (fm,x))) tr
  1391       tree_map (map (fn x => SAnd (fm,x))) tr
  1452     (* prop_formula list * interpretation list -> interpretation *)
       
  1453     fun prop_formula_list_dot_product_interpretation_list ([fm],[tr]) =
  1392     fun prop_formula_list_dot_product_interpretation_list ([fm],[tr]) =
  1454           prop_formula_times_interpretation (fm,tr)
  1393           prop_formula_times_interpretation (fm,tr)
  1455       | prop_formula_list_dot_product_interpretation_list (fm::fms,tr::trees) =
  1394       | prop_formula_list_dot_product_interpretation_list (fm::fms,tr::trees) =
  1456           interpretation_disjunction (prop_formula_times_interpretation (fm,tr),
  1395           interpretation_disjunction (prop_formula_times_interpretation (fm,tr),
  1457             prop_formula_list_dot_product_interpretation_list (fms,trees))
  1396             prop_formula_list_dot_product_interpretation_list (fms,trees))
  1458       | prop_formula_list_dot_product_interpretation_list (_,_) =
  1397       | prop_formula_list_dot_product_interpretation_list (_,_) =
  1459           raise REFUTE ("interpretation_apply", "empty list (in dot product)")
  1398           raise REFUTE ("interpretation_apply", "empty list (in dot product)")
  1460     (* returns a list of lists, each one consisting of one element from each *)
  1399     (* returns a list of lists, each one consisting of one element from each *)
  1461     (* element of 'xss'                                                      *)
  1400     (* element of 'xss'                                                      *)
  1462     (* 'a list list -> 'a list list *)
       
  1463     fun pick_all [xs] = map single xs
  1401     fun pick_all [xs] = map single xs
  1464       | pick_all (xs::xss) =
  1402       | pick_all (xs::xss) =
  1465           let val rec_pick = pick_all xss in
  1403           let val rec_pick = pick_all xss in
  1466             maps (fn x => map (cons x) rec_pick) xs
  1404             maps (fn x => map (cons x) rec_pick) xs
  1467           end
  1405           end
  1468       | pick_all _ = raise REFUTE ("interpretation_apply", "empty list (in pick_all)")
  1406       | pick_all _ = raise REFUTE ("interpretation_apply", "empty list (in pick_all)")
  1469     (* interpretation -> prop_formula list *)
       
  1470     fun interpretation_to_prop_formula_list (Leaf xs) = xs
  1407     fun interpretation_to_prop_formula_list (Leaf xs) = xs
  1471       | interpretation_to_prop_formula_list (Node trees) =
  1408       | interpretation_to_prop_formula_list (Node trees) =
  1472           map Prop_Logic.all (pick_all
  1409           map Prop_Logic.all (pick_all
  1473             (map interpretation_to_prop_formula_list trees))
  1410             (map interpretation_to_prop_formula_list trees))
  1474   in
  1411   in
  1482 
  1419 
  1483 (* ------------------------------------------------------------------------- *)
  1420 (* ------------------------------------------------------------------------- *)
  1484 (* eta_expand: eta-expands a term 't' by adding 'i' lambda abstractions      *)
  1421 (* eta_expand: eta-expands a term 't' by adding 'i' lambda abstractions      *)
  1485 (* ------------------------------------------------------------------------- *)
  1422 (* ------------------------------------------------------------------------- *)
  1486 
  1423 
  1487 (* Term.term -> int -> Term.term *)
       
  1488 
       
  1489 fun eta_expand t i =
  1424 fun eta_expand t i =
  1490   let
  1425   let
  1491     val Ts = Term.binder_types (Term.fastype_of t)
  1426     val Ts = Term.binder_types (Term.fastype_of t)
  1492     val t' = Term.incr_boundvars i t
  1427     val t' = Term.incr_boundvars i t
  1493   in
  1428   in
  1518 
  1453 
  1519 fun stlc_interpreter ctxt model args t =
  1454 fun stlc_interpreter ctxt model args t =
  1520   let
  1455   let
  1521     val (typs, terms) = model
  1456     val (typs, terms) = model
  1522     val {maxvars, def_eq, next_idx, bounds, wellformed} = args
  1457     val {maxvars, def_eq, next_idx, bounds, wellformed} = args
  1523     (* Term.typ -> (interpretation * model * arguments) option *)
       
  1524     fun interpret_groundterm T =
  1458     fun interpret_groundterm T =
  1525       let
  1459       let
  1526         (* unit -> (interpretation * model * arguments) option *)
       
  1527         fun interpret_groundtype () =
  1460         fun interpret_groundtype () =
  1528           let
  1461           let
  1529             (* the model must specify a size for ground types *)
  1462             (* the model must specify a size for ground types *)
  1530             val size =
  1463             val size =
  1531               if T = Term.propT then 2
  1464               if T = Term.propT then 2
  1532               else the (AList.lookup (op =) typs T)
  1465               else the (AList.lookup (op =) typs T)
  1533             val next = next_idx + size
  1466             val next = next_idx + size
  1534             (* check if 'maxvars' is large enough *)
  1467             (* check if 'maxvars' is large enough *)
  1535             val _ = (if next - 1 > maxvars andalso maxvars > 0 then
  1468             val _ = (if next - 1 > maxvars andalso maxvars > 0 then
  1536               raise MAXVARS_EXCEEDED else ())
  1469               raise MAXVARS_EXCEEDED else ())
  1537             (* prop_formula list *)
       
  1538             val fms  = map BoolVar (next_idx upto (next_idx + size - 1))
  1470             val fms  = map BoolVar (next_idx upto (next_idx + size - 1))
  1539             (* interpretation *)
       
  1540             val intr = Leaf fms
  1471             val intr = Leaf fms
  1541             (* prop_formula list -> prop_formula *)
       
  1542             fun one_of_two_false [] = True
  1472             fun one_of_two_false [] = True
  1543               | one_of_two_false (x::xs) = SAnd (Prop_Logic.all (map (fn x' =>
  1473               | one_of_two_false (x::xs) = SAnd (Prop_Logic.all (map (fn x' =>
  1544                   SOr (SNot x, SNot x')) xs), one_of_two_false xs)
  1474                   SOr (SNot x, SNot x')) xs), one_of_two_false xs)
  1545             (* prop_formula *)
       
  1546             val wf = one_of_two_false fms
  1475             val wf = one_of_two_false fms
  1547           in
  1476           in
  1548             (* extend the model, increase 'next_idx', add well-formedness *)
  1477             (* extend the model, increase 'next_idx', add well-formedness *)
  1549             (* condition                                                  *)
  1478             (* condition                                                  *)
  1550             SOME (intr, (typs, (t, intr)::terms), {maxvars = maxvars,
  1479             SOME (intr, (typs, (t, intr)::terms), {maxvars = maxvars,
  1559               (* interpretation for 'T2', which are then combined into a single *)
  1488               (* interpretation for 'T2', which are then combined into a single *)
  1560               (* new interpretation                                             *)
  1489               (* new interpretation                                             *)
  1561               (* make fresh copies, with different variable indices *)
  1490               (* make fresh copies, with different variable indices *)
  1562               (* 'idx': next variable index                         *)
  1491               (* 'idx': next variable index                         *)
  1563               (* 'n'  : number of copies                            *)
  1492               (* 'n'  : number of copies                            *)
  1564               (* int -> int -> (int * interpretation list * prop_formula *)
       
  1565               fun make_copies idx 0 = (idx, [], True)
  1493               fun make_copies idx 0 = (idx, [], True)
  1566                 | make_copies idx n =
  1494                 | make_copies idx n =
  1567                     let
  1495                     let
  1568                       val (copy, _, new_args) = interpret ctxt (typs, [])
  1496                       val (copy, _, new_args) = interpret ctxt (typs, [])
  1569                         {maxvars = maxvars, def_eq = false, next_idx = idx,
  1497                         {maxvars = maxvars, def_eq = false, next_idx = idx,
  1805 
  1733 
  1806 fun IDT_interpreter ctxt model args t =
  1734 fun IDT_interpreter ctxt model args t =
  1807   let
  1735   let
  1808     val thy = Proof_Context.theory_of ctxt
  1736     val thy = Proof_Context.theory_of ctxt
  1809     val (typs, terms) = model
  1737     val (typs, terms) = model
  1810     (* Term.typ -> (interpretation * model * arguments) option *)
       
  1811     fun interpret_term (Type (s, Ts)) =
  1738     fun interpret_term (Type (s, Ts)) =
  1812           (case Datatype.get_info thy s of
  1739           (case Datatype.get_info thy s of
  1813             SOME info =>  (* inductive datatype *)
  1740             SOME info =>  (* inductive datatype *)
  1814               let
  1741               let
  1815                 (* int option -- only recursive IDTs have an associated depth *)
  1742                 (* only recursive IDTs have an associated depth *)
  1816                 val depth = AList.lookup (op =) typs (Type (s, Ts))
  1743                 val depth = AList.lookup (op =) typs (Type (s, Ts))
  1817                 (* sanity check: depth must be at least 0 *)
  1744                 (* sanity check: depth must be at least 0 *)
  1818                 val _ =
  1745                 val _ =
  1819                   (case depth of SOME n =>
  1746                   (case depth of SOME n =>
  1820                     if n < 0 then
  1747                     if n < 0 then
  1851                     val next_idx = #next_idx args
  1778                     val next_idx = #next_idx args
  1852                     val next     = next_idx+size
  1779                     val next     = next_idx+size
  1853                     (* check if 'maxvars' is large enough *)
  1780                     (* check if 'maxvars' is large enough *)
  1854                     val _        = (if next-1 > #maxvars args andalso
  1781                     val _        = (if next-1 > #maxvars args andalso
  1855                       #maxvars args > 0 then raise MAXVARS_EXCEEDED else ())
  1782                       #maxvars args > 0 then raise MAXVARS_EXCEEDED else ())
  1856                     (* prop_formula list *)
       
  1857                     val fms      = map BoolVar (next_idx upto (next_idx+size-1))
  1783                     val fms      = map BoolVar (next_idx upto (next_idx+size-1))
  1858                     (* interpretation *)
       
  1859                     val intr     = Leaf fms
  1784                     val intr     = Leaf fms
  1860                     (* prop_formula list -> prop_formula *)
       
  1861                     fun one_of_two_false [] = True
  1785                     fun one_of_two_false [] = True
  1862                       | one_of_two_false (x::xs) = SAnd (Prop_Logic.all (map (fn x' =>
  1786                       | one_of_two_false (x::xs) = SAnd (Prop_Logic.all (map (fn x' =>
  1863                           SOr (SNot x, SNot x')) xs), one_of_two_false xs)
  1787                           SOr (SNot x, SNot x')) xs), one_of_two_false xs)
  1864                     (* prop_formula *)
       
  1865                     val wf = one_of_two_false fms
  1788                     val wf = one_of_two_false fms
  1866                   in
  1789                   in
  1867                     (* extend the model, increase 'next_idx', add well-formedness *)
  1790                     (* extend the model, increase 'next_idx', add well-formedness *)
  1868                     (* condition                                                  *)
  1791                     (* condition                                                  *)
  1869                     SOME (intr, (typs, (t, intr)::terms), {maxvars = #maxvars args,
  1792                     SOME (intr, (typs, (t, intr)::terms), {maxvars = #maxvars args,
  1901     val thy = Proof_Context.theory_of ctxt
  1824     val thy = Proof_Context.theory_of ctxt
  1902     (* returns a list of canonical representations for terms of the type 'T' *)
  1825     (* returns a list of canonical representations for terms of the type 'T' *)
  1903     (* It would be nice if we could just use 'print' for this, but 'print'   *)
  1826     (* It would be nice if we could just use 'print' for this, but 'print'   *)
  1904     (* for IDTs calls 'IDT_constructor_interpreter' again, and this could    *)
  1827     (* for IDTs calls 'IDT_constructor_interpreter' again, and this could    *)
  1905     (* lead to infinite recursion when we have (mutually) recursive IDTs.    *)
  1828     (* lead to infinite recursion when we have (mutually) recursive IDTs.    *)
  1906     (* (Term.typ * int) list -> Term.typ -> Term.term list *)
       
  1907     fun canonical_terms typs T =
  1829     fun canonical_terms typs T =
  1908           (case T of
  1830           (case T of
  1909             Type ("fun", [T1, T2]) =>
  1831             Type ("fun", [T1, T2]) =>
  1910             (* 'T2' might contain a recursive IDT, so we cannot use 'print' (at *)
  1832             (* 'T2' might contain a recursive IDT, so we cannot use 'print' (at *)
  1911             (* least not for 'T2'                                               *)
  1833             (* least not for 'T2'                                               *)
  1912             let
  1834             let
  1913               (* returns a list of lists, each one consisting of n (possibly *)
  1835               (* returns a list of lists, each one consisting of n (possibly *)
  1914               (* identical) elements from 'xs'                               *)
  1836               (* identical) elements from 'xs'                               *)
  1915               (* int -> 'a list -> 'a list list *)
       
  1916               fun pick_all 1 xs = map single xs
  1837               fun pick_all 1 xs = map single xs
  1917                 | pick_all n xs =
  1838                 | pick_all n xs =
  1918                     let val rec_pick = pick_all (n-1) xs in
  1839                     let val rec_pick = pick_all (n-1) xs in
  1919                       maps (fn x => map (cons x) rec_pick) xs
  1840                       maps (fn x => map (cons x) rec_pick) xs
  1920                     end
  1841                     end
  1927               val functions = map (curry (op ~~) terms1)
  1848               val functions = map (curry (op ~~) terms1)
  1928                 (pick_all (length terms1) terms2)
  1849                 (pick_all (length terms1) terms2)
  1929               (* [["(x1, y1)", ..., "(xn, y1)"], ..., *)
  1850               (* [["(x1, y1)", ..., "(xn, y1)"], ..., *)
  1930               (*   ["(x1, ym)", ..., "(xn, ym)"]]     *)
  1851               (*   ["(x1, ym)", ..., "(xn, ym)"]]     *)
  1931               val pairss = map (map HOLogic.mk_prod) functions
  1852               val pairss = map (map HOLogic.mk_prod) functions
  1932               (* Term.typ *)
       
  1933               val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  1853               val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  1934               val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  1854               val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  1935               (* Term.term *)
       
  1936               val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT)
  1855               val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT)
  1937               val HOLogic_insert    =
  1856               val HOLogic_insert    =
  1938                 Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  1857                 Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  1939             in
  1858             in
  1940               (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
  1859               (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
  2035                         [] =>
  1954                         [] =>
  2036                           (* 'Const (s, T)' is not a constructor of this datatype *)
  1955                           (* 'Const (s, T)' is not a constructor of this datatype *)
  2037                           NONE
  1956                           NONE
  2038                       | (_, ctypes)::_ =>
  1957                       | (_, ctypes)::_ =>
  2039                           let
  1958                           let
  2040                             (* int option -- only /recursive/ IDTs have an associated *)
  1959                             (* only /recursive/ IDTs have an associated depth *)
  2041                             (*               depth                                    *)
       
  2042                             val depth = AList.lookup (op =) typs (Type (s', Ts'))
  1960                             val depth = AList.lookup (op =) typs (Type (s', Ts'))
  2043                             (* this should never happen: at depth 0, this IDT fragment *)
  1961                             (* this should never happen: at depth 0, this IDT fragment *)
  2044                             (* is definitely empty, and in this case we don't need to  *)
  1962                             (* is definitely empty, and in this case we don't need to  *)
  2045                             (* interpret its constructors                              *)
  1963                             (* interpret its constructors                              *)
  2046                             val _ = (case depth of SOME 0 =>
  1964                             val _ = (case depth of SOME 0 =>
  2081                                   else
  1999                                   else
  2082                                     raise REFUTE ("IDT_constructor_interpreter",
  2000                                     raise REFUTE ("IDT_constructor_interpreter",
  2083                                       "offset >= total")
  2001                                       "offset >= total")
  2084                               | make_constr (d::ds) offset =
  2002                               | make_constr (d::ds) offset =
  2085                                   let
  2003                                   let
  2086                                     (* Term.typ *)
       
  2087                                     val dT = typ_of_dtyp descr typ_assoc d
  2004                                     val dT = typ_of_dtyp descr typ_assoc d
  2088                                     (* compute canonical term representations for all   *)
  2005                                     (* compute canonical term representations for all   *)
  2089                                     (* elements of the type 'd' (with the reduced depth *)
  2006                                     (* elements of the type 'd' (with the reduced depth *)
  2090                                     (* for the IDT)                                     *)
  2007                                     (* for the IDT)                                     *)
  2091                                     val terms' = canonical_terms typs' dT
  2008                                     val terms' = canonical_terms typs' dT
  2129                                           | search (_::_) [] =
  2046                                           | search (_::_) [] =
  2130                                               raise REFUTE ("IDT_constructor_interpreter",
  2047                                               raise REFUTE ("IDT_constructor_interpreter",
  2131                                                 "element order not preserved")
  2048                                                 "element order not preserved")
  2132                                           | search [] _ = ()
  2049                                           | search [] _ = ()
  2133                                       in search terms' terms end
  2050                                       in search terms' terms end
  2134                                     (* int * interpretation list *)
       
  2135                                     val (intrs, new_offset) =
  2051                                     val (intrs, new_offset) =
  2136                                       fold_map (fn t_elem => fn off =>
  2052                                       fold_map (fn t_elem => fn off =>
  2137                                         (* if 't_elem' existed at the previous depth,    *)
  2053                                         (* if 't_elem' existed at the previous depth,    *)
  2138                                         (* proceed recursively, otherwise map the entire *)
  2054                                         (* proceed recursively, otherwise map the entire *)
  2139                                         (* subtree to "undefined"                        *)
  2055                                         (* subtree to "undefined"                        *)
  2331                       (* Since the order on datatype elements is given by an     *)
  2247                       (* Since the order on datatype elements is given by an     *)
  2332                       (* order on constructors (and then by the order on         *)
  2248                       (* order on constructors (and then by the order on         *)
  2333                       (* argument tuples), we can simply copy corresponding      *)
  2249                       (* argument tuples), we can simply copy corresponding      *)
  2334                       (* subtrees from 'p_intrs', in the order in which they are *)
  2250                       (* subtrees from 'p_intrs', in the order in which they are *)
  2335                       (* given.                                                  *)
  2251                       (* given.                                                  *)
  2336                       (* interpretation * interpretation -> interpretation list *)
       
  2337                       fun ci_pi (Leaf xs, pi) =
  2252                       fun ci_pi (Leaf xs, pi) =
  2338                             (* if the constructor does not match the arguments to a *)
  2253                             (* if the constructor does not match the arguments to a *)
  2339                             (* defined element of the IDT, the corresponding value  *)
  2254                             (* defined element of the IDT, the corresponding value  *)
  2340                             (* of the parameter must be ignored                     *)
  2255                             (* of the parameter must be ignored                     *)
  2341                             if List.exists (equal True) xs then [pi] else []
  2256                             if List.exists (equal True) xs then [pi] else []
  2342                         | ci_pi (Node xs, Node ys) = maps ci_pi (xs ~~ ys)
  2257                         | ci_pi (Node xs, Node ys) = maps ci_pi (xs ~~ ys)
  2343                         | ci_pi (Node _, Leaf _) =
  2258                         | ci_pi (Node _, Leaf _) =
  2344                             raise REFUTE ("IDT_recursion_interpreter",
  2259                             raise REFUTE ("IDT_recursion_interpreter",
  2345                               "constructor takes more arguments than the " ^
  2260                               "constructor takes more arguments than the " ^
  2346                                 "associated parameter")
  2261                                 "associated parameter")
  2347                       (* (int * interpretation list) list *)
       
  2348                       val rec_operators = map (fn (idx, c_p_intrs) =>
  2262                       val rec_operators = map (fn (idx, c_p_intrs) =>
  2349                         (idx, maps ci_pi c_p_intrs)) mc_p_intrs
  2263                         (idx, maps ci_pi c_p_intrs)) mc_p_intrs
  2350                       (* sanity check: every recursion operator must provide as  *)
  2264                       (* sanity check: every recursion operator must provide as  *)
  2351                       (*               many values as the corresponding datatype *)
  2265                       (*               many values as the corresponding datatype *)
  2352                       (*               has elements                              *)
  2266                       (*               has elements                              *)
  2366                       (* (recursively obtained) interpretations for recursive    *)
  2280                       (* (recursively obtained) interpretations for recursive    *)
  2367                       (* constructor arguments.  To do so more efficiently, we   *)
  2281                       (* constructor arguments.  To do so more efficiently, we   *)
  2368                       (* copy 'rec_operators' into arrays first.  Each Boolean   *)
  2282                       (* copy 'rec_operators' into arrays first.  Each Boolean   *)
  2369                       (* indicates whether the recursive arguments have been     *)
  2283                       (* indicates whether the recursive arguments have been     *)
  2370                       (* considered already.                                     *)
  2284                       (* considered already.                                     *)
  2371                       (* (int * (bool * interpretation) Array.array) list *)
       
  2372                       val REC_OPERATORS = map (fn (idx, intrs) =>
  2285                       val REC_OPERATORS = map (fn (idx, intrs) =>
  2373                         (idx, Array.fromList (map (pair false) intrs)))
  2286                         (idx, Array.fromList (map (pair false) intrs)))
  2374                         rec_operators
  2287                         rec_operators
  2375                       (* takes an interpretation, and if some leaf of this     *)
  2288                       (* takes an interpretation, and if some leaf of this     *)
  2376                       (* interpretation is the 'elem'-th element of the type,  *)
  2289                       (* interpretation is the 'elem'-th element of the type,  *)
  2377                       (* the indices of the arguments leading to this leaf are *)
  2290                       (* the indices of the arguments leading to this leaf are *)
  2378                       (* returned                                              *)
  2291                       (* returned                                              *)
  2379                       (* interpretation -> int -> int list option *)
       
  2380                       fun get_args (Leaf xs) elem =
  2292                       fun get_args (Leaf xs) elem =
  2381                             if find_index (fn x => x = True) xs = elem then
  2293                             if find_index (fn x => x = True) xs = elem then
  2382                               SOME []
  2294                               SOME []
  2383                             else
  2295                             else
  2384                               NONE
  2296                               NONE
  2385                         | get_args (Node xs) elem =
  2297                         | get_args (Node xs) elem =
  2386                             let
  2298                             let
  2387                               (* interpretation list * int -> int list option *)
       
  2388                               fun search ([], _) =
  2299                               fun search ([], _) =
  2389                                 NONE
  2300                                 NONE
  2390                                 | search (x::xs, n) =
  2301                                 | search (x::xs, n) =
  2391                                 (case get_args x elem of
  2302                                 (case get_args x elem of
  2392                                   SOME result => SOME (n::result)
  2303                                   SOME result => SOME (n::result)
  2395                               search (xs, 0)
  2306                               search (xs, 0)
  2396                             end
  2307                             end
  2397                       (* returns the index of the constructor and indices for *)
  2308                       (* returns the index of the constructor and indices for *)
  2398                       (* its arguments that generate the 'elem'-th element of *)
  2309                       (* its arguments that generate the 'elem'-th element of *)
  2399                       (* the datatype given by 'idx'                          *)
  2310                       (* the datatype given by 'idx'                          *)
  2400                       (* int -> int -> int * int list *)
       
  2401                       fun get_cargs idx elem =
  2311                       fun get_cargs idx elem =
  2402                         let
  2312                         let
  2403                           (* int * interpretation list -> int * int list *)
       
  2404                           fun get_cargs_rec (_, []) =
  2313                           fun get_cargs_rec (_, []) =
  2405                                 raise REFUTE ("IDT_recursion_interpreter",
  2314                                 raise REFUTE ("IDT_recursion_interpreter",
  2406                                   "no matching constructor found for datatype element")
  2315                                   "no matching constructor found for datatype element")
  2407                             | get_cargs_rec (n, x::xs) =
  2316                             | get_cargs_rec (n, x::xs) =
  2408                                 (case get_args x elem of
  2317                                 (case get_args x elem of
  2412                           get_cargs_rec (0, the (AList.lookup (op =) mc_intrs idx))
  2321                           get_cargs_rec (0, the (AList.lookup (op =) mc_intrs idx))
  2413                         end
  2322                         end
  2414                       (* computes one entry in 'REC_OPERATORS', and recursively *)
  2323                       (* computes one entry in 'REC_OPERATORS', and recursively *)
  2415                       (* all entries needed for it, where 'idx' gives the       *)
  2324                       (* all entries needed for it, where 'idx' gives the       *)
  2416                       (* datatype and 'elem' the element of it                  *)
  2325                       (* datatype and 'elem' the element of it                  *)
  2417                       (* int -> int -> interpretation *)
       
  2418                       fun compute_array_entry idx elem =
  2326                       fun compute_array_entry idx elem =
  2419                         let
  2327                         let
  2420                           val arr = the (AList.lookup (op =) REC_OPERATORS idx)
  2328                           val arr = the (AList.lookup (op =) REC_OPERATORS idx)
  2421                           val (flag, intr) = Array.sub (arr, elem)
  2329                           val (flag, intr) = Array.sub (arr, elem)
  2422                         in
  2330                         in
  2425                             intr
  2333                             intr
  2426                           else
  2334                           else
  2427                             (* we have to apply 'intr' to interpretations for all *)
  2335                             (* we have to apply 'intr' to interpretations for all *)
  2428                             (* recursive arguments                                *)
  2336                             (* recursive arguments                                *)
  2429                             let
  2337                             let
  2430                               (* int * int list *)
       
  2431                               val (c, args) = get_cargs idx elem
  2338                               val (c, args) = get_cargs idx elem
  2432                               (* find the indices of the constructor's /recursive/ *)
  2339                               (* find the indices of the constructor's /recursive/ *)
  2433                               (* arguments                                         *)
  2340                               (* arguments                                         *)
  2434                               val (_, _, constrs) = the (AList.lookup (op =) descr idx)
  2341                               val (_, _, constrs) = the (AList.lookup (op =) descr idx)
  2435                               val (_, dtyps) = nth constrs c
  2342                               val (_, dtyps) = nth constrs c
  2483                       val _ =
  2390                       val _ =
  2484                           if idt_size <> size_of_type ctxt (typs, []) IDT then
  2391                           if idt_size <> size_of_type ctxt (typs, []) IDT then
  2485                             raise REFUTE ("IDT_recursion_interpreter",
  2392                             raise REFUTE ("IDT_recursion_interpreter",
  2486                               "unexpected size of IDT (wrong type associated?)")
  2393                               "unexpected size of IDT (wrong type associated?)")
  2487                           else ()
  2394                           else ()
  2488                       (* interpretation *)
       
  2489                       val rec_op = Node (map_range (compute_array_entry idt_index) idt_size)
  2395                       val rec_op = Node (map_range (compute_array_entry idt_index) idt_size)
  2490                     in
  2396                     in
  2491                       SOME (rec_op, model', args')
  2397                       SOME (rec_op, model', args')
  2492                     end
  2398                     end
  2493                 end
  2399                 end
  2551 fun Finite_Set_card_interpreter ctxt model args t =
  2457 fun Finite_Set_card_interpreter ctxt model args t =
  2552   case t of
  2458   case t of
  2553     Const (@{const_name Finite_Set.card},
  2459     Const (@{const_name Finite_Set.card},
  2554         Type ("fun", [Type (@{type_name set}, [T]), @{typ nat}])) =>
  2460         Type ("fun", [Type (@{type_name set}, [T]), @{typ nat}])) =>
  2555       let
  2461       let
  2556         (* interpretation -> int *)
       
  2557         fun number_of_elements (Node xs) =
  2462         fun number_of_elements (Node xs) =
  2558             fold (fn x => fn n =>
  2463             fold (fn x => fn n =>
  2559               if x = TT then
  2464               if x = TT then
  2560                 n + 1
  2465                 n + 1
  2561               else if x = FF then
  2466               else if x = FF then
  2568               raise REFUTE ("Finite_Set_card_interpreter",
  2473               raise REFUTE ("Finite_Set_card_interpreter",
  2569                 "interpretation for set type is a leaf")
  2474                 "interpretation for set type is a leaf")
  2570         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2475         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2571         (* takes an interpretation for a set and returns an interpretation *)
  2476         (* takes an interpretation for a set and returns an interpretation *)
  2572         (* for a 'nat' denoting the set's cardinality                      *)
  2477         (* for a 'nat' denoting the set's cardinality                      *)
  2573         (* interpretation -> interpretation *)
       
  2574         fun card i =
  2478         fun card i =
  2575           let
  2479           let
  2576             val n = number_of_elements i
  2480             val n = number_of_elements i
  2577           in
  2481           in
  2578             if n < size_of_nat then
  2482             if n < size_of_nat then
  2619         Type ("fun", [@{typ nat}, @{typ bool}])])) =>
  2523         Type ("fun", [@{typ nat}, @{typ bool}])])) =>
  2620       let
  2524       let
  2621         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2525         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2622         (* the 'n'-th nat is not less than the first 'n' nats, while it *)
  2526         (* the 'n'-th nat is not less than the first 'n' nats, while it *)
  2623         (* is less than the remaining 'size_of_nat - n' nats            *)
  2527         (* is less than the remaining 'size_of_nat - n' nats            *)
  2624         (* int -> interpretation *)
       
  2625         fun less n = Node ((replicate n FF) @ (replicate (size_of_nat - n) TT))
  2528         fun less n = Node ((replicate n FF) @ (replicate (size_of_nat - n) TT))
  2626       in
  2529       in
  2627         SOME (Node (map less (1 upto size_of_nat)), model, args)
  2530         SOME (Node (map less (1 upto size_of_nat)), model, args)
  2628       end
  2531       end
  2629   | _ => NONE;
  2532   | _ => NONE;
  2636   case t of
  2539   case t of
  2637     Const (@{const_name Groups.plus}, Type ("fun", [@{typ nat},
  2540     Const (@{const_name Groups.plus}, Type ("fun", [@{typ nat},
  2638         Type ("fun", [@{typ nat}, @{typ nat}])])) =>
  2541         Type ("fun", [@{typ nat}, @{typ nat}])])) =>
  2639       let
  2542       let
  2640         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2543         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2641         (* int -> int -> interpretation *)
       
  2642         fun plus m n =
  2544         fun plus m n =
  2643           let
  2545           let
  2644             val element = m + n
  2546             val element = m + n
  2645           in
  2547           in
  2646             if element > size_of_nat - 1 then
  2548             if element > size_of_nat - 1 then
  2663   case t of
  2565   case t of
  2664     Const (@{const_name Groups.minus}, Type ("fun", [@{typ nat},
  2566     Const (@{const_name Groups.minus}, Type ("fun", [@{typ nat},
  2665         Type ("fun", [@{typ nat}, @{typ nat}])])) =>
  2567         Type ("fun", [@{typ nat}, @{typ nat}])])) =>
  2666       let
  2568       let
  2667         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2569         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2668         (* int -> int -> interpretation *)
       
  2669         fun minus m n =
  2570         fun minus m n =
  2670           let
  2571           let
  2671             val element = Int.max (m-n, 0)
  2572             val element = Int.max (m-n, 0)
  2672           in
  2573           in
  2673             Leaf ((replicate element False) @ True ::
  2574             Leaf ((replicate element False) @ True ::
  2687   case t of
  2588   case t of
  2688     Const (@{const_name Groups.times}, Type ("fun", [@{typ nat},
  2589     Const (@{const_name Groups.times}, Type ("fun", [@{typ nat},
  2689         Type ("fun", [@{typ nat}, @{typ nat}])])) =>
  2590         Type ("fun", [@{typ nat}, @{typ nat}])])) =>
  2690       let
  2591       let
  2691         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2592         val size_of_nat = size_of_type ctxt model (@{typ nat})
  2692         (* nat -> nat -> interpretation *)
       
  2693         fun mult m n =
  2593         fun mult m n =
  2694           let
  2594           let
  2695             val element = m * n
  2595             val element = m * n
  2696           in
  2596           in
  2697             if element > size_of_nat - 1 then
  2597             if element > size_of_nat - 1 then
  2718         val size_elem = size_of_type ctxt model T
  2618         val size_elem = size_of_type ctxt model T
  2719         val size_list = size_of_type ctxt model (Type ("List.list", [T]))
  2619         val size_list = size_of_type ctxt model (Type ("List.list", [T]))
  2720         (* maximal length of lists; 0 if we only consider the empty list *)
  2620         (* maximal length of lists; 0 if we only consider the empty list *)
  2721         val list_length =
  2621         val list_length =
  2722           let
  2622           let
  2723             (* int -> int -> int -> int *)
       
  2724             fun list_length_acc len lists total =
  2623             fun list_length_acc len lists total =
  2725               if lists = total then
  2624               if lists = total then
  2726                 len
  2625                 len
  2727               else if lists < total then
  2626               else if lists < total then
  2728                 list_length_acc (len+1) (lists*size_elem) (total-lists)
  2627                 list_length_acc (len+1) (lists*size_elem) (total-lists)
  2773           end) elements ([], 0)
  2672           end) elements ([], 0)
  2774         (* we also need the reverse association (from length/offset to *)
  2673         (* we also need the reverse association (from length/offset to *)
  2775         (* index)                                                      *)
  2674         (* index)                                                      *)
  2776         val lenoff'_lists = map Library.swap lenoff_lists
  2675         val lenoff'_lists = map Library.swap lenoff_lists
  2777         (* returns the interpretation for "(list no. m) @ (list no. n)" *)
  2676         (* returns the interpretation for "(list no. m) @ (list no. n)" *)
  2778         (* nat -> nat -> interpretation *)
       
  2779         fun append m n =
  2677         fun append m n =
  2780           let
  2678           let
  2781             val (len_m, off_m) = the (AList.lookup (op =) lenoff_lists m)
  2679             val (len_m, off_m) = the (AList.lookup (op =) lenoff_lists m)
  2782             val (len_n, off_n) = the (AList.lookup (op =) lenoff_lists n)
  2680             val (len_n, off_n) = the (AList.lookup (op =) lenoff_lists n)
  2783             val len_elem = len_m + len_n
  2681             val len_elem = len_m + len_n
  2815         val i_sets = make_constants ctxt model (HOLogic.mk_setT T)
  2713         val i_sets = make_constants ctxt model (HOLogic.mk_setT T)
  2816         (* all functions that map sets to sets *)
  2714         (* all functions that map sets to sets *)
  2817         val i_funs = make_constants ctxt model (Type ("fun",
  2715         val i_funs = make_constants ctxt model (Type ("fun",
  2818           [HOLogic.mk_setT T, HOLogic.mk_setT T]))
  2716           [HOLogic.mk_setT T, HOLogic.mk_setT T]))
  2819         (* "lfp(f) == Inter({u. f(u) <= u})" *)
  2717         (* "lfp(f) == Inter({u. f(u) <= u})" *)
  2820         (* interpretation * interpretation -> bool *)
       
  2821         fun is_subset (Node subs, Node sups) =
  2718         fun is_subset (Node subs, Node sups) =
  2822               forall (fn (sub, sup) => (sub = FF) orelse (sup = TT)) (subs ~~ sups)
  2719               forall (fn (sub, sup) => (sub = FF) orelse (sup = TT)) (subs ~~ sups)
  2823           | is_subset (_, _) =
  2720           | is_subset (_, _) =
  2824               raise REFUTE ("lfp_interpreter",
  2721               raise REFUTE ("lfp_interpreter",
  2825                 "is_subset: interpretation for set is not a node")
  2722                 "is_subset: interpretation for set is not a node")
  2826         (* interpretation * interpretation -> interpretation *)
       
  2827         fun intersection (Node xs, Node ys) =
  2723         fun intersection (Node xs, Node ys) =
  2828               Node (map (fn (x, y) => if x=TT andalso y=TT then TT else FF)
  2724               Node (map (fn (x, y) => if x=TT andalso y=TT then TT else FF)
  2829                 (xs ~~ ys))
  2725                 (xs ~~ ys))
  2830           | intersection (_, _) =
  2726           | intersection (_, _) =
  2831               raise REFUTE ("lfp_interpreter",
  2727               raise REFUTE ("lfp_interpreter",
  2832                 "intersection: interpretation for set is not a node")
  2728                 "intersection: interpretation for set is not a node")
  2833         (* interpretation -> interpretaion *)
       
  2834         fun lfp (Node resultsets) =
  2729         fun lfp (Node resultsets) =
  2835               fold (fn (set, resultset) => fn acc =>
  2730               fold (fn (set, resultset) => fn acc =>
  2836                 if is_subset (resultset, set) then
  2731                 if is_subset (resultset, set) then
  2837                   intersection (acc, set)
  2732                   intersection (acc, set)
  2838                 else
  2733                 else
  2863         val i_sets = make_constants ctxt model (HOLogic.mk_setT T)
  2758         val i_sets = make_constants ctxt model (HOLogic.mk_setT T)
  2864         (* all functions that map sets to sets *)
  2759         (* all functions that map sets to sets *)
  2865         val i_funs = make_constants ctxt model (Type ("fun",
  2760         val i_funs = make_constants ctxt model (Type ("fun",
  2866           [HOLogic.mk_setT T, HOLogic.mk_setT T]))
  2761           [HOLogic.mk_setT T, HOLogic.mk_setT T]))
  2867         (* "gfp(f) == Union({u. u <= f(u)})" *)
  2762         (* "gfp(f) == Union({u. u <= f(u)})" *)
  2868         (* interpretation * interpretation -> bool *)
       
  2869         fun is_subset (Node subs, Node sups) =
  2763         fun is_subset (Node subs, Node sups) =
  2870               forall (fn (sub, sup) => (sub = FF) orelse (sup = TT))
  2764               forall (fn (sub, sup) => (sub = FF) orelse (sup = TT))
  2871                 (subs ~~ sups)
  2765                 (subs ~~ sups)
  2872           | is_subset (_, _) =
  2766           | is_subset (_, _) =
  2873               raise REFUTE ("gfp_interpreter",
  2767               raise REFUTE ("gfp_interpreter",
  2874                 "is_subset: interpretation for set is not a node")
  2768                 "is_subset: interpretation for set is not a node")
  2875         (* interpretation * interpretation -> interpretation *)
       
  2876         fun union (Node xs, Node ys) =
  2769         fun union (Node xs, Node ys) =
  2877               Node (map (fn (x,y) => if x=TT orelse y=TT then TT else FF)
  2770               Node (map (fn (x,y) => if x=TT orelse y=TT then TT else FF)
  2878                    (xs ~~ ys))
  2771                    (xs ~~ ys))
  2879           | union (_, _) =
  2772           | union (_, _) =
  2880               raise REFUTE ("gfp_interpreter",
  2773               raise REFUTE ("gfp_interpreter",
  2881                 "union: interpretation for set is not a node")
  2774                 "union: interpretation for set is not a node")
  2882         (* interpretation -> interpretaion *)
       
  2883         fun gfp (Node resultsets) =
  2775         fun gfp (Node resultsets) =
  2884               fold (fn (set, resultset) => fn acc =>
  2776               fold (fn (set, resultset) => fn acc =>
  2885                 if is_subset (set, resultset) then
  2777                 if is_subset (set, resultset) then
  2886                   union (acc, set)
  2778                   union (acc, set)
  2887                 else
  2779                 else
  2929 (* PRINTERS                                                                  *)
  2821 (* PRINTERS                                                                  *)
  2930 (* ------------------------------------------------------------------------- *)
  2822 (* ------------------------------------------------------------------------- *)
  2931 
  2823 
  2932 fun stlc_printer ctxt model T intr assignment =
  2824 fun stlc_printer ctxt model T intr assignment =
  2933   let
  2825   let
  2934     (* string -> string *)
       
  2935     val strip_leading_quote = perhaps (try (unprefix "'"))
  2826     val strip_leading_quote = perhaps (try (unprefix "'"))
  2936     (* Term.typ -> string *)
       
  2937     fun string_of_typ (Type (s, _)) = s
  2827     fun string_of_typ (Type (s, _)) = s
  2938       | string_of_typ (TFree (x, _)) = strip_leading_quote x
  2828       | string_of_typ (TFree (x, _)) = strip_leading_quote x
  2939       | string_of_typ (TVar ((x,i), _)) =
  2829       | string_of_typ (TVar ((x,i), _)) =
  2940           strip_leading_quote x ^ string_of_int i
  2830           strip_leading_quote x ^ string_of_int i
  2941     (* interpretation -> int *)
       
  2942     fun index_from_interpretation (Leaf xs) =
  2831     fun index_from_interpretation (Leaf xs) =
  2943           find_index (Prop_Logic.eval assignment) xs
  2832           find_index (Prop_Logic.eval assignment) xs
  2944       | index_from_interpretation _ =
  2833       | index_from_interpretation _ =
  2945           raise REFUTE ("stlc_printer",
  2834           raise REFUTE ("stlc_printer",
  2946             "interpretation for ground type is not a leaf")
  2835             "interpretation for ground type is not a leaf")
  2948     case T of
  2837     case T of
  2949       Type ("fun", [T1, T2]) =>
  2838       Type ("fun", [T1, T2]) =>
  2950         let
  2839         let
  2951           (* create all constants of type 'T1' *)
  2840           (* create all constants of type 'T1' *)
  2952           val constants = make_constants ctxt model T1
  2841           val constants = make_constants ctxt model T1
  2953           (* interpretation list *)
       
  2954           val results =
  2842           val results =
  2955             (case intr of
  2843             (case intr of
  2956               Node xs => xs
  2844               Node xs => xs
  2957             | _ => raise REFUTE ("stlc_printer",
  2845             | _ => raise REFUTE ("stlc_printer",
  2958               "interpretation for function type is a leaf"))
  2846               "interpretation for function type is a leaf"))
  2960           val pairs = map (fn (arg, result) =>
  2848           val pairs = map (fn (arg, result) =>
  2961             HOLogic.mk_prod
  2849             HOLogic.mk_prod
  2962               (print ctxt model T1 arg assignment,
  2850               (print ctxt model T1 arg assignment,
  2963                print ctxt model T2 result assignment))
  2851                print ctxt model T2 result assignment))
  2964             (constants ~~ results)
  2852             (constants ~~ results)
  2965           (* Term.typ *)
       
  2966           val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  2853           val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  2967           val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  2854           val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  2968           (* Term.term *)
       
  2969           val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT)
  2855           val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT)
  2970           val HOLogic_insert    =
  2856           val HOLogic_insert    =
  2971             Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  2857             Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  2972         in
  2858         in
  2973           SOME (fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) pairs HOLogic_empty_set)
  2859           SOME (fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) pairs HOLogic_empty_set)
  3003   (case T of
  2889   (case T of
  3004     Type (@{type_name set}, [T1]) =>
  2890     Type (@{type_name set}, [T1]) =>
  3005     let
  2891     let
  3006       (* create all constants of type 'T1' *)
  2892       (* create all constants of type 'T1' *)
  3007       val constants = make_constants ctxt model T1
  2893       val constants = make_constants ctxt model T1
  3008       (* interpretation list *)
       
  3009       val results = (case intr of
  2894       val results = (case intr of
  3010           Node xs => xs
  2895           Node xs => xs
  3011         | _       => raise REFUTE ("set_printer",
  2896         | _       => raise REFUTE ("set_printer",
  3012           "interpretation for set type is a leaf"))
  2897           "interpretation for set type is a leaf"))
  3013       (* Term.term list *)
       
  3014       val elements = List.mapPartial (fn (arg, result) =>
  2898       val elements = List.mapPartial (fn (arg, result) =>
  3015         case result of
  2899         case result of
  3016           Leaf [fmTrue, (* fmFalse *) _] =>
  2900           Leaf [fmTrue, (* fmFalse *) _] =>
  3017           if Prop_Logic.eval assignment fmTrue then
  2901           if Prop_Logic.eval assignment fmTrue then
  3018             SOME (print ctxt model T1 arg assignment)
  2902             SOME (print ctxt model T1 arg assignment)
  3020             NONE
  2904             NONE
  3021         | _ =>
  2905         | _ =>
  3022           raise REFUTE ("set_printer",
  2906           raise REFUTE ("set_printer",
  3023             "illegal interpretation for a Boolean value"))
  2907             "illegal interpretation for a Boolean value"))
  3024         (constants ~~ results)
  2908         (constants ~~ results)
  3025       (* Term.typ *)
       
  3026       val HOLogic_setT1     = HOLogic.mk_setT T1
  2909       val HOLogic_setT1     = HOLogic.mk_setT T1
  3027       (* Term.term *)
       
  3028       val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT1)
  2910       val HOLogic_empty_set = Const (@{const_abbrev Set.empty}, HOLogic_setT1)
  3029       val HOLogic_insert    =
  2911       val HOLogic_insert    =
  3030         Const (@{const_name insert}, T1 --> HOLogic_setT1 --> HOLogic_setT1)
  2912         Const (@{const_name insert}, T1 --> HOLogic_setT1 --> HOLogic_setT1)
  3031     in
  2913     in
  3032       SOME (Library.foldl (fn (acc, elem) => HOLogic_insert $ elem $ acc)
  2914       SOME (Library.foldl (fn (acc, elem) => HOLogic_insert $ elem $ acc)
  3076                     let
  2958                     let
  3077                       val cTerm      = Const (cname,
  2959                       val cTerm      = Const (cname,
  3078                         map (typ_of_dtyp descr typ_assoc) cargs ---> Type (s, Ts))
  2960                         map (typ_of_dtyp descr typ_assoc) cargs ---> Type (s, Ts))
  3079                       val (iC, _, _) = interpret ctxt (typs, []) {maxvars=0,
  2961                       val (iC, _, _) = interpret ctxt (typs, []) {maxvars=0,
  3080                         def_eq=false, next_idx=1, bounds=[], wellformed=True} cTerm
  2962                         def_eq=false, next_idx=1, bounds=[], wellformed=True} cTerm
  3081                       (* interpretation -> int list option *)
       
  3082                       fun get_args (Leaf xs) =
  2963                       fun get_args (Leaf xs) =
  3083                             if find_index (fn x => x = True) xs = element then
  2964                             if find_index (fn x => x = True) xs = element then
  3084                               SOME []
  2965                               SOME []
  3085                             else
  2966                             else
  3086                               NONE
  2967                               NONE
  3087                         | get_args (Node xs) =
  2968                         | get_args (Node xs) =
  3088                             let
  2969                             let
  3089                               (* interpretation * int -> int list option *)
       
  3090                               fun search ([], _) =
  2970                               fun search ([], _) =
  3091                                 NONE
  2971                                 NONE
  3092                                 | search (x::xs, n) =
  2972                                 | search (x::xs, n) =
  3093                                 (case get_args x of
  2973                                 (case get_args x of
  3094                                   SOME result => SOME (n::result)
  2974                                   SOME result => SOME (n::result)