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