You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
916 B
35 lines
916 B
{-# 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)
|