Some simple things to help me with my homework and to play around with Haskell.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

34 lines
968 B

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
import Control.Monad.Instances
import Coalgebra
-- F X = A x X, for a fixed A, The prelude already provides the functor instance :)
type F a = (,) a
-- This will give the fixpoint, ie a coalgebra.
type Stream a = Mu (F a)
(+:+) :: a -> Stream a -> Stream a
(+:+) a s = In (a, s)
-- For every other (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)