data-kinds

Unusual Kinds and Data Constructors

醉酒当歌 提交于 2020-01-02 05:39:12
问题 I don't know how I didn't notice this, but data constructors and function definitions alike can't use types with kinds other than * and it's variants * -> * etc., due to (->) 's kind signature, even under -XPolyKinds . Here is the code I have tried: {-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} data Nat = S Nat | Z data Foo where Foo :: 'Z -> Foo -- Fails foo :: 'Z -> Int -- Fails foo _ = 1 The error I'm getting is the following: <interactive>:8:12: Expected a type, but ‘Z’ has

Differently kinded ReaderT?

假如想象 提交于 2019-12-24 01:01:44
问题 At the risk of this becoming an XY Problem, is it possible to have a ReaderT with a differently kinded environment? I'm trying something like... type AppM (perms :: [*]) = ReaderT (perms :: [*]) IO ...but the compiler complains with... Expected a type, but ‘(perms :: [*])’ has kind ‘[*]’ ...presumably because ReaderT is defined as... newtype ReaderT r (m :: k -> *) (a :: k) = ReaderT {runReaderT :: r -> m a} ...where r is of kind * I'm trying to track permissions/roles at a type-level, and my

FromJSON instance with DataKinds

£可爱£侵袭症+ 提交于 2019-12-23 17:26:40
问题 Trying to do the JSON de-serialisation for a data type with TypeLits, I get stuck with the following problem: Couldn't match type ‘n’ with ‘2’ ‘n’ is a rigid type variable bound by the instance declaration at test.hs:14:10 Expected type: aeson-0.11.2.1:Data.Aeson.Types.Internal.Parser (X n) Actual type: aeson-0.11.2.1:Data.Aeson.Types.Internal.Parser (X 2) How would be the correct syntax to allow Nat generically in the FromJSON instance in the following example: {-# LANGUAGE DataKinds #-} {-#

Applicative instance of a vector with kind nats

◇◆丶佛笑我妖孽 提交于 2019-12-23 16:11:25
问题 I'm playing around with kind nats for the moment and got stuck when trying to define an applicative instance of a vector data type. A reasonable instance, I think, would be that pure 1 :: Vec 3 Int would give me a vector of length 3 all elements of the value 1 and the <*> operator would zip together functions with values. The problem where I'm stuck is that it will be recursive but depending on the value of the nat kind. As you see below I've used a lot of pragmas (I don't even know which are

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

DataKind Unions

我只是一个虾纸丫 提交于 2019-12-21 10:36:10
问题 I'm not sure if it is the right terminology, but is it possible to declare function types that take in an 'union' of datakinds? For example, I know I can do the following: {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} ... data Shape' = Circle' | Square' | Triangle' data Shape :: Shape' -> * where Circle :: { radius :: Int} -> Shape Circle' Square :: { side :: Int} -> Shape Square' Triangle :: { a :: Int , b :: Int , c :: Int} -> Shape Triangle' test1 :: Shape Circle' -> Int test1 =

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

Constructing Proxy type given the input

微笑、不失礼 提交于 2019-12-13 20:00:59
问题 Given the code below which looks up type-specific information in Data.HashMap for a type, is it possible to define a new function getMapVal2 as documented in the comments, to build the TypeKey argument given the type? {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DataKinds #-} import Data.Monoid ((<>)) import Data.Proxy (Proxy(Proxy)) import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) import qualified Data.HashMap.Strict as Map (HashMap, empty, insert, lookup)

Generating a type annotation using DataKinds within a TH QuasiQuote

你离开我真会死。 提交于 2019-12-13 15:29:19
问题 In a haskell project using template haskell, I am trying to generate an expression that has a type annotation as a phantom type. A simple example would be a situation with DataKinds and KindSignatures like: {-# LANGUAGE DataKinds, KindSignatures #-} data Foo = A | B data GenMe (w :: Foo) = GenMe Int [| $(generate some code) :: GenMe $(genType someCompileTimeData) |] How can I write a function, like genType such that genType :: Foo -> Q Type lifting just lifts the variable holding the compile

Restrict Pattern Matching to Subset of Constructors

和自甴很熟 提交于 2019-12-12 09:46:14
问题 Say I have the following: data Type = StringType | IntType | FloatType data Op = Add Type | Subtract Type I'd like to constrain the possible types under Subtract , such that it only allows for int or float. In other words, patternMatch :: Op -> () patternMatch (Add StringType) = () patternMatch (Add IntType) = () patternMatch (Add FloatType) = () patternMatch (Subtract IntType) = () patternMatch (Subtract FloatType) = () Should be an exhaustive pattern match. One way of doing this is to