And is this Haskell?

June 30th, 2009 by Conor

> {-# OPTIONS_GHC -F -pgmF she #-}
> {-# LANGUAGE TypeOperators #-}

> module Fix where

> data Fix f = In (f (Fix f))

> newtype (:+:) f g x = Plus (Either (f x) (g x))
> newtype (:*:) f g x = Times (f x, g x)
> newtype K a x = K a
> newtype I x = I x

> type ListF x = K () :+: (K x :*: I)

> type List x = Fix (ListF x)

> pattern NilF = Plus (Left (K ()))
> pattern ConsF x xs = Plus (Right (Times (K x, I xs)))
> pattern Nil = In NilF
> pattern Cons x xs = In (ConsF x xs)

> foldFix :: Functor f => (f t -> t) -> Fix f -> t
> foldFix phi (In xs) = phi (fmap (foldFix phi) xs)

> (+++) :: List x -> List x -> List x
> xs +++ ys = foldFix phi xs where
>   phi NilF = ys
>   phi (ConsF x xs) = Cons x xs

> blat :: List x -> [x]
> blat = foldFix phi where
>   phi NilF = []
>   phi (ConsF x xs) = x : xs

> talb :: [x] -> List x
> talb = foldr Cons Nil

> instance (Functor f, Functor g) => Functor (f :+: g) where
>   fmap p (Plus (Left fx)) = Plus (Left (fmap p fx))
>   fmap p (Plus (Right gx)) = Plus (Right (fmap p gx))

> instance (Functor f, Functor g) => Functor (f :*: g) where
>   fmap p (Times (fx, gx)) = Times (fmap p fx, fmap p gx)

> instance Functor (K a) where
>   fmap p (K a) = K a

> instance Functor I where
>   fmap p (I a) = I (p a)

I’ve pushed some patches, and now it is!

Is this Haskell?

June 30th, 2009 by Conor

Time flies like an applicative functor

June 26th, 2009 by Conor

Planet Haskell?

June 23rd, 2009 by Conor

Warming Up For Summer

June 23rd, 2009 by Conor

Flushing Buffers

June 21st, 2009 by Conor

Recent Scribbling

May 27th, 2009 by Conor

Whence Orange?

March 6th, 2008 by Conor

Thank goodness for the typechecker

November 28th, 2007 by Conor

November Blip

November 8th, 2007 by Conor

Meaningless Scrawlings

April 17th, 2007 by Conor

Edinburgh Hacking

March 29th, 2007 by Conor

OTT, Cough

February 9th, 2007 by Conor

Less strange, perhaps true

January 30th, 2007 by Conor

Idiomatica.lhs

December 9th, 2006 by Conor

Refactoring Value.lhs

November 28th, 2006 by Conor

It’s not as hard as we thought!

November 23rd, 2006 by Conor

<=>

November 20th, 2006 by Conor

Epigram on Carbon XEmacs

October 22nd, 2006 by Conor

New Year, New Time, New Faces, New Term

October 12th, 2006 by pwm