diff -r ebfc41a47641 -r 38fefd98c929 src/HOL/Corec_Examples/Paper_Examples.thy --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/HOL/Corec_Examples/Paper_Examples.thy Mon Mar 28 12:05:47 2016 +0200 @@ -0,0 +1,53 @@ +(* Title: HOL/Corec_Examples/Paper_Examples.thy + Author: Andreas Lochbihler, ETH Zuerich + Author: Andrei Popescu, TU Muenchen + Copyright 2016 + +Small examples from the paper. +*) + +section \Small Examples from the Paper\ + +theory Paper_Examples +imports "~~/src/HOL/Library/BNF_Corec" +begin + +subsection \Examples from the introduction\ + +codatatype 'a stream = SCons (hd: 'a) (tl: "'a stream") (infixr "\" 65) + +corec "from" :: "nat \ nat stream" where + "from n = n \ from (n + 1)" + +corec (friend) addOne :: "nat stream \ nat stream" where + "addOne n = (hd n + 1) \ addOne (tl n)" + +corec from' :: "nat \ nat stream" where + "from' n = n \ addOne (from' n)" + + +subsection \Examples from Section 5\ + +text \ +We curry the example functions in this section because infix syntax works only for curried +functions. +\ + +corec (friend) Plus :: "nat stream \ nat stream \ nat stream" (infix "\" 67) where + "x\<^sub>1 \ x\<^sub>2 = (hd x\<^sub>1 + hd x\<^sub>2) \ (tl x\<^sub>1 \ tl x\<^sub>2)" + +corec zeros :: "nat stream" where "zeros = 0 \ zeros" + +corec (friend) Plus' :: "nat stream \ nat stream \ nat stream" (infix "\''" 67) where + "x\<^sub>1 \' x\<^sub>2 = hd x\<^sub>1 \ (tl (tl zeros) \' x\<^sub>2)" + +corec (friend) heart :: "nat stream \ nat stream \ nat stream" (infix "\" 67) where + "x\<^sub>1 \ x\<^sub>2 = (hd x\<^sub>1 * hd x\<^sub>2) \ (x\<^sub>2 \ ((x\<^sub>1 \ ((x\<^sub>1 \ tl x\<^sub>2) \ (tl x\<^sub>1 \ x\<^sub>2)))))" + +corec spade :: "nat stream \ nat stream \ nat stream" (infix "\" 67) where + "x\<^sub>1 \ x\<^sub>2 = (hd x\<^sub>1 \ (x\<^sub>1 \ x\<^sub>2)) \ (hd x\<^sub>1 \ ((x\<^sub>1 \ tl x\<^sub>2) \ (tl x\<^sub>1 \ x\<^sub>2)))" + +corec collatz :: "nat \ nat stream" where + "collatz n = (if even n \ n > 0 then collatz (n div 2) else n \ collatz (3 * n + 1))" + +end