Presentation is loading. Please wait.

Presentation is loading. Please wait.

Type Classes with Functional Dependencies Mark P Jones, Oregon Graduate Institute The theory of relational databases meets.

Similar presentations


Presentation on theme: "Type Classes with Functional Dependencies Mark P Jones, Oregon Graduate Institute The theory of relational databases meets."— Presentation transcript:

1 Type Classes with Functional Dependencies Mark P Jones, Oregon Graduate Institute The theory of relational databases meets

2 (&&) :: Bool  Bool  Bool Some values have just one type: length ::  a. List a  Int Some values have many types:

3 Some values have several types: (+) :: Int  Int  Int (+) :: Float  Int  Float (+) :: Float  Float  Float (+) :: Int  Float  Float Different implementations in each case … (+) :: …  …  …. Extensible: allows us to add new variants...

4 (+) :: arg 1  arg 2  res Each type for (+) has the same shape: arg 1 arg 2 res Int Float IntFloat ……… Capture the differences in a table: Add =

5 Type Classes in Haskell: class Add arg 1 arg 2 res where (+) :: arg 1  arg 2  res instance Add Int Int Int where (+) = primIntAdd instance Add Float Float Float where (+) = primFloatAdd … instance Add Int Float Float where x+y = (int2Float x) + y

6 (1 + 2.3) + 4.5 IntFloat The Hope:

7 (1 + 2.3) + 4.5 IntFloat a whereAdd Int Float a b Add a Float b The Reality:

8 Type inference is supposed to infer the most general (principal) type possible. instance Add Int Float Complex where (+) = … Nothing in the program tells us that we won’t later find an extension of (+): … so the types that we infer have to be general enough to allow for this.

9  a.  b. (Add Int Float a, Add a Float b)  b The principal type of (1+2.3)+4.5 is: A complex type for such a simple example; An inaccurate type — we could be much more precise; An ambiguous type — we can’t give a well- defined semantics for this term.

10 WANTED! We need a way to:  Persuade type inference to produce better results …  … without compromising extensibility … Solution: let programmers specify type class relations more precisely.

11 Enter Database Theory: In the theory of (relational) databases:  Data is stored in tables/relations;  Designers specify constraints to capture semantic properties of the data;  Constraints help to ensure consistency and to avoid redundancy.

12 PILOTFLIGHTDATEDEPARTS Cushing839 Aug10:15am Cushing11610 Aug1:25pm Clark2818 Aug5:50pm Clark30112 Aug6:35pm Clark8311 Aug10:15am Chin8313 Aug10:15am Chin11612 Aug1:25pm From “The Theory of Relational Databases”, David Maier, 1983.

13 PILOTFLIGHTDATEDEPARTS Cushing839 Aug10:15am Cushing11610 Aug1:25pm Clark2818 Aug5:50pm Clark30112 Aug6:35pm Clark8311 Aug10:15am Chin8313 Aug10:15am Chin11612 Aug1:25pm DEPARTS is determined by FLIGHT

14 PILOTFLIGHTDATEDEPARTS Cushing839 Aug10:15am Cushing11610 Aug1:25pm Clark2818 Aug5:50pm Clark30112 Aug6:35pm Clark8311 Aug10:15am Chin8313 Aug10:15am Chin11612 Aug1:25pm PILOT is determined by FLIGHT, DATE

15 The database table corresponds to a relation on attributes {PILOT, FLIGHT, DATE, DEPARTS} that satisfies certain functional dependencies: {FLIGHT}  {DEPARTS} {FLIGHT, DATE}  {PILOT} The theory and practice of functional dependencies are well-developed.

16 X  Y If X and Y are sets of attributes, then: specifies that, for each tuple: the values of attributes in Y are uniquely determined by the values of the attributes in X. In symbols: for two tuples t, t’: If t |X =t’ |X, then t |Y = t’ |Y.

17 Type Classes with Functional Dependencies: class Add a b c where (+) :: a  b  c Type classes correspond to relations on types …

18 Type Classes with Functional Dependencies: class Add a b c | {a,b}  {c} where (+) :: a  b  c Type classes correspond to relations on types … use functional dependencies to specify them more precisely:

19 C is an arbitrary relation on types. class C a b where … D is a partial function on types. class D a b | {a}  {b} where … E is a partial 1-1 mapping on types. class E a b | {a}  {b}, {b}  {a} where …

20 The compiler must check that a program’s declaration are consistent with the dependencies: instance Add Int Int Int where (+) = primIntAdd instance Add Float Float Float where (+) = primFloatAdd … instance Add Int Float Float where x+y = (int2Float x) + y These are fine!

21 The compiler must check that a program’s declaration are consistent with the dependencies: instance Add Int Float Float where x+y = (int2Float x) + y instance Add Int Float Complex where (+) = … These are not!

22 (1 + 2.3) + 4.5 IntFloat a whereAdd Int Float a But:Add Int Float Float and so:a = Float The Payback:

23 (1 + 2.3) + 4.5 IntFloat whereAdd Float Float b And so:b = Float The Payback: Float b

24 (1 + 2.3) + 4.5 IntFloat The Payback:

25 Type Inference: We infer a type t and a set of constraints P for each term. We can apply “improving substitutions” at any point during type inference … and still get principal types.

26 Improvement: A substitution S improves a set of constraints P if it can be applied to P without changing the set of satisfiable instances. For example: [Float/b] improves {Add Float Float b} [a/b] improves {Add a b a, Add a b b}

27 More generally: Suppose the constraints in P entail C t and C t’, where t, t’ are tuples of types, and C has a dependency X  Y. If t |X =t’ |X, then: mgu(t |Y,t’ |Y ) improves P. mgu(t 1,t 2 ) computes most general unifiers.

28 class Collects e c where empty :: c insert :: e  c  c enum :: c  List e Other Examples:

29 class Collects e c | {c}  {e} where empty :: c insert :: e  c  c enum :: c  List e Other Examples:

30 class Collects e c | {c}  {e} where empty :: c insert :: e  c  c enum :: c  List e Other Examples: class FiniteMap i e fm where emptyFM:: fm lookup:: i  fm  Maybe e extend:: i  e  fm  fm

31 class Collects e c | {c}  {e} where empty :: c insert :: e  c  c enum :: c  List e Other Examples: class FiniteMap i e fm | {fm}  {i,e} where emptyFM:: fm lookup:: i  fm  Maybe e extend:: i  e  fm  fm

32 class Monad m  StateMonad s m where get:: m s set:: s  m () Other Examples:

33 class Monad m  StateMonad s m | {m}  {s} where get:: m s set:: s  m () Other Examples: etc …

34 Related Work: Type Classes (Wadler and Blott, 1989); Parametric Type Classes (Chen, Hudak, Odersky, 1992); Constructor Classes (Jones, 1993); Improvement for Qualified Types (Jones, 1995).

35 Conclusions: A small extension to the syntax of Haskell; A significant enhancement of multiple parameter type classes in several applications; Implementation distributed in recent versions of Hugs; Opportunities still for further improvement.

36 Functional dependencies can be used to explore the relationship between type classes and implicit parameters (Lewis et al, POPL’00). The interaction of functional dependencies with other aspects (e.g., overlapping instances) of extended Haskell type systems are not yet understood. Future Work:

37 What other opportunities might there be for exploiting the theory of databases in the design of practical type systems? Future Work:


Download ppt "Type Classes with Functional Dependencies Mark P Jones, Oregon Graduate Institute The theory of relational databases meets."

Similar presentations


Ads by Google