src/HOL/Predicate_Compile_Examples/Predicate_Compile_Quickcheck_Examples.thy
author bulwahn
Mon Mar 29 17:30:56 2010 +0200 (2010-03-29)
changeset 36040 fcd7bea01a93
parent 35955 e657fb805c68
child 36176 3fe7e97ccca8
permissions -rw-r--r--
adding skip_proof in the examples because proof procedure cannot handle alternative compilations yet
bulwahn@35955
     1
theory Predicate_Compile_Quickcheck_Examples
bulwahn@35955
     2
imports Predicate_Compile_Quickcheck
bulwahn@35955
     3
begin
bulwahn@35955
     4
bulwahn@35955
     5
section {* Sets *}
bulwahn@35955
     6
bulwahn@35955
     7
lemma "x \<in> {(1::nat)} ==> False"
bulwahn@35955
     8
quickcheck[generator=predicate_compile_wo_ff, iterations=10]
bulwahn@35955
     9
oops
bulwahn@35955
    10
bulwahn@35955
    11
lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x \<noteq> Suc 0"
bulwahn@35955
    12
quickcheck[generator=predicate_compile_wo_ff]
bulwahn@35955
    13
oops
bulwahn@35955
    14
bulwahn@35955
    15
lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x = Suc 0"
bulwahn@35955
    16
quickcheck[generator=predicate_compile_wo_ff]
bulwahn@35955
    17
oops
bulwahn@35955
    18
 
bulwahn@35955
    19
lemma "x \<in> {Suc 0, Suc (Suc 0)} ==> x <= Suc 0"
bulwahn@35955
    20
quickcheck[generator=predicate_compile_wo_ff]
bulwahn@35955
    21
oops
bulwahn@35955
    22
bulwahn@35955
    23
section {* Numerals *}
bulwahn@35955
    24
bulwahn@35955
    25
lemma
bulwahn@35955
    26
  "x \<in> {1, 2, (3::nat)} ==> x = 1 \<or> x = 2"
bulwahn@35955
    27
quickcheck[generator=predicate_compile_wo_ff]
bulwahn@35955
    28
oops
bulwahn@35955
    29
bulwahn@35955
    30
lemma "x \<in> {1, 2, (3::nat)} ==> x < 3"
bulwahn@35955
    31
quickcheck[generator=predicate_compile_wo_ff]
bulwahn@35955
    32
oops
bulwahn@35955
    33
bulwahn@35955
    34
lemma
bulwahn@35955
    35
  "x \<in> {1, 2} \<union> {3, 4} ==> x = (1::nat) \<or> x = (2::nat)"
bulwahn@35955
    36
quickcheck[generator=predicate_compile_wo_ff]
bulwahn@35955
    37
oops
bulwahn@35955
    38
bulwahn@35955
    39
section {* Context Free Grammar *}
bulwahn@35955
    40
bulwahn@35955
    41
datatype alphabet = a | b
bulwahn@35955
    42
bulwahn@35955
    43
inductive_set S\<^isub>1 and A\<^isub>1 and B\<^isub>1 where
bulwahn@35955
    44
  "[] \<in> S\<^isub>1"
bulwahn@35955
    45
| "w \<in> A\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
bulwahn@35955
    46
| "w \<in> B\<^isub>1 \<Longrightarrow> a # w \<in> S\<^isub>1"
bulwahn@35955
    47
| "w \<in> S\<^isub>1 \<Longrightarrow> a # w \<in> A\<^isub>1"
bulwahn@35955
    48
| "w \<in> S\<^isub>1 \<Longrightarrow> b # w \<in> S\<^isub>1"
bulwahn@35955
    49
| "\<lbrakk>v \<in> B\<^isub>1; v \<in> B\<^isub>1\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>1"
bulwahn@35955
    50
bulwahn@35955
    51
lemma
bulwahn@35955
    52
  "w \<in> S\<^isub>1 \<Longrightarrow> w = []"
bulwahn@35955
    53
quickcheck[generator = predicate_compile_ff_nofs, iterations=1]
bulwahn@35955
    54
oops
bulwahn@35955
    55
bulwahn@35955
    56
theorem S\<^isub>1_sound:
bulwahn@35955
    57
"w \<in> S\<^isub>1 \<Longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@35955
    58
quickcheck[generator=predicate_compile_ff_nofs, size=15]
bulwahn@35955
    59
oops
bulwahn@35955
    60
bulwahn@35955
    61
bulwahn@35955
    62
inductive_set S\<^isub>2 and A\<^isub>2 and B\<^isub>2 where
bulwahn@35955
    63
  "[] \<in> S\<^isub>2"
bulwahn@35955
    64
| "w \<in> A\<^isub>2 \<Longrightarrow> b # w \<in> S\<^isub>2"
bulwahn@35955
    65
| "w \<in> B\<^isub>2 \<Longrightarrow> a # w \<in> S\<^isub>2"
bulwahn@35955
    66
| "w \<in> S\<^isub>2 \<Longrightarrow> a # w \<in> A\<^isub>2"
bulwahn@35955
    67
| "w \<in> S\<^isub>2 \<Longrightarrow> b # w \<in> B\<^isub>2"
bulwahn@35955
    68
| "\<lbrakk>v \<in> B\<^isub>2; v \<in> B\<^isub>2\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>2"
bulwahn@35955
    69
(*
bulwahn@35955
    70
code_pred [random_dseq inductify] S\<^isub>2 .
bulwahn@35955
    71
thm S\<^isub>2.random_dseq_equation
bulwahn@35955
    72
thm A\<^isub>2.random_dseq_equation
bulwahn@35955
    73
thm B\<^isub>2.random_dseq_equation
bulwahn@35955
    74
bulwahn@35955
    75
values [random_dseq 1, 2, 8] 10 "{x. S\<^isub>2 x}"
bulwahn@35955
    76
bulwahn@35955
    77
lemma "w \<in> S\<^isub>2 ==> w \<noteq> [] ==> w \<noteq> [b, a] ==> w \<in> {}"
bulwahn@35955
    78
quickcheck[generator=predicate_compile, size=8]
bulwahn@35955
    79
oops
bulwahn@35955
    80
bulwahn@35955
    81
lemma "[x <- w. x = a] = []"
bulwahn@35955
    82
quickcheck[generator=predicate_compile]
bulwahn@35955
    83
oops
bulwahn@35955
    84
bulwahn@35955
    85
declare list.size(3,4)[code_pred_def]
bulwahn@35955
    86
bulwahn@35955
    87
(*
bulwahn@35955
    88
lemma "length ([x \<leftarrow> w. x = a]) = (0::nat)"
bulwahn@35955
    89
quickcheck[generator=predicate_compile]
bulwahn@35955
    90
oops
bulwahn@35955
    91
*)
bulwahn@35955
    92
bulwahn@35955
    93
lemma
bulwahn@35955
    94
"w \<in> S\<^isub>2 ==> length [x \<leftarrow> w. x = a] <= Suc (Suc 0)"
bulwahn@35955
    95
quickcheck[generator=predicate_compile, size = 10, iterations = 1]
bulwahn@35955
    96
oops
bulwahn@35955
    97
*)
bulwahn@35955
    98
theorem S\<^isub>2_sound:
bulwahn@35955
    99
"w \<in> S\<^isub>2 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@35955
   100
quickcheck[generator=predicate_compile_ff_nofs, size=5, iterations=10]
bulwahn@35955
   101
oops
bulwahn@35955
   102
bulwahn@35955
   103
inductive_set S\<^isub>3 and A\<^isub>3 and B\<^isub>3 where
bulwahn@35955
   104
  "[] \<in> S\<^isub>3"
bulwahn@35955
   105
| "w \<in> A\<^isub>3 \<Longrightarrow> b # w \<in> S\<^isub>3"
bulwahn@35955
   106
| "w \<in> B\<^isub>3 \<Longrightarrow> a # w \<in> S\<^isub>3"
bulwahn@35955
   107
| "w \<in> S\<^isub>3 \<Longrightarrow> a # w \<in> A\<^isub>3"
bulwahn@35955
   108
| "w \<in> S\<^isub>3 \<Longrightarrow> b # w \<in> B\<^isub>3"
bulwahn@35955
   109
| "\<lbrakk>v \<in> B\<^isub>3; w \<in> B\<^isub>3\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>3"
bulwahn@35955
   110
bulwahn@36040
   111
code_pred [inductify, skip_proof] S\<^isub>3 .
bulwahn@35955
   112
thm S\<^isub>3.equation
bulwahn@35955
   113
(*
bulwahn@35955
   114
values 10 "{x. S\<^isub>3 x}"
bulwahn@35955
   115
*)
bulwahn@35955
   116
bulwahn@35955
   117
bulwahn@35955
   118
lemma S\<^isub>3_sound:
bulwahn@35955
   119
"w \<in> S\<^isub>3 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@35955
   120
quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=10]
bulwahn@35955
   121
oops
bulwahn@35955
   122
bulwahn@35955
   123
lemma "\<not> (length w > 2) \<or> \<not> (length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b])"
bulwahn@35955
   124
quickcheck[size=10, generator = predicate_compile_ff_fs]
bulwahn@35955
   125
oops
bulwahn@35955
   126
bulwahn@35955
   127
theorem S\<^isub>3_complete:
bulwahn@35955
   128
"length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. b = x] \<longrightarrow> w \<in> S\<^isub>3"
bulwahn@35955
   129
(*quickcheck[generator=SML]*)
bulwahn@35955
   130
quickcheck[generator=predicate_compile_ff_fs, size=10, iterations=100]
bulwahn@35955
   131
oops
bulwahn@35955
   132
bulwahn@35955
   133
bulwahn@35955
   134
inductive_set S\<^isub>4 and A\<^isub>4 and B\<^isub>4 where
bulwahn@35955
   135
  "[] \<in> S\<^isub>4"
bulwahn@35955
   136
| "w \<in> A\<^isub>4 \<Longrightarrow> b # w \<in> S\<^isub>4"
bulwahn@35955
   137
| "w \<in> B\<^isub>4 \<Longrightarrow> a # w \<in> S\<^isub>4"
bulwahn@35955
   138
| "w \<in> S\<^isub>4 \<Longrightarrow> a # w \<in> A\<^isub>4"
bulwahn@35955
   139
| "\<lbrakk>v \<in> A\<^isub>4; w \<in> A\<^isub>4\<rbrakk> \<Longrightarrow> b # v @ w \<in> A\<^isub>4"
bulwahn@35955
   140
| "w \<in> S\<^isub>4 \<Longrightarrow> b # w \<in> B\<^isub>4"
bulwahn@35955
   141
| "\<lbrakk>v \<in> B\<^isub>4; w \<in> B\<^isub>4\<rbrakk> \<Longrightarrow> a # v @ w \<in> B\<^isub>4"
bulwahn@35955
   142
bulwahn@35955
   143
theorem S\<^isub>4_sound:
bulwahn@35955
   144
"w \<in> S\<^isub>4 \<longrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b]"
bulwahn@35955
   145
quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1]
bulwahn@35955
   146
oops
bulwahn@35955
   147
bulwahn@35955
   148
theorem S\<^isub>4_complete:
bulwahn@35955
   149
"length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] \<longrightarrow> w \<in> S\<^isub>4"
bulwahn@35955
   150
quickcheck[generator = predicate_compile_ff_nofs, size=5, iterations=1]
bulwahn@35955
   151
oops
bulwahn@35955
   152
bulwahn@35955
   153
hide const a b
bulwahn@35955
   154
bulwahn@35955
   155
subsection {* Lexicographic order *}
bulwahn@35955
   156
(* TODO *)
bulwahn@35955
   157
(*
bulwahn@35955
   158
lemma
bulwahn@35955
   159
  "(u, v) : lexord r ==> (x @ u, y @ v) : lexord r"
bulwahn@35955
   160
oops
bulwahn@35955
   161
*)
bulwahn@35955
   162
subsection {* IMP *}
bulwahn@35955
   163
bulwahn@35955
   164
types
bulwahn@35955
   165
  var = nat
bulwahn@35955
   166
  state = "int list"
bulwahn@35955
   167
bulwahn@35955
   168
datatype com =
bulwahn@35955
   169
  Skip |
bulwahn@35955
   170
  Ass var "int" |
bulwahn@35955
   171
  Seq com com |
bulwahn@35955
   172
  IF "state list" com com |
bulwahn@35955
   173
  While "state list" com
bulwahn@35955
   174
bulwahn@35955
   175
inductive exec :: "com => state => state => bool" where
bulwahn@35955
   176
  "exec Skip s s" |
bulwahn@35955
   177
  "exec (Ass x e) s (s[x := e])" |
bulwahn@35955
   178
  "exec c1 s1 s2 ==> exec c2 s2 s3 ==> exec (Seq c1 c2) s1 s3" |
bulwahn@35955
   179
  "s \<in> set b ==> exec c1 s t ==> exec (IF b c1 c2) s t" |
bulwahn@35955
   180
  "s \<notin> set b ==> exec c2 s t ==> exec (IF b c1 c2) s t" |
bulwahn@35955
   181
  "s \<notin> set b ==> exec (While b c) s s" |
bulwahn@35955
   182
  "s1 \<in> set b ==> exec c s1 s2 ==> exec (While b c) s2 s3 ==> exec (While b c) s1 s3"
bulwahn@35955
   183
bulwahn@35955
   184
code_pred [random_dseq] exec .
bulwahn@35955
   185
bulwahn@35955
   186
values [random_dseq 1, 2, 3] 10 "{(c, s, s'). exec c s s'}"
bulwahn@35955
   187
bulwahn@35955
   188
lemma
bulwahn@35955
   189
  "exec c s s' ==> exec (Seq c c) s s'"
bulwahn@35955
   190
(*quickcheck[generator = predicate_compile_wo_ff, size=2, iterations=10]*)
bulwahn@35955
   191
oops
bulwahn@35955
   192
bulwahn@35955
   193
subsection {* Lambda *}
bulwahn@35955
   194
bulwahn@35955
   195
datatype type =
bulwahn@35955
   196
    Atom nat
bulwahn@35955
   197
  | Fun type type    (infixr "\<Rightarrow>" 200)
bulwahn@35955
   198
bulwahn@35955
   199
datatype dB =
bulwahn@35955
   200
    Var nat
bulwahn@35955
   201
  | App dB dB (infixl "\<degree>" 200)
bulwahn@35955
   202
  | Abs type dB
bulwahn@35955
   203
bulwahn@35955
   204
primrec
bulwahn@35955
   205
  nth_el :: "'a list \<Rightarrow> nat \<Rightarrow> 'a option" ("_\<langle>_\<rangle>" [90, 0] 91)
bulwahn@35955
   206
where
bulwahn@35955
   207
  "[]\<langle>i\<rangle> = None"
bulwahn@35955
   208
| "(x # xs)\<langle>i\<rangle> = (case i of 0 \<Rightarrow> Some x | Suc j \<Rightarrow> xs \<langle>j\<rangle>)"
bulwahn@35955
   209
bulwahn@35955
   210
inductive nth_el' :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> bool"
bulwahn@35955
   211
where
bulwahn@35955
   212
  "nth_el' (x # xs) 0 x"
bulwahn@35955
   213
| "nth_el' xs i y \<Longrightarrow> nth_el' (x # xs) (Suc i) y"
bulwahn@35955
   214
bulwahn@35955
   215
inductive typing :: "type list \<Rightarrow> dB \<Rightarrow> type \<Rightarrow> bool"  ("_ \<turnstile> _ : _" [50, 50, 50] 50)
bulwahn@35955
   216
  where
bulwahn@35955
   217
    Var [intro!]: "nth_el' env x T \<Longrightarrow> env \<turnstile> Var x : T"
bulwahn@35955
   218
  | Abs [intro!]: "T # env \<turnstile> t : U \<Longrightarrow> env \<turnstile> Abs T t : (T \<Rightarrow> U)"
bulwahn@35955
   219
  | App [intro!]: "env \<turnstile> s : U \<Rightarrow> T \<Longrightarrow> env \<turnstile> t : T \<Longrightarrow> env \<turnstile> (s \<degree> t) : U"
bulwahn@35955
   220
bulwahn@35955
   221
primrec
bulwahn@35955
   222
  lift :: "[dB, nat] => dB"
bulwahn@35955
   223
where
bulwahn@35955
   224
    "lift (Var i) k = (if i < k then Var i else Var (i + 1))"
bulwahn@35955
   225
  | "lift (s \<degree> t) k = lift s k \<degree> lift t k"
bulwahn@35955
   226
  | "lift (Abs T s) k = Abs T (lift s (k + 1))"
bulwahn@35955
   227
bulwahn@35955
   228
primrec
bulwahn@35955
   229
  subst :: "[dB, dB, nat] => dB"  ("_[_'/_]" [300, 0, 0] 300)
bulwahn@35955
   230
where
bulwahn@35955
   231
    subst_Var: "(Var i)[s/k] =
bulwahn@35955
   232
      (if k < i then Var (i - 1) else if i = k then s else Var i)"
bulwahn@35955
   233
  | subst_App: "(t \<degree> u)[s/k] = t[s/k] \<degree> u[s/k]"
bulwahn@35955
   234
  | subst_Abs: "(Abs T t)[s/k] = Abs T (t[lift s 0 / k+1])"
bulwahn@35955
   235
bulwahn@35955
   236
inductive beta :: "[dB, dB] => bool"  (infixl "\<rightarrow>\<^sub>\<beta>" 50)
bulwahn@35955
   237
  where
bulwahn@35955
   238
    beta [simp, intro!]: "Abs T s \<degree> t \<rightarrow>\<^sub>\<beta> s[t/0]"
bulwahn@35955
   239
  | appL [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> s \<degree> u \<rightarrow>\<^sub>\<beta> t \<degree> u"
bulwahn@35955
   240
  | appR [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> u \<degree> s \<rightarrow>\<^sub>\<beta> u \<degree> t"
bulwahn@35955
   241
  | abs [simp, intro!]: "s \<rightarrow>\<^sub>\<beta> t ==> Abs T s \<rightarrow>\<^sub>\<beta> Abs T t"
bulwahn@35955
   242
bulwahn@35955
   243
lemma
bulwahn@35955
   244
  "\<Gamma> \<turnstile> t : U \<Longrightarrow> t \<rightarrow>\<^sub>\<beta> t' \<Longrightarrow> \<Gamma> \<turnstile> t' : U"
bulwahn@35955
   245
quickcheck[generator = predicate_compile_ff_fs, size = 7, iterations = 10]
bulwahn@35955
   246
oops
bulwahn@35955
   247
bulwahn@35955
   248
subsection {* JAD *}
bulwahn@35955
   249
bulwahn@35955
   250
definition matrix :: "('a :: semiring_0) list list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
bulwahn@35955
   251
  "matrix M rs cs \<longleftrightarrow> (\<forall> row \<in> set M. length row = cs) \<and> length M = rs"
bulwahn@35955
   252
(*
bulwahn@35955
   253
code_pred [random_dseq inductify] matrix .
bulwahn@35955
   254
thm matrix.random_dseq_equation
bulwahn@35955
   255
bulwahn@35955
   256
thm matrix_aux.random_dseq_equation
bulwahn@35955
   257
bulwahn@35955
   258
values [random_dseq 3, 2] 10 "{(M, rs, cs). matrix (M:: int list list) rs cs}"
bulwahn@35955
   259
*)
bulwahn@35955
   260
lemma [code_pred_intro]:
bulwahn@35955
   261
  "matrix [] 0 m"
bulwahn@35955
   262
  "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m"
bulwahn@35955
   263
proof -
bulwahn@35955
   264
  show "matrix [] 0 m" unfolding matrix_def by auto
bulwahn@35955
   265
next
bulwahn@35955
   266
  show "matrix xss n m ==> length xs = m ==> matrix (xs # xss) (Suc n) m"
bulwahn@35955
   267
    unfolding matrix_def by auto
bulwahn@35955
   268
qed
bulwahn@35955
   269
bulwahn@35955
   270
code_pred [random_dseq inductify] matrix
bulwahn@35955
   271
  apply (cases x)
bulwahn@35955
   272
  unfolding matrix_def apply fastsimp
bulwahn@35955
   273
  apply fastsimp done
bulwahn@35955
   274
bulwahn@35955
   275
bulwahn@35955
   276
values [random_dseq 2, 2, 15] 6 "{(M::int list list, n, m). matrix M n m}"
bulwahn@35955
   277
bulwahn@35955
   278
definition "scalar_product v w = (\<Sum> (x, y)\<leftarrow>zip v w. x * y)"
bulwahn@35955
   279
bulwahn@35955
   280
definition mv :: "('a \<Colon> semiring_0) list list \<Rightarrow> 'a list \<Rightarrow> 'a list"
bulwahn@35955
   281
  where [simp]: "mv M v = map (scalar_product v) M"
bulwahn@35955
   282
text {*
bulwahn@35955
   283
  This defines the matrix vector multiplication. To work properly @{term
bulwahn@35955
   284
"matrix M m n \<and> length v = n"} must hold.
bulwahn@35955
   285
*}
bulwahn@35955
   286
bulwahn@35955
   287
subsection "Compressed matrix"
bulwahn@35955
   288
bulwahn@35955
   289
definition "sparsify xs = [i \<leftarrow> zip [0..<length xs] xs. snd i \<noteq> 0]"
bulwahn@35955
   290
(*
bulwahn@35955
   291
lemma sparsify_length: "(i, x) \<in> set (sparsify xs) \<Longrightarrow> i < length xs"
bulwahn@35955
   292
  by (auto simp: sparsify_def set_zip)
bulwahn@35955
   293
bulwahn@35955
   294
lemma listsum_sparsify[simp]:
bulwahn@35955
   295
  fixes v :: "('a \<Colon> semiring_0) list"
bulwahn@35955
   296
  assumes "length w = length v"
bulwahn@35955
   297
  shows "(\<Sum>x\<leftarrow>sparsify w. (\<lambda>(i, x). v ! i) x * snd x) = scalar_product v w"
bulwahn@35955
   298
    (is "(\<Sum>x\<leftarrow>_. ?f x) = _")
bulwahn@35955
   299
  unfolding sparsify_def scalar_product_def
bulwahn@35955
   300
  using assms listsum_map_filter[where f="?f" and P="\<lambda> i. snd i \<noteq> (0::'a)"]
bulwahn@35955
   301
  by (simp add: listsum_setsum)
bulwahn@35955
   302
*)
bulwahn@35955
   303
definition [simp]: "unzip w = (map fst w, map snd w)"
bulwahn@35955
   304
bulwahn@35955
   305
primrec insert :: "('a \<Rightarrow> 'b \<Colon> linorder) => 'a \<Rightarrow> 'a list => 'a list" where
bulwahn@35955
   306
  "insert f x [] = [x]" |
bulwahn@35955
   307
  "insert f x (y # ys) = (if f y < f x then y # insert f x ys else x # y # ys)"
bulwahn@35955
   308
bulwahn@35955
   309
primrec sort :: "('a \<Rightarrow> 'b \<Colon> linorder) \<Rightarrow> 'a list => 'a list" where
bulwahn@35955
   310
  "sort f [] = []" |
bulwahn@35955
   311
  "sort f (x # xs) = insert f x (sort f xs)"
bulwahn@35955
   312
bulwahn@35955
   313
definition
bulwahn@35955
   314
  "length_permutate M = (unzip o sort (length o snd)) (zip [0 ..< length M] M)"
bulwahn@35955
   315
(*
bulwahn@35955
   316
definition
bulwahn@35955
   317
  "transpose M = [map (\<lambda> xs. xs ! i) (takeWhile (\<lambda> xs. i < length xs) M). i \<leftarrow> [0 ..< length (M ! 0)]]"
bulwahn@35955
   318
*)
bulwahn@35955
   319
definition
bulwahn@35955
   320
  "inflate upds = foldr (\<lambda> (i, x) upds. upds[i := x]) upds (replicate (length upds) 0)"
bulwahn@35955
   321
bulwahn@35955
   322
definition
bulwahn@35955
   323
  "jad = apsnd transpose o length_permutate o map sparsify"
bulwahn@35955
   324
bulwahn@35955
   325
definition
bulwahn@35955
   326
  "jad_mv v = inflate o split zip o apsnd (map listsum o transpose o map (map (\<lambda> (i, x). v ! i * x)))"
bulwahn@35955
   327
bulwahn@35955
   328
lemma "matrix (M::int list list) rs cs \<Longrightarrow> False"
bulwahn@35955
   329
quickcheck[generator = predicate_compile_ff_nofs, size = 6]
bulwahn@35955
   330
oops
bulwahn@35955
   331
bulwahn@35955
   332
lemma
bulwahn@35955
   333
  "\<lbrakk> matrix M rs cs ; length v = cs \<rbrakk> \<Longrightarrow> jad_mv v (jad M) = mv M v"
bulwahn@35955
   334
quickcheck[generator = predicate_compile_wo_ff]
bulwahn@35955
   335
oops
bulwahn@35955
   336
bulwahn@35955
   337
end