1
Fork 0
This repository has been archived on 2025-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
coalgebra-homework/Natinfi.hs
2012-10-27 22:20:12 +02:00

35 lines
No EOL
916 B
Haskell

{-# 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)