src/HOL/Nitpick_Examples/Mono_Nits.thy
author blanchet
Fri Dec 20 09:48:04 2013 +0100 (2013-12-20)
changeset 54833 68c8f88af87e
parent 51272 9c8d63b4b6be
child 55888 cac1add157e8
permissions -rw-r--r--
compile
     1 (*  Title:      HOL/Nitpick_Examples/Mono_Nits.thy
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2009-2011
     4 
     5 Examples featuring Nitpick's monotonicity check.
     6 *)
     7 
     8 header {* Examples Featuring Nitpick's Monotonicity Check *}
     9 
    10 theory Mono_Nits
    11 imports Main
    12         (* "~/afp/thys/DPT-SAT-Solver/DPT_SAT_Solver" *)
    13         (* "~/afp/thys/AVL-Trees/AVL2" "~/afp/thys/Huffman/Huffman" *)
    14 begin
    15 
    16 ML {*
    17 open Nitpick_Util
    18 open Nitpick_HOL
    19 open Nitpick_Preproc
    20 
    21 exception BUG
    22 
    23 val thy = @{theory}
    24 val ctxt = @{context}
    25 val stds = [(NONE, true)]
    26 val subst = []
    27 val tac_timeout = seconds 1.0
    28 val case_names = case_const_names ctxt stds
    29 val defs = all_defs_of thy subst
    30 val nondefs = all_nondefs_of ctxt subst
    31 val def_tables = const_def_tables ctxt subst defs
    32 val nondef_table = const_nondef_table nondefs
    33 val simp_table = Unsynchronized.ref (const_simp_table ctxt subst)
    34 val psimp_table = const_psimp_table ctxt subst
    35 val choice_spec_table = const_choice_spec_table ctxt subst
    36 val intro_table = inductive_intro_table ctxt subst def_tables
    37 val ground_thm_table = ground_theorem_table thy
    38 val ersatz_table = ersatz_table ctxt
    39 val hol_ctxt as {thy, ...} : hol_context =
    40   {thy = thy, ctxt = ctxt, max_bisim_depth = ~1, boxes = [],
    41    stds = stds, wfs = [], user_axioms = NONE, debug = false,
    42    whacks = [], binary_ints = SOME false, destroy_constrs = true,
    43    specialize = false, star_linear_preds = false, total_consts = NONE,
    44    needs = NONE, tac_timeout = tac_timeout, evals = [], case_names = case_names,
    45    def_tables = def_tables, nondef_table = nondef_table, nondefs = nondefs,
    46    simp_table = simp_table, psimp_table = psimp_table,
    47    choice_spec_table = choice_spec_table, intro_table = intro_table,
    48    ground_thm_table = ground_thm_table, ersatz_table = ersatz_table,
    49    skolems = Unsynchronized.ref [], special_funs = Unsynchronized.ref [],
    50    unrolled_preds = Unsynchronized.ref [], wf_cache = Unsynchronized.ref [],
    51    constr_cache = Unsynchronized.ref []}
    52 val binarize = false
    53 
    54 fun is_mono t =
    55   Nitpick_Mono.formulas_monotonic hol_ctxt binarize @{typ 'a} ([t], [])
    56 
    57 fun is_const t =
    58   let val T = fastype_of t in
    59     Logic.mk_implies (Logic.mk_equals (Free ("dummyP", T), t), @{const False})
    60     |> is_mono
    61   end
    62 
    63 fun mono t = is_mono t orelse raise BUG
    64 fun nonmono t = not (is_mono t) orelse raise BUG
    65 fun const t = is_const t orelse raise BUG
    66 fun nonconst t = not (is_const t) orelse raise BUG
    67 *}
    68 
    69 ML {* Nitpick_Mono.trace := false *}
    70 
    71 ML_val {* const @{term "A\<Colon>('a\<Rightarrow>'b)"} *}
    72 ML_val {* const @{term "(A\<Colon>'a set) = A"} *}
    73 ML_val {* const @{term "(A\<Colon>'a set set) = A"} *}
    74 ML_val {* const @{term "(\<lambda>x\<Colon>'a set. a \<in> x)"} *}
    75 ML_val {* const @{term "{{a\<Colon>'a}} = C"} *}
    76 ML_val {* const @{term "{f\<Colon>'a\<Rightarrow>nat} = {g\<Colon>'a\<Rightarrow>nat}"} *}
    77 ML_val {* const @{term "A \<union> (B\<Colon>'a set)"} *}
    78 ML_val {* const @{term "\<lambda>A B x\<Colon>'a. A x \<or> B x"} *}
    79 ML_val {* const @{term "P (a\<Colon>'a)"} *}
    80 ML_val {* const @{term "\<lambda>a\<Colon>'a. b (c (d\<Colon>'a)) (e\<Colon>'a) (f\<Colon>'a)"} *}
    81 ML_val {* const @{term "\<forall>A\<Colon>'a set. a \<in> A"} *}
    82 ML_val {* const @{term "\<forall>A\<Colon>'a set. P A"} *}
    83 ML_val {* const @{term "P \<or> Q"} *}
    84 ML_val {* const @{term "A \<union> B = (C\<Colon>'a set)"} *}
    85 ML_val {* const @{term "(\<lambda>A B x\<Colon>'a. A x \<or> B x) A B = C"} *}
    86 ML_val {* const @{term "(if P then (A\<Colon>'a set) else B) = C"} *}
    87 ML_val {* const @{term "let A = (C\<Colon>'a set) in A \<union> B"} *}
    88 ML_val {* const @{term "THE x\<Colon>'b. P x"} *}
    89 ML_val {* const @{term "(\<lambda>x\<Colon>'a. False)"} *}
    90 ML_val {* const @{term "(\<lambda>x\<Colon>'a. True)"} *}
    91 ML_val {* const @{term "(\<lambda>x\<Colon>'a. False) = (\<lambda>x\<Colon>'a. False)"} *}
    92 ML_val {* const @{term "(\<lambda>x\<Colon>'a. True) = (\<lambda>x\<Colon>'a. True)"} *}
    93 ML_val {* const @{term "Let (a\<Colon>'a) A"} *}
    94 ML_val {* const @{term "A (a\<Colon>'a)"} *}
    95 ML_val {* const @{term "insert (a\<Colon>'a) A = B"} *}
    96 ML_val {* const @{term "- (A\<Colon>'a set)"} *}
    97 ML_val {* const @{term "finite (A\<Colon>'a set)"} *}
    98 ML_val {* const @{term "\<not> finite (A\<Colon>'a set)"} *}
    99 ML_val {* const @{term "finite (A\<Colon>'a set set)"} *}
   100 ML_val {* const @{term "\<lambda>a\<Colon>'a. A a \<and> \<not> B a"} *}
   101 ML_val {* const @{term "A < (B\<Colon>'a set)"} *}
   102 ML_val {* const @{term "A \<le> (B\<Colon>'a set)"} *}
   103 ML_val {* const @{term "[a\<Colon>'a]"} *}
   104 ML_val {* const @{term "[a\<Colon>'a set]"} *}
   105 ML_val {* const @{term "[A \<union> (B\<Colon>'a set)]"} *}
   106 ML_val {* const @{term "[A \<union> (B\<Colon>'a set)] = [C]"} *}
   107 ML_val {* const @{term "{(\<lambda>x\<Colon>'a. x = a)} = C"} *}
   108 ML_val {* const @{term "(\<lambda>a\<Colon>'a. \<not> A a) = B"} *}
   109 ML_val {* const @{prop "\<forall>F f g (h\<Colon>'a set). F f \<and> F g \<and> \<not> f a \<and> g a \<longrightarrow> \<not> f a"} *}
   110 ML_val {* const @{term "\<lambda>A B x\<Colon>'a. A x \<and> B x \<and> A = B"} *}
   111 ML_val {* const @{term "p = (\<lambda>(x\<Colon>'a) (y\<Colon>'a). P x \<or> \<not> Q y)"} *}
   112 ML_val {* const @{term "p = (\<lambda>(x\<Colon>'a) (y\<Colon>'a). p x y \<Colon> bool)"} *}
   113 ML_val {* const @{term "p = (\<lambda>A B x. A x \<and> \<not> B x) (\<lambda>x. True) (\<lambda>y. x \<noteq> y)"} *}
   114 ML_val {* const @{term "p = (\<lambda>y. x \<noteq> y)"} *}
   115 ML_val {* const @{term "(\<lambda>x. (p\<Colon>'a\<Rightarrow>bool\<Rightarrow>bool) x False)"} *}
   116 ML_val {* const @{term "(\<lambda>x y. (p\<Colon>'a\<Rightarrow>'a\<Rightarrow>bool\<Rightarrow>bool) x y False)"} *}
   117 ML_val {* const @{term "f = (\<lambda>x\<Colon>'a. P x \<longrightarrow> Q x)"} *}
   118 ML_val {* const @{term "\<forall>a\<Colon>'a. P a"} *}
   119 
   120 ML_val {* nonconst @{term "\<forall>P (a\<Colon>'a). P a"} *}
   121 ML_val {* nonconst @{term "THE x\<Colon>'a. P x"} *}
   122 ML_val {* nonconst @{term "SOME x\<Colon>'a. P x"} *}
   123 ML_val {* nonconst @{term "(\<lambda>A B x\<Colon>'a. A x \<or> B x) = myunion"} *}
   124 ML_val {* nonconst @{term "(\<lambda>x\<Colon>'a. False) = (\<lambda>x\<Colon>'a. True)"} *}
   125 ML_val {* nonconst @{prop "\<forall>F f g (h\<Colon>'a set). F f \<and> F g \<and> \<not> a \<in> f \<and> a \<in> g \<longrightarrow> F h"} *}
   126 
   127 ML_val {* mono @{prop "Q (\<forall>x\<Colon>'a set. P x)"} *}
   128 ML_val {* mono @{prop "P (a\<Colon>'a)"} *}
   129 ML_val {* mono @{prop "{a} = {b\<Colon>'a}"} *}
   130 ML_val {* mono @{prop "(\<lambda>x. x = a) = (\<lambda>y. y = (b\<Colon>'a))"} *}
   131 ML_val {* mono @{prop "(a\<Colon>'a) \<in> P \<and> P \<union> P = P"} *}
   132 ML_val {* mono @{prop "\<forall>F\<Colon>'a set set. P"} *}
   133 ML_val {* mono @{prop "\<not> (\<forall>F f g (h\<Colon>'a set). F f \<and> F g \<and> \<not> a \<in> f \<and> a \<in> g \<longrightarrow> F h)"} *}
   134 ML_val {* mono @{prop "\<not> Q (\<forall>x\<Colon>'a set. P x)"} *}
   135 ML_val {* mono @{prop "\<not> (\<forall>x\<Colon>'a. P x)"} *}
   136 ML_val {* mono @{prop "myall P = (P = (\<lambda>x\<Colon>'a. True))"} *}
   137 ML_val {* mono @{prop "myall P = (P = (\<lambda>x\<Colon>'a. False))"} *}
   138 ML_val {* mono @{prop "\<forall>x\<Colon>'a. P x"} *}
   139 ML_val {* mono @{term "(\<lambda>A B x\<Colon>'a. A x \<or> B x) \<noteq> myunion"} *}
   140 
   141 ML_val {* nonmono @{prop "A = (\<lambda>x::'a. True) \<and> A = (\<lambda>x. False)"} *}
   142 ML_val {* nonmono @{prop "\<forall>F f g (h\<Colon>'a set). F f \<and> F g \<and> \<not> a \<in> f \<and> a \<in> g \<longrightarrow> F h"} *}
   143 
   144 ML {*
   145 val preproc_timeout = seconds 5.0
   146 val mono_timeout = seconds 1.0
   147 
   148 fun all_unconcealed_theorems_of thy =
   149   let val facts = Global_Theory.facts_of thy in
   150     Facts.fold_static
   151         (fn (name, ths) =>
   152             if Facts.is_concealed facts name then I
   153             else append (map (`(Thm.get_name_hint)) ths))
   154         facts []
   155   end
   156 
   157 fun is_forbidden_theorem name =
   158   length (Long_Name.explode name) <> 2 orelse
   159   String.isPrefix "type_definition" (List.last (Long_Name.explode name)) orelse
   160   String.isPrefix "arity_" (List.last (Long_Name.explode name)) orelse
   161   String.isSuffix "_def" name orelse
   162   String.isSuffix "_raw" name
   163 
   164 fun theorems_of thy =
   165   filter (fn (name, th) =>
   166              not (is_forbidden_theorem name) andalso
   167              (theory_of_thm th, thy) |> pairself Context.theory_name |> op =)
   168          (all_unconcealed_theorems_of thy)
   169 
   170 fun check_formulas tsp =
   171   let
   172     fun is_type_actually_monotonic T =
   173       Nitpick_Mono.formulas_monotonic hol_ctxt binarize T tsp
   174     val free_Ts = fold Term.add_tfrees (op @ tsp) [] |> map TFree
   175     val (mono_free_Ts, nonmono_free_Ts) =
   176       TimeLimit.timeLimit mono_timeout
   177           (List.partition is_type_actually_monotonic) free_Ts
   178   in
   179     if not (null mono_free_Ts) then "MONO"
   180     else if not (null nonmono_free_Ts) then "NONMONO"
   181     else "NIX"
   182   end
   183   handle TimeLimit.TimeOut => "TIMEOUT"
   184        | NOT_SUPPORTED _ => "UNSUP"
   185        | exn => if Exn.is_interrupt exn then reraise exn else "UNKNOWN"
   186 
   187 fun check_theory thy =
   188   let
   189     val path = File.tmp_path (Context.theory_name thy ^ ".out" |> Path.explode)
   190     val _ = File.write path ""
   191     fun check_theorem (name, th) =
   192       let
   193         val t = th |> prop_of |> Type.legacy_freeze |> close_form
   194         val neg_t = Logic.mk_implies (t, @{prop False})
   195         val (nondef_ts, def_ts, _, _, _, _) =
   196           TimeLimit.timeLimit preproc_timeout (preprocess_formulas hol_ctxt [])
   197                               neg_t
   198         val res = name ^ ": " ^ check_formulas (nondef_ts, def_ts)
   199       in File.append path (res ^ "\n"); writeln res end
   200       handle TimeLimit.TimeOut => ()
   201   in thy |> theorems_of |> List.app check_theorem end
   202 *}
   203 
   204 (*
   205 ML_val {* check_theory @{theory AVL2} *}
   206 ML_val {* check_theory @{theory Fun} *}
   207 ML_val {* check_theory @{theory Huffman} *}
   208 ML_val {* check_theory @{theory List} *}
   209 ML_val {* check_theory @{theory Map} *}
   210 ML_val {* check_theory @{theory Relation} *}
   211 *)
   212 
   213 ML {* getenv "ISABELLE_TMP" *}
   214 
   215 end