Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default constraint kinds are ignored

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?

like image 620
Boris Avatar asked Apr 30 '12 09:04

Boris


People also ask

Is DEFAULT a type of constraint?

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.

What is constraint DEFAULT constraint with example?

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.

What are check and DEFAULT constraints used for?

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.

What is the DEFAULT check 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.


2 Answers

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.

like image 113
Anthony Avatar answered Nov 04 '22 13:11

Anthony


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

like image 37
Philip JF Avatar answered Nov 04 '22 14:11

Philip JF