Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. basic data types (Stanis?aw Findeisen) 2. Re: basic data types (Brandon Allbery) 3. Ord (Stanis?aw Findeisen) 4. Re: Ord (Brandon Allbery) 5. Re: Ord (Henry Lockyer) 6. Re: Amanda (Chadda? Fouch?) 7. Re: Ord (Stanis?aw Findeisen) 8. Re: Ord (Brent Yorgey) 9. Re: Ord (Chadda? Fouch?) ---------------------------------------------------------------------- Message: 1 Date: Fri, 30 Dec 2011 12:35:59 +0100 From: Stanis?aw Findeisen <stf-l...@eisenbits.com> Subject: [Haskell-beginners] basic data types To: beginners@haskell.org Message-ID: <4efda21f....@eisenbits.com> Content-Type: text/plain; charset=UTF-8 Hi What are those basic data type instances? For example here: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool we have: Instances Bounded Bool Enum Bool Eq Bool Data Bool Ord Bool Read Bool Show Bool Ix Bool Typeable Bool Generic Bool Storable Bool What is the difference between, e.g., Bounded Bool and Enum Bool? -- This e-mail address is invalid, see: http://people.eisenbits.com/~stf/public-email-note.html . OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0 ------------------------------ Message: 2 Date: Fri, 30 Dec 2011 06:50:11 -0500 From: Brandon Allbery <allber...@gmail.com> Subject: Re: [Haskell-beginners] basic data types To: Stanis?aw Findeisen <stf-l...@eisenbits.com> Cc: beginners@haskell.org Message-ID: <cakfcl4wxf2ukt+krjr2awz6jg5fxamgqcx0_r5_rl8pbxub...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 2011/12/30 Stanis?aw Findeisen <stf-l...@eisenbits.com> > What is the difference between, e.g., Bounded Bool and Enum Bool? There is only one Bool type; it is an instance of multiple typeclasses. So, for example, it is a Bounded (meaning that there are "minBound" and "maxBound" values associated with it) and an Enum (meaning that there are "predecessor" and "successor" functions associated with it). Look at the definitions of the indicated classes to see what they mean. -- brandon s allbery allber...@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20111230/34adc8c9/attachment-0001.htm> ------------------------------ Message: 3 Date: Fri, 30 Dec 2011 12:51:13 +0100 From: Stanis?aw Findeisen <stf-l...@eisenbits.com> Subject: [Haskell-beginners] Ord To: beginners@haskell.org Message-ID: <4efda5b1.5020...@eisenbits.com> Content-Type: text/plain; charset=UTF-8 Hi Could anyone please explain to me what is going on here? ------------------------------------------------------------------------ -- | The 'Ord' class is used for totally ordered datatypes. -- -- Instances of 'Ord' can be derived for any user-defined -- datatype whose constituent types are in 'Ord'. The declared order -- of the constructors in the data declaration determines the ordering -- in derived 'Ord' instances. The 'Ordering' datatype allows a single -- comparison to determine the precise ordering of two objects. -- -- Minimal complete definition: either 'compare' or '<='. -- Using 'compare' can be more efficient for complex types. -- class (Eq a) => Ord a where compare :: a -> a -> Ordering (<), (<=), (>), (>=) :: a -> a -> Bool max, min :: a -> a -> a compare x y = if x == y then EQ -- NB: must be '<=' not '<' to validate the -- above claim about the minimal things that -- can be defined for an instance of Ord: else if x <= y then LT else GT x < y = case compare x y of { LT -> True; _ -> False } x <= y = case compare x y of { GT -> False; _ -> True } x > y = case compare x y of { GT -> True; _ -> False } x >= y = case compare x y of { LT -> False; _ -> True } -- These two default methods use '<=' rather than 'compare' -- because the latter is often more expensive max x y = if x <= y then y else x min x y = if x <= y then x else y http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Classes.html#Ord ------------------------------------------------------------------------ AFAIU this is the definition of the Ord type class in ghc. But what is this <= function that is used in the definition of compare? Here: else if x <= y then LT -- This e-mail address is invalid, see: http://people.eisenbits.com/~stf/public-email-note.html . OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0 ------------------------------ Message: 4 Date: Fri, 30 Dec 2011 06:58:42 -0500 From: Brandon Allbery <allber...@gmail.com> Subject: Re: [Haskell-beginners] Ord To: Stanis?aw Findeisen <stf-l...@eisenbits.com> Cc: beginners@haskell.org Message-ID: <cakfcl4vrym5oyxivuct7fhhmotj0y2fxbz4yldjbw_t7vvp...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 2011/12/30 Stanis?aw Findeisen <stf-l...@eisenbits.com> > Could anyone please explain to me what is going on here? > You must, when declaring an instance of Ord, provide *either* a definition of (compare) or a definition of (<=). Both have default implementations defined, but in terms of each other; so if you don't provide either one in your instance declaration, you get an infinite loop when you try to use Ord methods (because (compare) invokes (<=), which invokes (compare) ...). -- brandon s allbery allber...@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20111230/a2fa0b64/attachment-0001.htm> ------------------------------ Message: 5 Date: Fri, 30 Dec 2011 12:04:03 +0000 From: Henry Lockyer <henry.lock...@ntlworld.com> Subject: Re: [Haskell-beginners] Ord To: Stanis?aw Findeisen <stf-l...@eisenbits.com> Cc: beginners@haskell.org Message-ID: <a407caca-469c-4f97-ac99-316db87b3...@ntlworld.com> Content-Type: text/plain; charset=utf-8 As you can probably see, there are circular definitions with compare and <= To make 'yourtype' an an instance of ord you must supply > -- Minimal complete definition: either 'compare' or '<='. and then the circular definitions will be broken and your definition will provide the key missing part. br On 30 Dec 2011, at 11:51, Stanis?aw Findeisen wrote: > Hi > > Could anyone please explain to me what is going on here? > > ------------------------------------------------------------------------ > -- | The 'Ord' class is used for totally ordered datatypes. > -- > -- Instances of 'Ord' can be derived for any user-defined > -- datatype whose constituent types are in 'Ord'. The declared order > -- of the constructors in the data declaration determines the ordering > -- in derived 'Ord' instances. The 'Ordering' datatype allows a single > -- comparison to determine the precise ordering of two objects. > -- > -- Minimal complete definition: either 'compare' or '<='. > -- Using 'compare' can be more efficient for complex types. > -- > class (Eq a) => Ord a where > compare :: a -> a -> Ordering > (<), (<=), (>), (>=) :: a -> a -> Bool > max, min :: a -> a -> a > > compare x y = if x == y then EQ > -- NB: must be '<=' not '<' to validate the > -- above claim about the minimal things that > -- can be defined for an instance of Ord: > else if x <= y then LT > else GT > > x < y = case compare x y of { LT -> True; _ -> False } > x <= y = case compare x y of { GT -> False; _ -> True } > x > y = case compare x y of { GT -> True; _ -> False } > x >= y = case compare x y of { LT -> False; _ -> True } > > -- These two default methods use '<=' rather than 'compare' > -- because the latter is often more expensive > max x y = if x <= y then y else x > min x y = if x <= y then x else y > > http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Classes.html#Ord > ------------------------------------------------------------------------ > > AFAIU this is the definition of the Ord type class in ghc. But what is > this <= function that is used in the definition of compare? Here: > > else if x <= y then LT > > -- > This e-mail address is invalid, see: > http://people.eisenbits.com/~stf/public-email-note.html . > > OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0 > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners ------------------------------ Message: 6 Date: Fri, 30 Dec 2011 13:13:16 +0100 From: Chadda? Fouch? <chaddai.fou...@gmail.com> Subject: Re: [Haskell-beginners] Amanda To: peter.h...@memorphic.com Cc: beginners@haskell.org Message-ID: <CANfjZRYGEiFjzFCR2Um0_t=ze89sxax1jqwyn3djbeqhlri...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 On Fri, Dec 30, 2011 at 1:08 AM, Peter Hall <peter.h...@memorphic.com> wrote: > > That said, Miranda/Amanda really aren't as sophisticated as Haskell, > lacking type classes in particular. > > Here is a paper comparing Haskell with Miranda: > http://www.cs.mun.ca/~donald/techreports/2000-02-cmp_haskell_miranda.ps > It's a little bit outdated but covers the main differences pretty well. > Much of this report is still valuable but of course the part directly concerning Hugs behaviour are increasingly irrelevant since GHCi is now the interpreter of choice, having long since outstripped Hugs in almost every point : performance, user-friendly interface, quality of the error messages, and so on... So a large part of Haskell "disadvantages" are now nullified, the only remaining interest of Miranda being probably its less complicated type system, for educational purposes (though using a simplified Prelude without typeclasses would be at least as good). -- Jeda? ------------------------------ Message: 7 Date: Fri, 30 Dec 2011 13:28:42 +0100 From: Stanis?aw Findeisen <stf-l...@eisenbits.com> Subject: Re: [Haskell-beginners] Ord To: beginners@haskell.org Message-ID: <4efdae7a.8040...@eisenbits.com> Content-Type: text/plain; charset=UTF-8 On 2011-12-30 12:58, Brandon Allbery wrote: > 2011/12/30 Stanis?aw Findeisen <stf-l...@eisenbits.com > <mailto:stf-l...@eisenbits.com>> > > Could anyone please explain to me what is going on here? > > > You must, when declaring an instance of Ord, provide *either* a > definition of (compare) or a definition of (<=). Both have default > implementations defined, but in terms of each other; so if you don't > provide either one in your instance declaration, you get an infinite > loop when you try to use Ord methods (because (compare) invokes (<=), > which invokes (compare) ...). If you don't provide either one, then you get an infinite loop, but this is a runtime problem, i.e. the code will compile. Right? How to declare an instance of Ord? -- This e-mail address is invalid, see: http://people.eisenbits.com/~stf/public-email-note.html . OpenPGP: E3D9 C030 88F5 D254 434C 6683 17DD 22A0 8A3B 5CC0 ------------------------------ Message: 8 Date: Fri, 30 Dec 2011 10:48:05 -0500 From: Brent Yorgey <byor...@seas.upenn.edu> Subject: Re: [Haskell-beginners] Ord To: beginners@haskell.org Message-ID: <20111230154805.ga2...@seas.upenn.edu> Content-Type: text/plain; charset=utf-8 On Fri, Dec 30, 2011 at 01:28:42PM +0100, Stanis?aw Findeisen wrote: > On 2011-12-30 12:58, Brandon Allbery wrote: > > 2011/12/30 Stanis?aw Findeisen <stf-l...@eisenbits.com > > <mailto:stf-l...@eisenbits.com>> > > > > Could anyone please explain to me what is going on here? > > > > > > You must, when declaring an instance of Ord, provide *either* a > > definition of (compare) or a definition of (<=). Both have default > > implementations defined, but in terms of each other; so if you don't > > provide either one in your instance declaration, you get an infinite > > loop when you try to use Ord methods (because (compare) invokes (<=), > > which invokes (compare) ...). > > If you don't provide either one, then you get an infinite loop, but this > is a runtime problem, i.e. the code will compile. Right? Correct. > How to declare an instance of Ord? instance Ord SomeType where compare x y = ... Declaring instances of type classes should be covered in any Haskell tutorial. -Brent ------------------------------ Message: 9 Date: Fri, 30 Dec 2011 17:21:03 +0100 From: Chadda? Fouch? <chaddai.fou...@gmail.com> Subject: Re: [Haskell-beginners] Ord To: Stanis?aw Findeisen <stf-l...@eisenbits.com> Cc: beginners@haskell.org Message-ID: <canfjzryezdc3buk++ygp+q07wn+oglikd1g0bfe4fjnpz2n...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 2011/12/30 Stanis?aw Findeisen <stf-l...@eisenbits.com>: > > How to declare an instance of Ord? > You very rarely need to do so... In fact even in the report a lot of the basic instances are already derived automatically (which you can do by adding "deriving (Ord)" at the end of your data type definition, or "deriving instance Ord YourType" on a line by itself). If you do need to, it could look like that : > > instance Ord Bool where > compare True True = EQ > compare True False = GT > compare False True = LT > compare False False = EQ > defining compare is enough, or you could define just (<=) : > > instance Ord Bool where > False <= _ = True > True <= True = True > True <= False = False > Note that nothing prevents you from defining both, or even the other comparison functions if there are performance considerations. -- Jeda? ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 42, Issue 34 *****************************************