# HG changeset patch # User blanchet # Date 1467845007 -7200 # Node ID 74c609115df0859f8c4a7f521ce5810f12138c52 # Parent 89dd1345a04f71cb5ebb0b8a56de7d6e604566cf updated example diff -r 89dd1345a04f -r 74c609115df0 src/HOL/Corec_Examples/Paper_Examples.thy --- a/src/HOL/Corec_Examples/Paper_Examples.thy Wed Jul 06 23:19:28 2016 +0200 +++ b/src/HOL/Corec_Examples/Paper_Examples.thy Thu Jul 07 00:43:27 2016 +0200 @@ -19,12 +19,11 @@ corec "natsFrom" :: "nat \ nat stream" where "natsFrom n = n \ natsFrom (n + 1)" -corec (friend) addOne :: "nat stream \ nat stream" -where "addOne n = (shd n + 1) \ addOne (stl n)" +corec (friend) add1 :: "nat stream \ nat stream" +where "add1 n = (shd n + 1) \ add1 (stl n)" corec natsFrom' :: "nat \ nat stream" where - "natsFrom' n = n \ addOne (natsFrom' n)" - + "natsFrom' n = n \ add1 (natsFrom' n)" section \Examples from section 3\ @@ -33,30 +32,29 @@ corec (friend) Plus :: "nat stream \ nat stream \ nat stream" (infix "\" 67) where "x\<^sub>1 \ x\<^sub>2 = (shd x\<^sub>1 + shd x\<^sub>2) \ (stl x\<^sub>1 \ stl x\<^sub>2)" - section \Examples from section 4\ -corec collatz :: "nat \ nat stream" where - "collatz n = (if even n \ n > 0 then collatz (n div 2) else n \ collatz (3 * n + 1))" +codatatype 'a llist = LNil | LCons 'a "'a llist" + +corec collatz :: "nat \ nat llist" where + "collatz n = (if n \ 1 then LNil + else if even n then collatz (n div 2) + else LCons n (collatz (3 * n + 1)))" datatype 'a nelist = NEList (hd:'a) (tl:"'a list") -primrec (transfer) postpend :: "'a list \ 'a \ 'a nelist" (infix "\" 64) where +primrec (transfer) snoc :: "'a list \ 'a \ 'a nelist" (infix "\" 64) where "[] \ a = NEList a []" |"(b # bs) \ a = NEList b (bs @ [a])" corec (friend) inter :: "nat stream nelist \ nat stream" where -"inter x = shd (hd x) \ inter (tl x \ stl (hd x))" +"inter xss = shd (hd xss) \ inter (tl xss \ stl (hd xss))" -corec (friend) inter2 :: "nat stream nelist \ nat stream" where -"inter2 x = (case hd x of n \ y \ n \ inter2 (tl x \ y))" +corec (friend) inter' :: "nat stream nelist \ nat stream" where +"inter' xss = (case hd xss of x \ xs \ x \ inter' (tl xss \ xs))" corec zero :: "nat stream" where "zero = 0 \ zero" -corec (friend) inter3 :: "nat stream nelist \ nat stream" where -"inter3 x = shd (stl zero) \ inter3 ([hd x] \ stl (hd x))" - - section \Examples from Blanchette et al. (ICFP 2015)\ corec oneTwos :: "nat stream" where "oneTwos = 1 \ 2 \ oneTwos" @@ -91,7 +89,8 @@ where "tplus t u = Node (val t + val u) (map (\(t', u'). tplus t' u') (zip (sub t) (sub u)))" corec (friend) ttimes :: "tree \ tree \ tree" -where "ttimes t u = Node (val t * val u) (map (\(t , u ). tplus (ttimes t u ) (ttimes t u)) (zip (sub t) (sub u)))" +where "ttimes t u = Node (val t * val u) + (map (\(t, u). tplus (ttimes t u) (ttimes t u)) (zip (sub t) (sub u)))" corecursive primes :: "nat \ nat \ nat stream" where "primes m n =