src/HOL/ex/Predicate_Compile_Quickcheck_ex.thy
changeset 35324 c9f428269b38
parent 34948 2d5f2a9f7601
equal deleted inserted replaced
35309:997aa3a3e4bb 35324:c9f428269b38
     1 theory Predicate_Compile_Quickcheck_ex
     1 theory Predicate_Compile_Quickcheck_ex
     2 imports Predicate_Compile_Quickcheck
     2 imports Predicate_Compile_Quickcheck
     3   Predicate_Compile_Alternative_Defs
       
     4 begin
     3 begin
     5 
     4 
     6 ML {* Predicate_Compile_Alternative_Defs.get *}
       
     7 
       
     8 section {* Sets *}
     5 section {* Sets *}
     9 (*
     6 
    10 lemma "x \<in> {(1::nat)} ==> False"
     7 lemma "x \<in> {(1::nat)} ==> False"
    11 quickcheck[generator=predicate_compile, iterations=10]
     8 quickcheck[generator=predicate_compile_wo_ff, iterations=10]
    12 oops
     9 oops
    13 *)
    10 
    14 (* TODO: some error with doubled negation *)
       
    15 (*
       
    16 lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x \<noteq> Suc 0"
    11 lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x \<noteq> Suc 0"
    17 quickcheck[generator=predicate_compile]
    12 quickcheck[generator=predicate_compile_wo_ff]
    18 oops
    13 oops
    19 *)
    14 
    20 (*
       
    21 lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x = Suc 0"
    15 lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x = Suc 0"
    22 quickcheck[generator=predicate_compile]
    16 quickcheck[generator=predicate_compile_wo_ff]
    23 oops
    17 oops
    24 *) 
    18  
    25 lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x <= Suc 0"
    19 lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x <= Suc 0"
    26 (*quickcheck[generator=predicate_compile]*)
    20 quickcheck[generator=predicate_compile_wo_ff]
    27 oops
    21 oops
    28 
    22 
    29 section {* Numerals *}
    23 section {* Numerals *}
    30 (*
    24 
    31 lemma
    25 lemma
    32   "x \<in> {1, 2, (3::nat)} ==> x = 1 \<or> x = 2"
    26   "x \<in> {1, 2, (3::nat)} ==> x = 1 \<or> x = 2"
    33 quickcheck[generator=predicate_compile]
    27 quickcheck[generator=predicate_compile_wo_ff]
    34 oops
    28 oops
    35 *)
    29 
    36 lemma "x \<in> {1, 2, (3::nat)} ==> x < 3"
    30 lemma "x \<in> {1, 2, (3::nat)} ==> x < 3"
    37 (*quickcheck[generator=predicate_compile]*)
    31 quickcheck[generator=predicate_compile_wo_ff]
    38 oops
    32 oops
    39 
    33 
    40 lemma
    34 lemma
    41   "x \<in> {1, 2} \<union> {3, 4} ==> x = (1::nat) \<or> x = (2::nat)"
    35   "x \<in> {1, 2} \<union> {3, 4} ==> x = (1::nat) \<or> x = (2::nat)"
    42 (*quickcheck[generator=predicate_compile]*)
    36 quickcheck[generator=predicate_compile_wo_ff]
    43 oops
    37 oops
    44 
    38 
    45 section {* Context Free Grammar *}
    39 section {* Context Free Grammar *}
    46 
    40 
    47 datatype alphabet = a | b
    41 datatype alphabet = a | b
    51 | "w \<in> A\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
    45 | "w \<in> A\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
    52 | "w \<in> B\<^isub>1 \<Longrightarrow> a # w \<in> S\<^isub>1"
    46 | "w \<in> B\<^isub>1 \<Longrightarrow> a # w \<in> S\<^isub>1"
    53 | "w \<in> S\<^isub>1 \<Longrightarrow> a # w \<in> A\<^isub>1"
    47 | "w \<in> S\<^isub>1 \<Longrightarrow> a # w \<in> A\<^isub>1"
    54 | "w \<in> S\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
    48 | "w \<in> S\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
    55 | "\<lbrakk>v \<in> B\<^isub>1; v \<in> B\<^isub>1\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>1"
    49 | "\<lbrakk>v \<in> B\<^isub>1; v \<in> B\<^isub>1\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>1"
    56 (*
    50 
    57 code_pred [random_dseq inductify] "S\<^isub>1p" .
    51 lemma
    58 *)
    52   "w \<in> S\<^isub>1 \<Longrightarrow> w = []"
    59 (*thm B\<^isub>1p.random_dseq_equation*)
    53 quickcheck[generator = predicate_compile_ff_nofs, iterations=1]
    60 (*
       
    61 values [random_dseq 2, 2, 4] 10 "{x. S\<^isub>1p x}"
       
    62 values [random_dseq 1, 1, 5] 20 "{x. S\<^isub>1p x}"
       
    63 
       
    64 ML {* set ML_Context.trace *}
       
    65 *)
       
    66 ML {* set Toplevel.debug *}
       
    67 (*
       
    68 quickcheck[generator = predicate_compile, size = 10, iterations = 1]
       
    69 oops
       
    70 *)
       
    71 ML {* Spec_Rules.get *}
       
    72 ML {* Item_Net.retrieve *}
       
    73 local_setup {* Local_Theory.checkpoint *}
       
    74 ML {* Predicate_Compile_Data.get_specification @{theory} @{term "append"} *}
       
    75 lemma
       
    76   "w \<in> S\<^isub>1p \<Longrightarrow> w = []"
       
    77 quickcheck[generator = predicate_compile, iterations=1]
       
    78 oops
    54 oops
    79 
    55 
    80 theorem S\<^isub>1_sound:
    56 theorem S\<^isub>1_sound:
    81 "w \<in> S\<^isub>1p \<Longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
    57 "w \<in> S\<^isub>1 \<Longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
    82 quickcheck[generator=predicate_compile, size=15]
    58 quickcheck[generator=predicate_compile_ff_nofs, size=15]
    83 oops
    59 oops
    84 
    60 
    85 
    61 
    86 inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where
    62 inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where
    87   "[] \<in> S\<^isub>2"
    63   "[] \<in> S\<^isub>2"
    88 | "w \<in> A\<^isub>2 \<Longrightarrow> b # w \<in> S\<^isub>2"
    64 | "w \<in> A\<^isub>2 \<Longrightarrow> b # w \<in> S\<^isub>2"
    89 | "w \<in> B\<^isub>2 \<Longrightarrow> a # w \<in> S\<^isub>2"
    65 | "w \<in> B\<^isub>2 \<Longrightarrow> a # w \<in> S\<^isub>2"
    90 | "w \<in> S\<^isub>2 \<Longrightarrow> a # w \<in> A\<^isub>2"
    66 | "w \<in> S\<^isub>2 \<Longrightarrow> a # w \<in> A\<^isub>2"
    91 | "w \<in> S\<^isub>2 \<Longrightarrow> b # w \<in> B\<^isub>2"
    67 | "w \<in> S\<^isub>2 \<Longrightarrow> b # w \<in> B\<^isub>2"
    92 | "\<lbrakk>v \<in> B\<^isub>2; v \<in> B\<^isub>2\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>2"
    68 | "\<lbrakk>v \<in> B\<^isub>2; v \<in> B\<^isub>2\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>2"
    93 
    69 (*
    94 code_pred [random_dseq inductify] S\<^isub>2 .
    70 code_pred [random_dseq inductify] S\<^isub>2 .
    95 thm S\<^isub>2.random_dseq_equation
    71 thm S\<^isub>2.random_dseq_equation
    96 thm A\<^isub>2.random_dseq_equation
    72 thm A\<^isub>2.random_dseq_equation
    97 thm B\<^isub>2.random_dseq_equation
    73 thm B\<^isub>2.random_dseq_equation
    98 
    74 
   116 
    92 
   117 lemma
    93 lemma
   118 "w \<in> S\<^isub>2 ==> length [x \<leftarrow> w. x = a] <= Suc (Suc 0)"
    94 "w \<in> S\<^isub>2 ==> length [x \<leftarrow> w. x = a] <= Suc (Suc 0)"
   119 quickcheck[generator=predicate_compile, size = 10, iterations = 1]
    95 quickcheck[generator=predicate_compile, size = 10, iterations = 1]
   120 oops
    96 oops
   121 
    97 *)
   122 theorem S\<^isub>2_sound:
    98 theorem S\<^isub>2_sound:
   123 "w \<in> S\<^isub>2 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
    99 "w \<in> S\<^isub>2 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   124 quickcheck[generator=predicate_compile, size=15, iterations=1]
   100 quickcheck[generator=predicate_compile_ff_nofs, size=5, iterations=10]
   125 oops
   101 oops
   126 
   102 
   127 inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where
   103 inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where
   128   "[] \<in> S\<^isub>3"
   104   "[] \<in> S\<^isub>3"
   129 | "w \<in> A\<^isub>3 \<Longrightarrow> b # w \<in> S\<^isub>3"
   105 | "w \<in> A\<^isub>3 \<Longrightarrow> b # w \<in> S\<^isub>3"
   139 *)
   115 *)
   140 
   116 
   141 
   117 
   142 lemma S\<^isub>3_sound:
   118 lemma S\<^isub>3_sound:
   143 "w \<in> S\<^isub>3 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   119 "w \<in> S\<^isub>3 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   144 quickcheck[generator=predicate_compile, size=10, iterations=10]
   120 quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=10]
   145 oops
   121 oops
   146 
   122 
   147 lemma "\<not> (length w > 2) \<or> \<not> (length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b])"
   123 lemma "\<not> (length w > 2) \<or> \<not> (length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b])"
   148 quickcheck[size=10, generator = predicate_compile]
   124 quickcheck[size=10, generator = predicate_compile_ff_fs]
   149 oops
   125 oops
   150 
   126 
   151 theorem S\<^isub>3_complete:
   127 theorem S\<^isub>3_complete:
   152 "length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. b = x] \<longrightarrow> w \<in> S\<^isub>3"
   128 "length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. b = x] \<longrightarrow> w \<in> S\<^isub>3"
   153 (*quickcheck[generator=SML]*)
   129 (*quickcheck[generator=SML]*)
   154 quickcheck[generator=predicate_compile, size=10, iterations=100]
   130 quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=100]
   155 oops
   131 oops
   156 
   132 
   157 
   133 
   158 inductive_set S\<^isub>4 and A\<^isub>4 and B\<^isub>4 where
   134 inductive_set S\<^isub>4 and A\<^isub>4 and B\<^isub>4 where
   159   "[] \<in> S\<^isub>4"
   135   "[] \<in> S\<^isub>4"
   164 | "w \<in> S\<^isub>4 \<Longrightarrow> b # w \<in> B\<^isub>4"
   140 | "w \<in> S\<^isub>4 \<Longrightarrow> b # w \<in> B\<^isub>4"
   165 | "\<lbrakk>v \<in> B\<^isub>4; w \<in> B\<^isub>4\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>4"
   141 | "\<lbrakk>v \<in> B\<^isub>4; w \<in> B\<^isub>4\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>4"
   166 
   142 
   167 theorem S\<^isub>4_sound:
   143 theorem S\<^isub>4_sound:
   168 "w \<in> S\<^isub>4 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   144 "w \<in> S\<^isub>4 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   169 quickcheck[generator = predicate_compile, size=5, iterations=1]
   145 quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1]
   170 oops
   146 oops
   171 
   147 
   172 theorem S\<^isub>4_complete:
   148 theorem S\<^isub>4_complete:
   173 "length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] \<longrightarrow> w \<in> S\<^isub>4"
   149 "length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] \<longrightarrow> w \<in> S\<^isub>4"
   174 quickcheck[generator = predicate_compile, size=5, iterations=1]
   150 quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1]
   175 oops
   151 oops
   176 
   152 
   177 hide const b
   153 hide const a b
   178 
   154 
   179 subsection {* Lexicographic order *}
   155 subsection {* Lexicographic order *}
       
   156 (* TODO *)
       
   157 (*
   180 lemma
   158 lemma
   181   "(u, v) : lexord r ==> (x @ u, y @ v) : lexord r"
   159   "(u, v) : lexord r ==> (x @ u, y @ v) : lexord r"
   182 
   160 oops
       
   161 *)
   183 subsection {* IMP *}
   162 subsection {* IMP *}
   184 
   163 
   185 types
   164 types
   186   var = nat
   165   var = nat
   187   state = "int list"
   166   state = "int list"
   206 
   185 
   207 values [random_dseq 1, 2, 3] 10 "{(c, s, s'). exec c s s'}"
   186 values [random_dseq 1, 2, 3] 10 "{(c, s, s'). exec c s s'}"
   208 
   187 
   209 lemma
   188 lemma
   210   "exec c s s' ==> exec (Seq c c) s s'"
   189   "exec c s s' ==> exec (Seq c c) s s'"
   211 quickcheck[generator = predicate_compile, size=3, iterations=1]
   190 (*quickcheck[generator = predicate_compile_wo_ff, size=2, iterations=10]*)
   212 oops
   191 oops
   213 
   192 
   214 subsection {* Lambda *}
   193 subsection {* Lambda *}
   215 
   194 
   216 datatype type =
   195 datatype type =
   261   | appR [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
   240   | appR [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
   262   | abs [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> Abs T s \<rightarrow>\<^sub>\<beta> Abs T t"
   241   | abs [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> Abs T s \<rightarrow>\<^sub>\<beta> Abs T t"
   263 
   242 
   264 lemma
   243 lemma
   265   "\<Gamma> \<turnstile> t : U \<Longrightarrow> t \<rightarrow>\<^sub>\<beta> t' \<Longrightarrow> \<Gamma> \<turnstile> t' : U"
   244   "\<Gamma> \<turnstile> t : U \<Longrightarrow> t \<rightarrow>\<^sub>\<beta> t' \<Longrightarrow> \<Gamma> \<turnstile> t' : U"
   266 quickcheck[generator = predicate_compile, size = 7, iterations = 10]
   245 quickcheck[generator = predicate_compile_ff_fs, size = 7, iterations = 10]
   267 oops
   246 oops
   268 
       
   269 (*
       
   270 code_pred (expected_modes: i => i => o => bool, i => i => i => bool) typing .
       
   271 thm typing.equation
       
   272 
       
   273 code_pred (modes: i => i => bool,  i => o => bool as reduce') beta .
       
   274 thm beta.equation
       
   275 
       
   276 values "{x. App (Abs (Atom 0) (Var 0)) (Var 1) \<rightarrow>\<^sub>\<beta> x}"
       
   277 
       
   278 definition "reduce t = Predicate.the (reduce' t)"
       
   279 
       
   280 value "reduce (App (Abs (Atom 0) (Var 0)) (Var 1))"
       
   281 
       
   282 code_pred [random] typing .
       
   283 code_pred [random_dseq] typing .
       
   284 
       
   285 (*values [random] 1 "{(\<Gamma>, t, T). \<Gamma> \<turnstile> t : T}"
       
   286 *)*)
       
   287 
   247 
   288 subsection {* JAD *}
   248 subsection {* JAD *}
   289 
   249 
   290 definition matrix :: "('a :: semiring_0) list list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
   250 definition matrix :: "('a :: semiring_0) list list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
   291   "matrix M rs cs \<longleftrightarrow> (\<forall> row \<in> set M. length row = cs) \<and> length M = rs"
   251   "matrix M rs cs \<longleftrightarrow> (\<forall> row \<in> set M. length row = cs) \<and> length M = rs"
   298 values [random_dseq 3, 2] 10 "{(M, rs, cs). matrix (M:: int list list) rs cs}"
   258 values [random_dseq 3, 2] 10 "{(M, rs, cs). matrix (M:: int list list) rs cs}"
   299 *)
   259 *)
   300 lemma [code_pred_intro]:
   260 lemma [code_pred_intro]:
   301   "matrix [] 0 m"
   261   "matrix [] 0 m"
   302   "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m"
   262   "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m"
   303 sorry
   263 proof -
   304 
   264   show "matrix [] 0 m" unfolding matrix_def by auto
   305 code_pred [random_dseq inductify] matrix sorry
   265 next
       
   266   show "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m"
       
   267     unfolding matrix_def by auto
       
   268 qed
       
   269 
       
   270 code_pred [random_dseq inductify] matrix
       
   271   apply (cases x)
       
   272   unfolding matrix_def apply fastsimp
       
   273   apply fastsimp done
   306 
   274 
   307 
   275 
   308 values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}"
   276 values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}"
   309 
   277 
   310 definition "scalar_product v w = (\<Sum> (x, y)\<leftarrow>zip v w. x * y)"
   278 definition "scalar_product v w = (\<Sum> (x, y)\<leftarrow>zip v w. x * y)"
   342   "sort f [] = []" |
   310   "sort f [] = []" |
   343   "sort f (x # xs) = insert f x (sort f xs)"
   311   "sort f (x # xs) = insert f x (sort f xs)"
   344 
   312 
   345 definition
   313 definition
   346   "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)"
   314   "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)"
   347 
   315 (*
   348 definition
   316 definition
   349   "transpose M = [map (\<lambda> xs. xs ! i) (takeWhile (\<lambda> xs. i < length xs) M). i \<leftarrow> [0 ..< length (M ! 0)]]"
   317   "transpose M = [map (\<lambda> xs. xs ! i) (takeWhile (\<lambda> xs. i < length xs) M). i \<leftarrow> [0 ..< length (M ! 0)]]"
   350 
   318 *)
   351 definition
   319 definition
   352   "inflate upds = foldr (\<lambda> (i, x) upds. upds[i := x]) upds (replicate (length upds) 0)"
   320   "inflate upds = foldr (\<lambda> (i, x) upds. upds[i := x]) upds (replicate (length upds) 0)"
   353 
   321 
   354 definition
   322 definition
   355   "jad = apsnd transpose o length_permutate o map sparsify"
   323   "jad = apsnd transpose o length_permutate o map sparsify"
   356 
   324 
   357 definition
   325 definition
   358   "jad_mv v = inflate o split zip o apsnd (map listsum o transpose o map (map (\<lambda> (i, x). v ! i * x)))"
   326   "jad_mv v = inflate o split zip o apsnd (map listsum o transpose o map (map (\<lambda> (i, x). v ! i * x)))"
   359 ML {* ML_Context.trace := false *}
       
   360 
   327 
   361 lemma "matrix (M::int list list) rs cs \<Longrightarrow> False"
   328 lemma "matrix (M::int list list) rs cs \<Longrightarrow> False"
   362 quickcheck[generator = predicate_compile, size = 6]
   329 quickcheck[generator = predicate_compile_ff_nofs, size = 6]
   363 oops
   330 oops
   364 
   331 
   365 lemma
   332 lemma
   366   "\<lbrakk> matrix M rs cs ; length v = cs \<rbrakk> \<Longrightarrow> jad_mv v (jad M) = mv M v"
   333   "\<lbrakk> matrix M rs cs ; length v = cs \<rbrakk> \<Longrightarrow> jad_mv v (jad M) = mv M v"
   367 (*quickcheck[generator = predicate_compile]*)
   334 quickcheck[generator = predicate_compile_wo_ff]
   368 oops
   335 oops
   369 
   336 
   370 end
   337 end