Skip to content

Instantly share code, notes, and snippets.

@sadache
Created July 1, 2010 14:59
Show Gist options
  • Save sadache/460064 to your computer and use it in GitHub Desktop.
Save sadache/460064 to your computer and use it in GitHub Desktop.
module Main where
class Monad64 a ma mb | ma mb -> a where
(>>==) :: ma -> ( a -> mb) -> mb
data Maybe64 a = Just64 a
|Nothing64
deriving Show
instance Monad64 a (Maybe64 a) (Maybe64 b) where
(>>==) m f = case m of Just64 a -> f a
_ -> Nothing64
instance Monad64 a (Maybe64 a) (Maybe b) where
(>>==) m f = case m of Just64 a -> f a
_ -> Nothing
instance Monad64 a (Maybe a) ([b]) where
(>>==) m f = case m of Just a -> f a
_ -> []
instance Monad64 a (Maybe [a]) (Maybe [b]) where
(>>==) m f = case m of Just xs -> Just [ k | x <- xs , let ks = (maybe [] id) (f x), k <- ks]
_ -> Nothing
main= print another >> print doSomething
doSomething = ((Just64 1) >>== \i ->
Just (i*2)) >>== \j ->
[j]
another= Just [1,2,3] >>== \a ->
if a < 3 then Just [a] else Nothing
@sadache
Copy link
Author

sadache commented Jul 1, 2010

of course you need all GHC extensions on earth... :)

@sadache
Copy link
Author

sadache commented Jul 1, 2010

I guess this gives a lot of flexibility that are not illustrated in this example, including defining monads on * type kinds. I know code can be much cleaner (maybe type families or other extensions) but I guess it shows the point.

It is just that I am wondering why should a category be represented as a * -> * kind...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment