src/HOL/ex/Parallel_Example.thy
author haftmann
Sun, 22 Jul 2012 09:56:34 +0200
changeset 49442 571cb1df0768
child 58269 4044a7d1720f
permissions -rw-r--r--
library theories for debugging and parallel computing using code generation towards Isabelle/ML
haftmann@49442
     1
header {* A simple example demonstrating parallelism for code generated towards Isabelle/ML *}
haftmann@49442
     2
haftmann@49442
     3
theory Parallel_Example
haftmann@49442
     4
imports Complex_Main "~~/src/HOL/Library/Parallel" "~~/src/HOL/Library/Debug"
haftmann@49442
     5
begin
haftmann@49442
     6
haftmann@49442
     7
subsection {* Compute-intensive examples. *}
haftmann@49442
     8
haftmann@49442
     9
subsubsection {* Fragments of the harmonic series *}
haftmann@49442
    10
haftmann@49442
    11
definition harmonic :: "nat \<Rightarrow> rat" where
haftmann@49442
    12
  "harmonic n = listsum (map (\<lambda>n. 1 / of_nat n) [1..<n])"
haftmann@49442
    13
haftmann@49442
    14
haftmann@49442
    15
subsubsection {* The sieve of Erathostenes *}
haftmann@49442
    16
haftmann@49442
    17
text {*
haftmann@49442
    18
  The attentive reader may relate this ad-hoc implementation to the
haftmann@49442
    19
  arithmetic notion of prime numbers as a little exercise.
haftmann@49442
    20
*}
haftmann@49442
    21
haftmann@49442
    22
primrec mark :: "nat \<Rightarrow> nat \<Rightarrow> bool list \<Rightarrow> bool list" where
haftmann@49442
    23
  "mark _ _ [] = []"
haftmann@49442
    24
| "mark m n (p # ps) = (case n of 0 \<Rightarrow> False # mark m m ps
haftmann@49442
    25
    | Suc n \<Rightarrow> p # mark m n ps)"
haftmann@49442
    26
haftmann@49442
    27
lemma length_mark [simp]:
haftmann@49442
    28
  "length (mark m n ps) = length ps"
haftmann@49442
    29
  by (induct ps arbitrary: n) (simp_all split: nat.split)
haftmann@49442
    30
haftmann@49442
    31
function sieve :: "nat \<Rightarrow> bool list \<Rightarrow> bool list" where
haftmann@49442
    32
  "sieve m ps = (case dropWhile Not ps
haftmann@49442
    33
   of [] \<Rightarrow> ps
haftmann@49442
    34
    | p#ps' \<Rightarrow> let n = m - length ps' in takeWhile Not ps @ p # sieve m (mark n n ps'))"
haftmann@49442
    35
by pat_completeness auto
haftmann@49442
    36
haftmann@49442
    37
termination -- {* tuning of this proof is left as an exercise to the reader *}
haftmann@49442
    38
  apply (relation "measure (length \<circ> snd)")
haftmann@49442
    39
  apply rule
haftmann@49442
    40
  apply (auto simp add: length_dropWhile_le)
haftmann@49442
    41
proof -
haftmann@49442
    42
  fix ps qs q
haftmann@49442
    43
  assume "dropWhile Not ps = q # qs"
haftmann@49442
    44
  then have "length (q # qs) = length (dropWhile Not ps)" by simp
haftmann@49442
    45
  then have "length qs < length (dropWhile Not ps)" by simp
haftmann@49442
    46
  moreover have "length (dropWhile Not ps) \<le> length ps"
haftmann@49442
    47
    by (simp add: length_dropWhile_le)
haftmann@49442
    48
  ultimately show "length qs < length ps" by auto
haftmann@49442
    49
qed
haftmann@49442
    50
haftmann@49442
    51
primrec natify :: "nat \<Rightarrow> bool list \<Rightarrow> nat list" where
haftmann@49442
    52
  "natify _ [] = []"
haftmann@49442
    53
| "natify n (p#ps) = (if p then n # natify (Suc n) ps else natify (Suc n) ps)"
haftmann@49442
    54
haftmann@49442
    55
primrec list_primes where
haftmann@49442
    56
  "list_primes (Suc n) = natify 1 (sieve n (False # replicate n True))"
haftmann@49442
    57
haftmann@49442
    58
haftmann@49442
    59
subsubsection {* Naive factorisation *}
haftmann@49442
    60
haftmann@49442
    61
function factorise_from :: "nat \<Rightarrow> nat \<Rightarrow> nat list" where
haftmann@49442
    62
  "factorise_from k n = (if 1 < k \<and> k \<le> n
haftmann@49442
    63
    then
haftmann@49442
    64
      let (q, r) = divmod_nat n k 
haftmann@49442
    65
      in if r = 0 then k # factorise_from k q
haftmann@49442
    66
        else factorise_from (Suc k) n
haftmann@49442
    67
    else [])" 
haftmann@49442
    68
by pat_completeness auto
haftmann@49442
    69
haftmann@49442
    70
termination factorise_from -- {* tuning of this proof is left as an exercise to the reader *}
haftmann@49442
    71
term measure
haftmann@49442
    72
apply (relation "measure (\<lambda>(k, n). 2 * n - k)")
haftmann@49442
    73
apply (auto simp add: prod_eq_iff)
haftmann@49442
    74
apply (case_tac "k \<le> 2 * q")
haftmann@49442
    75
apply (rule diff_less_mono)
haftmann@49442
    76
apply auto
haftmann@49442
    77
done
haftmann@49442
    78
haftmann@49442
    79
definition factorise :: "nat \<Rightarrow> nat list" where
haftmann@49442
    80
  "factorise n = factorise_from 2 n"
haftmann@49442
    81
haftmann@49442
    82
haftmann@49442
    83
subsection {* Concurrent computation via futures *}
haftmann@49442
    84
haftmann@49442
    85
definition computation_harmonic :: "unit \<Rightarrow> rat" where
haftmann@49442
    86
  "computation_harmonic _ = Debug.timing (STR ''harmonic example'') harmonic 300"
haftmann@49442
    87
haftmann@49442
    88
definition computation_primes :: "unit \<Rightarrow> nat list" where
haftmann@49442
    89
  "computation_primes _ = Debug.timing (STR ''primes example'') list_primes 4000"
haftmann@49442
    90
haftmann@49442
    91
definition computation_future :: "unit \<Rightarrow> nat list \<times> rat" where
haftmann@49442
    92
  "computation_future = Debug.timing (STR ''overall computation'')
haftmann@49442
    93
   (\<lambda>() \<Rightarrow> let c = Parallel.fork computation_harmonic
haftmann@49442
    94
     in (computation_primes (), Parallel.join c))"
haftmann@49442
    95
haftmann@49442
    96
value [code] "computation_future ()"
haftmann@49442
    97
haftmann@49442
    98
definition computation_factorise :: "nat \<Rightarrow> nat list" where
haftmann@49442
    99
  "computation_factorise = Debug.timing (STR ''factorise'') factorise"
haftmann@49442
   100
haftmann@49442
   101
definition computation_parallel :: "unit \<Rightarrow> nat list list" where
haftmann@49442
   102
  "computation_parallel _ = Debug.timing (STR ''overall computation'')
haftmann@49442
   103
     (Parallel.map computation_factorise) [20000..<20100]"
haftmann@49442
   104
haftmann@49442
   105
value [code] "computation_parallel ()"
haftmann@49442
   106
haftmann@49442
   107
end
haftmann@49442
   108