src/HOL/Tools/refute.ML
changeset 29802 d201a838d0f7
parent 29288 253bcf2a5854
child 29820 07f53494cf20
equal deleted inserted replaced
29801:67266b31cd46 29802:d201a838d0f7
    55   val refute_subgoal :
    55   val refute_subgoal :
    56     theory -> (string * string) list -> Thm.thm -> int -> unit
    56     theory -> (string * string) list -> Thm.thm -> int -> unit
    57 
    57 
    58   val setup : theory -> theory
    58   val setup : theory -> theory
    59 
    59 
       
    60 (* ------------------------------------------------------------------------- *)
       
    61 (* Additional functions used by Nitpick (to be factored out)                 *)
       
    62 (* ------------------------------------------------------------------------- *)
       
    63 
       
    64   val close_form : Term.term -> Term.term
       
    65   val get_classdef : theory -> string -> (string * Term.term) option
       
    66   val get_def : theory -> string * Term.typ -> (string * Term.term) option
       
    67   val get_typedef : theory -> Term.typ -> (string * Term.term) option
       
    68   val is_IDT_constructor : theory -> string * Term.typ -> bool
       
    69   val is_IDT_recursor : theory -> string * Term.typ -> bool
       
    70   val is_const_of_class: theory -> string * Term.typ -> bool
       
    71   val monomorphic_term : Type.tyenv -> Term.term -> Term.term
       
    72   val specialize_type : theory -> (string * Term.typ) -> Term.term -> Term.term
       
    73   val string_of_typ : Term.typ -> string
       
    74   val typ_of_dtyp :
       
    75     DatatypeAux.descr -> (DatatypeAux.dtyp * Term.typ) list -> DatatypeAux.dtyp
       
    76     -> Term.typ
    60 end;  (* signature REFUTE *)
    77 end;  (* signature REFUTE *)
    61 
    78 
    62 structure Refute : REFUTE =
    79 structure Refute : REFUTE =
    63 struct
    80 struct
    64 
    81 
   575 
   592 
   576 (* ------------------------------------------------------------------------- *)
   593 (* ------------------------------------------------------------------------- *)
   577 (* get_typedef: looks up the definition of a type, as created by "typedef"   *)
   594 (* get_typedef: looks up the definition of a type, as created by "typedef"   *)
   578 (* ------------------------------------------------------------------------- *)
   595 (* ------------------------------------------------------------------------- *)
   579 
   596 
   580   (* theory -> (string * Term.typ) -> (string * Term.term) option *)
   597   (* theory -> Term.typ -> (string * Term.term) option *)
   581 
   598 
   582   fun get_typedef thy T =
   599   fun get_typedef thy T =
   583   let
   600   let
   584     (* (string * Term.term) list -> (string * Term.term) option *)
   601     (* (string * Term.term) list -> (string * Term.term) option *)
   585     fun get_typedef_ax [] = NONE
   602     fun get_typedef_ax [] = NONE
   653   let
   670   let
   654     (* Term.term -> Term.term *)
   671     (* Term.term -> Term.term *)
   655     fun unfold_loop t =
   672     fun unfold_loop t =
   656       case t of
   673       case t of
   657       (* Pure *)
   674       (* Pure *)
   658         Const ("all", _)                => t
   675         Const (@{const_name all}, _) => t
   659       | Const ("==", _)                 => t
   676       | Const (@{const_name "=="}, _) => t
   660       | Const ("==>", _)                => t
   677       | Const (@{const_name "==>"}, _) => t
   661       | Const ("TYPE", _)               => t  (* axiomatic type classes *)
   678       | Const (@{const_name TYPE}, _) => t  (* axiomatic type classes *)
   662       (* HOL *)
   679       (* HOL *)
   663       | Const ("Trueprop", _)           => t
   680       | Const (@{const_name Trueprop}, _) => t
   664       | Const ("Not", _)                => t
   681       | Const (@{const_name Not}, _) => t
   665       | (* redundant, since 'True' is also an IDT constructor *)
   682       | (* redundant, since 'True' is also an IDT constructor *)
   666         Const ("True", _)               => t
   683         Const (@{const_name True}, _) => t
   667       | (* redundant, since 'False' is also an IDT constructor *)
   684       | (* redundant, since 'False' is also an IDT constructor *)
   668         Const ("False", _)              => t
   685         Const (@{const_name False}, _) => t
   669       | Const (@{const_name undefined}, _)          => t
   686       | Const (@{const_name undefined}, _) => t
   670       | Const ("The", _)                => t
   687       | Const (@{const_name The}, _) => t
   671       | Const ("Hilbert_Choice.Eps", _) => t
   688       | Const ("Hilbert_Choice.Eps", _) => t
   672       | Const ("All", _)                => t
   689       | Const (@{const_name All}, _) => t
   673       | Const ("Ex", _)                 => t
   690       | Const (@{const_name Ex}, _) => t
   674       | Const ("op =", _)               => t
   691       | Const (@{const_name "op ="}, _) => t
   675       | Const ("op &", _)               => t
   692       | Const (@{const_name "op &"}, _) => t
   676       | Const ("op |", _)               => t
   693       | Const (@{const_name "op |"}, _) => t
   677       | Const ("op -->", _)             => t
   694       | Const (@{const_name "op -->"}, _) => t
   678       (* sets *)
   695       (* sets *)
   679       | Const ("Collect", _)            => t
   696       | Const (@{const_name Collect}, _) => t
   680       | Const ("op :", _)               => t
   697       | Const (@{const_name "op :"}, _) => t
   681       (* other optimizations *)
   698       (* other optimizations *)
   682       | Const ("Finite_Set.card", _)    => t
   699       | Const ("Finite_Set.card", _) => t
   683       | Const ("Finite_Set.Finites", _) => t
   700       | Const ("Finite_Set.finite", _) => t
   684       | Const ("Finite_Set.finite", _)  => t
       
   685       | Const (@{const_name HOL.less}, Type ("fun", [Type ("nat", []),
   701       | Const (@{const_name HOL.less}, Type ("fun", [Type ("nat", []),
   686         Type ("fun", [Type ("nat", []), Type ("bool", [])])])) => t
   702         Type ("fun", [Type ("nat", []), Type ("bool", [])])])) => t
   687       | Const (@{const_name HOL.plus}, Type ("fun", [Type ("nat", []),
   703       | Const (@{const_name HOL.plus}, Type ("fun", [Type ("nat", []),
   688         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
   704         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
   689       | Const (@{const_name HOL.minus}, Type ("fun", [Type ("nat", []),
   705       | Const (@{const_name HOL.minus}, Type ("fun", [Type ("nat", []),
   690         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
   706         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
   691       | Const (@{const_name HOL.times}, Type ("fun", [Type ("nat", []),
   707       | Const (@{const_name HOL.times}, Type ("fun", [Type ("nat", []),
   692         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
   708         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) => t
   693       | Const ("List.append", _)        => t
   709       | Const ("List.append", _) => t
   694       | Const ("Lfp.lfp", _)            => t
   710       | Const (@{const_name lfp}, _) => t
   695       | Const ("Gfp.gfp", _)            => t
   711       | Const (@{const_name gfp}, _) => t
   696       | Const ("fst", _)                => t
   712       | Const ("Product_Type.fst", _) => t
   697       | Const ("snd", _)                => t
   713       | Const ("Product_Type.snd", _) => t
   698       (* simply-typed lambda calculus *)
   714       (* simply-typed lambda calculus *)
   699       | Const (s, T) =>
   715       | Const (s, T) =>
   700         (if is_IDT_constructor thy (s, T)
   716         (if is_IDT_constructor thy (s, T)
   701           orelse is_IDT_recursor thy (s, T) then
   717           orelse is_IDT_recursor thy (s, T) then
   702           t  (* do not unfold IDT constructors/recursors *)
   718           t  (* do not unfold IDT constructors/recursors *)
   803       case T of
   819       case T of
   804       (* simple types *)
   820       (* simple types *)
   805         Type ("prop", [])      => axs
   821         Type ("prop", [])      => axs
   806       | Type ("fun", [T1, T2]) => collect_type_axioms
   822       | Type ("fun", [T1, T2]) => collect_type_axioms
   807         (collect_type_axioms (axs, T1), T2)
   823         (collect_type_axioms (axs, T1), T2)
   808       | Type ("set", [T1])     => collect_type_axioms (axs, T1)
       
   809       (* axiomatic type classes *)
   824       (* axiomatic type classes *)
   810       | Type ("itself", [T1])  => collect_type_axioms (axs, T1)
   825       | Type ("itself", [T1])  => collect_type_axioms (axs, T1)
   811       | Type (s, Ts)           =>
   826       | Type (s, Ts)           =>
   812         (case DatatypePackage.get_datatype thy s of
   827         (case DatatypePackage.get_datatype thy s of
   813           SOME info =>  (* inductive datatype *)
   828           SOME info =>  (* inductive datatype *)
   827       | TVar _                 => collect_sort_axioms (axs, T)
   842       | TVar _                 => collect_sort_axioms (axs, T)
   828     (* Term.term list * Term.term -> Term.term list *)
   843     (* Term.term list * Term.term -> Term.term list *)
   829     and collect_term_axioms (axs, t) =
   844     and collect_term_axioms (axs, t) =
   830       case t of
   845       case t of
   831       (* Pure *)
   846       (* Pure *)
   832         Const ("all", _)                => axs
   847         Const (@{const_name all}, _) => axs
   833       | Const ("==", _)                 => axs
   848       | Const (@{const_name "=="}, _) => axs
   834       | Const ("==>", _)                => axs
   849       | Const (@{const_name "==>"}, _) => axs
   835       (* axiomatic type classes *)
   850       (* axiomatic type classes *)
   836       | Const ("TYPE", T)               => collect_type_axioms (axs, T)
   851       | Const (@{const_name TYPE}, T) => collect_type_axioms (axs, T)
   837       (* HOL *)
   852       (* HOL *)
   838       | Const ("Trueprop", _)           => axs
   853       | Const (@{const_name Trueprop}, _) => axs
   839       | Const ("Not", _)                => axs
   854       | Const (@{const_name Not}, _) => axs
   840       (* redundant, since 'True' is also an IDT constructor *)
   855       (* redundant, since 'True' is also an IDT constructor *)
   841       | Const ("True", _)               => axs
   856       | Const (@{const_name True}, _) => axs
   842       (* redundant, since 'False' is also an IDT constructor *)
   857       (* redundant, since 'False' is also an IDT constructor *)
   843       | Const ("False", _)              => axs
   858       | Const (@{const_name False}, _) => axs
   844       | Const (@{const_name undefined}, T)          => collect_type_axioms (axs, T)
   859       | Const (@{const_name undefined}, T) => collect_type_axioms (axs, T)
   845       | Const ("The", T)                =>
   860       | Const (@{const_name The}, T) =>
   846         let
   861         let
   847           val ax = specialize_type thy ("The", T)
   862           val ax = specialize_type thy (@{const_name The}, T)
   848             (the (AList.lookup (op =) axioms "HOL.the_eq_trivial"))
   863             (the (AList.lookup (op =) axioms "HOL.the_eq_trivial"))
   849         in
   864         in
   850           collect_this_axiom ("HOL.the_eq_trivial", ax) axs
   865           collect_this_axiom ("HOL.the_eq_trivial", ax) axs
   851         end
   866         end
   852       | Const ("Hilbert_Choice.Eps", T) =>
   867       | Const ("Hilbert_Choice.Eps", T) =>
   854           val ax = specialize_type thy ("Hilbert_Choice.Eps", T)
   869           val ax = specialize_type thy ("Hilbert_Choice.Eps", T)
   855             (the (AList.lookup (op =) axioms "Hilbert_Choice.someI"))
   870             (the (AList.lookup (op =) axioms "Hilbert_Choice.someI"))
   856         in
   871         in
   857           collect_this_axiom ("Hilbert_Choice.someI", ax) axs
   872           collect_this_axiom ("Hilbert_Choice.someI", ax) axs
   858         end
   873         end
   859       | Const ("All", T)                => collect_type_axioms (axs, T)
   874       | Const (@{const_name All}, T) => collect_type_axioms (axs, T)
   860       | Const ("Ex", T)                 => collect_type_axioms (axs, T)
   875       | Const (@{const_name Ex}, T) => collect_type_axioms (axs, T)
   861       | Const ("op =", T)               => collect_type_axioms (axs, T)
   876       | Const (@{const_name "op ="}, T) => collect_type_axioms (axs, T)
   862       | Const ("op &", _)               => axs
   877       | Const (@{const_name "op &"}, _) => axs
   863       | Const ("op |", _)               => axs
   878       | Const (@{const_name "op |"}, _) => axs
   864       | Const ("op -->", _)             => axs
   879       | Const (@{const_name "op -->"}, _) => axs
   865       (* sets *)
   880       (* sets *)
   866       | Const ("Collect", T)            => collect_type_axioms (axs, T)
   881       | Const (@{const_name Collect}, T) => collect_type_axioms (axs, T)
   867       | Const ("op :", T)               => collect_type_axioms (axs, T)
   882       | Const (@{const_name "op :"}, T) => collect_type_axioms (axs, T)
   868       (* other optimizations *)
   883       (* other optimizations *)
   869       | Const ("Finite_Set.card", T)    => collect_type_axioms (axs, T)
   884       | Const ("Finite_Set.card", T) => collect_type_axioms (axs, T)
   870       | Const ("Finite_Set.Finites", T) => collect_type_axioms (axs, T)
   885       | Const ("Finite_Set.finite", T) => collect_type_axioms (axs, T)
   871       | Const ("Finite_Set.finite", T)  => collect_type_axioms (axs, T)
       
   872       | Const (@{const_name HOL.less}, T as Type ("fun", [Type ("nat", []),
   886       | Const (@{const_name HOL.less}, T as Type ("fun", [Type ("nat", []),
   873         Type ("fun", [Type ("nat", []), Type ("bool", [])])])) =>
   887         Type ("fun", [Type ("nat", []), Type ("bool", [])])])) =>
   874           collect_type_axioms (axs, T)
   888           collect_type_axioms (axs, T)
   875       | Const (@{const_name HOL.plus}, T as Type ("fun", [Type ("nat", []),
   889       | Const (@{const_name HOL.plus}, T as Type ("fun", [Type ("nat", []),
   876         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
   890         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
   879         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
   893         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
   880           collect_type_axioms (axs, T)
   894           collect_type_axioms (axs, T)
   881       | Const (@{const_name HOL.times}, T as Type ("fun", [Type ("nat", []),
   895       | Const (@{const_name HOL.times}, T as Type ("fun", [Type ("nat", []),
   882         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
   896         Type ("fun", [Type ("nat", []), Type ("nat", [])])])) =>
   883           collect_type_axioms (axs, T)
   897           collect_type_axioms (axs, T)
   884       | Const ("List.append", T)        => collect_type_axioms (axs, T)
   898       | Const ("List.append", T) => collect_type_axioms (axs, T)
   885       | Const ("Lfp.lfp", T)            => collect_type_axioms (axs, T)
   899       | Const (@{const_name lfp}, T) => collect_type_axioms (axs, T)
   886       | Const ("Gfp.gfp", T)            => collect_type_axioms (axs, T)
   900       | Const (@{const_name gfp}, T) => collect_type_axioms (axs, T)
   887       | Const ("fst", T)                => collect_type_axioms (axs, T)
   901       | Const ("Product_Type.fst", T) => collect_type_axioms (axs, T)
   888       | Const ("snd", T)                => collect_type_axioms (axs, T)
   902       | Const ("Product_Type.snd", T) => collect_type_axioms (axs, T)
   889       (* simply-typed lambda calculus *)
   903       (* simply-typed lambda calculus *)
   890       | Const (s, T)                    =>
   904       | Const (s, T) =>
   891           if is_const_of_class thy (s, T) then
   905           if is_const_of_class thy (s, T) then
   892             (* axiomatic type classes: add "OFCLASS(?'a::c, c_class)" *)
   906             (* axiomatic type classes: add "OFCLASS(?'a::c, c_class)" *)
   893             (* and the class definition                               *)
   907             (* and the class definition                               *)
   894             let
   908             let
   895               val class   = Logic.class_of_const s
   909               val class   = Logic.class_of_const s
   941   let
   955   let
   942     fun collect_types T acc =
   956     fun collect_types T acc =
   943       (case T of
   957       (case T of
   944         Type ("fun", [T1, T2]) => collect_types T1 (collect_types T2 acc)
   958         Type ("fun", [T1, T2]) => collect_types T1 (collect_types T2 acc)
   945       | Type ("prop", [])      => acc
   959       | Type ("prop", [])      => acc
   946       | Type ("set", [T1])     => collect_types T1 acc
       
   947       | Type (s, Ts)           =>
   960       | Type (s, Ts)           =>
   948         (case DatatypePackage.get_datatype thy s of
   961         (case DatatypePackage.get_datatype thy s of
   949           SOME info =>  (* inductive datatype *)
   962           SOME info =>  (* inductive datatype *)
   950           let
   963           let
   951             val index        = #index info
   964             val index        = #index info
  1301     (* while the SAT solver searches for an interpretation for Free's.   *)
  1314     (* while the SAT solver searches for an interpretation for Free's.   *)
  1302     (* Also we get more information back that way, namely an             *)
  1315     (* Also we get more information back that way, namely an             *)
  1303     (* interpretation which includes values for the (formerly)           *)
  1316     (* interpretation which includes values for the (formerly)           *)
  1304     (* quantified variables.                                             *)
  1317     (* quantified variables.                                             *)
  1305     (* maps  !!x1...xn. !xk...xm. t   to   t  *)
  1318     (* maps  !!x1...xn. !xk...xm. t   to   t  *)
  1306     fun strip_all_body (Const ("all", _) $ Abs (_, _, t)) = strip_all_body t
  1319     fun strip_all_body (Const (@{const_name all}, _) $ Abs (_, _, t)) = strip_all_body t
  1307       | strip_all_body (Const ("Trueprop", _) $ t)        = strip_all_body t
  1320       | strip_all_body (Const (@{const_name Trueprop}, _) $ t) = strip_all_body t
  1308       | strip_all_body (Const ("All", _) $ Abs (_, _, t)) = strip_all_body t
  1321       | strip_all_body (Const (@{const_name All}, _) $ Abs (_, _, t)) = strip_all_body t
  1309       | strip_all_body t                                  = t
  1322       | strip_all_body t = t
  1310     (* maps  !!x1...xn. !xk...xm. t   to   [x1, ..., xn, xk, ..., xm]  *)
  1323     (* maps  !!x1...xn. !xk...xm. t   to   [x1, ..., xn, xk, ..., xm]  *)
  1311     fun strip_all_vars (Const ("all", _) $ Abs (a, T, t)) =
  1324     fun strip_all_vars (Const (@{const_name all}, _) $ Abs (a, T, t)) =
  1312       (a, T) :: strip_all_vars t
  1325       (a, T) :: strip_all_vars t
  1313       | strip_all_vars (Const ("Trueprop", _) $ t)        =
  1326       | strip_all_vars (Const (@{const_name Trueprop}, _) $ t) =
  1314       strip_all_vars t
  1327       strip_all_vars t
  1315       | strip_all_vars (Const ("All", _) $ Abs (a, T, t)) =
  1328       | strip_all_vars (Const (@{const_name All}, _) $ Abs (a, T, t)) =
  1316       (a, T) :: strip_all_vars t
  1329       (a, T) :: strip_all_vars t
  1317       | strip_all_vars t                                  =
  1330       | strip_all_vars t =
  1318       [] : (string * typ) list
  1331       [] : (string * typ) list
  1319     val strip_t = strip_all_body ex_closure
  1332     val strip_t = strip_all_body ex_closure
  1320     val frees   = Term.rename_wrt_term strip_t (strip_all_vars ex_closure)
  1333     val frees   = Term.rename_wrt_term strip_t (strip_all_vars ex_closure)
  1321     val subst_t = Term.subst_bounds (map Free frees, strip_t)
  1334     val subst_t = Term.subst_bounds (map Free frees, strip_t)
  1322   in
  1335   in
  1762   (* theory -> model -> arguments -> Term.term ->
  1775   (* theory -> model -> arguments -> Term.term ->
  1763     (interpretation * model * arguments) option *)
  1776     (interpretation * model * arguments) option *)
  1764 
  1777 
  1765   fun Pure_interpreter thy model args t =
  1778   fun Pure_interpreter thy model args t =
  1766     case t of
  1779     case t of
  1767       Const ("all", _) $ t1 =>
  1780       Const (@{const_name all}, _) $ t1 =>
  1768       let
  1781       let
  1769         val (i, m, a) = interpret thy model args t1
  1782         val (i, m, a) = interpret thy model args t1
  1770       in
  1783       in
  1771         case i of
  1784         case i of
  1772           Node xs =>
  1785           Node xs =>
  1779           end
  1792           end
  1780         | _ =>
  1793         | _ =>
  1781           raise REFUTE ("Pure_interpreter",
  1794           raise REFUTE ("Pure_interpreter",
  1782             "\"all\" is followed by a non-function")
  1795             "\"all\" is followed by a non-function")
  1783       end
  1796       end
  1784     | Const ("all", _) =>
  1797     | Const (@{const_name all}, _) =>
  1785       SOME (interpret thy model args (eta_expand t 1))
  1798       SOME (interpret thy model args (eta_expand t 1))
  1786     | Const ("==", _) $ t1 $ t2 =>
  1799     | Const (@{const_name "=="}, _) $ t1 $ t2 =>
  1787       let
  1800       let
  1788         val (i1, m1, a1) = interpret thy model args t1
  1801         val (i1, m1, a1) = interpret thy model args t1
  1789         val (i2, m2, a2) = interpret thy m1 a1 t2
  1802         val (i2, m2, a2) = interpret thy m1 a1 t2
  1790       in
  1803       in
  1791         (* we use either 'make_def_equality' or 'make_equality' *)
  1804         (* we use either 'make_def_equality' or 'make_equality' *)
  1792         SOME ((if #def_eq args then make_def_equality else make_equality)
  1805         SOME ((if #def_eq args then make_def_equality else make_equality)
  1793           (i1, i2), m2, a2)
  1806           (i1, i2), m2, a2)
  1794       end
  1807       end
  1795     | Const ("==", _) $ t1 =>
  1808     | Const (@{const_name "=="}, _) $ t1 =>
  1796       SOME (interpret thy model args (eta_expand t 1))
  1809       SOME (interpret thy model args (eta_expand t 1))
  1797     | Const ("==", _) =>
  1810     | Const (@{const_name "=="}, _) =>
  1798       SOME (interpret thy model args (eta_expand t 2))
  1811       SOME (interpret thy model args (eta_expand t 2))
  1799     | Const ("==>", _) $ t1 $ t2 =>
  1812     | Const (@{const_name "==>"}, _) $ t1 $ t2 =>
  1800       (* 3-valued logic *)
  1813       (* 3-valued logic *)
  1801       let
  1814       let
  1802         val (i1, m1, a1) = interpret thy model args t1
  1815         val (i1, m1, a1) = interpret thy model args t1
  1803         val (i2, m2, a2) = interpret thy m1 a1 t2
  1816         val (i2, m2, a2) = interpret thy m1 a1 t2
  1804         val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
  1817         val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
  1805         val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
  1818         val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
  1806       in
  1819       in
  1807         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1820         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1808       end
  1821       end
  1809     | Const ("==>", _) $ t1 =>
  1822     | Const (@{const_name "==>"}, _) $ t1 =>
  1810       SOME (interpret thy model args (eta_expand t 1))
  1823       SOME (interpret thy model args (eta_expand t 1))
  1811     | Const ("==>", _) =>
  1824     | Const (@{const_name "==>"}, _) =>
  1812       SOME (interpret thy model args (eta_expand t 2))
  1825       SOME (interpret thy model args (eta_expand t 2))
  1813     | _ => NONE;
  1826     | _ => NONE;
  1814 
  1827 
  1815   (* theory -> model -> arguments -> Term.term ->
  1828   (* theory -> model -> arguments -> Term.term ->
  1816     (interpretation * model * arguments) option *)
  1829     (interpretation * model * arguments) option *)
  1818   fun HOLogic_interpreter thy model args t =
  1831   fun HOLogic_interpreter thy model args t =
  1819   (* Providing interpretations directly is more efficient than unfolding the *)
  1832   (* Providing interpretations directly is more efficient than unfolding the *)
  1820   (* logical constants.  In HOL however, logical constants can themselves be *)
  1833   (* logical constants.  In HOL however, logical constants can themselves be *)
  1821   (* arguments.  They are then translated using eta-expansion.               *)
  1834   (* arguments.  They are then translated using eta-expansion.               *)
  1822     case t of
  1835     case t of
  1823       Const ("Trueprop", _) =>
  1836       Const (@{const_name Trueprop}, _) =>
  1824       SOME (Node [TT, FF], model, args)
  1837       SOME (Node [TT, FF], model, args)
  1825     | Const ("Not", _) =>
  1838     | Const (@{const_name Not}, _) =>
  1826       SOME (Node [FF, TT], model, args)
  1839       SOME (Node [FF, TT], model, args)
  1827     (* redundant, since 'True' is also an IDT constructor *)
  1840     (* redundant, since 'True' is also an IDT constructor *)
  1828     | Const ("True", _) =>
  1841     | Const (@{const_name True}, _) =>
  1829       SOME (TT, model, args)
  1842       SOME (TT, model, args)
  1830     (* redundant, since 'False' is also an IDT constructor *)
  1843     (* redundant, since 'False' is also an IDT constructor *)
  1831     | Const ("False", _) =>
  1844     | Const (@{const_name False}, _) =>
  1832       SOME (FF, model, args)
  1845       SOME (FF, model, args)
  1833     | Const ("All", _) $ t1 =>  (* similar to "all" (Pure) *)
  1846     | Const (@{const_name All}, _) $ t1 =>  (* similar to "all" (Pure) *)
  1834       let
  1847       let
  1835         val (i, m, a) = interpret thy model args t1
  1848         val (i, m, a) = interpret thy model args t1
  1836       in
  1849       in
  1837         case i of
  1850         case i of
  1838           Node xs =>
  1851           Node xs =>
  1845           end
  1858           end
  1846         | _ =>
  1859         | _ =>
  1847           raise REFUTE ("HOLogic_interpreter",
  1860           raise REFUTE ("HOLogic_interpreter",
  1848             "\"All\" is followed by a non-function")
  1861             "\"All\" is followed by a non-function")
  1849       end
  1862       end
  1850     | Const ("All", _) =>
  1863     | Const (@{const_name All}, _) =>
  1851       SOME (interpret thy model args (eta_expand t 1))
  1864       SOME (interpret thy model args (eta_expand t 1))
  1852     | Const ("Ex", _) $ t1 =>
  1865     | Const (@{const_name Ex}, _) $ t1 =>
  1853       let
  1866       let
  1854         val (i, m, a) = interpret thy model args t1
  1867         val (i, m, a) = interpret thy model args t1
  1855       in
  1868       in
  1856         case i of
  1869         case i of
  1857           Node xs =>
  1870           Node xs =>
  1864           end
  1877           end
  1865         | _ =>
  1878         | _ =>
  1866           raise REFUTE ("HOLogic_interpreter",
  1879           raise REFUTE ("HOLogic_interpreter",
  1867             "\"Ex\" is followed by a non-function")
  1880             "\"Ex\" is followed by a non-function")
  1868       end
  1881       end
  1869     | Const ("Ex", _) =>
  1882     | Const (@{const_name Ex}, _) =>
  1870       SOME (interpret thy model args (eta_expand t 1))
  1883       SOME (interpret thy model args (eta_expand t 1))
  1871     | Const ("op =", _) $ t1 $ t2 =>  (* similar to "==" (Pure) *)
  1884     | Const (@{const_name "op ="}, _) $ t1 $ t2 =>  (* similar to "==" (Pure) *)
  1872       let
  1885       let
  1873         val (i1, m1, a1) = interpret thy model args t1
  1886         val (i1, m1, a1) = interpret thy model args t1
  1874         val (i2, m2, a2) = interpret thy m1 a1 t2
  1887         val (i2, m2, a2) = interpret thy m1 a1 t2
  1875       in
  1888       in
  1876         SOME (make_equality (i1, i2), m2, a2)
  1889         SOME (make_equality (i1, i2), m2, a2)
  1877       end
  1890       end
  1878     | Const ("op =", _) $ t1 =>
  1891     | Const (@{const_name "op ="}, _) $ t1 =>
  1879       SOME (interpret thy model args (eta_expand t 1))
  1892       SOME (interpret thy model args (eta_expand t 1))
  1880     | Const ("op =", _) =>
  1893     | Const (@{const_name "op ="}, _) =>
  1881       SOME (interpret thy model args (eta_expand t 2))
  1894       SOME (interpret thy model args (eta_expand t 2))
  1882     | Const ("op &", _) $ t1 $ t2 =>
  1895     | Const (@{const_name "op &"}, _) $ t1 $ t2 =>
  1883       (* 3-valued logic *)
  1896       (* 3-valued logic *)
  1884       let
  1897       let
  1885         val (i1, m1, a1) = interpret thy model args t1
  1898         val (i1, m1, a1) = interpret thy model args t1
  1886         val (i2, m2, a2) = interpret thy m1 a1 t2
  1899         val (i2, m2, a2) = interpret thy m1 a1 t2
  1887         val fmTrue       = PropLogic.SAnd (toTrue i1, toTrue i2)
  1900         val fmTrue       = PropLogic.SAnd (toTrue i1, toTrue i2)
  1888         val fmFalse      = PropLogic.SOr (toFalse i1, toFalse i2)
  1901         val fmFalse      = PropLogic.SOr (toFalse i1, toFalse i2)
  1889       in
  1902       in
  1890         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1903         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1891       end
  1904       end
  1892     | Const ("op &", _) $ t1 =>
  1905     | Const (@{const_name "op &"}, _) $ t1 =>
  1893       SOME (interpret thy model args (eta_expand t 1))
  1906       SOME (interpret thy model args (eta_expand t 1))
  1894     | Const ("op &", _) =>
  1907     | Const (@{const_name "op &"}, _) =>
  1895       SOME (interpret thy model args (eta_expand t 2))
  1908       SOME (interpret thy model args (eta_expand t 2))
  1896       (* this would make "undef" propagate, even for formulae like *)
  1909       (* this would make "undef" propagate, even for formulae like *)
  1897       (* "False & undef":                                          *)
  1910       (* "False & undef":                                          *)
  1898       (* SOME (Node [Node [TT, FF], Node [FF, FF]], model, args) *)
  1911       (* SOME (Node [Node [TT, FF], Node [FF, FF]], model, args) *)
  1899     | Const ("op |", _) $ t1 $ t2 =>
  1912     | Const (@{const_name "op |"}, _) $ t1 $ t2 =>
  1900       (* 3-valued logic *)
  1913       (* 3-valued logic *)
  1901       let
  1914       let
  1902         val (i1, m1, a1) = interpret thy model args t1
  1915         val (i1, m1, a1) = interpret thy model args t1
  1903         val (i2, m2, a2) = interpret thy m1 a1 t2
  1916         val (i2, m2, a2) = interpret thy m1 a1 t2
  1904         val fmTrue       = PropLogic.SOr (toTrue i1, toTrue i2)
  1917         val fmTrue       = PropLogic.SOr (toTrue i1, toTrue i2)
  1905         val fmFalse      = PropLogic.SAnd (toFalse i1, toFalse i2)
  1918         val fmFalse      = PropLogic.SAnd (toFalse i1, toFalse i2)
  1906       in
  1919       in
  1907         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1920         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1908       end
  1921       end
  1909     | Const ("op |", _) $ t1 =>
  1922     | Const (@{const_name "op |"}, _) $ t1 =>
  1910       SOME (interpret thy model args (eta_expand t 1))
  1923       SOME (interpret thy model args (eta_expand t 1))
  1911     | Const ("op |", _) =>
  1924     | Const (@{const_name "op |"}, _) =>
  1912       SOME (interpret thy model args (eta_expand t 2))
  1925       SOME (interpret thy model args (eta_expand t 2))
  1913       (* this would make "undef" propagate, even for formulae like *)
  1926       (* this would make "undef" propagate, even for formulae like *)
  1914       (* "True | undef":                                           *)
  1927       (* "True | undef":                                           *)
  1915       (* SOME (Node [Node [TT, TT], Node [TT, FF]], model, args) *)
  1928       (* SOME (Node [Node [TT, TT], Node [TT, FF]], model, args) *)
  1916     | Const ("op -->", _) $ t1 $ t2 =>  (* similar to "==>" (Pure) *)
  1929     | Const (@{const_name "op -->"}, _) $ t1 $ t2 =>  (* similar to "==>" (Pure) *)
  1917       (* 3-valued logic *)
  1930       (* 3-valued logic *)
  1918       let
  1931       let
  1919         val (i1, m1, a1) = interpret thy model args t1
  1932         val (i1, m1, a1) = interpret thy model args t1
  1920         val (i2, m2, a2) = interpret thy m1 a1 t2
  1933         val (i2, m2, a2) = interpret thy m1 a1 t2
  1921         val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
  1934         val fmTrue       = PropLogic.SOr (toFalse i1, toTrue i2)
  1922         val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
  1935         val fmFalse      = PropLogic.SAnd (toTrue i1, toFalse i2)
  1923       in
  1936       in
  1924         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1937         SOME (Leaf [fmTrue, fmFalse], m2, a2)
  1925       end
  1938       end
  1926     | Const ("op -->", _) $ t1 =>
  1939     | Const (@{const_name "op -->"}, _) $ t1 =>
  1927       SOME (interpret thy model args (eta_expand t 1))
  1940       SOME (interpret thy model args (eta_expand t 1))
  1928     | Const ("op -->", _) =>
  1941     | Const (@{const_name "op -->"}, _) =>
  1929       SOME (interpret thy model args (eta_expand t 2))
  1942       SOME (interpret thy model args (eta_expand t 2))
  1930       (* this would make "undef" propagate, even for formulae like *)
  1943       (* this would make "undef" propagate, even for formulae like *)
  1931       (* "False --> undef":                                        *)
  1944       (* "False --> undef":                                        *)
  1932       (* SOME (Node [Node [TT, FF], Node [TT, TT]], model, args) *)
  1945       (* SOME (Node [Node [TT, FF], Node [TT, TT]], model, args) *)
  1933     | _ => NONE;
  1946     | _ => NONE;
  1934 
       
  1935   (* theory -> model -> arguments -> Term.term ->
       
  1936     (interpretation * model * arguments) option *)
       
  1937 
       
  1938   fun set_interpreter thy model args t =
       
  1939   (* "T set" is isomorphic to "T --> bool" *)
       
  1940   let
       
  1941     val (typs, terms) = model
       
  1942   in
       
  1943     case AList.lookup (op =) terms t of
       
  1944       SOME intr =>
       
  1945       (* return an existing interpretation *)
       
  1946       SOME (intr, model, args)
       
  1947     | NONE =>
       
  1948       (case t of
       
  1949         Free (x, Type ("set", [T])) =>
       
  1950         let
       
  1951           val (intr, _, args') =
       
  1952             interpret thy (typs, []) args (Free (x, T --> HOLogic.boolT))
       
  1953         in
       
  1954           SOME (intr, (typs, (t, intr)::terms), args')
       
  1955         end
       
  1956       | Var ((x, i), Type ("set", [T])) =>
       
  1957         let
       
  1958           val (intr, _, args') =
       
  1959             interpret thy (typs, []) args (Var ((x,i), T --> HOLogic.boolT))
       
  1960         in
       
  1961           SOME (intr, (typs, (t, intr)::terms), args')
       
  1962         end
       
  1963       | Const (s, Type ("set", [T])) =>
       
  1964         let
       
  1965           val (intr, _, args') =
       
  1966             interpret thy (typs, []) args (Const (s, T --> HOLogic.boolT))
       
  1967         in
       
  1968           SOME (intr, (typs, (t, intr)::terms), args')
       
  1969         end
       
  1970       (* 'Collect' == identity *)
       
  1971       | Const ("Collect", _) $ t1 =>
       
  1972         SOME (interpret thy model args t1)
       
  1973       | Const ("Collect", _) =>
       
  1974         SOME (interpret thy model args (eta_expand t 1))
       
  1975       (* 'op :' == application *)
       
  1976       | Const ("op :", _) $ t1 $ t2 =>
       
  1977         SOME (interpret thy model args (t2 $ t1))
       
  1978       | Const ("op :", _) $ t1 =>
       
  1979         SOME (interpret thy model args (eta_expand t 1))
       
  1980       | Const ("op :", _) =>
       
  1981         SOME (interpret thy model args (eta_expand t 2))
       
  1982       | _ => NONE)
       
  1983   end;
       
  1984 
  1947 
  1985   (* theory -> model -> arguments -> Term.term ->
  1948   (* theory -> model -> arguments -> Term.term ->
  1986     (interpretation * model * arguments) option *)
  1949     (interpretation * model * arguments) option *)
  1987 
  1950 
  1988   (* interprets variables and constants whose type is an IDT (this is        *)
  1951   (* interprets variables and constants whose type is an IDT (this is        *)
  2118           val pairss = map (map HOLogic.mk_prod) functions
  2081           val pairss = map (map HOLogic.mk_prod) functions
  2119           (* Term.typ *)
  2082           (* Term.typ *)
  2120           val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  2083           val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  2121           val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  2084           val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  2122           (* Term.term *)
  2085           (* Term.term *)
  2123           val HOLogic_empty_set = Const ("{}", HOLogic_setT)
  2086           val HOLogic_empty_set = Const (@{const_name "{}"}, HOLogic_setT)
  2124           val HOLogic_insert    =
  2087           val HOLogic_insert    =
  2125             Const ("insert", HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  2088             Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  2126         in
  2089         in
  2127           (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
  2090           (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
  2128           map (foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  2091           map (foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  2129             HOLogic_empty_set) pairss
  2092             HOLogic_empty_set) pairss
  2130         end
  2093         end
  2686             NONE  (* not a recursion operator of this datatype *)
  2649             NONE  (* not a recursion operator of this datatype *)
  2687         ) (DatatypePackage.get_datatypes thy) NONE
  2650         ) (DatatypePackage.get_datatypes thy) NONE
  2688     | _ =>  (* head of term is not a constant *)
  2651     | _ =>  (* head of term is not a constant *)
  2689       NONE;
  2652       NONE;
  2690 
  2653 
       
  2654   (* TODO: Fix all occurrences of Type ("set", _). *)
       
  2655 
  2691   (* theory -> model -> arguments -> Term.term ->
  2656   (* theory -> model -> arguments -> Term.term ->
  2692     (interpretation * model * arguments) option *)
  2657     (interpretation * model * arguments) option *)
  2693 
  2658 
  2694   (* only an optimization: 'card' could in principle be interpreted with *)
  2659   (* only an optimization: 'card' could in principle be interpreted with *)
  2695   (* interpreters available already (using its definition), but the code *)
  2660   (* interpreters available already (using its definition), but the code *)
  2729               Leaf (replicate size_of_nat False)
  2694               Leaf (replicate size_of_nat False)
  2730           end
  2695           end
  2731         val set_constants = make_constants thy model (Type ("set", [T]))
  2696         val set_constants = make_constants thy model (Type ("set", [T]))
  2732       in
  2697       in
  2733         SOME (Node (map card set_constants), model, args)
  2698         SOME (Node (map card set_constants), model, args)
  2734       end
       
  2735     | _ =>
       
  2736       NONE;
       
  2737 
       
  2738   (* theory -> model -> arguments -> Term.term ->
       
  2739     (interpretation * model * arguments) option *)
       
  2740 
       
  2741   (* only an optimization: 'Finites' could in principle be interpreted with *)
       
  2742   (* interpreters available already (using its definition), but the code    *)
       
  2743   (* below is more efficient                                                *)
       
  2744 
       
  2745   fun Finite_Set_Finites_interpreter thy model args t =
       
  2746     case t of
       
  2747       Const ("Finite_Set.Finites", Type ("set", [Type ("set", [T])])) =>
       
  2748       let
       
  2749         val size_of_set = size_of_type thy model (Type ("set", [T]))
       
  2750       in
       
  2751         (* we only consider finite models anyway, hence EVERY set is in *)
       
  2752         (* "Finites"                                                    *)
       
  2753         SOME (Node (replicate size_of_set TT), model, args)
       
  2754       end
  2699       end
  2755     | _ =>
  2700     | _ =>
  2756       NONE;
  2701       NONE;
  2757 
  2702 
  2758   (* theory -> model -> arguments -> Term.term ->
  2703   (* theory -> model -> arguments -> Term.term ->
  2993 
  2938 
  2994   (* only an optimization: 'lfp' could in principle be interpreted with  *)
  2939   (* only an optimization: 'lfp' could in principle be interpreted with  *)
  2995   (* interpreters available already (using its definition), but the code *)
  2940   (* interpreters available already (using its definition), but the code *)
  2996   (* below is more efficient                                             *)
  2941   (* below is more efficient                                             *)
  2997 
  2942 
  2998   fun Lfp_lfp_interpreter thy model args t =
  2943   fun lfp_interpreter thy model args t =
  2999     case t of
  2944     case t of
  3000       Const ("Lfp.lfp", Type ("fun", [Type ("fun",
  2945       Const (@{const_name lfp}, Type ("fun", [Type ("fun",
  3001         [Type ("set", [T]), Type ("set", [_])]), Type ("set", [_])])) =>
  2946         [Type ("set", [T]), Type ("set", [_])]), Type ("set", [_])])) =>
  3002       let
  2947       let
  3003         val size_elem = size_of_type thy model T
  2948         val size_elem = size_of_type thy model T
  3004         (* the universe (i.e. the set that contains every element) *)
  2949         (* the universe (i.e. the set that contains every element) *)
  3005         val i_univ = Node (replicate size_elem TT)
  2950         val i_univ = Node (replicate size_elem TT)
  3012         (* interpretation * interpretation -> bool *)
  2957         (* interpretation * interpretation -> bool *)
  3013         fun is_subset (Node subs, Node sups) =
  2958         fun is_subset (Node subs, Node sups) =
  3014           List.all (fn (sub, sup) => (sub = FF) orelse (sup = TT))
  2959           List.all (fn (sub, sup) => (sub = FF) orelse (sup = TT))
  3015             (subs ~~ sups)
  2960             (subs ~~ sups)
  3016           | is_subset (_, _) =
  2961           | is_subset (_, _) =
  3017           raise REFUTE ("Lfp_lfp_interpreter",
  2962           raise REFUTE ("lfp_interpreter",
  3018             "is_subset: interpretation for set is not a node")
  2963             "is_subset: interpretation for set is not a node")
  3019         (* interpretation * interpretation -> interpretation *)
  2964         (* interpretation * interpretation -> interpretation *)
  3020         fun intersection (Node xs, Node ys) =
  2965         fun intersection (Node xs, Node ys) =
  3021           Node (map (fn (x, y) => if x=TT andalso y=TT then TT else FF)
  2966           Node (map (fn (x, y) => if x=TT andalso y=TT then TT else FF)
  3022             (xs ~~ ys))
  2967             (xs ~~ ys))
  3023           | intersection (_, _) =
  2968           | intersection (_, _) =
  3024           raise REFUTE ("Lfp_lfp_interpreter",
  2969           raise REFUTE ("lfp_interpreter",
  3025             "intersection: interpretation for set is not a node")
  2970             "intersection: interpretation for set is not a node")
  3026         (* interpretation -> interpretaion *)
  2971         (* interpretation -> interpretaion *)
  3027         fun lfp (Node resultsets) =
  2972         fun lfp (Node resultsets) =
  3028           foldl (fn ((set, resultset), acc) =>
  2973           foldl (fn ((set, resultset), acc) =>
  3029             if is_subset (resultset, set) then
  2974             if is_subset (resultset, set) then
  3030               intersection (acc, set)
  2975               intersection (acc, set)
  3031             else
  2976             else
  3032               acc) i_univ (i_sets ~~ resultsets)
  2977               acc) i_univ (i_sets ~~ resultsets)
  3033           | lfp _ =
  2978           | lfp _ =
  3034             raise REFUTE ("Lfp_lfp_interpreter",
  2979             raise REFUTE ("lfp_interpreter",
  3035               "lfp: interpretation for function is not a node")
  2980               "lfp: interpretation for function is not a node")
  3036       in
  2981       in
  3037         SOME (Node (map lfp i_funs), model, args)
  2982         SOME (Node (map lfp i_funs), model, args)
  3038       end
  2983       end
  3039     | _ =>
  2984     | _ =>
  3044 
  2989 
  3045   (* only an optimization: 'gfp' could in principle be interpreted with  *)
  2990   (* only an optimization: 'gfp' could in principle be interpreted with  *)
  3046   (* interpreters available already (using its definition), but the code *)
  2991   (* interpreters available already (using its definition), but the code *)
  3047   (* below is more efficient                                             *)
  2992   (* below is more efficient                                             *)
  3048 
  2993 
  3049   fun Gfp_gfp_interpreter thy model args t =
  2994   fun gfp_interpreter thy model args t =
  3050     case t of
  2995     case t of
  3051       Const ("Gfp.gfp", Type ("fun", [Type ("fun",
  2996       Const (@{const_name gfp}, Type ("fun", [Type ("fun",
  3052         [Type ("set", [T]), Type ("set", [_])]), Type ("set", [_])])) =>
  2997         [Type ("set", [T]), Type ("set", [_])]), Type ("set", [_])])) =>
  3053       let nonfix union (* because "union" is used below *)
  2998       let nonfix union (* because "union" is used below *)
  3054         val size_elem = size_of_type thy model T
  2999         val size_elem = size_of_type thy model T
  3055         (* the universe (i.e. the set that contains every element) *)
  3000         (* the universe (i.e. the set that contains every element) *)
  3056         val i_univ = Node (replicate size_elem TT)
  3001         val i_univ = Node (replicate size_elem TT)
  3063         (* interpretation * interpretation -> bool *)
  3008         (* interpretation * interpretation -> bool *)
  3064         fun is_subset (Node subs, Node sups) =
  3009         fun is_subset (Node subs, Node sups) =
  3065           List.all (fn (sub, sup) => (sub = FF) orelse (sup = TT))
  3010           List.all (fn (sub, sup) => (sub = FF) orelse (sup = TT))
  3066             (subs ~~ sups)
  3011             (subs ~~ sups)
  3067           | is_subset (_, _) =
  3012           | is_subset (_, _) =
  3068           raise REFUTE ("Gfp_gfp_interpreter",
  3013           raise REFUTE ("gfp_interpreter",
  3069             "is_subset: interpretation for set is not a node")
  3014             "is_subset: interpretation for set is not a node")
  3070         (* interpretation * interpretation -> interpretation *)
  3015         (* interpretation * interpretation -> interpretation *)
  3071         fun union (Node xs, Node ys) =
  3016         fun union (Node xs, Node ys) =
  3072             Node (map (fn (x,y) => if x=TT orelse y=TT then TT else FF)
  3017             Node (map (fn (x,y) => if x=TT orelse y=TT then TT else FF)
  3073                  (xs ~~ ys))
  3018                  (xs ~~ ys))
  3074           | union (_, _) =
  3019           | union (_, _) =
  3075           raise REFUTE ("Gfp_gfp_interpreter",
  3020           raise REFUTE ("gfp_interpreter",
  3076             "union: interpretation for set is not a node")
  3021             "union: interpretation for set is not a node")
  3077         (* interpretation -> interpretaion *)
  3022         (* interpretation -> interpretaion *)
  3078         fun gfp (Node resultsets) =
  3023         fun gfp (Node resultsets) =
  3079           foldl (fn ((set, resultset), acc) =>
  3024           foldl (fn ((set, resultset), acc) =>
  3080             if is_subset (set, resultset) then
  3025             if is_subset (set, resultset) then
  3081               union (acc, set)
  3026               union (acc, set)
  3082             else
  3027             else
  3083               acc) i_univ (i_sets ~~ resultsets)
  3028               acc) i_univ (i_sets ~~ resultsets)
  3084           | gfp _ =
  3029           | gfp _ =
  3085             raise REFUTE ("Gfp_gfp_interpreter",
  3030             raise REFUTE ("gfp_interpreter",
  3086               "gfp: interpretation for function is not a node")
  3031               "gfp: interpretation for function is not a node")
  3087       in
  3032       in
  3088         SOME (Node (map gfp i_funs), model, args)
  3033         SOME (Node (map gfp i_funs), model, args)
  3089       end
  3034       end
  3090     | _ =>
  3035     | _ =>
  3097   (* interpreters available already (using its definition), but the code *)
  3042   (* interpreters available already (using its definition), but the code *)
  3098   (* below is more efficient                                             *)
  3043   (* below is more efficient                                             *)
  3099 
  3044 
  3100   fun Product_Type_fst_interpreter thy model args t =
  3045   fun Product_Type_fst_interpreter thy model args t =
  3101     case t of
  3046     case t of
  3102       Const ("fst", Type ("fun", [Type ("*", [T, U]), _])) =>
  3047       Const ("Product_Type.fst", Type ("fun", [Type ("*", [T, U]), _])) =>
  3103       let
  3048       let
  3104         val constants_T = make_constants thy model T
  3049         val constants_T = make_constants thy model T
  3105         val size_U      = size_of_type thy model U
  3050         val size_U      = size_of_type thy model U
  3106       in
  3051       in
  3107         SOME (Node (List.concat (map (replicate size_U) constants_T)),
  3052         SOME (Node (List.concat (map (replicate size_U) constants_T)),
  3117   (* interpreters available already (using its definition), but the code *)
  3062   (* interpreters available already (using its definition), but the code *)
  3118   (* below is more efficient                                             *)
  3063   (* below is more efficient                                             *)
  3119 
  3064 
  3120   fun Product_Type_snd_interpreter thy model args t =
  3065   fun Product_Type_snd_interpreter thy model args t =
  3121     case t of
  3066     case t of
  3122       Const ("snd", Type ("fun", [Type ("*", [T, U]), _])) =>
  3067       Const ("Product_Type.snd", Type ("fun", [Type ("*", [T, U]), _])) =>
  3123       let
  3068       let
  3124         val size_T      = size_of_type thy model T
  3069         val size_T      = size_of_type thy model T
  3125         val constants_U = make_constants thy model U
  3070         val constants_U = make_constants thy model U
  3126       in
  3071       in
  3127         SOME (Node (List.concat (replicate size_T constants_U)), model, args)
  3072         SOME (Node (List.concat (replicate size_T constants_U)), model, args)
  3173           (constants ~~ results)
  3118           (constants ~~ results)
  3174         (* Term.typ *)
  3119         (* Term.typ *)
  3175         val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  3120         val HOLogic_prodT = HOLogic.mk_prodT (T1, T2)
  3176         val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  3121         val HOLogic_setT  = HOLogic.mk_setT HOLogic_prodT
  3177         (* Term.term *)
  3122         (* Term.term *)
  3178         val HOLogic_empty_set = Const ("{}", HOLogic_setT)
  3123         val HOLogic_empty_set = Const (@{const_name "{}"}, HOLogic_setT)
  3179         val HOLogic_insert    =
  3124         val HOLogic_insert    =
  3180           Const ("insert", HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  3125           Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  3181       in
  3126       in
  3182         SOME (foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  3127         SOME (foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  3183           HOLogic_empty_set pairs)
  3128           HOLogic_empty_set pairs)
  3184       end
  3129       end
  3185     | Type ("prop", [])      =>
  3130     | Type ("prop", [])      =>
  3203         SOME (Const (@{const_name undefined}, T))
  3148         SOME (Const (@{const_name undefined}, T))
  3204       else
  3149       else
  3205         SOME (Const (string_of_typ T ^
  3150         SOME (Const (string_of_typ T ^
  3206           string_of_int (index_from_interpretation intr), T))
  3151           string_of_int (index_from_interpretation intr), T))
  3207   end;
  3152   end;
  3208 
       
  3209   (* theory -> model -> Term.typ -> interpretation -> (int -> bool) ->
       
  3210     Term.term option *)
       
  3211 
       
  3212   fun set_printer thy model T intr assignment =
       
  3213     (case T of
       
  3214       Type ("set", [T1]) =>
       
  3215       let
       
  3216         (* create all constants of type 'T1' *)
       
  3217         val constants = make_constants thy model T1
       
  3218         (* interpretation list *)
       
  3219         val results = (case intr of
       
  3220             Node xs => xs
       
  3221           | _       => raise REFUTE ("set_printer",
       
  3222             "interpretation for set type is a leaf"))
       
  3223         (* Term.term list *)
       
  3224         val elements = List.mapPartial (fn (arg, result) =>
       
  3225           case result of
       
  3226             Leaf [fmTrue, fmFalse] =>
       
  3227             if PropLogic.eval assignment fmTrue then
       
  3228               SOME (print thy model T1 arg assignment)
       
  3229             else (* if PropLogic.eval assignment fmFalse then *)
       
  3230               NONE
       
  3231           | _ =>
       
  3232             raise REFUTE ("set_printer",
       
  3233               "illegal interpretation for a Boolean value"))
       
  3234           (constants ~~ results)
       
  3235         (* Term.typ *)
       
  3236         val HOLogic_setT1     = HOLogic.mk_setT T1
       
  3237         (* Term.term *)
       
  3238         val HOLogic_empty_set = Const ("{}", HOLogic_setT1)
       
  3239         val HOLogic_insert    =
       
  3240           Const ("insert", T1 --> HOLogic_setT1 --> HOLogic_setT1)
       
  3241       in
       
  3242         SOME (Library.foldl (fn (acc, elem) => HOLogic_insert $ elem $ acc)
       
  3243           (HOLogic_empty_set, elements))
       
  3244       end
       
  3245     | _ =>
       
  3246       NONE);
       
  3247 
  3153 
  3248   (* theory -> model -> Term.typ -> interpretation -> (int -> bool) ->
  3154   (* theory -> model -> Term.typ -> interpretation -> (int -> bool) ->
  3249     Term.term option *)
  3155     Term.term option *)
  3250 
  3156 
  3251   fun IDT_printer thy model T intr assignment =
  3157   fun IDT_printer thy model T intr assignment =
  3357 
  3263 
  3358   val setup =
  3264   val setup =
  3359      add_interpreter "stlc"    stlc_interpreter #>
  3265      add_interpreter "stlc"    stlc_interpreter #>
  3360      add_interpreter "Pure"    Pure_interpreter #>
  3266      add_interpreter "Pure"    Pure_interpreter #>
  3361      add_interpreter "HOLogic" HOLogic_interpreter #>
  3267      add_interpreter "HOLogic" HOLogic_interpreter #>
  3362      add_interpreter "set"     set_interpreter #>
       
  3363      add_interpreter "IDT"             IDT_interpreter #>
  3268      add_interpreter "IDT"             IDT_interpreter #>
  3364      add_interpreter "IDT_constructor" IDT_constructor_interpreter #>
  3269      add_interpreter "IDT_constructor" IDT_constructor_interpreter #>
  3365      add_interpreter "IDT_recursion"   IDT_recursion_interpreter #>
  3270      add_interpreter "IDT_recursion"   IDT_recursion_interpreter #>
  3366      add_interpreter "Finite_Set.card"    Finite_Set_card_interpreter #>
  3271      add_interpreter "Finite_Set.card"    Finite_Set_card_interpreter #>
  3367      add_interpreter "Finite_Set.Finites" Finite_Set_Finites_interpreter #>
       
  3368      add_interpreter "Finite_Set.finite"  Finite_Set_finite_interpreter #>
  3272      add_interpreter "Finite_Set.finite"  Finite_Set_finite_interpreter #>
  3369      add_interpreter "Nat_Orderings.less" Nat_less_interpreter #>
  3273      add_interpreter "Nat_Orderings.less" Nat_less_interpreter #>
  3370      add_interpreter "Nat_HOL.plus"       Nat_plus_interpreter #>
  3274      add_interpreter "Nat_HOL.plus"       Nat_plus_interpreter #>
  3371      add_interpreter "Nat_HOL.minus"      Nat_minus_interpreter #>
  3275      add_interpreter "Nat_HOL.minus"      Nat_minus_interpreter #>
  3372      add_interpreter "Nat_HOL.times"      Nat_times_interpreter #>
  3276      add_interpreter "Nat_HOL.times"      Nat_times_interpreter #>
  3373      add_interpreter "List.append" List_append_interpreter #>
  3277      add_interpreter "List.append" List_append_interpreter #>
  3374      add_interpreter "Lfp.lfp" Lfp_lfp_interpreter #>
  3278      add_interpreter "lfp" lfp_interpreter #>
  3375      add_interpreter "Gfp.gfp" Gfp_gfp_interpreter #>
  3279      add_interpreter "gfp" gfp_interpreter #>
  3376      add_interpreter "fst" Product_Type_fst_interpreter #>
  3280      add_interpreter "fst" Product_Type_fst_interpreter #>
  3377      add_interpreter "snd" Product_Type_snd_interpreter #>
  3281      add_interpreter "snd" Product_Type_snd_interpreter #>
  3378      add_printer "stlc" stlc_printer #>
  3282      add_printer "stlc" stlc_printer #>
  3379      add_printer "set"  set_printer #>
       
  3380      add_printer "IDT"  IDT_printer;
  3283      add_printer "IDT"  IDT_printer;
  3381 
  3284 
  3382 end  (* structure Refute *)
  3285 end  (* structure Refute *)