Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare a constrained data type using record syntax?

Is it possible to declare a constrained data type using record syntax? My attempt

data ConstrainedRecord a where
  ConstrainedRecord :: Num a => { first :: a, second :: a }

causes GHC to complain "Record syntax is illegal here".

like image 492
hkBst Avatar asked Jul 07 '16 09:07

hkBst


1 Answers

Yes, but in GADT syntax you always have to explicitly specify the return type of the constructor:

data ConstrainedRecord a where
  ConstrainedRecord :: Num a => { first :: a, second :: a } -> ConstrainedRecord a

(Also, this may come handy:

deriving instance (Show a) => Show (ConstrainedRecord a)

using StandaloneDeriving, since a normal deriving does not work.)

like image 57
phipsgabler Avatar answered Sep 27 '22 17:09

phipsgabler