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. Is this MR? Type restriction confusion (Mike Erickson)
2. Re: find element of tupels (Daniel Fischer)
3. Re: Is this MR? Type restriction confusion
(Brandon S. Allbery KF8NH)
4. Re: Is this MR? Type restriction confusion (Daniel Fischer)
5. Re: Is this MR? Type restriction confusion (Mike Erickson)
6. Re: Is this MR? Type restriction confusion (Brent Yorgey)
7. Re: find element of tupels (Henk-Jan van Tuyl)
8. Re: find element of tupels (Daniel Fischer)
9. Re: Enum for natural numbers ([email protected])
----------------------------------------------------------------------
Message: 1
Date: Thu, 17 Dec 2009 14:56:13 -0800
From: Mike Erickson <[email protected]>
Subject: [Haskell-beginners] Is this MR? Type restriction confusion
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
I was trying to convert a function,
f1 x l = map (\y -> y*x) l
to so-called point-free style, but in reducing it I found two
functions that seemed to work the same, until I discovered they have
different types, and aren't equivalent:
f1a x = map (*x) -- has type (Num a) => a -> [a] -> [a]
f1b = \x -> map (*x) -- has type Integer -> [Integer] -> [Integer]
Can someone help me understand why the latter has a more restrictive type?
If I add the type signature "f1b :: (Num a) => a -> [a] -> [a]", they
are once again equivalent, but am confused about what's going on. If
this is MR, what exactly am I overloading here?
I read through http://www.haskell.org/haskellwiki/Monomorphism_restriction
and
http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/pitfalls.html.
If anyone has a good reading recommendation here, I'd love to see it.
Because if this is MR, I'm not understanding what MR is correctly.
Also, They don't generate a type error, which makes me think it is not
MR, but something else.
Thanks,
Mike
------------------------------
Message: 2
Date: Fri, 18 Dec 2009 00:17:56 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] find element of tupels
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-15"
Am Donnerstag 17 Dezember 2009 20:44:34 schrieb Henk-Jan van Tuyl:
> On Thu, 17 Dec 2009 17:18:41 +0100, <[email protected]> wrote:
> > thanks Magnus, great, works so far. I added to check that myDay have to
> > be greater 0:
> >
> > legalDate :: Date -> Bool
> > legalDate (myDay, myMonth, myYear) = maybe False id $ do
> > days <- lookup myMonth monthAndMaxDay
> > return (not (myDay <= 0) && (myDay <= days))
> >
> > Another thing is, that I have to check for leapYears. I have a function
> > that checks if a year is a leap year
> >
> > isLeapYear :: Int -> Bool
> > isLeapYear year = mod year 4 == 0
>
> That is not entirely correct; from package time:
> isLeapYear :: Integer -> Bool
> isLeapYear year = (mod year 4 == 0) && ((mod year 400 == 0) || not (mod
> year 100 == 0))
>
> > monthAndMaxDay has 30 days for February which have to be fix. So i have
> > to check in legalDate if the year is a leap year. If so I have to check
> > myDay in February for 29 days, otherwise for 28 days. How can I
> > implement this to isLegalDate?
>
> Use fromGregorianValid from package time.
Or
legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
legalDate (d,m,y)
| m `elem` [Apr,Jun,Sep,Nov] = 0 < d && d < 31
| otherwise = 0 < d && d < 32
>
> Regards,
> Henk-Jan van Tuyl
>
>
> --
> http://Van.Tuyl.eu/
> http://members.chello.nl/hjgtuyl/tourdemonad.html
> --
------------------------------
Message: 3
Date: Thu, 17 Dec 2009 18:35:32 -0500
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Is this MR? Type restriction
confusion
To: Mike Erickson <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Dec 17, 2009, at 17:56 , Mike Erickson wrote:
> f1a x = map (*x) -- has type (Num a) => a -> [a] -> [a]
>
> f1b = \x -> map (*x) -- has type Integer -> [Integer] -> [Integer]
>
> Can someone help me understand why the latter has a more restrictive
> type?
That is indeed the monomorphism restriction.
In very short: if a top level form (like your f1, f1a, f1b) takes no
arguments and does not have an explicit type, its type is restricted
according to defaulting; if no suitable default exists, it is
rejected. In this case, defaulting gave it the type Integer because
Integer is the first type in the defaults list that allows the
declaration to typecheck. (The default "defaults list" is Integer,
Double, and in recent GHC () (the null/unit type).
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20091217/78bdbd6b/PGP-0001.bin
------------------------------
Message: 4
Date: Fri, 18 Dec 2009 00:45:26 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Is this MR? Type restriction
confusion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Donnerstag 17 Dezember 2009 23:56:13 schrieb Mike Erickson:
> I was trying to convert a function,
>
> f1 x l = map (\y -> y*x) l
>
> to so-called point-free style, but in reducing it I found two
> functions that seemed to work the same, until I discovered they have
> different types, and aren't equivalent:
>
> f1a x = map (*x) -- has type (Num a) => a -> [a] -> [a]
>
> f1b = \x -> map (*x) -- has type Integer -> [Integer] -> [Integer]
>
> Can someone help me understand why the latter has a more restrictive type?
Indeed it's the monomorphism restriction.
In
f1a x = map (*x)
, f1a is bound by a *function binding* (bound name and (>= 1) pattern on the
left of '='),
thus it has a polymorphic type. In
f1b = \x -> map (*x)
(btw, you can further point-free this as map . (*)), f1b is bound by a *simple
pattern
binding*, thus the monomorphism restriction says that without a type signature,
it must
have a monomorphic type.
Type inference determines the type (Num a) => a -> [a] -> [a] for the function.
This is a
"type class polymorphic" type [the monomorphism restriction doesn't restrict
unconstrained
types, only if a type class is involved does it kick in], hence by the
monomorphism
restriction, it must have a monomorphic type. The type class is Num, thus by
the default
defaulting rules, for (Num a), a is instantiated as Integer, hence
f1b :: Integer -> [Integer] -> [Integer]
>
> If I add the type signature "f1b :: (Num a) => a -> [a] -> [a]", they
> are once again equivalent, but am confused about what's going on. If
The monomorphism restriction only applies to functions without a type signature.
> this is MR, what exactly am I overloading here?
>
> I read through http://www.haskell.org/haskellwiki/Monomorphism_restriction
> and
> http://www.cs.auckland.ac.nz/references/haskell/haskell-intro-html/pitfalls
>.html. If anyone has a good reading recommendation here, I'd love to see it.
Read the report:http://haskell.org/onlinereport/decls.html#sect4.5.5
(start at section 4.5.4) and
http://haskell.org/onlinereport/decls.html#sect4.4.3 for
pattern and function bindings. It's a little technical, but with the other
resources it
should be understandable.
In short, if you declare a function point-free and the inferred type involves
type
classes, it cannot be polymorphic unless you give a type signature. Without a
type
signature, the implementation tries to determine a monomorphic type from the
inferred
polymorphic type by the defaulting rules
(http://haskell.org/onlinereport/decls.html#sect4.3.4). If that succeeds, this
type is
given to the function, otherwise it's a type error and the module doesn't
compile.
> Because if this is MR, I'm not understanding what MR is correctly. Also,
> They don't generate a type error, which makes me think it is not MR, but
> something else.
>
> Thanks,
>
> Mike
------------------------------
Message: 5
Date: Thu, 17 Dec 2009 16:46:34 -0800
From: Mike Erickson <[email protected]>
Subject: Re: [Haskell-beginners] Is this MR? Type restriction
confusion
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, Dec 17, 2009 at 3:45 PM, Daniel Fischer
<[email protected]> wrote:
> Am Donnerstag 17 Dezember 2009 23:56:13 schrieb Mike Erickson:
>> I was trying to convert a function,
>>
>> f1 x l = map (\y -> y*x) l
>>
>> to so-called point-free style, but in reducing it I found two
>> functions that seemed to work the same, until I discovered they have
>> different types, and aren't equivalent:
>>
>> f1a x = map (*x) -- has type (Num a) => a -> [a] -> [a]
>>
>> f1b = \x -> map (*x) -- has type Integer -> [Integer] -> [Integer]
>>
>> Can someone help me understand why the latter has a more restrictive type?
>
> Indeed it's the monomorphism restriction.
> [...]
This was very helpful. I think I understand, although it's still digesting.
> In short, if you declare a function point-free and the inferred type involves
> type
> classes, it cannot be polymorphic unless you give a type signature. Without a
> type
> signature, the implementation tries to determine a monomorphic type from the
> inferred
> polymorphic type by the defaulting rules
> (http://haskell.org/onlinereport/decls.html#sect4.3.4). If that succeeds,
> this type is
> given to the function, otherwise it's a type error and the module doesn't
> compile.
Shortly after posting (of course) I discovered the online report and
4.5.4 and 4.5.5 were helpful as well.
Thanks to all that responded,
Mike
------------------------------
Message: 6
Date: Thu, 17 Dec 2009 22:41:41 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Is this MR? Type restriction
confusion
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Thu, Dec 17, 2009 at 04:46:34PM -0800, Mike Erickson wrote:
> On Thu, Dec 17, 2009 at 3:45 PM, Daniel Fischer
> <[email protected]> wrote:
> > Am Donnerstag 17 Dezember 2009 23:56:13 schrieb Mike Erickson:
> >> I was trying to convert a function,
> >>
> >> f1 x l = map (\y -> y*x) l
> >>
> >> to so-called point-free style, but in reducing it I found two
> >> functions that seemed to work the same, until I discovered they have
> >> different types, and aren't equivalent:
> >>
> >> f1a x = map (*x) -- has type (Num a) => a -> [a] -> [a]
> >>
> >> f1b = \x -> map (*x) -- has type Integer -> [Integer] -> [Integer]
> >>
> >> Can someone help me understand why the latter has a more restrictive type?
> >
> > Indeed it's the monomorphism restriction.
> > [...]
>
> This was very helpful. I think I understand, although it's still digesting.
>
> > In short, if you declare a function point-free and the inferred type
> > involves type
> > classes, it cannot be polymorphic unless you give a type signature. Without
> > a type
> > signature, the implementation tries to determine a monomorphic type from
> > the inferred
> > polymorphic type by the defaulting rules
> > (http://haskell.org/onlinereport/decls.html#sect4.3.4). If that succeeds,
> > this type is
> > given to the function, otherwise it's a type error and the module doesn't
> > compile.
>
> Shortly after posting (of course) I discovered the online report and
> 4.5.4 and 4.5.5 were helpful as well.
I should point out that the monomorphism restriction will probably be
removed from a future version of the language standard. I recommend
turning it off, especially in ghci (by putting ':set
-XNoMonomorphismRestriction' in your .ghci file)---it is rarely useful
and just confuses people.
-Brent
------------------------------
Message: 7
Date: Fri, 18 Dec 2009 12:01:53 +0100
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] find element of tupels
To: "Daniel Fischer" <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Fri, 18 Dec 2009 00:17:56 +0100, Daniel Fischer
<[email protected]> wrote:
>
> legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
This should be:
legalDate (d, Feb, y) = 0 < d && d < (if isLeapYear y then 29 else 28)
> legalDate (d,m,y)
> | m `elem` [Apr,Jun,Sep,Nov] = 0 < d && d < 31
> | otherwise = 0 < d && d < 32
>
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
------------------------------
Message: 8
Date: Fri, 18 Dec 2009 13:03:06 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] find element of tupels
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-15"
Am Freitag 18 Dezember 2009 12:01:53 schrieb Henk-Jan van Tuyl:
> On Fri, 18 Dec 2009 00:17:56 +0100, Daniel Fischer
>
> <[email protected]> wrote:
> > legalDate (d,Feb,y) = 0 < d && d < (if isLeapYear y then 30 else 29)
>
> This should be:
> legalDate (d, Feb, y) = 0 < d && d < (if isLeapYear y then 29 else 28)
No, I test for strict inequality, hence 30 resp. 29.
>
> > legalDate (d,m,y)
> >
> > | m `elem` [Apr,Jun,Sep,Nov] = 0 < d && d < 31
> > | otherwise = 0 < d && d < 32
>
> Regards,
> Henk-Jan van Tuyl
------------------------------
Message: 9
Date: Fri, 18 Dec 2009 15:34:39 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] Enum for natural numbers
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
ok, but I shall use Enum in the exercise.
-------- Original-Nachricht --------
> Datum: Thu, 17 Dec 2009 13:56:30 -0500
> Von: "Brandon S. Allbery KF8NH" <[email protected]>
> An: [email protected]
> CC: "Brandon S. Allbery KF8NH" <[email protected]>, [email protected]
> Betreff: Re: [Haskell-beginners] Enum for natural numbers
> On Dec 17, 2009, at 12:21 , [email protected] wrote:
> > toEnum should return Z for negative numbers. I did something like
> > this but the problem is I don't know how to set "less than". I tried
> > > and lt:
> >
> > instance Enum Nat where
> > toEnum (gt 0) = S Z
> > toEnum (lt 1) = Z
>
>
> Take a look at guards.
>
> --
> brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
> system administrator [openafs,heimdal,too many hats] [email protected]
> electrical and computer engineering, carnegie mellon university KF8NH
>
>
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 18, Issue 14
*****************************************