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. Wrong type inferred for polymorphic function in a case
alternative (Ramin Honary)
2. Re: Wrong type inferred for polymorphic function in a case
alternative (Brent Yorgey)
3. Re: Wrong type inferred for polymorphic function in a case
alternative (Ramin Honary)
4. Converting a particular warning into a fatal error.
(Michael Litchard)
5. Re: netwire and "dynamic" list of objects (Ertugrul S?ylemez)
6. Re: A "show" error (Lyndon Maydwell)
----------------------------------------------------------------------
Message: 1
Date: Thu, 12 Apr 2012 00:06:28 +0900
From: Ramin Honary <[email protected]>
Subject: [Haskell-beginners] Wrong type inferred for polymorphic
function in a case alternative
To: [email protected]
Message-ID:
<cafhnqtqjnsx+m98duftrl6ve4rpomh0t3qe-2cympascvbf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi everyone,
I am using GHC 7.0.4.
I boiled-down my problem into a simple program which I attached to
this message. I don't know how to get the program to compile.
I need to pass arbitrary class member functions as a parameter to a
very large, complex, polymorphic function called "apply", and have
this class member function applied to abitrary fields of data
constructors of a very large, complex data type.
What I expect to happen is this:
For each case alternative, GHC will infer the type of the polymorphic
function from the matched pattern in the case alternative, and use
that inference to select the correct instance function for that type.
So for case alternative (TA x, TA y) -> TA (fn x y), the correct
instance for "fn" is selected based on the type of "x" and "y" which
are determined by the pattern match, and by the type of "TA". If that
case alternative does not match, then it should try the next
alternative (TB x, TB y) -> TB (fn x y), and since "fn" is
polymorphic, it will still be able to select the correct instance for
the type given by the pattern match. So I can pass "f1" or "f2" or
"f3" to "apply" as the "fn" parameter, and have the correct instance
of "f1" or "f2" or "f3" be selected depending on where in the "case"
expression "fn" is used.
What is actually happening is:
GHC arbitrarily selects one of the case alternatives (usually the last
case alternative, sometimes the first) and decides that from now on
this one type is the type for "fn" for every case alternative. So for
every other case alternative, it decides that "fn" is of the wrong
type and rejects the program, rather than trying to infer a different
type for "fn" specific to this case alternative. Said another way, the
type inference algorithm is being too strict, and I would like it to
be more lazy, computing the type once per each case alternative,
rather than computing the type only once for the entire case
statement. Is there a language option for that?
I have tried using language options, like -XNoMonoPatBinds
-XNoMonomorphismRestriction -XMonoLocalBinds -XPolymorphicComponents
-XImpredicativeTypes, but none of those help. My current solution is
to use Template Haskell and make the "apply" function a splice, so the
whole case statement is copied to every place it is used (which takes
a very, very long for it time to comple). Is there a way to do this
without Template Haskell? Can I get GHC to behave as I expected it to?
Thanks,
Ramin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ABC.hs
Type: application/octet-stream
Size: 2067 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120412/2f74c3f9/attachment-0001.obj>
------------------------------
Message: 2
Date: Wed, 11 Apr 2012 11:31:32 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Wrong type inferred for polymorphic
function in a case alternative
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hi Ramin,
What you need is Rank2Types:
{-# LANGUAGE Rank2Types #-}
module ABC where
...
apply :: (forall a. TClass a => (a -> a -> a)) -> T -> T -> T
apply fn x y =
case (x, y) of
(TA x, TA y) -> TA (fn x y)
(TB x, TB y) -> TB (fn x y)
(TC x, TC y) -> TC (fn x y)
_ -> error "T constructors do not match"
The problem with the old type signature of apply:
apply :: TClass a => (a -> a -> a) -> T -> T -> T
is that this type means that the *caller* of apply gets to choose the
type 'a', and may provide some function which only works for some
particular type a (which must be an instance of TClass). But that's
not what you want. The new type:
apply :: (forall a. TClass a => (a -> a -> a)) -> T -> T -> T
means that the caller must provide a function which is guaranteed to
work for *any* instance of TClass. (Note the 'forall' and the extra
parentheses.)
-Brent
On Thu, Apr 12, 2012 at 12:06:28AM +0900, Ramin Honary wrote:
> Hi everyone,
>
> I am using GHC 7.0.4.
> I boiled-down my problem into a simple program which I attached to
> this message. I don't know how to get the program to compile.
>
> I need to pass arbitrary class member functions as a parameter to a
> very large, complex, polymorphic function called "apply", and have
> this class member function applied to abitrary fields of data
> constructors of a very large, complex data type.
>
> What I expect to happen is this:
> For each case alternative, GHC will infer the type of the polymorphic
> function from the matched pattern in the case alternative, and use
> that inference to select the correct instance function for that type.
> So for case alternative (TA x, TA y) -> TA (fn x y), the correct
> instance for "fn" is selected based on the type of "x" and "y" which
> are determined by the pattern match, and by the type of "TA". If that
> case alternative does not match, then it should try the next
> alternative (TB x, TB y) -> TB (fn x y), and since "fn" is
> polymorphic, it will still be able to select the correct instance for
> the type given by the pattern match. So I can pass "f1" or "f2" or
> "f3" to "apply" as the "fn" parameter, and have the correct instance
> of "f1" or "f2" or "f3" be selected depending on where in the "case"
> expression "fn" is used.
>
> What is actually happening is:
> GHC arbitrarily selects one of the case alternatives (usually the last
> case alternative, sometimes the first) and decides that from now on
> this one type is the type for "fn" for every case alternative. So for
> every other case alternative, it decides that "fn" is of the wrong
> type and rejects the program, rather than trying to infer a different
> type for "fn" specific to this case alternative. Said another way, the
> type inference algorithm is being too strict, and I would like it to
> be more lazy, computing the type once per each case alternative,
> rather than computing the type only once for the entire case
> statement. Is there a language option for that?
>
> I have tried using language options, like -XNoMonoPatBinds
> -XNoMonomorphismRestriction -XMonoLocalBinds -XPolymorphicComponents
> -XImpredicativeTypes, but none of those help. My current solution is
> to use Template Haskell and make the "apply" function a splice, so the
> whole case statement is copied to every place it is used (which takes
> a very, very long for it time to comple). Is there a way to do this
> without Template Haskell? Can I get GHC to behave as I expected it to?
>
> Thanks,
> Ramin
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Thu, 12 Apr 2012 02:01:52 +0900
From: Ramin Honary <[email protected]>
Subject: Re: [Haskell-beginners] Wrong type inferred for polymorphic
function in a case alternative
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
<cafhnqtr0w+ptsdr3cut2bwnjhvggrc3hyzlnogom+ywyoku...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Thanks for your very prompt reply Brent.
I just now gave it a try, and it works just like you said.
Thank you!
On Thu, Apr 12, 2012 at 12:31 AM, Brent Yorgey <[email protected]> wrote:
> Hi Ramin,
>
> What you need is Rank2Types:
>
> ?{-# LANGUAGE Rank2Types #-}
>
> ?module ABC where
>
> ?...
>
> ?apply :: (forall a. TClass a => (a -> a -> a)) -> T -> T -> T
> ?apply fn x y =
> ? ?case (x, y) of
> ? ? ?(TA x, TA y) -> TA (fn x y)
> ? ? ?(TB x, TB y) -> TB (fn x y)
> ? ? ?(TC x, TC y) -> TC (fn x y)
> ? ? ?_ ? ? ? ? ? ?-> error "T constructors do not match"
>
> The problem with the old type signature of apply:
>
> ?apply :: TClass a => (a -> a -> a) -> T -> T -> T
>
> is that this type means that the *caller* of apply gets to choose the
> type 'a', and may provide some function which only works for some
> particular type a (which must be an instance of TClass). ?But that's
> not what you want. ?The new type:
>
> ?apply :: (forall a. TClass a => (a -> a -> a)) -> T -> T -> T
>
> means that the caller must provide a function which is guaranteed to
> work for *any* instance of TClass. ?(Note the 'forall' and the extra
> parentheses.)
>
> -Brent
>
>
> On Thu, Apr 12, 2012 at 12:06:28AM +0900, Ramin Honary wrote:
>> Hi everyone,
>>
>> I am using GHC 7.0.4.
>> I boiled-down my problem into a simple program which I attached to
>> this message. I don't know how to get the program to compile.
>>
>> I need to pass arbitrary class member functions as a parameter to a
>> very large, complex, polymorphic function called "apply", and have
>> this class member function applied to abitrary fields of data
>> constructors of a very large, complex data type.
>>
>> What I expect to happen is this:
>> For each case alternative, GHC will infer the type of the polymorphic
>> function from the matched pattern in the case alternative, and use
>> that inference to select the correct instance function for that type.
>> So for case alternative (TA x, TA y) -> TA (fn x y), the correct
>> instance for "fn" is selected based on the type of "x" and "y" which
>> are determined by the pattern match, and by the type of "TA". If that
>> case alternative does not match, then it should try the next
>> alternative (TB x, TB y) -> TB (fn x y), and since "fn" is
>> polymorphic, it will still be able to select the correct instance for
>> the type given by the pattern match. So I can pass "f1" or "f2" or
>> "f3" to "apply" as the "fn" parameter, and have the correct instance
>> of "f1" or "f2" or "f3" be selected depending on where in the "case"
>> expression "fn" is used.
>>
>> What is actually happening is:
>> GHC arbitrarily selects one of the case alternatives (usually the last
>> case alternative, sometimes the first) and decides that from now on
>> this one type is the type for "fn" for every case alternative. So for
>> every other case alternative, it decides that "fn" is of the wrong
>> type and rejects the program, rather than trying to infer a different
>> type for "fn" specific to this case alternative. Said another way, the
>> type inference algorithm is being too strict, and I would like it to
>> be more lazy, computing the type once per each case alternative,
>> rather than computing the type only once for the entire case
>> statement. Is there a language option for that?
>>
>> I have tried using language options, like -XNoMonoPatBinds
>> -XNoMonomorphismRestriction -XMonoLocalBinds -XPolymorphicComponents
>> -XImpredicativeTypes, but none of those help. My current solution is
>> to use Template Haskell and make the "apply" function a splice, so the
>> whole case statement is copied to every place it is used (which takes
>> a very, very long for it time to comple). Is there a way to do this
>> without Template Haskell? Can I get GHC to behave as I expected it to?
>>
>> Thanks,
>> ? ? Ramin
>
>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Wed, 11 Apr 2012 11:23:46 -0700
From: Michael Litchard <[email protected]>
Subject: [Haskell-beginners] Converting a particular warning into a
fatal error.
To: [email protected]
Message-ID:
<caezekypqp3+nk4qqxmj2julmavtfzawwqq_4k_+p0x-f4tg...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Is there a way to tell ghc to turn only `incomplete-pattern` warnings
into fatal errors, as opposed to `-Werror`?
------------------------------
Message: 5
Date: Wed, 11 Apr 2012 21:02:06 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] netwire and "dynamic" list of objects
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi there Nathan,
Nathan H?sken <[email protected]> wrote:
> Ok, I made another attempt. I have a function, to output the state of
> one object:
>
> objectWire :: StdGen -> MyWire () ObjectState
Minor note: You do not need to pass random number generators around.
Netwire has native support for noise. See Control.Wire.Prefab.Random.
A WRandom instance is predefined for IO. However I just realized a bug.
I forgot to export the WRandom class. =)
Will be fixed in the next release.
> The random generator is used to create the initial state. Now I want
> to handle a dynamic set of objects. This is my attempt:
>
> [...]
>
> But I do not understand how to use any of Control.Wire.Trans.Combine
> to handle my dynamic set of wires.
While the context wire transformers allow you to have dynamic wire sets
'distribute' is pretty much static. In an older release of Netwire
there used to be a 'manager' wire that was specifically designed to
maintain a set of dynamic wires. I decided to omit it in newer releases
and let library/application authors write a more domain-specific version
of it. You should be able to just use the old code (perhaps with minor
adjustments to fit the new types), which you can find here:
<http://hackage.haskell.org/packages/archive/netwire/1.2.7/
doc/html/FRP-NetWire-Request.html>
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120411/de5b5eec/attachment-0001.pgp>
------------------------------
Message: 6
Date: Thu, 12 Apr 2012 06:49:39 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] A "show" error
To: Brandon Allbery <[email protected]>
Cc: bahad?r altan <[email protected]>, "[email protected]"
<[email protected]>
Message-ID:
<CAM5QZtxwZVdxF8K8NZLVBtyyjQ1U27c=WDhS9Dpcm=kamhh...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Is 6 actually a limit, or is that just what is already defined? Could
a new Show instance for 7-tuples be defined?
On Mon, Mar 12, 2012 at 9:20 AM, Brandon Allbery <[email protected]> wrote:
> On Sun, Mar 11, 2012 at 16:00, bahad?r altan <[email protected]> wrote:
>>
>> I use Hugs. ?And I must use it...
>
>
> That's ... unfortunate. ?Use smaller tuples, then; I believe 6 is the limit
> for its Show and Read instances. ?Or define your own result ADT and derive a
> Show instance for it.
>
> --
> brandon s allbery ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [email protected]
> wandering unix systems administrator (available) ? ? (412) 475-9364 vm/sms
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 46, Issue 14
*****************************************