Re: [Haskell-cafe] functional dependencies question
On 01/07/10 13:11, Patrick Browne wrote: Neil, Does the following sum up the situation? The class Num has subclasses containing various numeric types and the literal 1 is a value for one or more of those types. Hence the Haskell compiler says the instance 1) is OK. But at run time, without the quantified (1:Int), the 1 could of more than one type, which causes a problem. I think you have the rough gist, but a few clarifications are necessary. I've added some angle brackets to show the differences: The class Num has various numeric types and the literal 1 which can be instantiated to any of those types. At , without the qualified (1::Int), the 1 could be instatianted to any type, and so determine which instance is required, because it doesn't know an exact type for the literal 1>. All this is at compile-time, not run-time. The problem comes down to needing to know a specific type in order to pick a type-class instance -- the functional dependencies are actually irrelevant to this aspect. Let's say I have this simpler class: class Big a where isBig :: a -> Bool instance Big Int where isBig x = (x > 100) -- big for an Int instance Big Integer where isBig x = (x > 1) -- big for an Integer If you ask "isBig ", the compiler needs to know which instance to use. The literal can be an Int or an Integer, so the instance is ambigious, and that will cause the error. So the compiler needs some clarification to choose an instance. Thanks, Neil. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] functional dependencies question
The class Num has subclasses containing various numeric types and the literal 1 is a value for one or more of those types. Well, the problem is not with subclasses, but with types. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] functional dependencies question
Patrick Browne writes: > Why do some cases such as 1) fail to run even if they are the only > instantiation. I think this is because literal numbers are polymorphic, i.e. a '1' in your source code is shorthand for 'fromIntegral 1', which is a type of Num a => a. Thus, 'spatialLocation 1' doesn't tell the compiler enough to know that you don't want the Float instance, for instance. Try 'spatialLocation (1::Int)'? Similar for floating point literals, they are of type Fractional a => a. -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] functional dependencies question
Neil, Does the following sum up the situation? The class Num has subclasses containing various numeric types and the literal 1 is a value for one or more of those types. Hence the Haskell compiler says the instance 1) is OK. But at run time, without the quantified (1:Int), the 1 could of more than one type, which causes a problem. Thanks for the quick and informative response, Pat Neil Brown wrote: > On 01/07/10 12:37, Patrick Browne wrote: >> Why do some cases such as 1) fail to run even if they are the only >> instantiation. >> >> -- 1) Compiles but does not run >> instance LocatedAt Int String where >> spatialLocation(1)="home" >> > > That instance is fine. I presume the problem is that you are trying to > run this function using "spatialLocation 1" as the function call. The > problem with that is that the "1" part has type Num a => a, i.e. it can > be any Num type. Even though you only have one instance, that's not > used to constrain the type for "1". The call "spatialLocation (1::Int)" > works correctly. Looking at your other examples, all the ones that > don't work have a numeric type for the parameter, so I suspect it is the > same issue throughout. > > Thanks, > > Neil. This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] functional dependencies question
On 01/07/10 12:37, Patrick Browne wrote: Why do some cases such as 1) fail to run even if they are the only instantiation. -- 1) Compiles but does not run instance LocatedAt Int String where spatialLocation(1)="home" That instance is fine. I presume the problem is that you are trying to run this function using "spatialLocation 1" as the function call. The problem with that is that the "1" part has type Num a => a, i.e. it can be any Num type. Even though you only have one instance, that's not used to constrain the type for "1". The call "spatialLocation (1::Int)" works correctly. Looking at your other examples, all the ones that don't work have a numeric type for the parameter, so I suspect it is the same issue throughout. Thanks, Neil. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe