# HG changeset patch # User bulwahn # Date 1269448844 -3600 # Node ID e657fb805c6879525adbd19ab48b27ef2cc5dcfa # Parent d87d85a5d9ab20b80eea79ab9eba6676d0255ee6 added predicate compiler quickcheck examples to new session Predicate_Compile_Examples diff -r d87d85a5d9ab -r e657fb805c68 src/HOL/IsaMakefile --- a/src/HOL/IsaMakefile Wed Mar 24 17:40:44 2010 +0100 +++ b/src/HOL/IsaMakefile Wed Mar 24 17:40:44 2010 +0100 @@ -1287,8 +1287,10 @@ HOL-Predicate_Compile_Examples: HOL $(LOG)/HOL-Predicate_Compile_Examples.gz -$(LOG)/HOL-Predicate_Compile_Examples.gz: $(OUT)/HOL \ - Predicate_Compile_Examples/ROOT.ML Predicate_Compile_Examples/Predicate_Compile_Examples.thy +$(LOG)/HOL-Predicate_Compile_Examples.gz: $(OUT)/HOL \ + Predicate_Compile_Examples/ROOT.ML \ + Predicate_Compile_Examples/Predicate_Compile_Examples.thy \ + Predicate_Compile_Examples/Predicate_Compile_Quickcheck_Examples.thy @$(ISABELLE_TOOL) usedir $(OUT)/HOL Predicate_Compile_Examples ## clean diff -r d87d85a5d9ab -r e657fb805c68 src/HOL/Predicate_Compile_Examples/Predicate_Compile_Quickcheck_Examples.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Predicate_Compile_Examples/Predicate_Compile_Quickcheck_Examples.thy Wed Mar 24 17:40:44 2010 +0100 @@ -0,0 +1,337 @@ +theory Predicate_Compile_Quickcheck_Examples +imports Predicate_Compile_Quickcheck +begin + +section {* Sets *} + +lemma "x \ {(1::nat)} ==> False" +quickcheck[generator=predicate_compile_wo_ff, iterations=10] +oops + +lemma "x \ {Suc 0, Suc (Suc 0)} ==> x \ Suc 0" +quickcheck[generator=predicate_compile_wo_ff] +oops + +lemma "x \ {Suc 0, Suc (Suc 0)} ==> x = Suc 0" +quickcheck[generator=predicate_compile_wo_ff] +oops + +lemma "x \ {Suc 0, Suc (Suc 0)} ==> x <= Suc 0" +quickcheck[generator=predicate_compile_wo_ff] +oops + +section {* Numerals *} + +lemma + "x \ {1, 2, (3::nat)} ==> x = 1 \ x = 2" +quickcheck[generator=predicate_compile_wo_ff] +oops + +lemma "x \ {1, 2, (3::nat)} ==> x < 3" +quickcheck[generator=predicate_compile_wo_ff] +oops + +lemma + "x \ {1, 2} \ {3, 4} ==> x = (1::nat) \ x = (2::nat)" +quickcheck[generator=predicate_compile_wo_ff] +oops + +section {* Context Free Grammar *} + +datatype alphabet = a | b + +inductive_set S\<^isub>1 and A\<^isub>1 and B\<^isub>1 where + "[] \ S\<^isub>1" +| "w \ A\<^isub>1 \ b # w \ S\<^isub>1" +| "w \ B\<^isub>1 \ a # w \ S\<^isub>1" +| "w \ S\<^isub>1 \ a # w \ A\<^isub>1" +| "w \ S\<^isub>1 \ b # w \ S\<^isub>1" +| "\v \ B\<^isub>1; v \ B\<^isub>1\ \ a # v @ w \ B\<^isub>1" + +lemma + "w \ S\<^isub>1 \ w = []" +quickcheck[generator = predicate_compile_ff_nofs, iterations=1] +oops + +theorem S\<^isub>1_sound: +"w \ S\<^isub>1 \ length [x \ w. x = a] = length [x \ w. x = b]" +quickcheck[generator=predicate_compile_ff_nofs, size=15] +oops + + +inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where + "[] \ S\<^isub>2" +| "w \ A\<^isub>2 \ b # w \ S\<^isub>2" +| "w \ B\<^isub>2 \ a # w \ S\<^isub>2" +| "w \ S\<^isub>2 \ a # w \ A\<^isub>2" +| "w \ S\<^isub>2 \ b # w \ B\<^isub>2" +| "\v \ B\<^isub>2; v \ B\<^isub>2\ \ a # v @ w \ B\<^isub>2" +(* +code_pred [random_dseq inductify] S\<^isub>2 . +thm S\<^isub>2.random_dseq_equation +thm A\<^isub>2.random_dseq_equation +thm B\<^isub>2.random_dseq_equation + +values [random_dseq 1, 2, 8] 10 "{x. S\<^isub>2 x}" + +lemma "w \ S\<^isub>2 ==> w \ [] ==> w \ [b, a] ==> w \ {}" +quickcheck[generator=predicate_compile, size=8] +oops + +lemma "[x <- w. x = a] = []" +quickcheck[generator=predicate_compile] +oops + +declare list.size(3,4)[code_pred_def] + +(* +lemma "length ([x \ w. x = a]) = (0::nat)" +quickcheck[generator=predicate_compile] +oops +*) + +lemma +"w \ S\<^isub>2 ==> length [x \ w. x = a] <= Suc (Suc 0)" +quickcheck[generator=predicate_compile, size = 10, iterations = 1] +oops +*) +theorem S\<^isub>2_sound: +"w \ S\<^isub>2 \ length [x \ w. x = a] = length [x \ w. x = b]" +quickcheck[generator=predicate_compile_ff_nofs, size=5, iterations=10] +oops + +inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where + "[] \ S\<^isub>3" +| "w \ A\<^isub>3 \ b # w \ S\<^isub>3" +| "w \ B\<^isub>3 \ a # w \ S\<^isub>3" +| "w \ S\<^isub>3 \ a # w \ A\<^isub>3" +| "w \ S\<^isub>3 \ b # w \ B\<^isub>3" +| "\v \ B\<^isub>3; w \ B\<^isub>3\ \ a # v @ w \ B\<^isub>3" + +code_pred [inductify] S\<^isub>3 . +thm S\<^isub>3.equation +(* +values 10 "{x. S\<^isub>3 x}" +*) + + +lemma S\<^isub>3_sound: +"w \ S\<^isub>3 \ length [x \ w. x = a] = length [x \ w. x = b]" +quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=10] +oops + +lemma "\ (length w > 2) \ \ (length [x \ w. x = a] = length [x \ w. x = b])" +quickcheck[size=10, generator = predicate_compile_ff_fs] +oops + +theorem S\<^isub>3_complete: +"length [x \ w. x = a] = length [x \ w. b = x] \ w \ S\<^isub>3" +(*quickcheck[generator=SML]*) +quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=100] +oops + + +inductive_set S\<^isub>4 and A\<^isub>4 and B\<^isub>4 where + "[] \ S\<^isub>4" +| "w \ A\<^isub>4 \ b # w \ S\<^isub>4" +| "w \ B\<^isub>4 \ a # w \ S\<^isub>4" +| "w \ S\<^isub>4 \ a # w \ A\<^isub>4" +| "\v \ A\<^isub>4; w \ A\<^isub>4\ \ b # v @ w \ A\<^isub>4" +| "w \ S\<^isub>4 \ b # w \ B\<^isub>4" +| "\v \ B\<^isub>4; w \ B\<^isub>4\ \ a # v @ w \ B\<^isub>4" + +theorem S\<^isub>4_sound: +"w \ S\<^isub>4 \ length [x \ w. x = a] = length [x \ w. x = b]" +quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1] +oops + +theorem S\<^isub>4_complete: +"length [x \ w. x = a] = length [x \ w. x = b] \ w \ S\<^isub>4" +quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1] +oops + +hide const a b + +subsection {* Lexicographic order *} +(* TODO *) +(* +lemma + "(u, v) : lexord r ==> (x @ u, y @ v) : lexord r" +oops +*) +subsection {* IMP *} + +types + var = nat + state = "int list" + +datatype com = + Skip | + Ass var "int" | + Seq com com | + IF "state list" com com | + While "state list" com + +inductive exec :: "com => state => state => bool" where + "exec Skip s s" | + "exec (Ass x e) s (s[x := e])" | + "exec c1 s1 s2 ==> exec c2 s2 s3 ==> exec (Seq c1 c2) s1 s3" | + "s \ set b ==> exec c1 s t ==> exec (IF b c1 c2) s t" | + "s \ set b ==> exec c2 s t ==> exec (IF b c1 c2) s t" | + "s \ set b ==> exec (While b c) s s" | + "s1 \ set b ==> exec c s1 s2 ==> exec (While b c) s2 s3 ==> exec (While b c) s1 s3" + +code_pred [random_dseq] exec . + +values [random_dseq 1, 2, 3] 10 "{(c, s, s'). exec c s s'}" + +lemma + "exec c s s' ==> exec (Seq c c) s s'" +(*quickcheck[generator = predicate_compile_wo_ff, size=2, iterations=10]*) +oops + +subsection {* Lambda *} + +datatype type = + Atom nat + | Fun type type (infixr "\" 200) + +datatype dB = + Var nat + | App dB dB (infixl "\" 200) + | Abs type dB + +primrec + nth_el :: "'a list \ nat \ 'a option" ("_\_\" [90, 0] 91) +where + "[]\i\ = None" +| "(x # xs)\i\ = (case i of 0 \ Some x | Suc j \ xs \j\)" + +inductive nth_el' :: "'a list \ nat \ 'a \ bool" +where + "nth_el' (x # xs) 0 x" +| "nth_el' xs i y \ nth_el' (x # xs) (Suc i) y" + +inductive typing :: "type list \ dB \ type \ bool" ("_ \ _ : _" [50, 50, 50] 50) + where + Var [intro!]: "nth_el' env x T \ env \ Var x : T" + | Abs [intro!]: "T # env \ t : U \ env \ Abs T t : (T \ U)" + | App [intro!]: "env \ s : U \ T \ env \ t : T \ env \ (s \ t) : U" + +primrec + lift :: "[dB, nat] => dB" +where + "lift (Var i) k = (if i < k then Var i else Var (i + 1))" + | "lift (s \ t) k = lift s k \ lift t k" + | "lift (Abs T s) k = Abs T (lift s (k + 1))" + +primrec + subst :: "[dB, dB, nat] => dB" ("_[_'/_]" [300, 0, 0] 300) +where + subst_Var: "(Var i)[s/k] = + (if k < i then Var (i - 1) else if i = k then s else Var i)" + | subst_App: "(t \ u)[s/k] = t[s/k] \ u[s/k]" + | subst_Abs: "(Abs T t)[s/k] = Abs T (t[lift s 0 / k+1])" + +inductive beta :: "[dB, dB] => bool" (infixl "\\<^sub>\" 50) + where + beta [simp, intro!]: "Abs T s \ t \\<^sub>\ s[t/0]" + | appL [simp, intro!]: "s \\<^sub>\ t ==> s \ u \\<^sub>\ t \ u" + | appR [simp, intro!]: "s \\<^sub>\ t ==> u \ s \\<^sub>\ u \ t" + | abs [simp, intro!]: "s \\<^sub>\ t ==> Abs T s \\<^sub>\ Abs T t" + +lemma + "\ \ t : U \ t \\<^sub>\ t' \ \ \ t' : U" +quickcheck[generator = predicate_compile_ff_fs, size = 7, iterations = 10] +oops + +subsection {* JAD *} + +definition matrix :: "('a :: semiring_0) list list \ nat \ nat \ bool" where + "matrix M rs cs \ (\ row \ set M. length row = cs) \ length M = rs" +(* +code_pred [random_dseq inductify] matrix . +thm matrix.random_dseq_equation + +thm matrix_aux.random_dseq_equation + +values [random_dseq 3, 2] 10 "{(M, rs, cs). matrix (M:: int list list) rs cs}" +*) +lemma [code_pred_intro]: + "matrix [] 0 m" + "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m" +proof - + show "matrix [] 0 m" unfolding matrix_def by auto +next + show "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m" + unfolding matrix_def by auto +qed + +code_pred [random_dseq inductify] matrix + apply (cases x) + unfolding matrix_def apply fastsimp + apply fastsimp done + + +values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}" + +definition "scalar_product v w = (\ (x, y)\zip v w. x * y)" + +definition mv :: "('a \ semiring_0) list list \ 'a list \ 'a list" + where [simp]: "mv M v = map (scalar_product v) M" +text {* + This defines the matrix vector multiplication. To work properly @{term +"matrix M m n \ length v = n"} must hold. +*} + +subsection "Compressed matrix" + +definition "sparsify xs = [i \ zip [0.. 0]" +(* +lemma sparsify_length: "(i, x) \ set (sparsify xs) \ i < length xs" + by (auto simp: sparsify_def set_zip) + +lemma listsum_sparsify[simp]: + fixes v :: "('a \ semiring_0) list" + assumes "length w = length v" + shows "(\x\sparsify w. (\(i, x). v ! i) x * snd x) = scalar_product v w" + (is "(\x\_. ?f x) = _") + unfolding sparsify_def scalar_product_def + using assms listsum_map_filter[where f="?f" and P="\ i. snd i \ (0::'a)"] + by (simp add: listsum_setsum) +*) +definition [simp]: "unzip w = (map fst w, map snd w)" + +primrec insert :: "('a \ 'b \ linorder) => 'a \ 'a list => 'a list" where + "insert f x [] = [x]" | + "insert f x (y # ys) = (if f y < f x then y # insert f x ys else x # y # ys)" + +primrec sort :: "('a \ 'b \ linorder) \ 'a list => 'a list" where + "sort f [] = []" | + "sort f (x # xs) = insert f x (sort f xs)" + +definition + "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)" +(* +definition + "transpose M = [map (\ xs. xs ! i) (takeWhile (\ xs. i < length xs) M). i \ [0 ..< length (M ! 0)]]" +*) +definition + "inflate upds = foldr (\ (i, x) upds. upds[i := x]) upds (replicate (length upds) 0)" + +definition + "jad = apsnd transpose o length_permutate o map sparsify" + +definition + "jad_mv v = inflate o split zip o apsnd (map listsum o transpose o map (map (\ (i, x). v ! i * x)))" + +lemma "matrix (M::int list list) rs cs \ False" +quickcheck[generator = predicate_compile_ff_nofs, size = 6] +oops + +lemma + "\ matrix M rs cs ; length v = cs \ \ jad_mv v (jad M) = mv M v" +quickcheck[generator = predicate_compile_wo_ff] +oops + +end diff -r d87d85a5d9ab -r e657fb805c68 src/HOL/Predicate_Compile_Examples/ROOT.ML --- a/src/HOL/Predicate_Compile_Examples/ROOT.ML Wed Mar 24 17:40:44 2010 +0100 +++ b/src/HOL/Predicate_Compile_Examples/ROOT.ML Wed Mar 24 17:40:44 2010 +0100 @@ -1,1 +1,1 @@ -use_thys ["Predicate_Compile_Examples"]; +use_thys ["Predicate_Compile_Examples", "Predicate_Compile_Quickcheck_Examples"]; diff -r d87d85a5d9ab -r e657fb805c68 src/HOL/ex/Predicate_Compile_Quickcheck_ex.thy --- a/src/HOL/ex/Predicate_Compile_Quickcheck_ex.thy Wed Mar 24 17:40:44 2010 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,337 +0,0 @@ -theory Predicate_Compile_Quickcheck_ex -imports Predicate_Compile_Quickcheck -begin - -section {* Sets *} - -lemma "x \ {(1::nat)} ==> False" -quickcheck[generator=predicate_compile_wo_ff, iterations=10] -oops - -lemma "x \ {Suc 0, Suc (Suc 0)} ==> x \ Suc 0" -quickcheck[generator=predicate_compile_wo_ff] -oops - -lemma "x \ {Suc 0, Suc (Suc 0)} ==> x = Suc 0" -quickcheck[generator=predicate_compile_wo_ff] -oops - -lemma "x \ {Suc 0, Suc (Suc 0)} ==> x <= Suc 0" -quickcheck[generator=predicate_compile_wo_ff] -oops - -section {* Numerals *} - -lemma - "x \ {1, 2, (3::nat)} ==> x = 1 \ x = 2" -quickcheck[generator=predicate_compile_wo_ff] -oops - -lemma "x \ {1, 2, (3::nat)} ==> x < 3" -quickcheck[generator=predicate_compile_wo_ff] -oops - -lemma - "x \ {1, 2} \ {3, 4} ==> x = (1::nat) \ x = (2::nat)" -quickcheck[generator=predicate_compile_wo_ff] -oops - -section {* Context Free Grammar *} - -datatype alphabet = a | b - -inductive_set S\<^isub>1 and A\<^isub>1 and B\<^isub>1 where - "[] \ S\<^isub>1" -| "w \ A\<^isub>1 \ b # w \ S\<^isub>1" -| "w \ B\<^isub>1 \ a # w \ S\<^isub>1" -| "w \ S\<^isub>1 \ a # w \ A\<^isub>1" -| "w \ S\<^isub>1 \ b # w \ S\<^isub>1" -| "\v \ B\<^isub>1; v \ B\<^isub>1\ \ a # v @ w \ B\<^isub>1" - -lemma - "w \ S\<^isub>1 \ w = []" -quickcheck[generator = predicate_compile_ff_nofs, iterations=1] -oops - -theorem S\<^isub>1_sound: -"w \ S\<^isub>1 \ length [x \ w. x = a] = length [x \ w. x = b]" -quickcheck[generator=predicate_compile_ff_nofs, size=15] -oops - - -inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where - "[] \ S\<^isub>2" -| "w \ A\<^isub>2 \ b # w \ S\<^isub>2" -| "w \ B\<^isub>2 \ a # w \ S\<^isub>2" -| "w \ S\<^isub>2 \ a # w \ A\<^isub>2" -| "w \ S\<^isub>2 \ b # w \ B\<^isub>2" -| "\v \ B\<^isub>2; v \ B\<^isub>2\ \ a # v @ w \ B\<^isub>2" -(* -code_pred [random_dseq inductify] S\<^isub>2 . -thm S\<^isub>2.random_dseq_equation -thm A\<^isub>2.random_dseq_equation -thm B\<^isub>2.random_dseq_equation - -values [random_dseq 1, 2, 8] 10 "{x. S\<^isub>2 x}" - -lemma "w \ S\<^isub>2 ==> w \ [] ==> w \ [b, a] ==> w \ {}" -quickcheck[generator=predicate_compile, size=8] -oops - -lemma "[x <- w. x = a] = []" -quickcheck[generator=predicate_compile] -oops - -declare list.size(3,4)[code_pred_def] - -(* -lemma "length ([x \ w. x = a]) = (0::nat)" -quickcheck[generator=predicate_compile] -oops -*) - -lemma -"w \ S\<^isub>2 ==> length [x \ w. x = a] <= Suc (Suc 0)" -quickcheck[generator=predicate_compile, size = 10, iterations = 1] -oops -*) -theorem S\<^isub>2_sound: -"w \ S\<^isub>2 \ length [x \ w. x = a] = length [x \ w. x = b]" -quickcheck[generator=predicate_compile_ff_nofs, size=5, iterations=10] -oops - -inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where - "[] \ S\<^isub>3" -| "w \ A\<^isub>3 \ b # w \ S\<^isub>3" -| "w \ B\<^isub>3 \ a # w \ S\<^isub>3" -| "w \ S\<^isub>3 \ a # w \ A\<^isub>3" -| "w \ S\<^isub>3 \ b # w \ B\<^isub>3" -| "\v \ B\<^isub>3; w \ B\<^isub>3\ \ a # v @ w \ B\<^isub>3" - -code_pred [inductify] S\<^isub>3 . -thm S\<^isub>3.equation -(* -values 10 "{x. S\<^isub>3 x}" -*) - - -lemma S\<^isub>3_sound: -"w \ S\<^isub>3 \ length [x \ w. x = a] = length [x \ w. x = b]" -quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=10] -oops - -lemma "\ (length w > 2) \ \ (length [x \ w. x = a] = length [x \ w. x = b])" -quickcheck[size=10, generator = predicate_compile_ff_fs] -oops - -theorem S\<^isub>3_complete: -"length [x \ w. x = a] = length [x \ w. b = x] \ w \ S\<^isub>3" -(*quickcheck[generator=SML]*) -quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=100] -oops - - -inductive_set S\<^isub>4 and A\<^isub>4 and B\<^isub>4 where - "[] \ S\<^isub>4" -| "w \ A\<^isub>4 \ b # w \ S\<^isub>4" -| "w \ B\<^isub>4 \ a # w \ S\<^isub>4" -| "w \ S\<^isub>4 \ a # w \ A\<^isub>4" -| "\v \ A\<^isub>4; w \ A\<^isub>4\ \ b # v @ w \ A\<^isub>4" -| "w \ S\<^isub>4 \ b # w \ B\<^isub>4" -| "\v \ B\<^isub>4; w \ B\<^isub>4\ \ a # v @ w \ B\<^isub>4" - -theorem S\<^isub>4_sound: -"w \ S\<^isub>4 \ length [x \ w. x = a] = length [x \ w. x = b]" -quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1] -oops - -theorem S\<^isub>4_complete: -"length [x \ w. x = a] = length [x \ w. x = b] \ w \ S\<^isub>4" -quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1] -oops - -hide const a b - -subsection {* Lexicographic order *} -(* TODO *) -(* -lemma - "(u, v) : lexord r ==> (x @ u, y @ v) : lexord r" -oops -*) -subsection {* IMP *} - -types - var = nat - state = "int list" - -datatype com = - Skip | - Ass var "int" | - Seq com com | - IF "state list" com com | - While "state list" com - -inductive exec :: "com => state => state => bool" where - "exec Skip s s" | - "exec (Ass x e) s (s[x := e])" | - "exec c1 s1 s2 ==> exec c2 s2 s3 ==> exec (Seq c1 c2) s1 s3" | - "s \ set b ==> exec c1 s t ==> exec (IF b c1 c2) s t" | - "s \ set b ==> exec c2 s t ==> exec (IF b c1 c2) s t" | - "s \ set b ==> exec (While b c) s s" | - "s1 \ set b ==> exec c s1 s2 ==> exec (While b c) s2 s3 ==> exec (While b c) s1 s3" - -code_pred [random_dseq] exec . - -values [random_dseq 1, 2, 3] 10 "{(c, s, s'). exec c s s'}" - -lemma - "exec c s s' ==> exec (Seq c c) s s'" -(*quickcheck[generator = predicate_compile_wo_ff, size=2, iterations=10]*) -oops - -subsection {* Lambda *} - -datatype type = - Atom nat - | Fun type type (infixr "\" 200) - -datatype dB = - Var nat - | App dB dB (infixl "\" 200) - | Abs type dB - -primrec - nth_el :: "'a list \ nat \ 'a option" ("_\_\" [90, 0] 91) -where - "[]\i\ = None" -| "(x # xs)\i\ = (case i of 0 \ Some x | Suc j \ xs \j\)" - -inductive nth_el' :: "'a list \ nat \ 'a \ bool" -where - "nth_el' (x # xs) 0 x" -| "nth_el' xs i y \ nth_el' (x # xs) (Suc i) y" - -inductive typing :: "type list \ dB \ type \ bool" ("_ \ _ : _" [50, 50, 50] 50) - where - Var [intro!]: "nth_el' env x T \ env \ Var x : T" - | Abs [intro!]: "T # env \ t : U \ env \ Abs T t : (T \ U)" - | App [intro!]: "env \ s : U \ T \ env \ t : T \ env \ (s \ t) : U" - -primrec - lift :: "[dB, nat] => dB" -where - "lift (Var i) k = (if i < k then Var i else Var (i + 1))" - | "lift (s \ t) k = lift s k \ lift t k" - | "lift (Abs T s) k = Abs T (lift s (k + 1))" - -primrec - subst :: "[dB, dB, nat] => dB" ("_[_'/_]" [300, 0, 0] 300) -where - subst_Var: "(Var i)[s/k] = - (if k < i then Var (i - 1) else if i = k then s else Var i)" - | subst_App: "(t \ u)[s/k] = t[s/k] \ u[s/k]" - | subst_Abs: "(Abs T t)[s/k] = Abs T (t[lift s 0 / k+1])" - -inductive beta :: "[dB, dB] => bool" (infixl "\\<^sub>\" 50) - where - beta [simp, intro!]: "Abs T s \ t \\<^sub>\ s[t/0]" - | appL [simp, intro!]: "s \\<^sub>\ t ==> s \ u \\<^sub>\ t \ u" - | appR [simp, intro!]: "s \\<^sub>\ t ==> u \ s \\<^sub>\ u \ t" - | abs [simp, intro!]: "s \\<^sub>\ t ==> Abs T s \\<^sub>\ Abs T t" - -lemma - "\ \ t : U \ t \\<^sub>\ t' \ \ \ t' : U" -quickcheck[generator = predicate_compile_ff_fs, size = 7, iterations = 10] -oops - -subsection {* JAD *} - -definition matrix :: "('a :: semiring_0) list list \ nat \ nat \ bool" where - "matrix M rs cs \ (\ row \ set M. length row = cs) \ length M = rs" -(* -code_pred [random_dseq inductify] matrix . -thm matrix.random_dseq_equation - -thm matrix_aux.random_dseq_equation - -values [random_dseq 3, 2] 10 "{(M, rs, cs). matrix (M:: int list list) rs cs}" -*) -lemma [code_pred_intro]: - "matrix [] 0 m" - "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m" -proof - - show "matrix [] 0 m" unfolding matrix_def by auto -next - show "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m" - unfolding matrix_def by auto -qed - -code_pred [random_dseq inductify] matrix - apply (cases x) - unfolding matrix_def apply fastsimp - apply fastsimp done - - -values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}" - -definition "scalar_product v w = (\ (x, y)\zip v w. x * y)" - -definition mv :: "('a \ semiring_0) list list \ 'a list \ 'a list" - where [simp]: "mv M v = map (scalar_product v) M" -text {* - This defines the matrix vector multiplication. To work properly @{term -"matrix M m n \ length v = n"} must hold. -*} - -subsection "Compressed matrix" - -definition "sparsify xs = [i \ zip [0.. 0]" -(* -lemma sparsify_length: "(i, x) \ set (sparsify xs) \ i < length xs" - by (auto simp: sparsify_def set_zip) - -lemma listsum_sparsify[simp]: - fixes v :: "('a \ semiring_0) list" - assumes "length w = length v" - shows "(\x\sparsify w. (\(i, x). v ! i) x * snd x) = scalar_product v w" - (is "(\x\_. ?f x) = _") - unfolding sparsify_def scalar_product_def - using assms listsum_map_filter[where f="?f" and P="\ i. snd i \ (0::'a)"] - by (simp add: listsum_setsum) -*) -definition [simp]: "unzip w = (map fst w, map snd w)" - -primrec insert :: "('a \ 'b \ linorder) => 'a \ 'a list => 'a list" where - "insert f x [] = [x]" | - "insert f x (y # ys) = (if f y < f x then y # insert f x ys else x # y # ys)" - -primrec sort :: "('a \ 'b \ linorder) \ 'a list => 'a list" where - "sort f [] = []" | - "sort f (x # xs) = insert f x (sort f xs)" - -definition - "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)" -(* -definition - "transpose M = [map (\ xs. xs ! i) (takeWhile (\ xs. i < length xs) M). i \ [0 ..< length (M ! 0)]]" -*) -definition - "inflate upds = foldr (\ (i, x) upds. upds[i := x]) upds (replicate (length upds) 0)" - -definition - "jad = apsnd transpose o length_permutate o map sparsify" - -definition - "jad_mv v = inflate o split zip o apsnd (map listsum o transpose o map (map (\ (i, x). v ! i * x)))" - -lemma "matrix (M::int list list) rs cs \ False" -quickcheck[generator = predicate_compile_ff_nofs, size = 6] -oops - -lemma - "\ matrix M rs cs ; length v = cs \ \ jad_mv v (jad M) = mv M v" -quickcheck[generator = predicate_compile_wo_ff] -oops - -end \ No newline at end of file