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/Streams.hs
2012-10-27 22:20:12 +02:00

26 lines
No EOL
690 B
Haskell

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