Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell class definition question about restrictions

Tags:

haskell

class IndexSelect k (m :: k -> (* -> *) -> *) | m -> k where
    type Restriction m (p :: k) :: Constraint
    indexSelect :: Restriction m p => Sing (p :: k) -> Proxy m -> LocalDb f -> f (TableEntity (m p))

I'm new to the Haskell language. I'm having trouble deciphering the class definition in some code in the code repo at the company I just started at. What is this doing?

like image 307
ddtraveller Avatar asked Feb 27 '19 22:02

ddtraveller


1 Answers

There is a lot going on here. I am going to start by referring you to Sections 7.6 Class and Instance Declarations, 7.7 Type Families, and 7.8.4 Explicitly-kinded qualifications of the GHC language extension documentation. (I am by no means an expert on any of these and clicked on your question hoping someone had supplied further enlightenment.)

We are defining a multi-parameter type class called IndexSelect with parameters k and m. (Multi-parameter type classes 7.6.1.1)

The second parameter of the class, m, is given an explicit kind qualification: k -> (* -> *) -> * in English m must be a function which takes a k and a function and returns a value. (7.8.4 Explicitly-kinded quantification)

The class has a functional dependency | m -> k. Where the choice of m must uniquely determine k Given the name of this function that implies that a collection m must have only one kind of key k which is reasonable. (7.6.2 Functional Dependencies)

The class forms a indexed type family type Restriction m (p :: k) :: Constraint. It appears inside a class definition so it is an associated type synonym. (7.7.2.1.1 Associated type family declarations). It takes some m and a p which must be of type k and results in a Constraint.

The class has one listed method indexSelect which one might guess manages to extract information from a collection. Without knowing what Sing, LocalDb and TableEntity do I cannot say more.

like image 168
John F. Miller Avatar answered Nov 18 '22 15:11

John F. Miller