Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Using StateT with List monad (Brent Yorgey)
   2. Re:  type signature error in a where clause (Mark Wallace)
   3. Re:  type signature error in a where clause (Brandon Allbery)
   4. Re:  type signature error in a where clause (Daniel Fischer)
   5. Re:  Using StateT with List monad (Ozgur Akgun)
   6. Re:  type signature error in a where clause (Chadda? Fouch?)
   7. Re:  Using StateT with List monad (Karl Voelker)
   8. Re:  type signature error in a where clause (Karl Voelker)


----------------------------------------------------------------------

Message: 1
Date: Sat, 24 Nov 2012 10:15:49 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Using StateT with List monad
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On Sat, Nov 24, 2012 at 02:52:42PM +0100, Nathan H?sken wrote:
> Hey,
> 
> I have an example function in the list monad.
> 
> twoSucc :: Int -> [Int]
> twoSucc i = [i+1, i+2]
> 
> Now I want write something similar with the StateT monad transformer.
> 
> twoSucc :: StateT Int [] ()
> twoSucc = do
>   i <- get
>   put (i+1) -- how do I put [i+1,i+2] here?
> 
> As mentioned in the comment, when doing
> 
> runStateT twoSucc n
> 
> it outputs [((),n+1)] (which makes sense). How do I write it that it
> behaves similar to the original twoSucc and outputs [((),n+1),((),n+2)]?

StateT Int [] is an instance of MonadPlus which gives you access to
the nondeterminism of the list monad.  So you can do

  twoSucc :: StateT Int [] ()
  twoSucc = do
    i <- get
    put (i+1) `mplus` put (i+2)

Intuitively, mplus splits the computation into two independent
parallel branches.

-Brent



------------------------------

Message: 2
Date: Sat, 24 Nov 2012 23:32:58 +0800
From: Mark Wallace <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
        clause
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 11/24/2012 11:07 PM, Daniel Fischer wrote:
> On Samstag, 24. November 2012, 22:04:15, Mark Wallace wrote:
>> I'm writing a merge sort function, but I get type error under such
>> implementation:
>>
>>      mergesort :: (a -> a -> Ordering) -> [a] -> [a]
>>      mergesort cmp xs = mergeAll (map (\x -> [x]) xs)
>>           where
>>             mergeAll :: [[a]] -> [a]
>>             mergeAll [x] = x
>>             mergeAll xs = mergeAll (mergePairs xs)
>>
>>             mergePairs :: [[a]] -> [[a]]
>>             mergePairs (a:b:xs) = merge a b : mergePairs xs
>>             mergePairs xs = xs
>>
>>             merge :: [a] -> [a] -> [a]
>>             merge as@(a:as') bs@(b:bs')
>>
>>                 | cmp a b == GT = b : merge as bs'
>>                 | otherwise     = a : merge as' bs
>>
>>             merge [] bs = bs
>>             merge as [] = as
>>
>> And ghc says:
>>
>>           Couldn't match type `a1' with `a'
>>             `a1' is a rigid type variable bound by
>>                  the type signature for merge :: [a1] -> [a1] -> [a1]
>>                  at
>>      /home/ice/Study/Haskell/tutorials/99Questions/21to30.hs:135:7
>>             `a' is a rigid type variable bound by
>>                 the type signature for
>>                   mergesort :: (a -> a -> Ordering) -> [a] -> [a]
>>                 at
>>      /home/ice/Study/Haskell/tutorials/99Questions/21to30.hs:124:1
>>           In the first argument of `cmp', namely `a'
>>           In the first argument of `(==)', namely `cmp a b'
>>           In the expression: cmp a b == GT
>>
>> But if I comment all type signatures, ghc works fine on it.
>> I would really appreciate it if you can point out what causes this
>> question?
> Type variables are implicitly for all-quantified. Thus the type variable a in
> the signatures of the local functions is a fresh type variable and has nothing
> to do with the a from the top-level signature.
>
> It is equivalent to you writing
>
>      merge :: [b] -> [b] -> [b]
>
> except there it is obvious that the type signature is wrong.
>
>> And how to fix it without changing the structure of the program (i.e. not
> adding function `cmp' as a parameter of `merge' etc.).
>
> 1. Just omit the type signatures, they can be inferred.
>
> That's the portable way.
>
> 2. Bring the type variable a into scope
>
>      {-# LANGUAGE ScopedTypeVariables #-}
>
>      mergesort :: forall a. (a-> a-> Ordering) -> [a] -> [a]
>
> then an (unquantified) a in a local type signature refers to the type from the
> top-level signature.
>
> That's a GHC-only (as far as I know) way.
Thanks for answering so fast. And all of your answers are very helpful.
I've tested these two solutions, all works fine.
Now I understand how type signature works in such condition.

Somehow it might seem a bit easier to me to grasp the function of a 
function with the help of type signature.
I'll try just omitting the signatures, it's easier and more handy isn't it?



------------------------------

Message: 3
Date: Sat, 24 Nov 2012 10:37:12 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
        clause
To: Mark Wallace <[email protected]>
Cc: [email protected], Daniel Fischer
        <[email protected]>
Message-ID:
        <cakfcl4u49xn0156yhryeutnpdaubwhpxabrtk1ssuvr_347...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sat, Nov 24, 2012 at 10:32 AM, Mark Wallace <[email protected]> wrote:

> Somehow it might seem a bit easier to me to grasp the function of a
> function with the help of type signature.
> I'll try just omitting the signatures, it's easier and more handy isn't it?


Sometimes, sometimes not.  Type signatures can help localize type errors,
as otherwise ghc can infer an unexpected type and then report it to you at
a different place, making it difficult to untangle where it got the weird
type from.

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix/linux, openafs, kerberos, infrastructure          http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121124/6b83a66d/attachment-0001.htm>

------------------------------

Message: 4
Date: Sat, 24 Nov 2012 16:45:07 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
        clause
To: Mark Wallace <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Samstag, 24. November 2012, 23:32:58, Mark Wallace wrote:
> Somehow it might seem a bit easier to me to grasp the function of a 
> function with the help of type signature.

Definitely, type signatures are a good and unfortunately underused thing.

> I'll try just omitting the signatures, it's easier and more handy isn't it?

You can also just comment them out, so they are there to guide you, but hidden 
from the compiler, so that doesn't complain.

That could be a bit irritating, though, if you forget that the type variables 
in the commented-out signatures refer to the type variables in the top-level 
signature.



------------------------------

Message: 5
Date: Sat, 24 Nov 2012 15:57:10 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Using StateT with List monad
To: Brent Yorgey <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
        <CALzazPDjRRFzHpZEYN9E0Qo+=71xdvn3vpwwtahsz4jftnn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

On 24 November 2012 15:15, Brent Yorgey <[email protected]> wrote:

>   twoSucc :: StateT Int [] ()
>   twoSucc = do
>     i <- get
>     put (i+1) `mplus` put (i+2)
>

Another way of doing the same thing, which I find more intuitive, is the
following:

twoSucc :: StateT Int [] ()
twoSucc = do
    i <- get
    j <- lift [i+1, i+2]
    put j

-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121124/6e4d8149/attachment-0001.htm>

------------------------------

Message: 6
Date: Sat, 24 Nov 2012 18:41:53 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
        clause
To: Mark Wallace <[email protected]>
Cc: beginners <[email protected]>,  Daniel Fischer
        <[email protected]>
Message-ID:
        <CANfjZRZo9yugT7iUE98xZ+ddnw6SFHzaykkoN71Dhc76TTV=g...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Nov 24, 2012 at 4:32 PM, Mark Wallace <[email protected]> wrote:
>
> Somehow it might seem a bit easier to me to grasp the function of a function
> with the help of type signature.
> I'll try just omitting the signatures, it's easier and more handy isn't it?
>

As said, type signature are sometimes very useful to catch a mistake
earlier (if your code _can_ be typed, but don't do what you want it
to) and they're also a bit of automatically checked documentation
(always invaluable). A reasonable compromise is to only put type
signatures on the top-level, that's usually sufficient.

--
Jeda?



------------------------------

Message: 7
Date: Sat, 24 Nov 2012 10:12:03 -0800
From: Karl Voelker <[email protected]>
Subject: Re: [Haskell-beginners] Using StateT with List monad
To: Nathan H?sken <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
        <caffow0xlbsmhqnqexleymsq-xc8fvqmzwxwwfviezjf1edl...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Sat, Nov 24, 2012 at 5:52 AM, Nathan H?sken <[email protected]>wrote:

> I have an example function in the list monad.
>
> twoSucc :: Int -> [Int]
> twoSucc i = [i+1, i+2]
>
> Now I want write something similar with the StateT monad transformer.
>

The simplest addition of StateT would look like this:

twoSucc :: StateT () [] Int
twoSucc i = lift [i+1, i+2]


> twoSucc :: StateT Int [] ()
> twoSucc = do
>   i <- get
>   put (i+1) -- how do I put [i+1,i+2] here?
>

In this transformation, you've moved your Int result from being the monadic
result to being the stored state. I don't know which transformation you
really want.

-Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121124/6aa8afa5/attachment-0001.htm>

------------------------------

Message: 8
Date: Sat, 24 Nov 2012 10:18:59 -0800
From: Karl Voelker <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
        clause
To: Mark Wallace <[email protected]>
Cc: Haskell Beginners <[email protected]>,  Daniel Fischer
        <[email protected]>
Message-ID:
        <CAFfow0wKUr=WNNVyksSM=qzwrsetjclchp5rpnbqnxax2bk...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Sat, Nov 24, 2012 at 7:32 AM, Mark Wallace <[email protected]> wrote:

> Somehow it might seem a bit easier to me to grasp the function of a
> function with the help of type signature.
> I'll try just omitting the signatures, it's easier and more handy isn't it?


Writing down type signatures can be very helpful. I would recommend that,
in a complex situation like this, you move your helper functions to the top
level. That way, you can test and debug each individual helper in ghci.

-Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121124/44b32406/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 53, Issue 31
*****************************************

Reply via email to