gadt

How can I derive a Data instance for a GADT in Haskell?

怎甘沉沦 提交于 2020-01-01 10:16:34
问题 I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts forward definitions if parameter is ForwardPossible. data OrForward t forward where OFKnown :: t -> OrForward t forward OFForward :: NamespaceID -> SrcSpan -> BS.ByteString -> OrForward t ForwardPossible deriving instance Eq t => Eq (OrForward t

How can I derive a Data instance for a GADT in Haskell?

℡╲_俬逩灬. 提交于 2020-01-01 10:16:27
问题 I have a GADT which is only ever used with two different parameters, ForwardPossible and (): -- | Used when a forward definition is possible. data ForwardPossible = ForwardPossible deriving (Eq, Ord, Typeable, Data, Show) -- | GADT which accepts forward definitions if parameter is ForwardPossible. data OrForward t forward where OFKnown :: t -> OrForward t forward OFForward :: NamespaceID -> SrcSpan -> BS.ByteString -> OrForward t ForwardPossible deriving instance Eq t => Eq (OrForward t

Function returning result of any constructor of a GADT

余生颓废 提交于 2019-12-24 01:47:06
问题 I'm currently having a fight with the typechecker when I try to create a function returning Thing a (where Thing is a GADT). A minimal contrived example: {-#LANGUAGE GADTs, EmptyDataDecls #-} module Main where -- Define a contrived GADT data TFoo data TBar data Thing a where Foo :: Int -> Thing TFoo Bar :: String -> Thing TBar combine :: [Thing a] combine = [Foo 1, Bar "abc"] main :: IO () main = undefined The typechecker gets unhappy that a doesn't match TBar . Presumably this is because it

How to use GADTs in Hugs

帅比萌擦擦* 提交于 2019-12-23 01:40:11
问题 I'd like to write a Haskell program that uses GADTs interactively on a platform not supported by GHCi (namely, GNU/Linux on mipsel). The problem is, the construct that can be used to define a GADT in GHC, for example: data Term a where Lit :: Int -> Term Int Pair :: Term a -> Term b -> Term (a,b) ... doesn't seem working on Hugs. Can't GADTs really be defined in Hugs? My TA at a Haskell class said it was possible in Hugs, but he seemed unsure. If not, can GADT be encoded by using other syntax

How can I programatically produce this datatype from the other?

谁说胖子不能爱 提交于 2019-12-22 09:48:29
问题 I'd like to use DSum for something. To work with DSum , you need to have a 'tag' type which takes one type argument, e.g. data Tag a where AFirst :: Tag Int ASecond :: Tag String However, I'd like to use this internally in a library. I want the interface that I expose to users to take any old datatype, e.g. data SomeUserType1 = Foo Int | Bar String it's clearly quite mechanical to go from this to the Tag a type given above. So, is it possible to do this in code, with some sort of generic

Overlapping instances via Nat-kind

倖福魔咒の 提交于 2019-12-22 08:27:12
问题 This problem actually emerged from attempt to implement few mathematical groups as types. Cyclic groups have no problem (instance of Data.Group defined elsewhere): newtype Cyclic (n :: Nat) = Cyclic {cIndex :: Integer} deriving (Eq, Ord) cyclic :: forall n. KnownNat n => Integer -> Cyclic n cyclic x = Cyclic $ x `mod` toInteger (natVal (Proxy :: Proxy n)) But symmetric groups have some problem on defining some instances (implementation via factorial number system): infixr 6 :. data Symmetric

GADTs for a DSL: swings and roundabouts?

跟風遠走 提交于 2019-12-22 05:53:06
问题 The typical examples for the benefits of a GADT are representing the syntax for a DSL; say here on the wiki or the PLDI 2005 paper. I can see that if you have a AST that's type-correct by construction, writing an eval function is easy. How to build the GADT handling into a REPL? Or more specifically into a Read-Parse-Typecheck-Eval-Print-Loop? I'm seeing that you just push the complexity from the eval step into earlier steps. Does GHCi use a GADT internally to represent expressions it's

Trouble with DataKinds

两盒软妹~` 提交于 2019-12-20 03:20:42
问题 I have created a very simple example of a problem I'm having using GADTs and DataKinds. My real application is obviously more complicated but this captures the essence of my situation clearly. I'm trying to create a function that can return any of the values (T1, T2) of type Test. Is there a way to accomplish this or am I getting into the realm of dependent types? The questions here seem similar but I could not find (or comprehend) an answer to my question from them. I'm just starting to

GHC complains about non-exhaustive patterns that are enforced by the type checker

可紊 提交于 2019-12-19 16:36:16
问题 I have the following code {-# LANGUAGE DataKinds, GADTs, TypeOperators #-} data Vect v a where Nil :: Vect '[] a Vec :: a -> Vect v a -> Vect (() ': v) a instance Eq a => Eq (Vect v a) where (==) Nil Nil = True (Vec e0 v0) == (Vec e1 v1) = e0 == e1 && v0 == v1 When compiling or interpreting with -Wall the following warning is given: Pattern match(es) are non-exhaustive In an equation for `==': Patterns not matched: Nil (Vec _ _) (Vec _ _) Nil Normally this is to be expected. Normally, even if

GHC complains about non-exhaustive patterns that are enforced by the type checker

时间秒杀一切 提交于 2019-12-19 16:36:10
问题 I have the following code {-# LANGUAGE DataKinds, GADTs, TypeOperators #-} data Vect v a where Nil :: Vect '[] a Vec :: a -> Vect v a -> Vect (() ': v) a instance Eq a => Eq (Vect v a) where (==) Nil Nil = True (Vec e0 v0) == (Vec e1 v1) = e0 == e1 && v0 == v1 When compiling or interpreting with -Wall the following warning is given: Pattern match(es) are non-exhaustive In an equation for `==': Patterns not matched: Nil (Vec _ _) (Vec _ _) Nil Normally this is to be expected. Normally, even if