[Haskell-cafe] Are all monads functions?

2011-12-31 Thread Yves Parès
Hello Café,

One thought occur to me recently when explaining the concept of Monad to
non-haskellers: internally, all standard Monads are newtypes wrapping
functions:
StateT is, WriterT is, ContT is. Even IO and ST are, both conceptually and
in their implementation by GHC.
ParsecT (not part of mtl, but still) is. And so on...

So I'm saying that most of the time, we define a new monad:
- either by building a monad stack of existing transformers (thus melting
their internal functions),
- or by creating a newtype containing a custom function.

Thinking thusly, = would then just be a special function composition
operator: in the spirit of '.' but for a specific type of functions.

So my questions are:
- Is it reasonable to think like that, or are there too many monads that
cannot be defined like that, and then contradict me?
- Is it reasonable to present monads to newcomers by saying : monads are
basically always functions. 'return x' will then be a function that always
return 'x' regardless of its input and = is a special composition for
this occasion.


-- 
The ⊥ is a lie.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Jerzy Karczmarczuk

Yves Parès :

all standard Monads are newtypes wrapping functions

What about Maybe and [] ?

Jerzy Karczmarczuk


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Stephen Tetley
On 31 December 2011 12:26, Jerzy Karczmarczuk
jerzy.karczmarc...@unicaen.fr wrote:
 Yves Parès :

 all standard Monads are newtypes wrapping functions

 What about Maybe and [] ?

And Identity ...

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Yves Parès
Maybe and [] have still the same meaning: they can be seen as functions:
- they represent the result(s) that might or might not have a computation
- *they have to be called/ran/executed* (wichever term you prefer) through
Data.Maybe.maybe or Data.List.foldX, so that we can extract some value out
of them.
It's just that their input is () (void). But in Haskell, the type:
() - Maybe a
is useless, Maybe a is sufficient.

Maybe in that case procedure is then a better term than function.

2011/12/31 Jerzy Karczmarczuk jerzy.karczmarc...@unicaen.fr

 Yves Parès :

  all standard Monads are newtypes wrapping functions

 What about Maybe and [] ?

 Jerzy Karczmarczuk


 __**_
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Roman Cheplyaka
* Yves Parès limestrael+hask...@gmail.com [2011-12-31 13:09:37+0100]
 One thought occur to me recently when explaining the concept of Monad to
 non-haskellers: internally, all standard Monads are newtypes wrapping
 functions:
 StateT is, WriterT is, ContT is. Even IO and ST are, both conceptually and
 in their implementation by GHC.
 ParsecT (not part of mtl, but still) is. And so on...

Writer(T) is not a function, it's just a tuple.

Jerzy already mentioned [] and Maybe.

Another example is a free monad generated by any polynomial functor.
This subsumes Maybe and (almost) [], as described here:
http://blog.omega-prime.co.uk/?p=34

To summarise, since there are only so many ways to form types in Haskell
(sum, product and exponentiation (functions)), it's no surprise that
functions do occur often, but that's not something fundamental to monads.

-- 
Roman I. Cheplyaka :: http://ro-che.info/

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Yves Parès
Thanks for the explanation on free monads, it's interesting.

But still, I maintain my previous view. I could clarify that by saying that
(e.g. for Maybe) we could separate it in two types, Maybe itself and its
monad:

-- The plain Maybe type
data Maybe a = Just a | Nothing

-- The MaybeMonad
newtype MaybeMonad a = MM ( () - Maybe a )

That's what using Maybe as a monad semantically means, doesn't it?

It's just that as I said before, the function () - Maybe a is useless,
thus making the whole type MaybeMonad entirely equivalent to Maybe.

So Roman, as I understand it, a free monad can only be a stateless monad.
So *again*, FreeM is equivalent to :

data FreeM f a = Return a
   | Bind (* () -* f (FreeM f a) )

You cannot express StateT for instance with a functor and FreeM, can you?

But if you change FreeM to be:

data FreeM f s a = Return a
   | Bind (* s -* f (FreeM f a) )

Then maybe it becomes possible...

2011/12/31 Roman Cheplyaka r...@ro-che.info

 * Yves Parès limestrael+hask...@gmail.com [2011-12-31 13:09:37+0100]
  One thought occur to me recently when explaining the concept of Monad to
  non-haskellers: internally, all standard Monads are newtypes wrapping
  functions:
  StateT is, WriterT is, ContT is. Even IO and ST are, both conceptually
 and
  in their implementation by GHC.
  ParsecT (not part of mtl, but still) is. And so on...

 Writer(T) is not a function, it's just a tuple.

 Jerzy already mentioned [] and Maybe.

 Another example is a free monad generated by any polynomial functor.
 This subsumes Maybe and (almost) [], as described here:
 http://blog.omega-prime.coRoman.uk/?p=34http://blog.omega-prime.co.uk/?p=34

 To summarise, since there are only so many ways to form types in Haskell
 (sum, product and exponentiation (functions)), it's no surprise that
 functions do occur often, but that's not something fundamental to monads.

 --
 Roman I. Cheplyaka :: http://ro-che.info/




-- 
The ⊥ is a lie.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Chris Smith
On Dec 31, 2011 8:19 AM, Yves Parès limestrael+hask...@gmail.com wrote:
 -- The plain Maybe type
 data Maybe a = Just a | Nothing

 -- The MaybeMonad
 newtype MaybeMonad a = MM ( () - Maybe a )

 That's what using Maybe as a monad semantically means, doesn't it?

I'd have to say no.  That Maybe types are isomorphic to functions from ()
is not related to their being monads... indeed it's true of all types.  I'm
not sure what meaning you see in the function, but I don't see anything of
monads in it.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Avoiding parametric function binding

2011-12-31 Thread Kevin Quick
I'm having some difficulty avoiding a tight parametric binding of function  
parameters, which is limiting the (de)composability of my expressions.   
I'm curious as to whether there is an option or method I haven't tried to  
achieve this.


Here's an example test case:


data Var = V1 (Maybe Int)
 | V2 (Maybe String)

test = V1 (Just 1)


Given this structure, I can do something like this:


elemStr :: (Show a) = Maybe a - String
elemStr = show

varStr (V1 x) = elemStr $ id x
varStr (V2 x) = elemStr $ id x

main = putStrLn . varStr $ test


This operation extracted the internal value from the Var container, and  
then passes it to a parametric function (id) and that result to another  
parametric function with a class restriction on the input.  This is fine  
so-far.


However, what I'd like to do is decompose this to allow more flexibility  
(using id is pretty boring) without having to repeat the extraction  
boilerplate each time.  My first attempt:



varElem :: forall x . (Show x) = Var - x
varElem (V1 x) = x
varElem (V2 x) = x

main = putStrLn . elemStr . varElem $ test


This fails because even though I've specified the same class constraint  
for the output type of varElem that elemStr requires on its input element,  
the compiler binds the parametric type of x when it processes the first  
(V1) definition and fails with an error on the second definition because  
it asserts that x must be an Int and it found a String.


I realized that the parametric output type was awkward, so I tried  
inverting the design (somewhat similar to fmap):



onVarElem :: forall a . (Show a) = (Maybe a - String) - Var - String
onVarElem f (V1 x) = f x
onVarElem f (V2 x) = f x

main = putStrLn . onVarElem elemStr $ test


This is probably a better design, but still fails for the same reason:

Couldn't match expected type `Int' with actual type `[Char]'
Expected type: Maybe Int
  Actual type: Maybe String
In the first argument of `f', namely `x'
In the expression: f x

Even changing onVarElem so the second parameter was a simple variable and  
performing the pattern match in an internal where or let binding failed  
because the first application of f bind its parametric values.


Is there a way to delay this parametric binding to allow composable  
function specifications?


Thanks,
  Kevin

--
-KQ

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Avoiding parametric function binding

2011-12-31 Thread Stephen Tetley
Maybe you want a deconstructor (sometime called an eliminator)?

deconsVar :: (Maybe Int - a) - (Maybe String - a) - Var - a
deconsVar f g (V1 a) = f a
deconsVar f g (V2 b) = g b

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Avoiding parametric function binding

2011-12-31 Thread Kevin Quick
On Sat, 31 Dec 2011 08:50:05 -0700, Stephen Tetley  
stephen.tet...@gmail.com wrote:



Maybe you want a deconstructor (sometime called an eliminator)?

deconsVar :: (Maybe Int - a) - (Maybe String - a) - Var - a
deconsVar f g (V1 a) = f a
deconsVar f g (V2 b) = g b


That works and has the advantage of allowing a single deconstructor  
definition that can be reused in multiple places, but it requires me to  
add an argument for each wrapped type, which causes some ripple effect if  
the type changes.


Also, if that argument is parametric over the possible inputs (as asserted  
by a class restriction) then it starts to get a bit tedious:


main = putStrLn . deconsVar elemStr elemStr $ test

If I find myself adding a V3 and a V4 to my datatype in the future then  
that would change to:


main = putStrLn . deconsVar elemStr elemStr elemStr elemStr $ test

I was hoping to find a way that would let the functor argument retain its  
parametricity and therefore not need such repetition.


-Kevin

--
-KQ

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Steve Horne

On 31/12/2011 13:18, Yves Parès wrote:
But still, I maintain my previous view. I could clarify that by saying 
that (e.g. for Maybe) we could separate it in two types, Maybe itself 
and its monad:


-- The plain Maybe type
data Maybe a = Just a | Nothing

-- The MaybeMonad
newtype MaybeMonad a = MM ( () - Maybe a )

You've just reminded me of a painful time - lot's a scratching my head 
and saying but these parser functions are monadic - the tutorial 
clearly says they're monadic - why does my every attempt at making the 
type an instance of Monad fail?


Answer - I only had the equivalent of the Maybe type, and I was trying 
to force it where the MaybeMonad should go.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Ertugrul Söylemez
Yves Parès limestrael+hask...@gmail.com wrote:

 But still, I maintain my previous view. I could clarify that by saying
 that (e.g. for Maybe) we could separate it in two types, Maybe itself
 and its monad:

 -- The plain Maybe type
 data Maybe a = Just a | Nothing

 -- The MaybeMonad
 newtype MaybeMonad a = MM ( () - Maybe a )

 That's what using Maybe as a monad semantically means, doesn't it?

That's a statement like the sky is blue.  You can represent any value
as a function of ().  You are saying that every integer is a function.

newtype MyInt = MyInt (() - Int)
newtype My a = My (() - a)

Think of it this way:  There is something like a canonical
representation of every monad.  If you let that one be the one with the
least order (which is reasonable), then no, not every monad's canonical
representation is a function, because the base library definition of
Maybe is the canonical one (order zero).

In that sense every value in maths is a function.  In other words: Your
extension of everything (!) to functions is redundant.

You get the idea.


Greets,
Ertugrul

-- 
nightmare = unsafePerformIO (getWrongWife = sex)
http://ertes.de/


signature.asc
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Avoiding parametric function binding

2011-12-31 Thread Yucheng Zhang
On Sat, Dec 31, 2011 at 11:09 PM, Kevin Quick qu...@sparq.org wrote:
 varElem :: forall x . (Show x) = Var - x
 varElem (V1 x) = x
 varElem (V2 x) = x

 main = putStrLn . elemStr . varElem $ test


The problem here is that you want the return type to depend on the
'value' of Var,
which is not known until runtime.

Maybe you can wrap the 'conflicting' return type inside an existential type:

data VarWrap = forall x . (Show x) = Wrap x

instance Show VarWrap where
show (Wrap x) = show x

varElem :: Var - VarWrap
varElem (V1 x) = Wrap x
varElem (V2 x) = Wrap x

You will need an 'ExistentialQuantification' language extension to do this.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Are all monads functions?

2011-12-31 Thread Conal Elliott

 In that sense every value in maths is a function.  In other words: Your
 extension of everything (!) to functions is redundant.


And function is not unique in this way. All types can be embedded into
pairs also, e.g., newtype MyInt = MyInt ((),Int), or newtype MyInt = MyInt
(((),Int),()), etc.

  - Conal

2011/12/31 Ertugrul Söylemez e...@ertes.de

  Yves Parès limestrael+hask...@gmail.com wrote:

  But still, I maintain my previous view. I could clarify that by saying
  that (e.g. for Maybe) we could separate it in two types, Maybe itself
  and its monad:
 
  -- The plain Maybe type
  data Maybe a = Just a | Nothing
 
  -- The MaybeMonad
  newtype MaybeMonad a = MM ( () - Maybe a )
 
  That's what using Maybe as a monad semantically means, doesn't it?

 That's a statement like the sky is blue.  You can represent any value
 as a function of ().  You are saying that every integer is a function.

newtype MyInt = MyInt (() - Int)
newtype My a = My (() - a)

 Think of it this way:  There is something like a canonical
 representation of every monad.  If you let that one be the one with the
 least order (which is reasonable), then no, not every monad's canonical
 representation is a function, because the base library definition of
 Maybe is the canonical one (order zero).

 In that sense every value in maths is a function.  In other words: Your
 extension of everything (!) to functions is redundant.

 You get the idea.


 Greets,
 Ertugrul


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe