data-kinds

IdGeneratorStrategy unique for each kind

假装没事ソ 提交于 2019-12-11 19:13:00
问题 Is there any way to create a primary key that is only unique inside one specific kind (assuming I am asking the right question here! - apologies if not) I notice there is an "IdentityType.APPLICATION" option but "Application" seems to be the "smallest" available option!! I have the following: @PersistenceCapable(identityType = IdentityType.APPLICATION) public class AuditTrail { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long ID; @Persistent private Date

Return Type as a result of Term or Value calculation

那年仲夏 提交于 2019-12-11 01:37:25
问题 I'm trying to get a good grasp on Kinds, Types & Terms(or Values, not sure which is correct) and the GHC extensions for manipulating them. I understand that we can use TypeFamilies to write functions with Types and now we can also manipulate Kinds to some extent using DataKinds, PolyKinds etc. I have read this paper on Singleton Types which seems interesting although I don't fully understand it yet. This has all led me to wonder, is there a way to create a function that calculates the return

DataKinds and type class instances

跟風遠走 提交于 2019-12-10 13:53:33
问题 The following example is a boiled-down version of my real-life problem. It seems to be in some way similar to Retrieving information from DataKinds constrained existential types, but I could not quite get the answers I was seeking. Suppose we have a finite, promoted DataKind K with types A and B , and a poly-kinded Proxy data-type to generate terms with types of kind *. {-# LANGUAGE DataKinds, PolyKinds, GADTs, FlexibleInstances, FlexibleContexts #-} data K = A | B data Proxy :: k -> * where

Retrieving information from DataKinds constrained existential types

早过忘川 提交于 2019-12-10 02:50:24
问题 If I have a type constrained by a finite DataKind {-# LANGUAGE DataKinds #-} data K = A | B data Ty (a :: K) = Ty { ... } and an existential type which forgets the exact choice of K in the type... but remembers it in a passed dictionary. class AK (t :: K) where k :: Ty t -> K instance AK A where k _ = A instance AK B where k _ = B data ATy where ATy :: AK a => Ty a -> ATy it's genuinely the case that ATy <-> Either (Ty A) (Ty B) , but if I want to write one of those witnesses I need to use

Confused on DataKinds extension

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 05:59:36
问题 I learn type programming of Haskell from Basic Type Level Programming in Haskell but when it introduce DataKinds extension, there are something seems confusing from the example: {-# LANGUAGE DataKinds #-} data Nat = Zero | Succ Nat Now, Nat is promoted to Kind , it is okay. But how about Zero and Succ ? I try to get some information from GHCi, so I type: :kind Zero it gives Zero :: Nat that is okay, Zero is a type has kind Nat , right? and I try: :type Zero it still gives: Zero :: Nat that

Playing with DataKinds - Kind mis-match errors

我的未来我决定 提交于 2019-12-07 04:37:03
问题 I've been teaching myself about type-level programming and wanted to write a simple natural number addition type function. My first version which works is as follows: data Z data S n type One = S Z type Two = S (S Z) type family Plus m n :: * type instance Plus Z n = n type instance Plus (S m) n = S (Plus m n) So in GHCi I can do: ghci> :t undefined :: Plus One Two undefined :: Plus One Two :: S * (S * (S * Z)) Which works as expected. I then decided to try out the DataKinds extension by

Unusual Kinds and Data Constructors

感情迁移 提交于 2019-12-05 18:19:14
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 kind ‘Nat’ In the type signature for ‘foo’: foo :: 'Z -> Int Why shouldn't we allow pattern matching

Overlapping instances via Nat-kind

◇◆丶佛笑我妖孽 提交于 2019-12-05 13:55: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 (n :: Nat) where S1 :: Symmetric 1 (:.) :: (KnownNat n, 2 <= n) => Cyclic n -> Symmetric (n-1) ->

Restrict Pattern Matching to Subset of Constructors

孤街浪徒 提交于 2019-12-05 12:57:58
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 introduce separate datatypes for each operation, where it only has the allowed subtypes: newtype StringType

Playing with DataKinds - Kind mis-match errors

你说的曾经没有我的故事 提交于 2019-12-05 09:32:10
I've been teaching myself about type-level programming and wanted to write a simple natural number addition type function. My first version which works is as follows: data Z data S n type One = S Z type Two = S (S Z) type family Plus m n :: * type instance Plus Z n = n type instance Plus (S m) n = S (Plus m n) So in GHCi I can do: ghci> :t undefined :: Plus One Two undefined :: Plus One Two :: S * (S * (S * Z)) Which works as expected. I then decided to try out the DataKinds extension by modifying the Z and S types to: data Nat = Z | S Nat And the Plus family now returns a Nat kind: type