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.
26 lines
690 B
26 lines
690 B
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
|
|
|
|
import Control.Monad.Instances
|
|
import Coalgebra
|
|
|
|
-- F X = A x X, for a fixed A, this has a functor instance
|
|
type F a = (,) a
|
|
|
|
-- This will give the fixpoint, ie a coalgebra, because F is a functor
|
|
type Stream a = Nu (F a)
|
|
|
|
-- auxiliary functions
|
|
toList :: Stream a -> [a]
|
|
toList s = a0 : toList a' where (a0, a') = psi s
|
|
|
|
|
|
-- example with a very simple (F Int)-coalgebra, 1 -> 2, 2 -> 3, 3 -> 2
|
|
data X = One | Two | Three
|
|
instance Coalgebra (F Int) X where
|
|
psi One = (1, Two)
|
|
psi Two = (2, Three)
|
|
psi Three = (3, Two)
|
|
|
|
main :: IO ()
|
|
main = do
|
|
putStrLn $ show $ take 20 $ toList $ (semantics One :: Stream Int)
|