src/HOL/Tools/refute.ML
changeset 33339 d41f77196338
parent 33317 b4534348b8fd
child 33522 737589bb9bb8
equal deleted inserted replaced
33338:de76079f973a 33339:d41f77196338
   952                 ^ Syntax.string_of_typ_global thy T ^ ") is not a variable")
   952                 ^ Syntax.string_of_typ_global thy T ^ ") is not a variable")
   953             else ()
   953             else ()
   954             (* required for mutually recursive datatypes; those need to   *)
   954             (* required for mutually recursive datatypes; those need to   *)
   955             (* be added even if they are an instance of an otherwise non- *)
   955             (* be added even if they are an instance of an otherwise non- *)
   956             (* recursive datatype                                         *)
   956             (* recursive datatype                                         *)
   957             fun collect_dtyp (d, acc) =
   957             fun collect_dtyp d acc =
   958             let
   958             let
   959               val dT = typ_of_dtyp descr typ_assoc d
   959               val dT = typ_of_dtyp descr typ_assoc d
   960             in
   960             in
   961               case d of
   961               case d of
   962                 DatatypeAux.DtTFree _ =>
   962                 DatatypeAux.DtTFree _ =>
   963                 collect_types dT acc
   963                 collect_types dT acc
   964               | DatatypeAux.DtType (_, ds) =>
   964               | DatatypeAux.DtType (_, ds) =>
   965                 collect_types dT (List.foldr collect_dtyp acc ds)
   965                 collect_types dT (fold_rev collect_dtyp ds acc)
   966               | DatatypeAux.DtRec i =>
   966               | DatatypeAux.DtRec i =>
   967                 if dT mem acc then
   967                 if dT mem acc then
   968                   acc  (* prevent infinite recursion *)
   968                   acc  (* prevent infinite recursion *)
   969                 else
   969                 else
   970                   let
   970                   let
   974                     val acc_dT = if Library.exists (fn (_, ds) =>
   974                     val acc_dT = if Library.exists (fn (_, ds) =>
   975                       Library.exists DatatypeAux.is_rec_type ds) dconstrs then
   975                       Library.exists DatatypeAux.is_rec_type ds) dconstrs then
   976                         insert (op =) dT acc
   976                         insert (op =) dT acc
   977                       else acc
   977                       else acc
   978                     (* collect argument types *)
   978                     (* collect argument types *)
   979                     val acc_dtyps = List.foldr collect_dtyp acc_dT dtyps
   979                     val acc_dtyps = fold_rev collect_dtyp dtyps acc_dT
   980                     (* collect constructor types *)
   980                     (* collect constructor types *)
   981                     val acc_dconstrs = List.foldr collect_dtyp acc_dtyps (maps snd dconstrs)
   981                     val acc_dconstrs = fold_rev collect_dtyp (maps snd dconstrs) acc_dtyps
   982                   in
   982                   in
   983                     acc_dconstrs
   983                     acc_dconstrs
   984                   end
   984                   end
   985             end
   985             end
   986           in
   986           in
   987             (* argument types 'Ts' could be added here, but they are also *)
   987             (* argument types 'Ts' could be added here, but they are also *)
   988             (* added by 'collect_dtyp' automatically                      *)
   988             (* added by 'collect_dtyp' automatically                      *)
   989             collect_dtyp (DatatypeAux.DtRec index, acc)
   989             collect_dtyp (DatatypeAux.DtRec index) acc
   990           end
   990           end
   991         | NONE =>
   991         | NONE =>
   992           (* not an inductive datatype, e.g. defined via "typedef" or *)
   992           (* not an inductive datatype, e.g. defined via "typedef" or *)
   993           (* "typedecl"                                               *)
   993           (* "typedecl"                                               *)
   994           insert (op =) T (fold collect_types Ts acc))
   994           insert (op =) T (fold collect_types Ts acc))
  1594   fun eta_expand t i =
  1594   fun eta_expand t i =
  1595   let
  1595   let
  1596     val Ts = Term.binder_types (Term.fastype_of t)
  1596     val Ts = Term.binder_types (Term.fastype_of t)
  1597     val t' = Term.incr_boundvars i t
  1597     val t' = Term.incr_boundvars i t
  1598   in
  1598   in
  1599     List.foldr (fn (T, term) => Abs ("<eta_expand>", T, term))
  1599     fold_rev (fn T => fn term => Abs ("<eta_expand>", T, term))
  1600       (Term.list_comb (t', map Bound (i-1 downto 0))) (List.take (Ts, i))
  1600       (List.take (Ts, i))
       
  1601       (Term.list_comb (t', map Bound (i-1 downto 0)))
  1601   end;
  1602   end;
  1602 
  1603 
  1603 (* ------------------------------------------------------------------------- *)
  1604 (* ------------------------------------------------------------------------- *)
  1604 (* size_of_dtyp: the size of (an initial fragment of) an inductive data type *)
  1605 (* size_of_dtyp: the size of (an initial fragment of) an inductive data type *)
  1605 (*               is the sum (over its constructors) of the product (over     *)
  1606 (*               is the sum (over its constructors) of the product (over     *)
  2056           val HOLogic_empty_set = HOLogic.mk_set HOLogic_prodT []
  2057           val HOLogic_empty_set = HOLogic.mk_set HOLogic_prodT []
  2057           val HOLogic_insert    =
  2058           val HOLogic_insert    =
  2058             Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  2059             Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  2059         in
  2060         in
  2060           (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
  2061           (* functions as graphs, i.e. as a (HOL) set of pairs "(x, y)" *)
  2061           map (List.foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  2062           map (fn ps => fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) ps
  2062             HOLogic_empty_set) pairss
  2063             HOLogic_empty_set) pairss
  2063         end
  2064         end
  2064       | Type (s, Ts) =>
  2065       | Type (s, Ts) =>
  2065         (case Datatype.get_info thy s of
  2066         (case Datatype.get_info thy s of
  2066           SOME info =>
  2067           SOME info =>
  2588                             "non-recursive codomain in recursive dtyp")
  2589                             "non-recursive codomain in recursive dtyp")
  2589                         (* obtain interpretations for recursive arguments *)
  2590                         (* obtain interpretations for recursive arguments *)
  2590                         (* interpretation list *)
  2591                         (* interpretation list *)
  2591                         val arg_intrs = map (uncurry rec_intr) rec_dtyps_intrs
  2592                         val arg_intrs = map (uncurry rec_intr) rec_dtyps_intrs
  2592                         (* apply 'intr' to all recursive arguments *)
  2593                         (* apply 'intr' to all recursive arguments *)
  2593                         val result = List.foldl (fn (arg_i, i) =>
  2594                         val result = fold (fn arg_i => fn i =>
  2594                           interpretation_apply (i, arg_i)) intr arg_intrs
  2595                           interpretation_apply (i, arg_i)) arg_intrs intr
  2595                         (* update 'REC_OPERATORS' *)
  2596                         (* update 'REC_OPERATORS' *)
  2596                         val _ = Array.update (arr, elem, (true, result))
  2597                         val _ = Array.update (arr, elem, (true, result))
  2597                       in
  2598                       in
  2598                         result
  2599                         result
  2599                       end
  2600                       end
  2968           | intersection (_, _) =
  2969           | intersection (_, _) =
  2969           raise REFUTE ("lfp_interpreter",
  2970           raise REFUTE ("lfp_interpreter",
  2970             "intersection: interpretation for set is not a node")
  2971             "intersection: interpretation for set is not a node")
  2971         (* interpretation -> interpretaion *)
  2972         (* interpretation -> interpretaion *)
  2972         fun lfp (Node resultsets) =
  2973         fun lfp (Node resultsets) =
  2973           List.foldl (fn ((set, resultset), acc) =>
  2974           fold (fn (set, resultset) => fn acc =>
  2974             if is_subset (resultset, set) then
  2975             if is_subset (resultset, set) then
  2975               intersection (acc, set)
  2976               intersection (acc, set)
  2976             else
  2977             else
  2977               acc) i_univ (i_sets ~~ resultsets)
  2978               acc) (i_sets ~~ resultsets) i_univ
  2978           | lfp _ =
  2979           | lfp _ =
  2979             raise REFUTE ("lfp_interpreter",
  2980             raise REFUTE ("lfp_interpreter",
  2980               "lfp: interpretation for function is not a node")
  2981               "lfp: interpretation for function is not a node")
  2981       in
  2982       in
  2982         SOME (Node (map lfp i_funs), model, args)
  2983         SOME (Node (map lfp i_funs), model, args)
  3023           | union (_, _) =
  3024           | union (_, _) =
  3024           raise REFUTE ("gfp_interpreter",
  3025           raise REFUTE ("gfp_interpreter",
  3025             "union: interpretation for set is not a node")
  3026             "union: interpretation for set is not a node")
  3026         (* interpretation -> interpretaion *)
  3027         (* interpretation -> interpretaion *)
  3027         fun gfp (Node resultsets) =
  3028         fun gfp (Node resultsets) =
  3028           List.foldl (fn ((set, resultset), acc) =>
  3029           fold (fn (set, resultset) => fn acc =>
  3029             if is_subset (set, resultset) then
  3030             if is_subset (set, resultset) then
  3030               union (acc, set)
  3031               union (acc, set)
  3031             else
  3032             else
  3032               acc) i_univ (i_sets ~~ resultsets)
  3033               acc) (i_sets ~~ resultsets) i_univ
  3033           | gfp _ =
  3034           | gfp _ =
  3034             raise REFUTE ("gfp_interpreter",
  3035             raise REFUTE ("gfp_interpreter",
  3035               "gfp: interpretation for function is not a node")
  3036               "gfp: interpretation for function is not a node")
  3036       in
  3037       in
  3037         SOME (Node (map gfp i_funs), model, args)
  3038         SOME (Node (map gfp i_funs), model, args)
  3125         (* Term.term *)
  3126         (* Term.term *)
  3126         val HOLogic_empty_set = HOLogic.mk_set HOLogic_prodT []
  3127         val HOLogic_empty_set = HOLogic.mk_set HOLogic_prodT []
  3127         val HOLogic_insert    =
  3128         val HOLogic_insert    =
  3128           Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  3129           Const (@{const_name insert}, HOLogic_prodT --> HOLogic_setT --> HOLogic_setT)
  3129       in
  3130       in
  3130         SOME (List.foldr (fn (pair, acc) => HOLogic_insert $ pair $ acc)
  3131         SOME (fold_rev (fn pair => fn acc => HOLogic_insert $ pair $ acc) pairs HOLogic_empty_set)
  3131           HOLogic_empty_set pairs)
       
  3132       end
  3132       end
  3133     | Type ("prop", [])      =>
  3133     | Type ("prop", [])      =>
  3134       (case index_from_interpretation intr of
  3134       (case index_from_interpretation intr of
  3135         ~1 => SOME (HOLogic.mk_Trueprop (Const (@{const_name undefined}, HOLogic.boolT)))
  3135         ~1 => SOME (HOLogic.mk_Trueprop (Const (@{const_name undefined}, HOLogic.boolT)))
  3136       | 0  => SOME (HOLogic.mk_Trueprop HOLogic.true_const)
  3136       | 0  => SOME (HOLogic.mk_Trueprop HOLogic.true_const)