src/HOL/ex/Code_Lazy_Demo.thy
changeset 68639 357fca99a65a
child 69597 ff784d5a5bfb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/ex/Code_Lazy_Demo.thy	Sun Jul 15 23:44:38 2018 +0200
     1.3 @@ -0,0 +1,178 @@
     1.4 +(* Author: Andreas Lochbihler, Digital Asset *)
     1.5 +
     1.6 +theory Code_Lazy_Demo imports
     1.7 +  "HOL-Library.Code_Lazy"
     1.8 +  "HOL-Library.Debug"
     1.9 +  "HOL-Library.RBT_Impl"
    1.10 +begin
    1.11 +
    1.12 +text \<open>This theory demonstrates the use of the @{theory "HOL-Library.Code_Lazy"} theory.\<close>
    1.13 +
    1.14 +section \<open>Streams\<close>
    1.15 +
    1.16 +text \<open>Lazy evaluation for streams\<close>
    1.17 +
    1.18 +codatatype 'a stream = 
    1.19 +  SCons (shd: 'a) (stl: "'a stream") (infixr "##" 65)
    1.20 +
    1.21 +primcorec up :: "nat \<Rightarrow> nat stream" where
    1.22 +  "up n = n ## up (n + 1)"
    1.23 +
    1.24 +primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where
    1.25 +  "stake 0 xs = []"
    1.26 +| "stake (Suc n) xs = shd xs # stake n (stl xs)"
    1.27 +
    1.28 +code_thms up stake \<comment> \<open>The original code equations\<close>
    1.29 +
    1.30 +code_lazy_type stream
    1.31 +
    1.32 +code_thms up stake \<comment> \<open>The lazified code equations\<close>
    1.33 +
    1.34 +value "stake 5 (up 3)"
    1.35 +
    1.36 +
    1.37 +section \<open>Finite lazy lists\<close>
    1.38 +
    1.39 +text \<open>Lazy types need not be infinite. We can also have lazy types that are finite.\<close>
    1.40 +
    1.41 +datatype 'a llist
    1.42 +  = LNil ("\<^bold>\<lbrakk>\<^bold>\<rbrakk>") 
    1.43 +  | LCons (lhd: 'a) (ltl: "'a llist") (infixr "###" 65)
    1.44 +
    1.45 +syntax "_llist" :: "args => 'a list"    ("\<^bold>\<lbrakk>(_)\<^bold>\<rbrakk>")
    1.46 +translations
    1.47 +  "\<^bold>\<lbrakk>x, xs\<^bold>\<rbrakk>" == "x###\<^bold>\<lbrakk>xs\<^bold>\<rbrakk>"
    1.48 +  "\<^bold>\<lbrakk>x\<^bold>\<rbrakk>" == "x###\<^bold>\<lbrakk>\<^bold>\<rbrakk>"
    1.49 +
    1.50 +fun lnth :: "nat \<Rightarrow> 'a llist \<Rightarrow> 'a" where
    1.51 +  "lnth 0 (x ### xs) = x"
    1.52 +| "lnth (Suc n) (x ### xs) = lnth n xs"
    1.53 +
    1.54 +definition llist :: "nat llist" where
    1.55 +  "llist = \<^bold>\<lbrakk>1, 2, 3, hd [], 4\<^bold>\<rbrakk>"
    1.56 +
    1.57 +code_lazy_type llist
    1.58 +
    1.59 +value [code] "llist"
    1.60 +value [code] "lnth 2 llist"
    1.61 +value [code] "let x = lnth 2 llist in (x, llist)"
    1.62 +
    1.63 +fun lfilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where
    1.64 +  "lfilter P \<^bold>\<lbrakk>\<^bold>\<rbrakk> = \<^bold>\<lbrakk>\<^bold>\<rbrakk>"
    1.65 +| "lfilter P (x ### xs) = 
    1.66 +   (if P x then x ### lfilter P xs else lfilter P xs)"
    1.67 +
    1.68 +export_code lfilter in SML
    1.69 +
    1.70 +value [code] "lfilter odd llist"
    1.71 +
    1.72 +value [code] "lhd (lfilter odd llist)"
    1.73 +
    1.74 +
    1.75 +section \<open>Iterator for red-black trees\<close>
    1.76 +
    1.77 +text \<open>Thanks to laziness, we do not need to program a complicated iterator for a tree. 
    1.78 +  A conversion function to lazy lists is enough.\<close>
    1.79 +
    1.80 +primrec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist"
    1.81 +  (infixr "@@" 65) where
    1.82 +  "\<^bold>\<lbrakk>\<^bold>\<rbrakk> @@ ys = ys"
    1.83 +| "(x ### xs) @@ ys = x ### (xs @@ ys)"
    1.84 +
    1.85 +primrec rbt_iterator :: "('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) llist" where
    1.86 +  "rbt_iterator rbt.Empty = \<^bold>\<lbrakk>\<^bold>\<rbrakk>"
    1.87 +| "rbt_iterator (Branch _ l k v r) = 
    1.88 +   (let _ = Debug.flush (STR ''tick'') in 
    1.89 +   rbt_iterator l @@ (k, v) ### rbt_iterator r)"
    1.90 +
    1.91 +definition tree :: "(nat, unit) rbt"
    1.92 +  where "tree = fold (\<lambda>k. rbt_insert k ()) [0..<100] rbt.Empty"
    1.93 +
    1.94 +definition find_min :: "('a :: linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) option" where
    1.95 +  "find_min rbt = 
    1.96 +  (case rbt_iterator rbt of \<^bold>\<lbrakk>\<^bold>\<rbrakk> \<Rightarrow> None 
    1.97 +   | kv ### _ \<Rightarrow> Some kv)"
    1.98 +
    1.99 +value "find_min tree" \<comment> \<open>Observe that @{const rbt_iterator} is evaluated only for going down 
   1.100 +  to the first leaf, not for the whole tree (as seen by the ticks).\<close>
   1.101 +
   1.102 +text \<open>With strict lists, the whole tree is converted into a list.\<close>
   1.103 +
   1.104 +deactivate_lazy_type llist
   1.105 +value "find_min tree"
   1.106 +activate_lazy_type llist
   1.107 +
   1.108 +
   1.109 +
   1.110 +section \<open>Branching datatypes\<close>
   1.111 +
   1.112 +datatype tree
   1.113 +  = L              ("\<spadesuit>") 
   1.114 +  | Node tree tree (infix "\<triangle>" 900)
   1.115 +
   1.116 +notation (output) Node ("\<triangle>(//\<^bold>l: _//\<^bold>r: _)")
   1.117 +
   1.118 +code_lazy_type tree
   1.119 +
   1.120 +fun mk_tree :: "nat \<Rightarrow> tree" where mk_tree_0:
   1.121 +  "mk_tree 0 = \<spadesuit>"
   1.122 +| "mk_tree (Suc n) = (let t = mk_tree n in t \<triangle> t)"
   1.123 +
   1.124 +declare mk_tree.simps [code]
   1.125 +
   1.126 +code_thms mk_tree
   1.127 +
   1.128 +function subtree :: "bool list \<Rightarrow> tree \<Rightarrow> tree" where
   1.129 +  "subtree [] t = t"
   1.130 +| "subtree (True # p) (l \<triangle> r) = subtree p l"
   1.131 +| "subtree (False # p) (l \<triangle> r) = subtree p r"
   1.132 +| "subtree _ \<spadesuit> = \<spadesuit>"
   1.133 +  by pat_completeness auto
   1.134 +termination by lexicographic_order
   1.135 +
   1.136 +value [code] "mk_tree 10"
   1.137 +value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t"
   1.138 +  \<comment> \<open>Since @{const mk_tree} shares the two subtrees of a node thanks to the let binding,
   1.139 +      digging into one subtree spreads to the whole tree.\<close>
   1.140 +value [code] "let t = mk_tree 3; _ = subtree [True, True, False, False] t in t"
   1.141 +
   1.142 +lemma mk_tree_Suc_debug [code]: \<comment> \<open>Make the evaluation visible with tracing.\<close>
   1.143 +  "mk_tree (Suc n) = 
   1.144 +  (let _ = Debug.flush (STR ''tick''); t = mk_tree n in t \<triangle> t)"
   1.145 +  by simp
   1.146 +
   1.147 +value [code] "mk_tree 10"
   1.148 +  \<comment> \<open>The recursive call to @{const mk_tree} is not guarded by a lazy constructor,
   1.149 +      so all the suspensions are built up immediately.\<close>
   1.150 +
   1.151 +lemma mk_tree_Suc [code]: "mk_tree (Suc n) = mk_tree n \<triangle> mk_tree n"
   1.152 +  \<comment> \<open>In this code equation, there is no sharing and the recursive calls are guarded by a constructor.\<close>
   1.153 +  by(simp add: Let_def)
   1.154 +
   1.155 +value [code] "mk_tree 10"
   1.156 +value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t"
   1.157 +
   1.158 +lemma mk_tree_Suc_debug' [code]: 
   1.159 +  "mk_tree (Suc n) = (let _ = Debug.flush (STR ''tick'') in mk_tree n \<triangle> mk_tree n)"
   1.160 +  by(simp add: Let_def)
   1.161 +
   1.162 +value [code] "mk_tree 10" \<comment> \<open>Only one tick thanks to the guarding constructor\<close>
   1.163 +value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t"
   1.164 +value [code] "let t = mk_tree 3; _ = subtree [True, True, False, False] t in t"
   1.165 +
   1.166 +
   1.167 +section \<open>Pattern matching elimination\<close>
   1.168 +
   1.169 +text \<open>The pattern matching elimination handles deep pattern matches and overlapping equations
   1.170 + and only eliminates necessary pattern matches.\<close>
   1.171 +
   1.172 +function crazy :: "nat llist llist \<Rightarrow> tree \<Rightarrow> bool \<Rightarrow> unit" where
   1.173 +  "crazy (\<^bold>\<lbrakk>0\<^bold>\<rbrakk> ### xs) _ _    = Debug.flush (1 :: integer)"
   1.174 +| "crazy xs          \<spadesuit> True = Debug.flush (2 :: integer)"
   1.175 +| "crazy xs          t  b   = Debug.flush (3 :: integer)"
   1.176 +  by pat_completeness auto
   1.177 +termination by lexicographic_order
   1.178 +
   1.179 +code_thms crazy
   1.180 +
   1.181 +end
   1.182 \ No newline at end of file