Our proof proceeds by structural induction on a.
For the base case let a be Zero. plus Zero Zero = match Zero with |Zero -> Zero | Succ(c) -> let r = plus c b in Succ(r) so we know plus Zero Zero = Zero.
Let a be a nat. Assume the inductive hypothesis that plus a Zero = a. We now show that plus (Succ(a)) Zero = Succ(a) this is obvious since plus (Succ(a)) Zero = match a with |Zero -> Zero | Succ(a) -> let r = plus a Zero in Succ(r) = let r = a in Succ(r) = Succ(a)
Thus, by induction plus a Zero = a for all a in nat
- For the base case let a be Zero.
plus Zero Zero = match Zero with | Zero -> Zero | Succ(c) -> (let r = plus c b in Succ(r)) = Zeroso we knowplus Zero Zero = Zero. - Let
abe a nat. Assume the inductive hypothesis thatplus a Zero = a. We now show thatplus (Succ(a)) Zero = Succ(a). This is obvious sinceplus (Succ(a)) Zero = match (Succ(a)) with | Zero -> Zero | Succ(c) -> (let r = plus c Zero in Succ(r)) = (let r = plus a Zero in Succ(r)) = (let r = a in Succ(r)) = Succ(a) - Thus, by induction
plus a Zero = afor allain nat
data Stream a = StreamSCons a (Stream a) --"data" not "newtype"
and this is a perfectly valid definition. Whats more, we can perform co-recursion on this co-data. Actually, it is possible for a function to be both co-recursive and recursive. While recursion was defined by the function having a domain consisting of data, co-recursion just means it has a co-domain (also called the range) that is co-data. Primitive
While recursion was defined by the function having a domain consisting of data, co-recursion just means it has a co-domain (also called the range) that is co-data.
Primitive recursion meant always "calling oneself" on smaller data until reaching some smallest data. Primitive co-recursion always "calls itself" on data greater than or equal to what you had before.
fibs = 0:1:zipWith (+) fibs (tail fibs)
fib' n = fibs !! n --the !! is haskell syntax for index"at atindex"
an interesting example of induction/coinduction is proving that these two definitions, fib and fib', compute the same thing. This is left as an exercise for the reader.