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. Using StateT with List monad (Nathan H?sken)
2. type signature error in a where clause (Mark Wallace)
3. Re: type signature error in a where clause (Chadda? Fouch?)
4. Re: type signature error in a where clause (Brandon Allbery)
5. Re: type signature error in a where clause (Chadda? Fouch?)
6. Re: type signature error in a where clause (Daniel Fischer)
7. Re: type signature error in a where clause (Brandon Allbery)
----------------------------------------------------------------------
Message: 1
Date: Sat, 24 Nov 2012 14:52:42 +0100
From: Nathan H?sken <[email protected]>
Subject: [Haskell-beginners] Using StateT with List monad
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
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)]?
Thanks!
Nathan
------------------------------
Message: 2
Date: Sat, 24 Nov 2012 22:04:15 +0800
From: Mark Wallace <[email protected]>
Subject: [Haskell-beginners] type signature error in a where clause
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
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? And how
to fix it without changing the structure of the program (i.e. not adding
function `cmp' as a parameter of `merge' etc.).
Thx.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121124/804bde0d/attachment-0001.htm>
------------------------------
Message: 3
Date: Sat, 24 Nov 2012 15:46:55 +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]>
Message-ID:
<CANfjZRbq42TXBCF1ZsuX7fzD2ncMw67eYoH=0R+=bjhhjz_...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Basically your "a" in the signature in your where-clause is not the
same as the "a" from the signature of your whole function so it like
you're writing :
mergesort :: (a -> a -> Ordering) -> [a] -> [a]
mergesort cmp xs = mergeAll (map (\x -> [x]) xs)
where
mergeAll :: [[b]] -> [b]
mergeAll [x] = x
mergeAll xs = mergeAll (mergePairs xs)
mergePairs :: [[c]] -> [[c]]
mergePairs (a:b:xs) = merge a b : mergePairs xs
mergePairs xs = xs
merge :: [d] -> [d] -> [d]
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 since you use "cmp" in "merge" and "cmp" only works with the "a"
from your whole function... There is a conflict : you're saying that
merge can work for any type but cmp only work for the type with which
this mergesort was called.
Now I suppose you wanted your "a" in the where-clause to refer to the
same "a" as the signature of mergesort, but to do that you'll have to
explicitly introduce the "a" type variable with a forall so :
mergesort :: forall a . (a -> a -> Ordering) -> [a] -> [a]
Then the scope of the "a" type variable will extend to the whole
function declaration (where-clause included) instead of being only the
signature in which it appears as is the case by default.
Note that you'll need to authorize the use of the forall keywords by adding :
{-# LANGUAGE ExplicitForall #-}
to the beginning of your file.
--
Jeda?
------------------------------
Message: 4
Date: Sat, 24 Nov 2012 10:00:39 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
clause
To: Chadda? Fouch? <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<cakfcl4xleiie1-2ln1_vspatlykowewvf6ucklkeerlhahl...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sat, Nov 24, 2012 at 9:46 AM, Chadda? Fouch? <[email protected]>wrote:
> Note that you'll need to authorize the use of the forall keywords by
> adding :
>
> {-# LANGUAGE ExplicitForall #-}
>
> to the beginning of your file.
>
I think you forgot {-# LANGUAGE ScopedTypeVariables #-} ?
--
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/842107de/attachment-0001.htm>
------------------------------
Message: 5
Date: Sat, 24 Nov 2012 16:07:39 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
clause
To: Brandon Allbery <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<canfjzrbcf6pqcfxxaj8zuhsha2x+fkw3_hncfxe8gzurhqu...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Right, sorry I should have tested :)
I guess ScopedTypeVariables allows the forall keyword too, like myriad
of other extensions ? (In my opinion, this extension should really be
part of the Haskell standard, it's pretty useful, though not
indispensable)
On Sat, Nov 24, 2012 at 4:00 PM, Brandon Allbery <[email protected]> wrote:
> On Sat, Nov 24, 2012 at 9:46 AM, Chadda? Fouch? <[email protected]>
> wrote:
>>
>> Note that you'll need to authorize the use of the forall keywords by
>> adding :
>>
>> {-# LANGUAGE ExplicitForall #-}
>>
>> to the beginning of your file.
>
>
> I think you forgot {-# LANGUAGE ScopedTypeVariables #-} ?
>
> --
> brandon s allbery kf8nh sine nomine associates
> [email protected] [email protected]
> unix/linux, openafs, kerberos, infrastructure http://sinenomine.net
>
------------------------------
Message: 6
Date: Sat, 24 Nov 2012 16:07:44 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
clause
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
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.
------------------------------
Message: 7
Date: Sat, 24 Nov 2012 10:12:04 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] type signature error in a where
clause
To: Chadda? Fouch? <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<cakfcl4x6birek-h3j1brzazoazz5u6oja4vu6du+hgrqkfy...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sat, Nov 24, 2012 at 10:07 AM, Chadda? Fouch?
<[email protected]>wrote:
> Right, sorry I should have tested :)
>
> I guess ScopedTypeVariables allows the forall keyword too, like myriad
>
Since it's a prerequisite, yes.
(I think almost all uses of ScopedTypeVariables can be replaced by not
specifying type signatures, possibly with `asTypeOf` to help type inference
along, but is sometimes required if you want to write a type signature for
such functions.)
--
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/030462a7/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 53, Issue 30
*****************************************