last day (381 days later) » 

1:35 PM
Yay!
Prelude> :type (>>)
(>>) :: (Monad m) => m a -> m b -> m b
Prelude> (>>) :: (Monad m) => m a -> m a -> m a

<interactive>:1:0:
    Ambiguous type variable `m' in the constraint:
      `Monad m'
        arising from an expression type signature at <interactive>:1:0-37
    Probable fix: add a type signature that fixes these type variable(s)
 
I still don't understand why your interpreter accepts sequence [].
 
Why can't I restrict, or why am I not doing it right?
 
You need to restrict it more.
At least the quantification has to go away.
I think GHC has an extension to allow something like you have, but it's not extremely useful.
 
Okay there's something I don't get with the syntax for types here.
Prelude> (>>) :: (Monad m) => m a -> m b -> m b

<interactive>:1:0:
    Ambiguous type variable `m' in the constraint:
      `Monad m'
        arising from an expression type signature at <interactive>:1:0-37
    Probable fix: add a type signature that fixes these type variable(s)
Prelude> :type (>>)
(>>) :: (Monad m) => m a -> m b -> m b
Just parroting what ghci shows me doesn't seem to work.
So far I've thought I had to put the instances of typeclass on the lhs of => and then use lowercase types on the rhs.
 
Try (>>) :: Maybe a -> Maybe b -> Maybe b. You can't force quantified types like that.
 
1:40 PM
Oh okay.
Right, is there a name for the different levels of polymorphism?
 
Hmm, maybe.
But I don't now.
 
i.e. a -> b is what level; Typeclass t => t a -> t b and so on.
 
Types with Foo m => are existentially quantified types.
Wait, no. Even a -> b is an EQT.
 
'There is an instance m of Foo, such that...'?
 
Yes.
a -> b can be written forall a b. a -> b (I think you can coerce GHCi to give you something like that)
Which makes it an EQT.
 
1:44 PM
I thought the forall and others were a further level, heh.
 
Maybe I'm confusing the terms though.
Existential types seems to be a GHC extension.
I don't really know the name of those things then.
 
No matter.
Back to what's at hand; what insight should have I got from sequence [].
 
The interpreter is assuming the list monad for no reason. A full program with that does not pick any particular monad, and thus fails to compile: ideone.com/k718d
No, even in the list monad, sequence [] should give [[]].
 
Mmmh I already know sequence [] has type Monad m => m [a] though.
 
I don't know what the interpreter is doing there.
 
1:50 PM
Ambiguous type variable `m' in the constraint:
      `Monad m' arising from a use of `sequence' at enum.hs:4:5-15
    Possible cause: the monomorphism restriction applied to the following:
      se :: forall a. m [a] (bound at enum.hs:4:0)
    Probable fix: give these definition(s) an explicit type signature
                  or use -XNoMonomorphismRestriction
From let se = sequence [] outside of ghci.
 
As expected.
 
*Main> se
Just []
 
You forced Maybe?
 
Yes.
 
So, do you see now what the "neutral element" you were looking for is?
 
1:54 PM
I'm thinking return [] but can't coerce it quite right.
 
Right, that's the base value of the fold. But you're on the wrong track with >>.
Despite the name sequence doesn't use >> :)
sequence_ can use that though.
Any success?
 
I'm going in all kinds of directions.
 
The problem with >> is that it discards the value of the left hand monad.
And sequence needs to collect them all in a list.
 
Hey, that's a good point!
 
Try figuring out the type of the first argument of the fold (given that the second is return []).
 
2:22 PM
Well, I'm trying to use (>>=) to 'collect' the a from m a in [m a] and to cons it.
 
That might work. Might get ugly, but is feasible.
 
I can't get it to typecheck so far.
 
The type of that function should be Monad m => m a -> m [a] -> m [a]. Is that what you're trying to get?
 
:type (>>=)
(>>=) :: (Monad m) => m a -> (a -> m b) -> m b
 
Right, but the argument to fold is not like that.
 
2:24 PM
@RMartinhoFernandes Right.
 
Are your striving for the one-liner?
It may be easier by not attempting that.
 
Not so much one liner in the sense of 'one line' but in the sense of composition, writing in terms of folds and the like.
I could write the explicit recursion but I know how to do that already (allegedly).
 
Right, not that.
 
But yeah I'm getting a type mismatch using >>=
 
So you receive a wrapped a and a wrapped [a].
To cons, you need both unwrapped.
Does that help?
 
2:28 PM
I've been considering using two >>=, yeah.
 
Right that would work. What do you have?
 
Allow me to typecheck it first.
The lambda expression `\ x y -> x >>= ...' has two arguments,
but its type `[m a]' has none
I keep hitting that, no matter what follows the -> heh.
Nevermind those attempts.
 
You have that as foldr (... lambda ...) (return []) right?
 
Yes.
 
Can't repro that error.
 
2:32 PM
So are you suggesting
I get rid of the return, ridding me of the need for one >>=?
 
No, no, if you're going with >>= you need two.
 
Then I return $ foldr...?
 
Wait. I sense a great confusion there.
sequence is strictly a fold. There's no return afterwards.
sequence = foldr (... lambda ...) (return [])
 
That would defeat the point in the exercise, yes.
 
return $ foldr would never work for IO.
 
2:35 PM
No ordering?
 
foldr could not return a naked [a].
The foldr must return wrapped values when taking wrapped arguments.
 
sequence (\x y -> x >>= \xx -> y >>= \yy -> xx:yy) (return [])
So far.
 
Tell me you mistyped the foldr.
 
Well, I copied verbatim actually.
 
Well, you're almost there, just need to make it sequence = foldr lambda (return []).
 
2:40 PM
I think I've got the double unwrapping correct. Need to tweak the expression inside the lambda.
 
Oh, yeah there's a little detail missing.
> The foldr must return wrapped values when taking wrapped arguments.
 
Is that a higher level yet?
 
Your lambda gives xx:yy, which is a naked list.
 
Well.
Wouldn't >>= do the wrapping?
 
(>>=) :: Monad m => m a -> (a -> m b) -> m b
The second parameter needs to do it itself (returns m b).
 
2:43 PM
Good point!
 
Works now?
 
> Ambiguous type variable `m' in the constraint:
 
What did you evaluate?
 
Missing another return I think, after all I have two >>=.
 
No, that will make a m (m [a]).
 
2:45 PM
let s = foldr (\x y -> x >>= \xx -> y >>= \yy -> return (xx:yy)) (return [])
Time to put that in a source file and constrain it.
 
s = foldr (\x y -> x >>= \xx -> y >>= \yy -> return (xx:yy)) (return []) should work.
Do you have the habit of using let all over from OCaml or something?
 
No, I'm in ghci.
 
Oh, right.
 
Let me see if :make is setup for .hs files.
Great, it is!
 
:)
Wanna see mine?
sequence' = foldr (liftM2 (:)) (return [])
Because using lambdas is not pointfree enough.
 
2:48 PM
Okay, it compiles.
makeprg=ghc -e :q %
Is that good?
 
Where does the lifting stuff resides in?
 
I only ever used ghci and ghc --make directly from the command-line.
@LucDanton Control.Monad.
 
Oh I see. Perfect for one liners.
To adapt pure stuff to monads?
 
Yep.
Unwrap, compute, wrap again.
 
2:52 PM
And the number is arity. That's perfect!
 
Yeah, the language doesn't allow for a liftM for arbitrary arity functions.
 
I don't mind giving up variadic functions given the type system and syntax.
 
If you think about it, it's not only variadic functions that are needed.
 
Needed for what?
 
For a universal liftM.
You would need a kind of polymorphism that made a -> b and a -> b -> c both acceptable as a param.
Function overloading could work too.
They could use the same name then.
 
2:55 PM
Tbh I haven't given too much thought to the matter.
 
Hmm, I wonder if that can be captured in a class.
 
For instance, overloading the name conflicts with partial application/automatic currying at least for some corner cases, doesn't it?
 
@LucDanton Ah, good point.
 
I don't know how Lisps and derived manage. Dynamic type system I assume?
Oh wait. Syntax, no?
 
Maybe. Friedman's book neutered my desire to learn a LISP :(
 
2:58 PM
You don't receive N argument so much as one list of N arguments.
Still tied to the type system since expressing the type of such a list isn't exactly fun.
 
Oh now I understand why Haskell didn't use a class for that like I was experimenting right now.
It needs multi-parameter classes.
And that's a GHC extension.
Even with that it gets ugly pretty fast :( Nevermind.
So, you're building something, or just practicing?
 
3:21 PM
Well I've been reading Real World Haskell for some time now.
But very slowly, something like half a chapter every week.
And now I've finally come to interesting stuff so I'm toying to learn better.
 
Monads are neat, but I like Control.Arrow better.
 
Also I've just set up vim for workflow, too.
I might need to learn Cabal. Have you used that?
 
No, that's a package management system, right?
 
Yep.
class (Control.Category.Category a) => Arrow a where
  arr :: (b -> c) -> a b c
I'm not sure how to parse a b c?
 
a is a diadic type constructor.
 
3:28 PM
class Control.Category.Category cat where
Where can I find out it's diadic?
 
It takes two params. It's inferred.
 
Alright.
 
3:39 PM
Haha I accidentally entered let b = arr b
 
I'm now sure how *** distributes?
 
What happens?
@LucDanton (f *** g) x = (f x, g x).
(for the (->) instance).
 
Guess I've done something wrong, I've given increment and decrement on either side but (0, 0) -> (1, 1) where I would expect (1, -1) for *** then.
 
Works fine here.
How did you use decrement?
Beware that (-1) is "negative one", not "subtract one".
 
3:49 PM
((-) 1) IIRC.
 
((-) 1) x = 1 - x!
 
Yes, that makes sense.
The (+ 1) notation is new to me, that's not possible in OCaml.
 
How do you do it in OCaml? Something like (+) 1?
And flip (-) 1 for decrement?
 
Yeah, prefix notation is available. Which isn't the right thing for -.
 
That's where flip comes in :)
 
3:53 PM
# flip;;
Error: Unbound value flip
 
No standard flip?
Certainly you can write your own, no?
 
Oh, could be. I just don't know the name.
 
4:24 PM
ohhh Haskell
 
Hallo, hallo.
 
Yay, visitors.
 
familiar faces
lol
@RMartinhoFernandes so are you gonna be hanging out here more then?
 
Since we tend to discuss Haskell in the C++ lounge, is it okay if I ask C++ questions here? :)
 
I think I'm going to do a little bit of Haskell every day or close to.
 
4:26 PM
@FredOverflow perhaps yea
 
Let me ownerize some people.
Er, how do I do that again?
 
Is that even possible in a purely functional Channel? :)
 
4:28 PM
lol, does that mean I'm gonna have to learn Haskell now?
damn.
can't even remember if it is an interpreted or compiled language?
 
Can be used as both.
@TonyTheLion Recommended!
 
lol, where the hell to start
 
room topic changed to Haskell: subsets = filterM (const [True, False]) (no tags)
 
I remember I started watching some Channel 9 video's on it
but that's a long time ago
 
room topic changed to Haskell: subsets = filterM (const [True, False]) [haskell]
 
4:30 PM
@TonyTheLion The video series by Erik Meijer? That is brilliant!
@RMartinhoFernandes Topics can be tagged? What's the purpose of doing that?
 
@FredOverflow yes those. Got confused though :(
 
@FredOverflow It's the room that gets tagged. Just like in the lounge. If you refresh you'll see.
 
Ah, so we could have multiple Haskell channels that focus on particular aspects of the language?
And we could also have a dedicated channel for C++/Haskell enthusiasts? :)
 
so what does the tag line mean then?
 
Possibly.
@TonyTheLion It's a definition of the subsets function.
 
4:33 PM
@TonyTheLion It's the implementation of a function that computes all subsets of a set.
 
A pretty awesome use of the list monad.
And pointfree!
 
but not pointless :)
 
I'm a pointfree junkie.
 
@RMartinhoFernandes ah ok
 
Prelude Control.Monad> let subsets = filterM (const [True, False])
Prelude Control.Monad> subsets [1,2,3]
[[1,2,3],[1,2],[1,3],[1],[2,3],[2],[3],[]]
 
4:35 PM
Now try writing that in C++.
 
I'm pretty sure it can't be done in a single line (of reasonable length) :)
 
@FredOverflow so this filters the subset on what condition?
 
@TonyTheLion It doesn't filter the subset, it creates a set of all subsets.
 
Notice that the output is larger than the input.
It's not a "filter" in the traditional sense.
 
@FredOverflow huh, but in my eyes, it merely says (const [True, False])?
 
4:38 PM
That's a function that always returns that list with True and False.
 
You probably don't know currying, so this might be easier to read for you:
let subsets list = filterM (const [True, False]) list
@RMartinhoFernandes The m in filterM stands for "mind-blowing" ;)
 
You need to understand the monadic behaviour of lists.
 
hmmm lots to learn then :)
I've heard of currying
 
You can view that function as making up a list of all permutations of True and False of the length of the original list and then for each permutation picking the elements from the original list whose element in the same position in the permutation has True.
I'm not sure that explanation is simpler.
 
@RMartinhoFernandes Oooh I recall what happened when I shoved lists into lists and the like.
 
4:47 PM
@RMartinhoFernandes I just looked at the Monad instance declaration for lists and found:
m >>= k  = foldr ((++) . k) [] m
Wouldn't this be a lot easer?
m >>= k  = concatMap k m
 
Hmm, yeah, weird. I'd define (>>=) = flip concatMap.
 
You really are a pointfree nazi ;)
 
Maybe concatMap is defined in terms of (>>=)?
 
Laziness purposes perhaps? foldr behaves well in those circumstances, doesn't it?
 
@RMartinhoFernandes Indeed it is:
concatMap f =  foldr ((++) . f) []
 
4:49 PM
concatMap doesn't need to be strict.
 
So why not use concatMap in (>>=)?
 
@RMartinhoFernandes No, but that doesn't mean it behaves well either. But it looks like it does.
 
Hmm. Reduce module dependencies?
concatMap is not in the Prelude, is it?
 
@RMartinhoFernandes Actually, it is.
 
Why does it make sense to consider lists as Monads though?
That sounds like the wrong question. Are there good names for the monad operations as applied to lists?
 
4:54 PM
@RMartinhoFernandes By the way, here is a pointfree version of concatMap:
concatMap = (concat .) . map
 
@LucDanton Er, because they can satisfy the monad laws?
And then you can write generic monad functions and have them work with lists for free.
 
@RMartinhoFernandes Yes, yes. Wrong question.
 
@LucDanton The list monad models nondeterministic computations.
 
The list monad models computations with more than one result.
I think that's better.
(Nondeterministic may give the wrong impression.)
Is square root nondeterministic?
 
> [1, 2, 3] >>= \x -> [x, x + 1, x + 2]
[1,2,3,2,3,4,3,4,5]
@RMartinhoFernandes That makes sense, thanks.
 
4:57 PM
@RMartinhoFernandes No, sqrt has exactly one result, which is always non-negative.
 
And now I'm itching to use it as a monad. Now that was the right question.
 
@FredOverflow Er, ok, let f be a function that computes the solutions of a quadratic equation.
Is it nondeterministic?
 
> liftM2 (+) [1, 2, 3, 4, 5] [8, 9, 0]
[9,10,1,10,11,2,11,12,3,12,13,4,13,14,5]
So much fun.
 
Well, since I'm used to the nondeterminism term from automata theory, I really don't see much difference between deterministically returning a set of results and non-deterministically returning a single result.
 
@FredOverflow Ah. Ok.
 
5:01 PM
But it is probably a lot easier to understand the list monad as you described it.
 

  last day (381 days later) »