src/HOL/Predicate_Compile_Examples/Predicate_Compile_Examples.thy
changeset 36055 537876d0fa62
parent 36040 fcd7bea01a93
child 36176 3fe7e97ccca8
equal deleted inserted replaced
36054:93d62439506c 36055:537876d0fa62
    13 values [expected "{}" pred] "{x. False'}"
    13 values [expected "{}" pred] "{x. False'}"
    14 values [expected "{}" dseq 1] "{x. False'}"
    14 values [expected "{}" dseq 1] "{x. False'}"
    15 values [expected "{}" random_dseq 1, 1, 1] "{x. False'}"
    15 values [expected "{}" random_dseq 1, 1, 1] "{x. False'}"
    16 
    16 
    17 value "False'"
    17 value "False'"
    18 
       
    19 
    18 
    20 inductive True' :: "bool"
    19 inductive True' :: "bool"
    21 where
    20 where
    22   "True ==> True'"
    21   "True ==> True'"
    23 
    22 
   839 thm test_lexord.random_dseq_equation
   838 thm test_lexord.random_dseq_equation
   840 
   839 
   841 values "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"
   840 values "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"
   842 (*values [depth_limited 5] "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"*)
   841 (*values [depth_limited 5] "{x. test_lexord ([1, 2, 3], [1, 2, 5])}"*)
   843 
   842 
   844 declare list.size(3,4)[code_pred_def]
       
   845 lemmas [code_pred_def] = lexn_conv lex_conv lenlex_conv
   843 lemmas [code_pred_def] = lexn_conv lex_conv lenlex_conv
   846 (*
   844 (*
   847 code_pred [inductify] lexn .
   845 code_pred [inductify] lexn .
   848 thm lexn.equation
   846 thm lexn.equation
   849 *)
   847 *)
   886     apply auto
   884     apply auto
   887     apply (rule_tac x="x # xys" in exI)
   885     apply (rule_tac x="x # xys" in exI)
   888     by auto
   886     by auto
   889 qed
   887 qed
   890 
   888 
   891 code_pred [random_dseq inductify] lexn
   889 code_pred [random_dseq] lexn
   892 proof -
   890 proof -
   893   fix r n xs ys
   891   fix r n xs ys
   894   assume 1: "lexn r n (xs, ys)"
   892   assume 1: "lexn r n (xs, ys)"
   895   assume 2: "\<And>r' i x xs' y ys'. r = r' ==> n = Suc i ==> (xs, ys) = (x # xs', y # ys') ==> has_length xs' i ==> has_length ys' i ==> r' (x, y) ==> thesis"
   893   assume 2: "\<And>r' i x xs' y ys'. r = r' ==> n = Suc i ==> (xs, ys) = (x # xs', y # ys') ==> has_length xs' i ==> has_length ys' i ==> r' (x, y) ==> thesis"
   896   assume 3: "\<And>r' i x xs' ys'. r = r' ==> n = Suc i ==> (xs, ys) = (x # xs', x # ys') ==> lexn r' i (xs', ys') ==> thesis"
   894   assume 3: "\<And>r' i x xs' ys'. r = r' ==> n = Suc i ==> (xs, ys) = (x # xs', x # ys') ==> lexn r' i (xs', ys') ==> thesis"
   901     apply auto
   899     apply auto
   902     apply fastsimp
   900     apply fastsimp
   903     apply fastsimp done
   901     apply fastsimp done
   904 qed
   902 qed
   905 
   903 
   906 
       
   907 values [random_dseq 1, 2, 5] 10 "{(n, xs, ys::int list). lexn (%(x, y). x <= y) n (xs, ys)}"
   904 values [random_dseq 1, 2, 5] 10 "{(n, xs, ys::int list). lexn (%(x, y). x <= y) n (xs, ys)}"
   908 thm lenlex_conv
   905 
   909 thm lex_conv
       
   910 declare list.size(3,4)[code_pred_def]
       
   911 (*code_pred [inductify, show_steps, show_intermediate_results] length .*)
       
   912 setup {* Predicate_Compile_Data.ignore_consts [@{const_name Orderings.top_class.top}] *}
       
   913 code_pred [inductify, skip_proof] lex .
   906 code_pred [inductify, skip_proof] lex .
   914 thm lex.equation
   907 thm lex.equation
   915 thm lex_def
   908 thm lex_def
   916 declare lenlex_conv[code_pred_def]
   909 declare lenlex_conv[code_pred_def]
   917 code_pred [inductify, skip_proof] lenlex .
   910 code_pred [inductify, skip_proof] lenlex .
   919 
   912 
   920 code_pred [random_dseq inductify] lenlex .
   913 code_pred [random_dseq inductify] lenlex .
   921 thm lenlex.random_dseq_equation
   914 thm lenlex.random_dseq_equation
   922 
   915 
   923 values [random_dseq 4, 2, 4] 100 "{(xs, ys::int list). lenlex (%(x, y). x <= y) (xs, ys)}"
   916 values [random_dseq 4, 2, 4] 100 "{(xs, ys::int list). lenlex (%(x, y). x <= y) (xs, ys)}"
       
   917 
   924 thm lists.intros
   918 thm lists.intros
   925 
       
   926 code_pred [inductify] lists .
   919 code_pred [inductify] lists .
   927 thm lists.equation
   920 thm lists.equation
   928 
   921 
   929 subsection {* AVL Tree *}
   922 subsection {* AVL Tree *}
   930 
   923 
   963 
   956 
   964 code_pred (expected_modes: i => bool) [inductify] is_ord .
   957 code_pred (expected_modes: i => bool) [inductify] is_ord .
   965 thm is_ord_aux.equation
   958 thm is_ord_aux.equation
   966 thm is_ord.equation
   959 thm is_ord.equation
   967 
   960 
   968 
       
   969 subsection {* Definitions about Relations *}
   961 subsection {* Definitions about Relations *}
       
   962 
   970 term "converse"
   963 term "converse"
   971 code_pred (modes:
   964 code_pred (modes:
   972   (i * i => bool) => i * i => bool,
   965   (i * i => bool) => i * i => bool,
   973   (i * o => bool) => o * i => bool,
   966   (i * o => bool) => o * i => bool,
   974   (i * o => bool) => i * i => bool,
   967   (i * o => bool) => i * i => bool,
  1028 code_pred [inductify] inv_image .
  1021 code_pred [inductify] inv_image .
  1029 thm inv_image.equation
  1022 thm inv_image.equation
  1030 
  1023 
  1031 subsection {* Inverting list functions *}
  1024 subsection {* Inverting list functions *}
  1032 
  1025 
  1033 (*code_pred [inductify] length .
  1026 code_pred [inductify] size_list .
  1034 code_pred [random inductify] length .
  1027 code_pred [new_random_dseq inductify] size_list .
  1035 thm size_listP.equation
  1028 thm size_listP.equation
  1036 thm size_listP.random_equation
  1029 thm size_listP.new_random_dseq_equation
  1037 *)
  1030 
  1038 (*values [random] 1 "{xs. size_listP (xs::nat list) (5::nat)}"*)
  1031 values [new_random_dseq 2,3,10] 3 "{xs. size_listP (xs::nat list) (5::nat)}"
  1039 
  1032 
  1040 code_pred (expected_modes: i => o => bool, o => i => bool, i => i => bool) [inductify, skip_proof] List.concat .
  1033 code_pred (expected_modes: i => o => bool, o => i => bool, i => i => bool) [inductify, skip_proof] List.concat .
  1041 thm concatP.equation
  1034 thm concatP.equation
  1042 
  1035 
  1043 values "{ys. concatP [[1, 2], [3, (4::int)]] ys}"
  1036 values "{ys. concatP [[1, 2], [3, (4::int)]] ys}"
  1173 | "w \<in> S\<^isub>4 \<Longrightarrow> b # w \<in> B\<^isub>4"
  1166 | "w \<in> S\<^isub>4 \<Longrightarrow> b # w \<in> B\<^isub>4"
  1174 | "\<lbrakk>v \<in> B\<^isub>4; w \<in> B\<^isub>4\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>4"
  1167 | "\<lbrakk>v \<in> B\<^isub>4; w \<in> B\<^isub>4\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>4"
  1175 
  1168 
  1176 code_pred (expected_modes: o => bool, i => bool) S\<^isub>4p .
  1169 code_pred (expected_modes: o => bool, i => bool) S\<^isub>4p .
  1177 
  1170 
       
  1171 hide const a b
       
  1172 
  1178 subsection {* Lambda *}
  1173 subsection {* Lambda *}
  1179 
  1174 
  1180 datatype type =
  1175 datatype type =
  1181     Atom nat
  1176     Atom nat
  1182   | Fun type type    (infixr "\<Rightarrow>" 200)
  1177   | Fun type type    (infixr "\<Rightarrow>" 200)
  1273 code_pred eval_var .
  1268 code_pred eval_var .
  1274 thm eval_var.equation
  1269 thm eval_var.equation
  1275 
  1270 
  1276 values "{val. eval_var (Add (IrConst 1) (IrConst 2)) (| Env = (\<lambda>x. 0)|) val}"
  1271 values "{val. eval_var (Add (IrConst 1) (IrConst 2)) (| Env = (\<lambda>x. 0)|) val}"
  1277 
  1272 
       
  1273 section {* Function predicate replacement *}
       
  1274 
       
  1275 text {*
       
  1276 If the mode analysis uses the functional mode, we
       
  1277 replace predicates that resulted from functions again by their functions.
       
  1278 *}
       
  1279 
       
  1280 inductive test_append
       
  1281 where
       
  1282   "List.append xs ys = zs ==> test_append xs ys zs"
       
  1283 
       
  1284 code_pred [inductify, skip_proof] test_append .
       
  1285 thm test_append.equation
       
  1286 
       
  1287 text {* If append is not turned into a predicate, then the mode
       
  1288   o => o => i => bool could not be inferred. *}
       
  1289 
       
  1290 values 4 "{(xs::int list, ys). test_append xs ys [1, 2, 3, 4]}"
       
  1291 
       
  1292 text {* If appendP is not reverted back to a function, then mode i => i => o => bool
       
  1293   fails after deleting the predicate equation. *}
       
  1294 
       
  1295 declare appendP.equation[code del]
       
  1296 
       
  1297 values "{xs::int list. test_append [1,2] [3,4] xs}"
       
  1298 values "{xs::int list. test_append (replicate 1000 1) (replicate 1000 2) xs}"
       
  1299 values "{xs::int list. test_append (replicate 2000 1) (replicate 2000 2) xs}"
       
  1300 
       
  1301 subsection {* Function with tuples *}
       
  1302 
       
  1303 fun append'
       
  1304 where
       
  1305   "append' ([], ys) = ys"
       
  1306 | "append' (x # xs, ys) = x # append' (xs, ys)"
       
  1307 
       
  1308 inductive test_append'
       
  1309 where
       
  1310   "append' (xs, ys) = zs ==> test_append' xs ys zs"
       
  1311 
       
  1312 code_pred [inductify, skip_proof] test_append' .
       
  1313 
       
  1314 thm test_append'.equation
       
  1315 
       
  1316 values "{(xs::int list, ys). test_append' xs ys [1, 2, 3, 4]}"
       
  1317 
       
  1318 declare append'P.equation[code del]
       
  1319 
       
  1320 values "{zs :: int list. test_append' [1,2,3] [4,5] zs}"
       
  1321 
       
  1322 section {* Arithmetic examples *}
       
  1323 
       
  1324 subsection {* Examples on nat *}
       
  1325 
       
  1326 inductive plus_nat_test :: "nat => nat => nat => bool"
       
  1327 where
       
  1328   "x + y = z ==> plus_nat_test x y z"
       
  1329 
       
  1330 code_pred [inductify, skip_proof] plus_nat_test .
       
  1331 code_pred [new_random_dseq inductify] plus_nat_test .
       
  1332 
       
  1333 thm plus_nat_test.equation
       
  1334 thm plus_nat_test.new_random_dseq_equation
       
  1335 
       
  1336 values [expected "{9::nat}"] "{z. plus_nat_test 4 5 z}"
       
  1337 values [expected "{9::nat}"] "{z. plus_nat_test 7 2 z}"
       
  1338 values [expected "{4::nat}"] "{y. plus_nat_test 5 y 9}"
       
  1339 values [expected "{}"] "{y. plus_nat_test 9 y 8}"
       
  1340 values [expected "{6::nat}"] "{y. plus_nat_test 1 y 7}"
       
  1341 values [expected "{2::nat}"] "{x. plus_nat_test x 7 9}"
       
  1342 values [expected "{}"] "{x. plus_nat_test x 9 7}"
       
  1343 values [expected "{(0::nat,0::nat)}"] "{(x, y). plus_nat_test x y 0}"
       
  1344 values [expected "{(0, Suc 0), (Suc 0, 0)}"] "{(x, y). plus_nat_test x y 1}"
       
  1345 values [expected "{(0, 5), (4, Suc 0), (3, 2), (2, 3), (Suc 0, 4), (5, 0)}"]
       
  1346   "{(x, y). plus_nat_test x y 5}"
       
  1347 
       
  1348 inductive minus_nat_test :: "nat => nat => nat => bool"
       
  1349 where
       
  1350   "x - y = z ==> minus_nat_test x y z"
       
  1351 
       
  1352 code_pred [inductify, skip_proof] minus_nat_test .
       
  1353 code_pred [new_random_dseq inductify] minus_nat_test .
       
  1354 
       
  1355 thm minus_nat_test.equation
       
  1356 thm minus_nat_test.new_random_dseq_equation
       
  1357 
       
  1358 values [expected "{0::nat}"] "{z. minus_nat_test 4 5 z}"
       
  1359 values [expected "{5::nat}"] "{z. minus_nat_test 7 2 z}"
       
  1360 values [expected "{16::nat}"] "{x. minus_nat_test x 7 9}"
       
  1361 values [expected "{16::nat}"] "{x. minus_nat_test x 9 7}"
       
  1362 values [expected "{0, Suc 0, 2, 3}"] "{x. minus_nat_test x 3 0}"
       
  1363 values [expected "{0::nat}"] "{x. minus_nat_test x 0 0}"
       
  1364 
       
  1365 subsection {* Examples on int *}
       
  1366 
       
  1367 inductive plus_int_test :: "int => int => int => bool"
       
  1368 where
       
  1369   "a + b = c ==> plus_int_test a b c"
       
  1370 
       
  1371 code_pred [inductify, skip_proof] plus_int_test .
       
  1372 code_pred [new_random_dseq inductify] plus_int_test .
       
  1373 
       
  1374 thm plus_int_test.equation
       
  1375 thm plus_int_test.new_random_dseq_equation
       
  1376 
       
  1377 values [expected "{1::int}"] "{a. plus_int_test a 6 7}"
       
  1378 values [expected "{1::int}"] "{b. plus_int_test 6 b 7}"
       
  1379 values [expected "{11::int}"] "{c. plus_int_test 5 6 c}"
       
  1380 
       
  1381 inductive minus_int_test :: "int => int => int => bool"
       
  1382 where
       
  1383   "a - b = c ==> minus_int_test a b c"
       
  1384 
       
  1385 code_pred [inductify, skip_proof] minus_int_test .
       
  1386 code_pred [new_random_dseq inductify] minus_int_test .
       
  1387 
       
  1388 thm minus_int_test.equation
       
  1389 thm minus_int_test.new_random_dseq_equation
       
  1390 
       
  1391 values [expected "{4::int}"] "{c. minus_int_test 9 5 c}"
       
  1392 values [expected "{9::int}"] "{a. minus_int_test a 4 5}"
       
  1393 values [expected "{- 1::int}"] "{b. minus_int_test 4 b 5}"
       
  1394 
       
  1395 
       
  1396 
       
  1397 
  1278 end
  1398 end