Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Motivation of having Functional Dependencies

What is the motivation of having functional dependencies in Haskell ?

One example of a functional dependency:

class (Monad m) => MonadSupply s m | m -> s where
  next :: m (Maybe s)

It is stated in the RWH book, that functional dependency helps the type checker. How does it actually help ?

Also, this piece of code actually compiles:

class (Monad m) => MonadSupply s m where
      next :: m (Maybe s)

But I guess, it will produce an runtime error.

like image 685
Sibi Avatar asked Dec 11 '13 18:12

Sibi


People also ask

Why do we need functional dependency?

The benefits of functional dependency and an overall database management system can help businesses, organizations and companies to: Prevent data redundancy. Functional dependency helps ensure the same data doesn't exist repetitively across a database or network of databases. Maintain the quality and integrity of data.

What is functional dependency explain how do you use it?

A functional dependency (FD) is a relationship between two attributes, typically between the PK and other non-key attributes within a table. For any relation R, attribute Y is functionally dependent on attribute X (usually the PK), if for every valid instance of X, that value of X uniquely determines the value of Y.

What are the basic concepts of functional dependencies?

A functional dependency is a constraint that specifies the relationship between two sets of attributes where one set can accurately determine the value of other sets. It is denoted as X → Y, where X is a set of attributes that is capable of determining the value of Y.

Why functional dependency is important during normalization?

Functional dependencies and Normalization play an important role in relational database design. Functional dependencies are key to establishing the relationship between key and non-key attributes in a relation. Normalization process works towards removing anomalies in a relation and prevents data redundancy.


1 Answers

It's perfectly fine to write code not using functional dependencies, it's just a pain to use since the inference sucks.

Basically without FDs, the function get :: MonadState m s => m s will have to figure out m and s independently. Usually m is quite easily inferred, but often s would require an explicit annotation.

Moreover, this is much more general than we need, so instead we can restrict our typechecker to say "For m, there is exactly 1 s", this way, once m is inferred, s is obvious to the type inference algorithm

like image 104
Daniel Gratzer Avatar answered Oct 05 '22 01:10

Daniel Gratzer