A different Maybe maybe

[Also on http://www.joachim-breitner.de/blog/archives/229-A-different-Maybe-maybe.htm...] Hi, For a while I have been thinking: Isn’t there a way to get rid of the intermediate Maybe construct in a common expression like “fromMaybe default . lookup”. It seems that a way to do that would be to pass more information to the Maybe-generating function: What to do with a Just-Value, and what to return in case of Nothing. This leads to a new definion of the Maybe data type as a function. Later I discovered that this seems to work for any algebraic data type. This is probably nothing new, but was offline at the time of writing, so I didn’t check. This means that this might also be total rubbish. Enjoy. * “Algebraic Data Type Done Differently” (PDF-File) at http://www.joachim-breitner.de/various/FunctionalMaybe.pdf * “Algebraic Data Type Done Differently” (Literate Haskell Source) at http://www.joachim-breitner.de/various/FunctionalMaybe.lhs Comments are appreciated. Greetings from my holidays, Joachim -- Joachim "nomeata" Breitner mail: mail@joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C JID: joachimbreitner@amessage.de | http://www.joachim-breitner.de/ Debian Developer: nomeata@debian.org

Hi
For a while I have been thinking: Isn't there a way to get rid of the intermediate Maybe construct in a common expression like "fromMaybe default . lookup". It seems that a way to do that would be to pass more information to the Maybe-generating function: What to do with a Just-Value, and what to return in case of Nothing. This leads to a new definion of the Maybe data type as a function. Later I discovered that this seems to work for any algebraic data type.
http://www.cs.nott.ac.uk/~nhn/TFP2006/Papers/03-JansenKoopmanPlasmeijer-Effi... Is that the kind of concept you mean? I found this paper quite accessible, so it might be of interest to you. Thanks Neil

Hello, Your definition for Bool reminds me a bit of the definition for booleans in Robert Dockins pure, untyped lambda calculus evaluator: http://www.eecs.tufts.edu/~rdocki01/lambda/prelude.lam http://www.eecs.tufts.edu/~rdocki01/lambda.html j.

Joachim Breitner
[Also on http://www.joachim-breitner.de/blog/archives/229-A-different-Maybe-maybe.htm...]
This is known as the Church encoding of algebraic data types. In this generality, it seems to be first described by Corrado Böhm and Alessandro Berarducci (1985) in "Automatic synthesis of typed L-programs on term algebras", Theoretical Computer Science 39:135-154. -- Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig Web 2.0 is a commemorative coin minted in celebration of the end of the dot-com crash. Like all commemorative coins, it has no actual value. -- Michael Swaine, Dr.Dobb's J., March 2006

On Wed, Mar 07, 2007 at 11:32:08PM +0100, Joachim Breitner wrote:
[Also on http://www.joachim-breitner.de/blog/archives/229-A-different-Maybe-maybe.htm...]
Hi,
For a while I have been thinking: Isn???t there a way to get rid of the intermediate Maybe construct in a common expression like ???fromMaybe default . lookup???. It seems that a way to do that would be to pass more information to the Maybe-generating function: What to do with a Just-Value, and what to return in case of Nothing. This leads to a new definion of the Maybe data type as a function. Later I discovered that this seems to work for any algebraic data type.
This is probably nothing new, but was offline at the time of writing, so I didn???t check. This means that this might also be total rubbish. Enjoy.
* ???Algebraic Data Type Done Differently??? (PDF-File) at http://www.joachim-breitner.de/various/FunctionalMaybe.pdf * ???Algebraic Data Type Done Differently??? (Literate Haskell Source) at http://www.joachim-breitner.de/various/FunctionalMaybe.lhs
Congratulations! You've just reinvented Church encoding. Indeed, calculi such as the untyped lambda calculus and the rankN typed System F omit algebraic datatypes precisely because of the possibility of Church encoding. Some more examples: (|~| x . type is system-f syntax for forall x . type): data Bool = False | True --> |~| r . r -> r -> r (AKA church booleans) data Nat = Z | S Nat --> |~| r . r -> (r -> r) -> r (AKA church numerals, recursive) data Maybe x = Nothing | Just x --> \x::* . |~| r . r -> (x -> r) -> r (Fw) data List x = Nil | Cons x (List x) --> \x::* . |~| r . r -> (x -> r -> r) -> r (Fw, recursive) data Mu f = Mu (f (Mu f)) --> \f::*->* . |~| r . (f r -> r) -> r (Fw, induction) There are also coinductive translations for recursion: data Nat = Z | S Nat --> |~| r . (|~| s . s -> (s -> Maybe s) -> r) -> r data List x = Nil | Cons x (List x) --> \x::* . |~| r . (|~| s . s -> (s -> Either s (s, x)) -> r) -> r data Mu f = Mu (f (Mu f)) --> \f::*->* . |~| r . (|~| s . s -> (s -> f s) -> r) -> r

Congratulations! You've just reinvented Church encoding.
Do not let the multiple responses of how you apparently wasted your time reinventing (or do they mean stealing?) something Church did long ago dampen your enthusiasm to learn exciting things and then share them. I for one am always eager to hear them, and am now reading up on Church encoding so that I can sound as in the know as everybody else on this list. The Nobel-prize-winning Richard Feinmann loved to rederive previously discovered things. On occasion, he even managed to invent new physical theories by finding flaw in them. I suspect he might well have replied to you with somewhat more encouraging words than others have seen fit to offer. Dan

It's not wasted time at all. Discovering things is fun and interesting; it doen't matter if someone else has discovered it first. On Mar 8, 2007, at 02:51 , Dan Weston wrote:
Congratulations! You've just reinvented Church encoding.
Do not let the multiple responses of how you apparently wasted your time reinventing (or do they mean stealing?) something Church did long ago dampen your enthusiasm to learn exciting things and then share them.
I for one am always eager to hear them, and am now reading up on Church encoding so that I can sound as in the know as everybody else on this list.
The Nobel-prize-winning Richard Feinmann loved to rederive previously discovered things. On occasion, he even managed to invent new physical theories by finding flaw in them. I suspect he might well have replied to you with somewhat more encouraging words than others have seen fit to offer.
Dan
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 08/03/07, Dan Weston
Do not let the multiple responses of how you apparently wasted your time reinventing (or do they mean stealing?) something Church did long ago dampen your enthusiasm to learn exciting things and then share them.
I agree with your sentiment that discovering things is fun, no matter how many times it has been done before. But I don't think the other respondents were critical. I read them as properly congratulatory, not sarcastic. It's not everyone that gets to claim they had the same idea as Alonzo Church! ;-) Maybe we need a You Could Have Invented Church Encoding (And Maybe You Already Have) [1] blog post somewhere... D. [1]: http://sigfpe.blogspot.com/2006/08/you-could-have-invented-monads-and.html

I was not implying that the others' responses actually were critical. I was implying that many (especially people newer to Haskell or lacking postgraduate degrees in mathematics or computer science) would interpret them as such, and if not quickly clarified, might discourage non-gurus from contributing new ideas in the off-chance that they are not new and will be shot down. :( Knowledge can only be gained, but the freshness of ideas can only be lost. Thus do libertines and virgins each have something to offer the other. I personally feel that a well-placed emoticon (such as the ;-) you have so effectively used) goes a long way in a medium without face-to-face contact. Dan Dougal Stanton wrote:
On 08/03/07, Dan Weston
wrote: Do not let the multiple responses of how you apparently wasted your time reinventing (or do they mean stealing?) something Church did long ago dampen your enthusiasm to learn exciting things and then share them.
I agree with your sentiment that discovering things is fun, no matter how many times it has been done before. But I don't think the other respondents were critical. I read them as properly congratulatory, not sarcastic. It's not everyone that gets to claim they had the same idea as Alonzo Church! ;-)
Maybe we need a You Could Have Invented Church Encoding (And Maybe You Already Have) [1] blog post somewhere...
D.
[1]: http://sigfpe.blogspot.com/2006/08/you-could-have-invented-monads-and.html _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Joachim,
Don't stop at the booleans. Next you can define the natural numbers by
newtype Number = N (forall a.(a->a) -> (a->a))
--
Dan
On 3/7/07, Joachim Breitner
For a while I have been thinking: Isn't there a way to get rid of the intermediate Maybe construct in a common expression like "fromMaybe default . lookup". It seems that a way to do that would be to pass more information to the Maybe-generating function: What to do with a Just-Value, and what to return in case of Nothing. This leads to a new definion of the Maybe data type as a function. Later I discovered that this seems to work for any algebraic data type.

Hey, welcome to to the group who have reinvented Church encoding of data types. :) -- Lennart On Mar 7, 2007, at 22:32 , Joachim Breitner wrote:
[Also on http://www.joachim-breitner.de/blog/archives/229-A- different-Maybe-maybe.html]
Hi,
For a while I have been thinking: Isn’t there a way to get rid of the intermediate Maybe construct in a common expression like “fromMaybe default . lookup”. It seems that a way to do that would be to pass more information to the Maybe-generating function: What to do with a Just-Value, and what to return in case of Nothing. This leads to a new definion of the Maybe data type as a function. Later I discovered that this seems to work for any algebraic data type.
This is probably nothing new, but was offline at the time of writing, so I didn’t check. This means that this might also be total rubbish. Enjoy.
* “Algebraic Data Type Done Differently” (PDF-File) at http://www.joachim-breitner.de/various/FunctionalMaybe.pdf * “Algebraic Data Type Done Differently” (Literate Haskell Source) at http://www.joachim-breitner.de/various/FunctionalMaybe.lhs
Comments are appreciated.
Greetings from my holidays, Joachim
-- Joachim "nomeata" Breitner mail: mail@joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C JID: joachimbreitner@amessage.de | http://www.joachim-breitner.de/ Debian Developer: nomeata@debian.org _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi, Am Mittwoch, den 07.03.2007, 23:32 +0100 schrieb Joachim Breitner:
[Also on http://www.joachim-breitner.de/blog/archives/229-A-different-Maybe-maybe.htm...] [Skipping Church encoding]
Thanks for all the answers and links. I was expecting to have discovered something that already exists, so I didn’t take it as criticism. Greetings, Joachim -- Joachim Breitner e-Mail: mail@joachim-breitner.de Homepage: http://www.joachim-breitner.de ICQ#: 74513189

G'day all.
Quoting Joachim Breitner
This is probably nothing new, but was offline at the time of writing, so I didn?t check.
One more thing. As you've probably worked out, one way to construct the Church encoding of a type is to realise that the only thing that you can really do with an algebraic data type is do a case-switch on it. Taking Maybe as the example: just :: a -> Maybe a nothing :: Maybe a And a function caseMaybe such that: caseMaybe (just x) j n = j x caseMaybe (nothing) j n = n You can obtain the Church encoding by defining caseMaybe to be id: just x j n = j x nothing j n = n caseMaybe = id You can work out the type of Maybe in the Church encoding by type checking just and nothing, or you can get it straight from the "data" declaration of Maybe. What you may not have realised, is that you can get monad transformers the same way, by explicitly stating that the return type of the continuation is in the underlying monad. As you probably know, Maybe is a monad: type Maybe a = forall b. (a -> b) -> b -> b just x = \j n -> j x nothing = \j n -> n fail _ = nothing return = just (m >>= k) = \j n -> m (\x -> k x j n) n Introduce a monad, m, and replace b by "m b": type MaybeT m a = forall b. (a -> m b) -> m b -> m b justT x = \j n -> j x nothingT = \j n -> n fail _ = nothingT return = justT (m >>= k) = \j n -> m (\x -> k x j n) n lift m = \j n -> m >>= j ...and you have an instant monad transformer. A Maybe transformer is like ErrorT, only without an error message. As an exercise, try this with List: type List a = forall b. (a -> b -> b) -> b -> b type ListT m a = forall b. (a -> m b -> m b) -> m b -> m b If you get stuck, this (unsurprisingly) is the same type as Ralf Hinze's backtracking monad transformer. Cheers, Andrew Bromage
participants (10)
-
ajb@spamcop.net
-
Chung-chieh Shan
-
Dan Piponi
-
Dan Weston
-
Dougal Stanton
-
Jeremy Shaw
-
Joachim Breitner
-
Lennart Augustsson
-
Neil Mitchell
-
Stefan O'Rear