I encountered a strange problem when defining a default constraint. If a constraint is unit, the default instance is not chosen. In all other cases, it works as expected.
{-# LANGUAGE TypeFamilies, ConstraintKinds #-}
import qualified GHC.Exts as E
class Expression a where
type Constr a v :: E.Constraint
--type Constr a v = () -- with this line compilation fails
--type Constr a v = v ~ v -- compiles
wrap :: Constr a v => a -> Maybe v
instance Expression () where
wrap () = Just undefined
main = print (wrap () :: Maybe Int)
Can someone clarify the reasons of the typechecker behavior?
The DEFAULT constraint is used to set a default value for a column. The default value will be added to all new records, if no other value is specified.
In SQL, the DEFAULT constraint is used to set a default value if we try to insert an empty value in a column. For example, CREATE TABLE Colleges ( college_id INT PRIMARY KEY, college_code VARCHAR(20), college_country VARCHAR(20) DEFAULT 'US' ); Run Code. Here, the default value of the college_country column is US.
These are used to bound the type of data that can go into a table. This ensures the accuracy and consistency of the data. If there is any violation between the constraint and the data action, the action is aborted by the constraint.
The default constraint is used to set the value of the attributes in case no value is passed while insertion is taking place in the database. This ensures that ambiguity does not arise in the stored data and the stored data remains meaningful.
This is a bug with associated type defaults in 7.4.1. A few weeks ago, I was told on #haskell that it is a known bug that has been fixed, but I can't find mention of it on the GHC trac.
Not really an answer, but this isn't about ConstraintKinds
class Expression a where
type Type a v
type Type a v = ()
wrap :: (Type a v) ~ () => a -> Maybe v
instance Expression () where
wrap () = Just undefined
main = print (wrap () :: Maybe Int)
does not compile, but
class Expression a where
type Type a v
type Type a v = v
wrap :: (Type a v) ~ v => a -> Maybe v
instance Expression () where
wrap () = Just undefined
main = print (wrap () :: Maybe Int)
does
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With