[Haskell-cafe] functional dependencies question
patrick.browne at dit.ie
Thu Jul 1 07:37:41 EDT 2010
My understanding of functional dependencies is that they can be used to
ensure that one type depends on another type.
For example, the type of location depends could on the type of the
object at that location.
Consider two models
1) The location of an aircraft landing should have location of the type
2) The location of a car's destination should have a type
AirportCodes should not be used as locations in 2) and
CartesianCoordinates should not be used as locations in 1).
Haskell class, instances, and fundeps were used to represent the above
requirements. I generalized the requirement a bit, testing on a variety
of types (below).
Obviously, some cases are ambigous and/or conflicting and the Haskell
compiler correctly flags this.
Why do some cases such as 1) fail to run even if they are the only
-XFunctionalDependencies -fglasgow-exts -fallow-undecidable-instances
class LocatedAt object location | object -> location where
spatialLocation :: object -> location
-- 1) Compiles but does not run
instance LocatedAt Int String where
-- 2) Compiles and runs OK
instance LocatedAt String Int where
-- 3) Compiles and runs OK, but obviously not in conjunction with 2
instance LocatedAt String String where
-- 4) Compiles and runs OK
instance LocatedAt Bool String where
-- 5) Compiles and runs OK, but obviously not in conjunction with 2
instance LocatedAt String Bool where
spatialLocation("home") = True
-- 6) Not OK
instance LocatedAt Float String where
-- 7) Compiles but does not run
instance LocatedAt Int Char where
-- 8) Compiles and runs OK
instance LocatedAt Char Int where
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
More information about the Haskell-Cafe