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
981 B
35 lines
981 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 = Mu (F a)
|
|
|
|
-- basic constructor
|
|
(+:+) :: a -> Stream a -> Stream a
|
|
(+:+) a s = phi (a, s)
|
|
|
|
-- For every (F a)-coalgebra x, there is a arrow x -> Stream a
|
|
-- and it is unique, so `Stream a` is the final (F a)-coalgebra!
|
|
sem :: (Coalgebra (F a) x) => x -> Stream a
|
|
sem x = x0 +:+ sem x' where (x0, x') = psi x
|
|
|
|
-- auxilary 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 $ (sem One :: Stream Int)
|