Joshua Moerman
12 years ago
4 changed files with 45 additions and 9 deletions
@ -0,0 +1,35 @@ |
|||
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} |
|||
|
|||
import Control.Monad.Instances |
|||
import Coalgebra |
|||
|
|||
-- F X = 1 + X |
|||
type F = Maybe |
|||
|
|||
-- This will give the fixpoint, ie a coalgebra, because F is a functor |
|||
type Natinfi = Nu F |
|||
|
|||
-- The semantics from the following coalgebra to Natinfi is "the selection function" (I hope) |
|||
-- In some sense this behaviour searches through all natural numbers |
|||
instance Coalgebra F (Natinfi -> Bool) where |
|||
psi p |
|||
| p (phi Nothing) == False = Nothing |
|||
| otherwise = Just (\x -> p $ phi $ Just x) |
|||
|
|||
-- On "numbers" bigger that one, return True |
|||
test :: Natinfi -> Bool |
|||
test p = case q of |
|||
Nothing -> True |
|||
Just y -> False |
|||
where q = psi p |
|||
|
|||
-- Of course this will not always terminate! |
|||
toInt :: Natinfi -> Int |
|||
toInt s = case q of |
|||
Nothing -> 0 |
|||
Just y -> 1 + toInt y |
|||
where q = psi s |
|||
|
|||
main :: IO () |
|||
main = do |
|||
putStrLn $ show $ toInt $ (semantics test :: Natinfi) |
Reference in new issue