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. Re: dependent types (Maur??cio) 2. Re: Re: dependent types (pat browne) 3. Re: Re: dependent types (Felipe Lessa) 4. Re: dependent types (Maur??cio) 5. Re: Re: dependent types (Isaac Dupree) 6. Double's (Thomas Friedrich) 7. Re: Double's (John Dorsey) 8. Re: Double's (Thomas Friedrich) 9. Re: Double's (Felipe Lessa) 10. Rank beginner question about debugging (Ben Wise) ---------------------------------------------------------------------- Message: 1 Date: Mon, 20 Jul 2009 17:46:14 -0300 From: Maur??cio <briqueabra...@yahoo.com> Subject: [Haskell-beginners] Re: dependent types To: beginners@haskell.org Message-ID: <h42l2n$45...@ger.gmane.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed > The intended semantics are: > Two objects are equal if they have the same name. > The type of the name depends on the type of the object > 3) How would I write an instance of Named that implements the name > function correctly. I think (but someone with better knowledge) the term "dependent type" means something else: http://en.wikipedia.org/wiki/Dependent_types I'm sure you'll be interested in type families, which offer a better and richer syntax to what you want: http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html I tried rewriting your example using ghc-6.8.2 but I wasn't able to build it, and since ghc-6.8.* isn't actually supposed to properly support type families I though I could give you a bad example even if I get it to build :) So I'm leaving it here just in case someone wants to use it as a base: ------ module Main (main) where data (Eq a) => Object a = Object a class (Eq (NameType o)) => Named o where type NameType o :: * name :: o -> NameType o equals :: o -> o -> Bool equals o1 o2 = (name o1) == (name o2) instance (Named a) => Eq a where (==) = equals instance Named (Object Integer) where type NameType (Object Integer) = Integer name (Object i) = i f,g :: Object Integer f = Object 1 g = Object 2 main = putStrLn $ show $ f == g ------ Best, Maurício ------------------------------ Message: 2 Date: Mon, 20 Jul 2009 22:18:15 +0100 From: pat browne <patrick.bro...@comp.dit.ie> Subject: Re: [Haskell-beginners] Re: dependent types To: Maur??cio <briqueabra...@yahoo.com> Cc: beginners@haskell.org Message-ID: <4a64df17.3030...@comp.dit.ie> Content-Type: text/plain; charset=ISO-8859-1 Maurício, Thanks for your rapid feedback, I will study your code. >> I think (but someone with better knowledge) the term "dependent >> type" means something else: >> http://en.wikipedia.org/wiki/Dependent_types >From your first link I think the term *dependent type* is defined as a type that depends on *values* of other types. The semantic that I am trying to express is that one type depends on another type. Specifically, the type of a name depends on the type of the object. For example a Person with a Name would give a Persons-Name and a Dog with a Name would give a Dogs-Name. The paper [1] describe several styles of dependency Types depending on values called dependent types Types depending on types called parametric and type-indexed types [1]Comparing Approaches to Generic Programming Haskell by Hinze, Jeuring, and LÄoh Best regards, Pat Maurício wrote: >> The intended semantics are: >> Two objects are equal if they have the same name. >> The type of the name depends on the type of the object > >> 3) How would I write an instance of Named that implements the name >> function correctly. > > I think (but someone with better knowledge) the term "dependent > type" means something else: > > http://en.wikipedia.org/wiki/Dependent_types > > I'm sure you'll be interested in type families, which offer a > better and richer syntax to what you want: > > http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html > > I tried rewriting your example using ghc-6.8.2 but I wasn't > able to build it, and since ghc-6.8.* isn't actually supposed > to properly support type families I though I could give you a > bad example even if I get it to build :) So I'm leaving it here > just in case someone wants to use it as a base: > > ------ > module Main (main) where > > data (Eq a) => Object a = Object a > > class (Eq (NameType o)) => Named o where > type NameType o :: * > name :: o -> NameType o > equals :: o -> o -> Bool > equals o1 o2 = (name o1) == (name o2) > > instance (Named a) => Eq a where > (==) = equals > > instance Named (Object Integer) where > type NameType (Object Integer) = Integer > name (Object i) = i > > f,g :: Object Integer > f = Object 1 > g = Object 2 > > main = putStrLn $ show $ f == g > ------ > > Best, > Maurício > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners ------------------------------ Message: 3 Date: Mon, 20 Jul 2009 18:39:50 -0300 From: Felipe Lessa <felipe.le...@gmail.com> Subject: Re: [Haskell-beginners] Re: dependent types To: beginners@haskell.org Message-ID: <20090720213950.ga13...@kira.casa> Content-Type: text/plain; charset=iso-8859-1 On Mon, Jul 20, 2009 at 05:46:14PM -0300, Maurício wrote: > data (Eq a) => Object a = Object a I'd rewrite this as newtype Object a = Object a > instance (Named a) => Eq a where > (==) = equals Be careful with this instance! (specially since this is haskell-beginners) > instance Named (Object Integer) where > type NameType (Object Integer) = Integer > name (Object i) = i I'd rewrite this as instance Eq a => Named (Object a) where type NameType (Object a) = a name (Object a) = a instance Eq a => Eq (Object a) where (==) = equals However, I guess something like data Name n a = Name n a instance Functor (Name n) where fmap f (Name n a) = Name n (f a) instance Eq n => Named (Name n a) where type NameType (Name n a) = n name (Name n _) = n instance Copointed (Name n) where -- from category-extras extract (Name _ a) = a would be a whole lot more useful, no? -- Felipe. ------------------------------ Message: 4 Date: Mon, 20 Jul 2009 22:39:45 -0300 From: Maur??cio <briqueabra...@yahoo.com> Subject: [Haskell-beginners] Re: dependent types To: beginners@haskell.org Message-ID: <h43692$fq...@ger.gmane.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed > Specifically, the type of a name depends on the type of > the object. For example a Person with a Name would give a > Persons-Name and a Dog with a Name would give a Dogs-Name. One radically simple way to get that would be something like this (you can also use 'newtype' instead of 'data'): -- warning: untested code -- data (Eq b) => Named a b = Named a b equivalent :: Named a b -> Named c b -> Bool equivalent (Named _ a1 ) (Named _ a2) = (a1 == a2) Then you can check Persons-Name and Dogs-Name (Named Persons Name and Named Dogs Name) for equivalence based only on their values of Name type. Note that you can't use (==) from Eq class because Persons-Name and Dogs-Name are different types, and Eq class uses only one when instanciated. If you would like to use a equivalence relashionship that is not limited to your parametric type you could write a class like this: class Equivalent a b where equivalent :: a -> b -> Bool instance (Eq b) => Equivalent (Named a b) (Named c b) where ... > Thanks for your rapid feedback, I will study your code. Instead, study Felipe's example. It shows a nice way to "attach" a name to anything, and shows some interesting classes you will like to learn. Best, Maurício ------------------------------ Message: 5 Date: Mon, 20 Jul 2009 21:57:11 -0400 From: Isaac Dupree <m...@isaac.cedarswampstudios.org> Subject: Re: [Haskell-beginners] Re: dependent types To: Maur??cio <briqueabra...@yahoo.com> Cc: beginners@haskell.org Message-ID: <4a652077.2080...@isaac.cedarswampstudios.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Maurício wrote: > -- warning: untested code -- > > data (Eq b) => Named a b = Named a b > > equivalent :: Named a b -> Named c b -> Bool > equivalent (Named _ a1 ) (Named _ a2) = (a1 == a2) which doesn't work. "data (Eq b) =>" is useless broken syntax that should be removed from the standard. Either you can use data Named a b = Named a b equivalent :: (Eq b) => Named a b -> Named c b -> Bool or use some GHC extension (GADTs) and do data Named a b where Named :: (Eq b) => a -> b -> Named a b equivalent :: Named a b -> Named c b -> Bool -Isaac ------------------------------ Message: 6 Date: Tue, 21 Jul 2009 17:52:05 -0400 From: Thomas Friedrich <i...@suud.de> Subject: [Haskell-beginners] Double's To: beginners <beginners@haskell.org> Message-ID: <4a663885.8080...@suud.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Hi everybody, please have a look at the following lines of code: x :: [Double] x = [2,4,6,8,10] p :: [Double] p = [0.05, 0.2, 0.35, 0.3, 0.1] expectation :: Double expectation = sum $ zipWith (*) x p variance :: Double variance = sum $ zipWith f x p where f i p = (i - expectation)^2 * p when I load this into ghci I get the following: *Main> expectation 6.3999999999999995 *Main> expectation == 6.4 False *Main> variance 4.24 *Main> Why does 'expectation' get evaluated to 6.39999... instead of 6.4?? This is very strange. I have another example which is even more annoying: Have a look at the following code: import qualified Data.Map as M newtype Distribution a = Distribution (M.Map a Double) isDistribution :: Distribution a -> Bool isDistribution (Distribution d) = sum (M.elems d) == 1 uniform :: Ord a => [a] -> Distribution a uniform xs = Distribution (M.fromList (map go xs)) where go i = (i, 1 / fromIntegral (length xs)) When I load this into ghci I get the following: *Probability> isDistribution $ uniform [1..6] False *Probability> (\(Distribution d) -> sum (M.elems d)) $ uniform [1..6] 0.9999999999999999 *Probability> I mean ... hey, what is going on here? Is there any way of getting around this? Cheers, Thomas ------------------------------ Message: 7 Date: Tue, 21 Jul 2009 18:58:29 -0400 From: John Dorsey <hask...@colquitt.org> Subject: Re: [Haskell-beginners] Double's To: beginners <beginners@haskell.org> Message-ID: <20090721225829.gd14...@colquitt.org> Content-Type: text/plain; charset=us-ascii Thomas, The strangeness you're experiencing is normal behavior for floating-point (FP) math, which by definition doesn't obey the usual algebraic laws you'd like it to. FP is inexact. FP operations silently round at every turn. Comparison of two FP values for equality is usually a programming error, with a few notable exceptions. Usually what you want is either to compare the difference between two FP values to see if they're within some appropriate tolerance, or to avoid FP altogether. In the latter case, rational numbers may be what you need; Haskell has native types that support exact rational numbers directly, and you can convert to floating point if needed for approximate trig functions or whatever. Regards, John ------------------------------ Message: 8 Date: Tue, 21 Jul 2009 19:10:40 -0400 From: Thomas Friedrich <i...@suud.de> Subject: Re: [Haskell-beginners] Double's To: John Dorsey <hask...@colquitt.org> Cc: beginners <beginners@haskell.org> Message-ID: <4a664af0.2070...@suud.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed John Dorsey wrote: > Thomas, > > The strangeness you're experiencing is normal behavior for floating-point > (FP) math, which by definition doesn't obey the usual algebraic laws you'd > like it to. > > FP is inexact. FP operations silently round at every turn. Comparison of > two FP values for equality is usually a programming error, with a few > notable exceptions. Usually what you want is either to compare the > difference between two FP values to see if they're within some appropriate > tolerance, or to avoid FP altogether. In the latter case, rational numbers > may be what you need; Haskell has native types that support exact rational > numbers directly, and you can convert to floating point if needed for > approximate trig functions or whatever. > > Regards, > John > Thanks John! Changing Double to Rational in the type declaration solved the problem. Also thanks for pointing out that one should never test flouting point numbers for equality and expect the result to be trustworthy. I really should have thought about this, but I didn't. Cheers, Thomas ------------------------------ Message: 9 Date: Tue, 21 Jul 2009 20:51:05 -0300 From: Felipe Lessa <felipe.le...@gmail.com> Subject: Re: [Haskell-beginners] Double's To: beginners@haskell.org Message-ID: <20090721235105.gc13...@kira.casa> Content-Type: text/plain; charset=us-ascii On Tue, Jul 21, 2009 at 07:10:40PM -0400, Thomas Friedrich wrote: > I really should have thought about this, but I didn't. Note that Rational's are really really really slow, probably it would be better to spend some time reading Goldberg's paper[1]. [1] http://docs.sun.com/source/806-3568/ncg_goldberg.html -- Felipe. ------------------------------ Message: 10 Date: Tue, 21 Jul 2009 09:26:19 -0400 From: Ben Wise <bw...@oceanofstones.net> Subject: [Haskell-beginners] Rank beginner question about debugging To: beginners@haskell.org Message-ID: <4a65c1fb.9020...@oceanofstones.net> Content-Type: text/plain; charset=ISO-8859-1 Folks, I'm a rank beginner in Haskell, and though Haskell seems like a great language to start using, I've got a serious concern about debugging. In about 15 years of Lisp experience, then 15 years of C++, I've gotten pretty accustomed to the idea of using a debugger with lots of pre- and post-conditions on functions, breakpoints, stack trace, and variable inspection -- even though it gets tricky with delayed evaluation, macros, etc. in Lisp! So I don't understand how serious programmers can work on large projects in Haskell with nothing but compiler type-checking and QuickCheck. Yet they get it done, so I must be missing something. Things like the Haskell version of Eclipse seem to provide nothing but syntax coloring and pass-through to 'make': no break points, stack trace, or variable inspection that I can find. Is there some serious IDE (even "emacs + gdb" is serious enough for me) I'm missing? Do Haskell shops develop their own home-grown tools, or do they do something completely different? -- Ben P. Wise, PhD GPG: 0xCAF514E1 ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 13, Issue 11 *****************************************