gadt

How to put constraints on type variable of kind `Constraint`?

人走茶凉 提交于 2021-02-07 05:02:20
问题 I'm playing around with the ConstraintKinds extension of GHC. I have the following data type, which is just a box for things fulfilling some one parameter constraint c : data Some (c :: * -> Constraint) where Some :: forall a. c a => a -> Some c For example, I could construct a box with some kind of number (arguably not very useful). x :: Some Num x = Some (1 :: Int) Now, as long as c includes the constraint Show , I could provide an instance of Show (Some c) . instance ??? => Show (Some c)

how to parse json with field of optional and variant type in Haskell?

假装没事ソ 提交于 2020-03-06 09:31:15
问题 How I can parse the input json inside this file ? https://github.com/smogon/pokemon-showdown/blob/master/data/moves.js For the secondary and flags properties? They are optional and contains variant type. A minimal example would be this one: [ {}, { "secondary": false }, { "secondary": { "chance": 10, "boosts": { "spd": -1 } } }, { "secondary": { "chance": 30, "volatileStatus": "flinch" } }, { "secondary": { "chance": 30 } }, { "secondary": { "chance": 10, "self": { "boosts": { "atk": 1, "def"

Haskell Inaccessible code bug?

北战南征 提交于 2020-02-03 05:05:06
问题 Say I have the following (erroneous) code. data A a b where APure :: (A a b) AApply :: A (A b c) c test :: (A a b) -> a -> b test (APure) a = a test AApply a = undefined GHC will then give me this error: Couldn't match type `b' with `A b1 b' `b' is a rigid type variable bound by the type signature for test :: A a b -> a -> b Inaccessible code in a pattern with constructor AApply :: forall c b. A (A b c) c, in an equation for `test' In the pattern: AApply In an equation for `test': test AApply

Generalizing from a specific type to a class in a GADT

空扰寡人 提交于 2020-01-24 00:41:04
问题 I have the following definitions {-# LANGUAGE GADTs, TypeInType, RankNTypes #-} import Data.Kind class Character (a :: * -> *) where showVal :: a b -> b -> String data ExampleCharacter a where Variable :: ExampleCharacter String EqualSign :: ExampleCharacter () Deref :: ExampleCharacter () instance Character ExampleCharacter where showVal Variable = id showVal EqualSign = const "=" showVal Deref = const "*" data Symbol :: forall a. ExampleCharacter a -> * where Terminal :: a -> Symbol (b ::

How can I recover sharing in a GADT?

杀马特。学长 韩版系。学妹 提交于 2020-01-12 02:57:11
问题 In Type-Safe Observable Sharing in Haskell Andy Gill shows how to recover sharing that existed on the Haskell level, in a DSL. His solution is implemented in the data-reify package. Can this approach be modified to work with GADTs? For example, given this GADT: data Ast e where IntLit :: Int -> Ast Int Add :: Ast Int -> Ast Int -> Ast Int BoolLit :: Bool -> Ast Bool IfThenElse :: Ast Bool -> Ast e -> Ast e -> Ast e I'd like to recover sharing by transforming the above AST to type Name =

makeLenses for GADTs (Haskell)

巧了我就是萌 提交于 2020-01-11 08:29:09
问题 Is there an equivalent of makeLenses for GADTs? If I have a simple GADT like: data D a b where D :: (Ord a, Ord b) => !a -> !b -> D a b Is there a way to generate lenses automatically by passing in a constructor and a list of field names? 回答1: I don't think it can be done automatically, but writing some lenses by hand isn't that hard in this particular case: {-# LANGUAGE GADTs #-} import Control.Lens data D a b where D :: (Ord a, Ord b) => !a -> !b -> D a b field1 :: Lens' (D a b) a field1 f

Simplifying a GADT with Uniplate

╄→гoц情女王★ 提交于 2020-01-09 09:49:45
问题 I'm trying to answer this stackoverflow question, using uniplate as I suggested, but the only solution I've come up with so far is pretty ugly. This seems like a fairly common issue, so I wanted to know if there was a more elegant solution. Basically, we've got a GADT which resolves to either Expression Int or Expression Bool (ignoring codataIf = If (B True) codataIf codataIf ): data Expression a where I :: Int -> Expression Int B :: Bool -> Expression Bool Add :: Expression Int -> Expression

Ambiguous type using parameterized types Haskell

谁说我不能喝 提交于 2020-01-07 02:04:29
问题 I have a pretty straightforward function that takes a parameterized data type and returns the same type: {-# LANGUAGE ScopedTypeVariables #-} class IntegerAsType a where value :: a -> Integer newtype (Num a, IntegerAsType n) => PolyRing a n = PolyRing [a] deriving (Eq) normalize :: (Num a, IntegerAsType n) => (PolyRing a n) -> (PolyRing a n) normalize r@(PolyRing xs) | (genericLength xs) == len = r | ... [other cases] where len = (value (undefined :: n)) The idea is that normalize will take a

Ambiguous type using parameterized types Haskell

别等时光非礼了梦想. 提交于 2020-01-07 02:04:23
问题 I have a pretty straightforward function that takes a parameterized data type and returns the same type: {-# LANGUAGE ScopedTypeVariables #-} class IntegerAsType a where value :: a -> Integer newtype (Num a, IntegerAsType n) => PolyRing a n = PolyRing [a] deriving (Eq) normalize :: (Num a, IntegerAsType n) => (PolyRing a n) -> (PolyRing a n) normalize r@(PolyRing xs) | (genericLength xs) == len = r | ... [other cases] where len = (value (undefined :: n)) The idea is that normalize will take a

Congruence for heterogenous equality

时光毁灭记忆、已成空白 提交于 2020-01-03 15:18:11
问题 I'm trying to use heterogenous equality to prove statements involving this indexed datatype: data Counter : ℕ → Set where cut : (i j : ℕ) → Counter (suc i + j) I was able to write my proofs using Relation.Binary.HeterogenousEquality.≅-Reasoning , but only by assuming the following congruence property: Counter-cong : ∀ {n n′} {k : Counter n} {k′ : Counter n′} → {A : ℕ → Set} → (f : ∀{n} → Counter n → A n) → k ≅ k′ → f k ≅ f k′ Counter-cong f k≅k′ = {!!} However, I can't pattern match on k≅k′