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

Slides:



Advertisements
Similar presentations
Three-Step Database Design
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Reconciling OO and Haskell-Style Overloading Mark Shields Simon Peyton Jones Microsoft Research Cambridge
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Jyh-haw Yeh Dept. of Computer Science Boise State University
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Databases 6: Normalization
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
RIZWAN REHMAN, CCS, DU. Advantages of ORDBMSs  The main advantages of extending the relational data model come from reuse and sharing.  Reuse comes.
EER vs. UML Terminology EER Diagram Entity Type Entity Attribute
Introduction to Schema Refinement. Different problems may arise when converting a relation into standard form They are Data redundancy Update Anomalies.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Module Title? DBMS E-R Model to Relational Model.
Week 6 Lecture Normalization
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Rensselaer Polytechnic Institute CSCI-4380 – Database Systems David Goldschmidt, Ph.D.
Generalized Hough Transform
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
1 Chapter 1 Introduction. 2 Introduction n Definition A database management system (DBMS) is a general-purpose software system that facilitates the process.
1 5 Normalization. 2 5 Database Design Give some body of data to be represented in a database, how do we decide on a suitable logical structure for that.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Course: Software Engineering ©Alessandra RussoUnit 2: States and Operations, slide number 1 States and Operations This unit aims to:  Define: State schemas.
Unit III. Views A table that is derived from other tables Considered as a virtual table Does not store data.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
Chapter 4 Introduction to Classes, Objects, Methods and strings
SQL John Nowobilski. What is SQL? Structured Query Language Manages Data in Database Management Systems based on the Relational Model Developed in 1970s.
1 Haskell Kevin Atkinson John Simmons. 2 Contents Introduction Type System Variables Functions Module System I/O Conclusions.
Do-it-yourself dictionaries in Haskell1 Ingmar Brouns & Lukas Spee.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Modeling Security-Relevant Data Semantics Xue Ying Chen Department of Computer Science.
1 Testing in Haskell: using HUnit Notes, thanks to Mark P Jones Portland State University.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Normalization Theory. 2 Limitations of E-R Designs Provides a set of guidelines, does not result in a unique database schema Does not provide a way.
COP Introduction to Database Structures
SysML-Modelica: A Redefinition & Modification Use Case
Entity-Relationship Model
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Haskell Chapter 2.
Mark Shields Oregon Graduate Institute Erik Meijer Utrecht University
Potter’s Wheel: An Interactive Data Cleaning System
Business Process Measures
A SIMPLE GUIDE TO FIVE NORMAL FORMS (See the next slide for required reading) Prof. Ghandeharizadeh 2018/11/14.
Fourth normal form: 4NF.
Module 5: Overview of Normalization
Electrical and Computer Engineering Department
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Sub-system interfaces
PROGRAMMING IN HASKELL
COP 3330 Object-oriented Programming in C++
Chapter 7a: Overview of Database Design -- Normalization
Presentation transcript:

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

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

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...

(+) :: 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 =

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

( ) IntFloat The Hope:

( ) IntFloat a whereAdd Int Float a b Add a Float b The Reality:

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.

 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.

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.

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.

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.

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

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

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.

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.

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

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:

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 …

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!

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!

( ) IntFloat a whereAdd Int Float a But:Add Int Float Float and so:a = Float The Payback:

( ) IntFloat whereAdd Float Float b And so:b = Float The Payback: Float b

( ) IntFloat The Payback:

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.

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}

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.

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

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

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

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

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

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

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).

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.

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:

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