src/HOL/ex/Predicate_Compile_ex.thy
changeset 33141 89b23fad5e02
parent 33140 83951822bfd0
child 33143 730a2e8a6ec6
equal deleted inserted replaced
33140:83951822bfd0 33141:89b23fad5e02
     5 inductive even :: "nat \<Rightarrow> bool" and odd :: "nat \<Rightarrow> bool" where
     5 inductive even :: "nat \<Rightarrow> bool" and odd :: "nat \<Rightarrow> bool" where
     6     "even 0"
     6     "even 0"
     7   | "even n \<Longrightarrow> odd (Suc n)"
     7   | "even n \<Longrightarrow> odd (Suc n)"
     8   | "odd n \<Longrightarrow> even (Suc n)"
     8   | "odd n \<Longrightarrow> even (Suc n)"
     9 
     9 
    10 code_pred (mode: [], [1]) [show_steps] even .
    10 code_pred (mode: [], [1]) even .
    11 code_pred [depth_limited, show_compilation] even .
    11 code_pred [depth_limited] even .
    12 (*code_pred [rpred] even .*)
    12 code_pred [rpred] even .
    13 
    13 
    14 thm odd.equation
    14 thm odd.equation
    15 thm even.equation
    15 thm even.equation
    16 thm odd.depth_limited_equation
    16 thm odd.depth_limited_equation
    17 thm even.depth_limited_equation
    17 thm even.depth_limited_equation
    18 (*
       
    19 thm even.rpred_equation
    18 thm even.rpred_equation
    20 thm odd.rpred_equation
    19 thm odd.rpred_equation
    21 *)
    20 
    22 (*
       
    23 lemma unit: "unit_case f = (\<lambda>x. f)"
       
    24 apply (rule ext)
       
    25 apply (case_tac x)
       
    26 apply (simp only: unit.cases)
       
    27 done
       
    28 
       
    29 lemma "even_1 (Suc (Suc n)) = even_1 n"
       
    30 thm even.equation(2)
       
    31 unfolding even.equation(1)[of "Suc (Suc n)"]
       
    32 unfolding odd.equation
       
    33 unfolding single_bind
       
    34 apply simp
       
    35 apply (simp add: unit)
       
    36 unfolding bind_single
       
    37 apply (rule refl)
       
    38 done
       
    39 *)
       
    40 values "{x. even 2}"
    21 values "{x. even 2}"
    41 values "{x. odd 2}"
    22 values "{x. odd 2}"
    42 values 10 "{n. even n}"
    23 values 10 "{n. even n}"
    43 values 10 "{n. odd n}"
    24 values 10 "{n. odd n}"
    44 values [depth_limit = 2] "{x. even 6}"
    25 values [depth_limit = 2] "{x. even 6}"
    45 values [depth_limit = 7] "{x. even 6}"
    26 values [depth_limit = 7] "{x. even 6}"
    46 values [depth_limit = 2] "{x. odd 7}"
    27 values [depth_limit = 2] "{x. odd 7}"
    47 values [depth_limit = 8] "{x. odd 7}"
    28 values [depth_limit = 8] "{x. odd 7}"
    48 
    29 
    49 values [depth_limit = 7] 10 "{n. even n}"
    30 values [depth_limit = 7] 10 "{n. even n}"
       
    31 
    50 definition odd' where "odd' x == \<not> even x"
    32 definition odd' where "odd' x == \<not> even x"
    51 
    33 
    52 code_pred [inductify] odd' .
    34 code_pred [inductify] odd' .
    53 code_pred [inductify, depth_limited] odd' .
    35 code_pred [inductify, depth_limited] odd' .
    54 (*code_pred [inductify, rpred] odd' .*)
    36 code_pred [inductify, rpred] odd' .
    55 
    37 
    56 thm odd'.depth_limited_equation
    38 thm odd'.depth_limited_equation
    57 values [depth_limit = 2] "{x. odd' 7}"
    39 values [depth_limit = 2] "{x. odd' 7}"
    58 values [depth_limit = 9] "{x. odd' 7}"
    40 values [depth_limit = 9] "{x. odd' 7}"
    59 
    41 
    60 definition even'' where "even'' = even"
       
    61 definition odd'' where "odd'' = odd"
       
    62 
       
    63 
       
    64 
       
    65 lemma [code_pred_intros]:
       
    66   "even'' 0"
       
    67   "odd'' x ==> even'' (Suc x)"
       
    68   "\<not> even'' x ==> odd'' x"
       
    69 apply (auto simp add: even''_def odd''_def intro: even_odd.intros)
       
    70 sorry
       
    71 
       
    72 code_pred odd'' sorry
       
    73 thm odd''.equation
       
    74 code_pred [depth_limited] odd'' sorry
       
    75 
       
    76 values "{x. odd'' 10}"
       
    77 values "{x. even'' 10}"
       
    78 values [depth_limit=5] "{x. odd'' 10}"
       
    79 
       
    80 inductive append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
    42 inductive append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> bool" where
    81     "append [] xs xs"
    43     "append [] xs xs"
    82   | "append xs ys zs \<Longrightarrow> append (x # xs) ys (x # zs)"
    44   | "append xs ys zs \<Longrightarrow> append (x # xs) ys (x # zs)"
    83 
    45 
    84 code_pred (mode: [1, 2], [3], [2, 3], [1, 3], [1, 2, 3]) [inductify] append .
    46 code_pred (mode: [1, 2], [3], [2, 3], [1, 3], [1, 2, 3]) [inductify] append .
    85 code_pred [depth_limited] append .
    47 code_pred [depth_limited] append .
    86 (*code_pred [rpred] append .*)
    48 code_pred [rpred] append .
    87 
    49 
    88 thm append.equation
    50 thm append.equation
    89 thm append.depth_limited_equation
    51 thm append.depth_limited_equation
    90 (*thm append.rpred_equation*)
    52 thm append.rpred_equation
    91 
    53 
    92 values "{(ys, xs). append xs ys [0, Suc 0, 2]}"
    54 values "{(ys, xs). append xs ys [0, Suc 0, 2]}"
    93 values "{zs. append [0, Suc 0, 2] [17, 8] zs}"
    55 values "{zs. append [0, Suc 0, 2] [17, 8] zs}"
    94 values "{ys. append [0, Suc 0, 2] ys [0, Suc 0, 2, 17, 0, 5]}"
    56 values "{ys. append [0, Suc 0, 2] ys [0, Suc 0, 2, 17, 0, 5]}"
    95 values [depth_limit = 3] "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
    57 values [depth_limit = 3] "{(xs, ys). append xs ys [1, 2, 3, 4, (5::nat)]}"
    96 (*values [depth_limit = 10 random] 15 "{(ys, zs). append [1::nat, 2] ys zs}"*)
    58 values [depth_limit = 10 random] 15 "{(ys, zs). append [1::nat, 2] ys zs}"
    97 
    59 
    98 value [code] "Predicate.the (append_1_2 [0::int, 1, 2] [3, 4, 5])"
    60 value [code] "Predicate.the (append_1_2 [0::int, 1, 2] [3, 4, 5])"
    99 value [code] "Predicate.the (append_3 ([]::int list))"
    61 value [code] "Predicate.the (append_3 ([]::int list))"
   100 
    62 
   101 subsection {* Tricky case with alternative rules *}
    63 subsection {* Tricky case with alternative rules *}
   129 where
    91 where
   130   "tupled_append ([], xs, xs)"
    92   "tupled_append ([], xs, xs)"
   131 | "tupled_append (xs, ys, zs) \<Longrightarrow> tupled_append (x # xs, ys, x # zs)"
    93 | "tupled_append (xs, ys, zs) \<Longrightarrow> tupled_append (x # xs, ys, x # zs)"
   132 
    94 
   133 code_pred tupled_append .
    95 code_pred tupled_append .
   134 (*code_pred [rpred] tupled_append .*)
    96 code_pred [rpred] tupled_append .
   135 thm tupled_append.equation
    97 thm tupled_append.equation
   136 (*
    98 (*
   137 TODO: values with tupled modes
    99 TODO: values with tupled modes
   138 values "{xs. tupled_append ([1,2,3], [4,5], xs)}"
   100 values "{xs. tupled_append ([1,2,3], [4,5], xs)}"
   139 *)
   101 *)
   162   "tupled_append''' ([], xs, xs)"
   124   "tupled_append''' ([], xs, xs)"
   163 | "yszs = (ys, zs) ==> tupled_append''' (xs, yszs) \<Longrightarrow> tupled_append''' (x # xs, ys, x # zs)"
   125 | "yszs = (ys, zs) ==> tupled_append''' (xs, yszs) \<Longrightarrow> tupled_append''' (x # xs, ys, x # zs)"
   164 
   126 
   165 code_pred [inductify] tupled_append''' .
   127 code_pred [inductify] tupled_append''' .
   166 thm tupled_append'''.equation
   128 thm tupled_append'''.equation
   167 (* TODO: Missing a few modes *)
       
   168 
   129 
   169 inductive map_ofP :: "('a \<times> 'b) list \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
   130 inductive map_ofP :: "('a \<times> 'b) list \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
   170 where
   131 where
   171   "map_ofP ((a, b)#xs) a b"
   132   "map_ofP ((a, b)#xs) a b"
   172 | "map_ofP xs a b \<Longrightarrow> map_ofP (x#xs) a b"
   133 | "map_ofP xs a b \<Longrightarrow> map_ofP (x#xs) a b"
   173 
   134 
   174 code_pred (mode: [1], [1, 2], [1, 2, 3], [1, 3]) map_ofP .
   135 code_pred (mode: [1], [1, 2], [1, 2, 3], [1, 3]) map_ofP .
   175 thm map_ofP.equation
   136 thm map_ofP.equation
       
   137 
   176 section {* reverse *}
   138 section {* reverse *}
   177 
   139 
   178 inductive rev where
   140 inductive rev where
   179     "rev [] []"
   141     "rev [] []"
   180   | "rev xs xs' ==> append xs' [x] ys ==> rev (x#xs) ys"
   142   | "rev xs xs' ==> append xs' [x] ys ==> rev (x#xs) ys"
   197     "partition f [] [] []"
   159     "partition f [] [] []"
   198   | "f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) (x # ys) zs"
   160   | "f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) (x # ys) zs"
   199   | "\<not> f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) ys (x # zs)"
   161   | "\<not> f x \<Longrightarrow> partition f xs ys zs \<Longrightarrow> partition f (x # xs) ys (x # zs)"
   200 
   162 
   201 code_pred (mode: [1], [2, 3], [1, 2], [1, 3], [1, 2, 3]) partition .
   163 code_pred (mode: [1], [2, 3], [1, 2], [1, 3], [1, 2, 3]) partition .
   202 (*code_pred [depth_limited] partition .*)
   164 code_pred [depth_limited] partition .
   203 (*thm partition.depth_limited_equation*)
   165 code_pred [rpred] partition .
   204 (*code_pred [rpred] partition .*)
       
   205 
   166 
   206 inductive tupled_partition :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<times> 'a list \<times> 'a list) \<Rightarrow> bool"
   167 inductive tupled_partition :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<times> 'a list \<times> 'a list) \<Rightarrow> bool"
   207   for f where
   168   for f where
   208    "tupled_partition f ([], [], [])"
   169    "tupled_partition f ([], [], [])"
   209   | "f x \<Longrightarrow> tupled_partition f (xs, ys, zs) \<Longrightarrow> tupled_partition f (x # xs, x # ys, zs)"
   170   | "f x \<Longrightarrow> tupled_partition f (xs, ys, zs) \<Longrightarrow> tupled_partition f (x # xs, x # ys, zs)"
   211 
   172 
   212 code_pred tupled_partition .
   173 code_pred tupled_partition .
   213 
   174 
   214 thm tupled_partition.equation
   175 thm tupled_partition.equation
   215 
   176 
   216 inductive member
   177 
   217 for xs
       
   218 where "x \<in> set xs ==> member xs x"
       
   219 
       
   220 lemma [code_pred_intros]:
       
   221   "member (x#xs') x"
       
   222 by (auto intro: member.intros)
       
   223 
       
   224 lemma [code_pred_intros]:
       
   225 "member xs x ==> member (x'#xs) x"
       
   226 by (auto intro: member.intros elim!: member.cases)
       
   227 (* strange bug must be repaired! *)
       
   228 (*
       
   229 code_pred member sorry
       
   230 *)
       
   231 inductive is_even :: "nat \<Rightarrow> bool"
   178 inductive is_even :: "nat \<Rightarrow> bool"
   232 where
   179 where
   233   "n mod 2 = 0 \<Longrightarrow> is_even n"
   180   "n mod 2 = 0 \<Longrightarrow> is_even n"
   234 
   181 
   235 code_pred is_even .
   182 code_pred is_even .
   248 proof -
   195 proof -
   249   case tranclp
   196   case tranclp
   250   from this converse_tranclpE[OF this(1)] show thesis by metis
   197   from this converse_tranclpE[OF this(1)] show thesis by metis
   251 qed
   198 qed
   252 
   199 
   253 (*code_pred [inductify, rpred] tranclp .*)
   200 code_pred [depth_limited] tranclp .
       
   201 code_pred [rpred] tranclp .
   254 thm tranclp.equation
   202 thm tranclp.equation
   255 (*thm tranclp.rpred_equation*)
   203 thm tranclp.rpred_equation
   256 
   204 
   257 inductive succ :: "nat \<Rightarrow> nat \<Rightarrow> bool" where
   205 inductive succ :: "nat \<Rightarrow> nat \<Rightarrow> bool" where
   258     "succ 0 1"
   206     "succ 0 1"
   259   | "succ m n \<Longrightarrow> succ (Suc m) (Suc n)"
   207   | "succ m n \<Longrightarrow> succ (Suc m) (Suc n)"
   260 
   208 
   261 code_pred succ .
   209 code_pred succ .
   262 (*code_pred [rpred] succ .*)
   210 code_pred [rpred] succ .
   263 thm succ.equation
   211 thm succ.equation
   264 (*thm succ.rpred_equation*)
   212 thm succ.rpred_equation
   265 
   213 
   266 values 10 "{(m, n). succ n m}"
   214 values 10 "{(m, n). succ n m}"
   267 values "{m. succ 0 m}"
   215 values "{m. succ 0 m}"
   268 values "{m. succ m 0}"
   216 values "{m. succ m 0}"
   269 
   217 
   370 where "Min s r x \<equiv> s x \<and> (\<forall>y. r x y \<longrightarrow> x = y)"
   318 where "Min s r x \<equiv> s x \<and> (\<forall>y. r x y \<longrightarrow> x = y)"
   371 
   319 
   372 code_pred [inductify] Min .
   320 code_pred [inductify] Min .
   373 
   321 
   374 subsection {* Examples with lists *}
   322 subsection {* Examples with lists *}
   375 (*
   323 
   376 inductive filterP for Pa where
   324 subsubsection {* Lexicographic order *}
   377 "(filterP::('a => bool) => 'a list => 'a list => bool) (Pa::'a => bool) [] []"
   325 
   378 | "[| (res::'a list) = (y::'a) # (resa::'a list); (filterP::('a => bool) => 'a list => 'a list => bool) (Pa::'a => bool) (xt::'a list) resa; Pa y |]
       
   379 ==> filterP Pa (y # xt) res"
       
   380 | "[| (filterP::('a => bool) => 'a list => 'a list => bool) (Pa::'a => bool) (xt::'a list) (res::'a list); ~ Pa (y::'a) |] ==> filterP Pa (y # xt) res"
       
   381 *)
       
   382 (*
       
   383 code_pred (inductify_all) (rpred) filterP .
       
   384 thm filterP.rpred_equation
       
   385 *)
       
   386 thm lexord_def
   326 thm lexord_def
   387 code_pred [inductify] lexord .
   327 code_pred [inductify] lexord .
   388 (*code_pred [inductify, rpred] lexord .*)
   328 code_pred [inductify, rpred] lexord .
   389 thm lexord.equation
   329 thm lexord.equation
       
   330 thm lexord.rpred_equation
       
   331 
   390 inductive less_than_nat :: "nat * nat => bool"
   332 inductive less_than_nat :: "nat * nat => bool"
   391 where
   333 where
   392   "less_than_nat (0, x)"
   334   "less_than_nat (0, x)"
   393 | "less_than_nat (x, y) ==> less_than_nat (Suc x, Suc y)"
   335 | "less_than_nat (x, y) ==> less_than_nat (Suc x, Suc y)"
   394  
   336  
   395 code_pred less_than_nat .
   337 code_pred less_than_nat .
       
   338 
   396 code_pred [depth_limited] less_than_nat .
   339 code_pred [depth_limited] less_than_nat .
   397 (*code_pred [rpred] less_than_nat .*)
   340 code_pred [rpred] less_than_nat .
   398 
   341 
   399 inductive test_lexord :: "nat list * nat list => bool"
   342 inductive test_lexord :: "nat list * nat list => bool"
   400 where
   343 where
   401   "lexord less_than_nat (xs, ys) ==> test_lexord (xs, ys)"
   344   "lexord less_than_nat (xs, ys) ==> test_lexord (xs, ys)"
   402 
   345 
   403 (*code_pred [rpred] test_lexord .*)
   346 code_pred [rpred] test_lexord .
   404 code_pred [depth_limited] test_lexord .
   347 code_pred [depth_limited] test_lexord .
   405 thm test_lexord.depth_limited_equation
   348 thm test_lexord.depth_limited_equation
   406 (*thm test_lexord.rpred_equation*)
   349 thm test_lexord.rpred_equation
   407 
   350 
   408 (*values [depth_limit = 5] "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"*)
   351 values "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"
   409 
   352 values [depth_limit = 5] "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"
   410 (*values [depth_limit = 25 random] "{xys. test_lexord xys}"*)
   353 values [depth_limit = 12 random] "{xys. test_lexord xys}"
   411 
   354 values [depth_limit = 5 random] "{xy. lexord less_than_nat xy}"
   412 (*values [depth_limit = 5 random] "{xy. lexord less_than_nat xy}"*)
   355 (*
   413 
       
   414 lemma "(u, v) : lexord less_than_nat ==> (x @ u, y @ v) : lexord less_than_nat"
   356 lemma "(u, v) : lexord less_than_nat ==> (x @ u, y @ v) : lexord less_than_nat"
   415 (*quickcheck[generator=pred_compile]*)
   357 quickcheck[generator=pred_compile]
   416 oops
   358 oops
   417 
   359 *)
   418 lemmas [code_pred_def] = lexn_conv lex_conv lenlex_conv
   360 lemmas [code_pred_def] = lexn_conv lex_conv lenlex_conv
   419 (*
   361 
   420 code_pred [inductify] lexn .
   362 code_pred [inductify] lexn .
   421 thm lexn.equation
   363 thm lexn.equation
   422 *)
   364 
   423 (*code_pred [inductify, rpred] lexn .*)
   365 code_pred [rpred] lexn .
   424 
   366 
   425 (*thm lexn.rpred_equation*)
   367 thm lexn.rpred_equation
   426 
   368 
   427 code_pred [inductify] lenlex .
   369 code_pred [inductify] lenlex .
   428 thm lenlex.equation
   370 thm lenlex.equation
   429 (*
   371 
   430 code_pred [inductify, rpred] lenlex .
   372 code_pred [inductify, rpred] lenlex .
   431 thm lenlex.rpred_equation
   373 thm lenlex.rpred_equation
   432 *)
   374 
   433 thm lists.intros
   375 thm lists.intros
   434 code_pred [inductify] lists .
   376 code_pred [inductify] lists .
   435 
   377 
   436 thm lists.equation
   378 thm lists.equation
   437 
   379 
   445 consts avl :: "'a tree => bool"
   387 consts avl :: "'a tree => bool"
   446 primrec
   388 primrec
   447   "avl ET = True"
   389   "avl ET = True"
   448   "avl (MKT x l r h) = ((height l = height r \<or> height l = 1 + height r \<or> height r = 1+height l) \<and> 
   390   "avl (MKT x l r h) = ((height l = height r \<or> height l = 1 + height r \<or> height r = 1+height l) \<and> 
   449   h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
   391   h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"
   450 (*
   392 
   451 code_pred [inductify] avl .
   393 code_pred [inductify] avl .
   452 thm avl.equation
   394 thm avl.equation
   453 *)
   395 
   454 (*code_pred [inductify, rpred] avl .
   396 code_pred [rpred] avl .
   455 thm avl.rpred_equation
   397 thm avl.rpred_equation
   456 values [depth_limit = 50 random] 10 "{t. avl (t::int tree)}"
   398 values [depth_limit = 50 random] 10 "{t. avl (t::int tree)}"
   457 *)(*
   399 
   458 lemma "avl t ==> t = ET"
       
   459 quickcheck[generator=code]
       
   460 quickcheck[generator = pred_compile]
       
   461 oops
       
   462 *)
       
   463 fun set_of
   400 fun set_of
   464 where
   401 where
   465 "set_of ET = {}"
   402 "set_of ET = {}"
   466 | "set_of (MKT n l r h) = insert n (set_of l \<union> set_of r)"
   403 | "set_of (MKT n l r h) = insert n (set_of l \<union> set_of r)"
   467 
   404 
   473 
   410 
   474 code_pred (mode: [1], [1, 2]) [inductify] set_of .
   411 code_pred (mode: [1], [1, 2]) [inductify] set_of .
   475 thm set_of.equation
   412 thm set_of.equation
   476 
   413 
   477 (* FIXME *)
   414 (* FIXME *)
   478 (*
   415 
   479 code_pred (inductify_all) is_ord .
   416 code_pred [inductify] is_ord .
   480 thm is_ord.equation
   417 thm is_ord.equation
   481 *)
   418 code_pred [rpred] is_ord .
       
   419 thm is_ord.rpred_equation
       
   420 
   482 section {* Definitions about Relations *}
   421 section {* Definitions about Relations *}
   483 
   422 
   484 code_pred [inductify] converse .
   423 code_pred [inductify] converse .
   485 thm converse.equation
   424 thm converse.equation
   486 
   425 
   489 
   428 
   490 section {* List functions *}
   429 section {* List functions *}
   491 
   430 
   492 code_pred [inductify] length .
   431 code_pred [inductify] length .
   493 thm size_listP.equation
   432 thm size_listP.equation
   494 (*
       
   495 code_pred [inductify, rpred] length .
   433 code_pred [inductify, rpred] length .
   496 thm size_listP.rpred_equation
   434 thm size_listP.rpred_equation
   497 values [depth_limit = 10 random] 20 "{xs. size_listP (xs::nat list) (5::nat)}"
   435 values [depth_limit = 10 random] 20 "{xs. size_listP (xs::nat list) (5::nat)}"
   498 *)
   436 
   499 code_pred [inductify] concat .
   437 code_pred [inductify] concat .
   500 thm concatP.equation
       
   501 
       
   502 code_pred [inductify] hd .
   438 code_pred [inductify] hd .
   503 code_pred [inductify] tl .
   439 code_pred [inductify] tl .
   504 code_pred [inductify] last .
   440 code_pred [inductify] last .
   505 code_pred [inductify] butlast .
   441 code_pred [inductify] butlast .
   506 thm listsum.simps
       
   507 (*code_pred [inductify] listsum .*)
   442 (*code_pred [inductify] listsum .*)
   508 code_pred [inductify] take .
   443 code_pred [inductify] take .
   509 code_pred [inductify] drop .
   444 code_pred [inductify] drop .
   510 code_pred [inductify] zip .
   445 code_pred [inductify] zip .
   511 code_pred [inductify] upt .
   446 code_pred [inductify] upt .
   518 code_pred [inductify] List.rev .
   453 code_pred [inductify] List.rev .
   519 code_pred [inductify] map .
   454 code_pred [inductify] map .
   520 code_pred [inductify] foldr .
   455 code_pred [inductify] foldr .
   521 code_pred [inductify] foldl .
   456 code_pred [inductify] foldl .
   522 code_pred [inductify] filter .
   457 code_pred [inductify] filter .
   523 (*
       
   524 code_pred [inductify, rpred] filter .
   458 code_pred [inductify, rpred] filter .
   525 thm filterP.equation
   459 thm filterP.rpred_equation
   526 *)
   460 
   527 definition test where "test xs = filter (\<lambda>x. x = (1::nat)) xs"
   461 definition test where "test xs = filter (\<lambda>x. x = (1::nat)) xs"
   528 
       
   529 (* TODO: implement higher-order replacement correctly *)
       
   530 code_pred [inductify] test .
   462 code_pred [inductify] test .
   531 thm testP.equation
   463 thm testP.equation
   532 (*
       
   533 code_pred [inductify, rpred] test .
   464 code_pred [inductify, rpred] test .
   534 *)
   465 thm testP.rpred_equation
   535 section {* Handling set operations *}
       
   536 
       
   537 
       
   538 
   466 
   539 section {* Context Free Grammar *}
   467 section {* Context Free Grammar *}
   540 
   468 
   541 datatype alphabet = a | b
   469 datatype alphabet = a | b
   542 
   470 
   547 | "w \<in> S\<^isub>1 \<Longrightarrow> a # w \<in> A\<^isub>1"
   475 | "w \<in> S\<^isub>1 \<Longrightarrow> a # w \<in> A\<^isub>1"
   548 | "w \<in> S\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
   476 | "w \<in> S\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
   549 | "\<lbrakk>v \<in> B\<^isub>1; v \<in> B\<^isub>1\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>1"
   477 | "\<lbrakk>v \<in> B\<^isub>1; v \<in> B\<^isub>1\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>1"
   550 
   478 
   551 code_pred [inductify] S\<^isub>1p .
   479 code_pred [inductify] S\<^isub>1p .
   552 
   480 code_pred [inductify, rpred] S\<^isub>1p .
   553 thm S\<^isub>1p.equation
   481 thm S\<^isub>1p.equation
   554 (*thm S\<^isub>1p.rpred_equation*)
   482 thm S\<^isub>1p.rpred_equation
   555 
   483 
   556 (*values [depth_limit = 5 random] "{x. S\<^isub>1p x}"*)
   484 values [depth_limit = 5 random] "{x. S\<^isub>1p x}"
   557 
   485 
   558 inductive is_a where
   486 inductive is_a where
   559 "is_a a"
   487   "is_a a"
   560 
   488 
   561 inductive is_b where
   489 inductive is_b where
   562   "is_b b"
   490   "is_b b"
   563 
   491 
   564 code_pred is_a .
   492 code_pred is_a .
   565 code_pred [depth_limited] is_a .
   493 code_pred [depth_limited] is_a .
   566 code_pred [rpred] is_a .
   494 code_pred [rpred] is_a .
   567 
   495 
   568 values [depth_limit=5 random] "{x. is_a x}"
   496 values [depth_limit=5 random] "{x. is_a x}"
       
   497 code_pred [depth_limited] is_b .
   569 code_pred [rpred] is_b .
   498 code_pred [rpred] is_b .
   570 
   499 
   571 (*code_pred [rpred] filterP .*)
   500 code_pred [inductify, depth_limited] filter .
   572 (*values [depth_limit=5 random] "{x. filterP is_a [a, b] x}"
   501 
   573 values [depth_limit=3 random] "{x. filterP is_b [a, b] x}"
   502 values [depth_limit=5] "{x. filterP is_a [a, b] x}"
   574 *)
   503 values [depth_limit=3] "{x. filterP is_b [a, b] x}"
   575 (*
   504 
   576 lemma "w \<in> S\<^isub>1 \<Longrightarrow> length (filter (\<lambda>x. x = a) w) = 1"
   505 lemma "w \<in> S\<^isub>1 \<Longrightarrow> length (filter (\<lambda>x. x = a) w) = 1"
   577 quickcheck[generator=pred_compile, size=10]
   506 quickcheck[generator=pred_compile, size=10]
   578 oops
   507 oops
   579 *)
   508 
   580 inductive test_lemma where
   509 inductive test_lemma where
   581   "S\<^isub>1p w ==> filterP is_a w r1 ==> size_listP r1 r2 ==> filterP is_b w r3 ==> size_listP r3 r4 ==> r2 \<noteq> r4 ==> test_lemma w"
   510   "S\<^isub>1p w ==> filterP is_a w r1 ==> size_listP r1 r2 ==> filterP is_b w r3 ==> size_listP r3 r4 ==> r2 \<noteq> r4 ==> test_lemma w"
   582 (*
   511 (*
   583 code_pred [rpred] test_lemma .
   512 code_pred [rpred] test_lemma .
   584 *)
   513 *)
   592 (*thm test_lemma'.rpred_equation*)
   521 (*thm test_lemma'.rpred_equation*)
   593 (*
   522 (*
   594 values [depth_limit=3 random] "{x. S\<^isub>1 x}"
   523 values [depth_limit=3 random] "{x. S\<^isub>1 x}"
   595 *)
   524 *)
   596 code_pred [depth_limited] is_b .
   525 code_pred [depth_limited] is_b .
   597 
   526 (*
   598 code_pred [inductify, depth_limited] filter .
   527 code_pred [inductify, depth_limited] filter .
   599 
   528 *)
   600 thm filterP.intros
   529 thm filterP.intros
   601 thm filterP.equation
   530 thm filterP.equation
   602 
   531 (*
   603 values [depth_limit=3] "{x. filterP is_b [a, b] x}"
   532 values [depth_limit=3] "{x. filterP is_b [a, b] x}"
   604 find_theorems "test_lemma'_hoaux"
   533 find_theorems "test_lemma'_hoaux"
   605 code_pred [depth_limited] test_lemma'_hoaux .
   534 code_pred [depth_limited] test_lemma'_hoaux .
   606 thm test_lemma'_hoaux.depth_limited_equation
   535 thm test_lemma'_hoaux.depth_limited_equation
   607 values [depth_limit=2] "{x. test_lemma'_hoaux b}"
   536 values [depth_limit=2] "{x. test_lemma'_hoaux b}"
   618 thm filterP.intros
   547 thm filterP.intros
   619 thm filterP.depth_limited_equation
   548 thm filterP.depth_limited_equation
   620 values [depth_limit=3] "{x. filterP test_lemma'_hoaux [a, b] x}"
   549 values [depth_limit=3] "{x. filterP test_lemma'_hoaux [a, b] x}"
   621 values [depth_limit=4 random] "{w. test_lemma w}"
   550 values [depth_limit=4 random] "{w. test_lemma w}"
   622 values [depth_limit=4 random] "{w. test_lemma' w}"
   551 values [depth_limit=4 random] "{w. test_lemma' w}"
   623 
   552 *)
       
   553 (*
   624 theorem S\<^isub>1_sound:
   554 theorem S\<^isub>1_sound:
   625 "w \<in> S\<^isub>1p \<Longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   555 "w \<in> S\<^isub>1p \<Longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   626 quickcheck[generator=pred_compile, size=5]
   556 quickcheck[generator=pred_compile, size=15]
   627 oops
   557 oops
   628 
   558 *)
   629 
       
   630 inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where
   559 inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where
   631   "[] \<in> S\<^isub>2"
   560   "[] \<in> S\<^isub>2"
   632 | "w \<in> A\<^isub>2 \<Longrightarrow> b # w \<in> S\<^isub>2"
   561 | "w \<in> A\<^isub>2 \<Longrightarrow> b # w \<in> S\<^isub>2"
   633 | "w \<in> B\<^isub>2 \<Longrightarrow> a # w \<in> S\<^isub>2"
   562 | "w \<in> B\<^isub>2 \<Longrightarrow> a # w \<in> S\<^isub>2"
   634 | "w \<in> S\<^isub>2 \<Longrightarrow> a # w \<in> A\<^isub>2"
   563 | "w \<in> S\<^isub>2 \<Longrightarrow> a # w \<in> A\<^isub>2"
   638 code_pred [inductify, rpred] S\<^isub>2 .
   567 code_pred [inductify, rpred] S\<^isub>2 .
   639 thm S\<^isub>2.rpred_equation
   568 thm S\<^isub>2.rpred_equation
   640 thm A\<^isub>2.rpred_equation
   569 thm A\<^isub>2.rpred_equation
   641 thm B\<^isub>2.rpred_equation
   570 thm B\<^isub>2.rpred_equation
   642 
   571 
   643 values [depth_limit = 4 random] "{x. S\<^isub>2 x}"
   572 values [depth_limit = 15 random] "{x. S\<^isub>2 x}"
   644 
   573 
   645 theorem S\<^isub>2_sound:
   574 theorem S\<^isub>2_sound:
   646 "w \<in> S\<^isub>2 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   575 "w \<in> S\<^isub>2 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   647 (*quickcheck[generator=SML]*)
   576 (*quickcheck[generator=SML]*)
   648 quickcheck[generator=pred_compile, size=4, iterations=1]
   577 (*quickcheck[generator=pred_compile, size=15, iterations=1]*)
   649 oops
   578 oops
   650 
   579 
   651 inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where
   580 inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where
   652   "[] \<in> S\<^isub>3"
   581   "[] \<in> S\<^isub>3"
   653 | "w \<in> A\<^isub>3 \<Longrightarrow> b # w \<in> S\<^isub>3"
   582 | "w \<in> A\<^isub>3 \<Longrightarrow> b # w \<in> S\<^isub>3"
   658 
   587 
   659 code_pred [inductify] S\<^isub>3 .
   588 code_pred [inductify] S\<^isub>3 .
   660 thm S\<^isub>3.equation
   589 thm S\<^isub>3.equation
   661 
   590 
   662 values 10 "{x. S\<^isub>3 x}"
   591 values 10 "{x. S\<^isub>3 x}"
   663 
   592 (*
   664 theorem S\<^isub>3_sound:
   593 theorem S\<^isub>3_sound:
   665 "w \<in> S\<^isub>3 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   594 "w \<in> S\<^isub>3 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
   666 quickcheck[generator=pred_compile, size=10, iterations=1]
   595 quickcheck[generator=pred_compile, size=10, iterations=1]
   667 oops
   596 oops
   668 
   597 *)
   669 lemma "\<not> (length w > 2) \<or> \<not> (length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b])"
   598 lemma "\<not> (length w > 2) \<or> \<not> (length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b])"
   670 (*quickcheck[size=10, generator = pred_compile]*)
   599 (*quickcheck[size=10, generator = pred_compile]*)
   671 oops
   600 oops
   672 (*
   601 (*
   673 inductive test
   602 inductive test