gadt

Why won't GHC reduce my type family?

邮差的信 提交于 2019-12-05 07:23:41
Here's an untyped lambda calculus whose terms are indexed by their free variables. I'm using the singletons library for singleton values of type-level strings. {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} import Data.Singletons import Data.Singletons.TypeLits data Expr (free :: [Symbol]) where Var :: Sing a -> Expr '[a] Lam :: Sing a -> Expr as -> Expr (Remove a as) App :: Expr free1 -> Expr free2 -> Expr (Union free1 free2) A Var introduces a free variable. A

How can I get GHC to generate instances of Data.Typeable for GADTs with Typeable in the context?

穿精又带淫゛_ 提交于 2019-12-05 05:12:48
Suppose I have the following code: {-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-} import Data.Typeable class Eq t => OnlyEq t class (Eq t, Typeable t) => BothEqAndTypeable t data Wrapper a where Wrap :: BothEqAndTypeable a => a -> Wrapper a deriving instance Eq (Wrapper a) deriving instance Typeable1 Wrapper Then, the following instance declaration works, without a constraint on t : instance OnlyEq (Wrapper t) and does what I expect it to do. But the following instance declaration doesn't work: instance BothEqAndTypeable (Wrapper t) since GHC - I'm using 7.6.1 - complains that:

GADT's - applications and usefulness?

余生长醉 提交于 2019-12-05 04:09:27
问题 I'm covering GADT's using learnyouahaskell and I'm interested in their possible uses. I understand that their main characteristic is allowing explicit type setting. Such as: data Users a where GetUserName :: Int -> Users String GetUserId :: String -> Users Int usersFunction :: Users a -> a usersFunction (GetUserName id) | id == 100 = "Bob" | id == 200 = "Phil" | otherwise = "No corresponding user" usersFunction (GetUserId name) | name == "Bob" = 100 | name == "Phil" = 200 | otherwise = 0 main

Haskell: Heterogeneous list for data with phantom variable

╄→гoц情女王★ 提交于 2019-12-05 01:58:45
I'm learning about existential quantification, phantom types, and GADTs at the moment. How do I go about creating a heterogeneous list of a data type with a phantom variable? For example: {-# LANGUAGE GADTs #-} {-# LANGUAGE ExistentialQuantification #-} data Toy a where TBool :: Bool -> Toy Bool TInt :: Int -> Toy Int instance Show (Toy a) where show (TBool b) = "TBool " ++ show b show (TInt i) = "TInt " ++ show i bools :: [Toy Bool] bools = [TBool False, TBool True] ints :: [Toy Int] ints = map TInt [0..9] Having functions like below are OK: isBool :: Toy a -> Bool isBool (TBool _) = True

Pattern matching in Observational Type Theory

帅比萌擦擦* 提交于 2019-12-04 16:32:05
问题 In the end of the "5. Full OTT" section of Towards Observational Type Theory the authors show how to define coercible-under-constructors indexed data types in OTT. The idea is basically to turn indexed data types into parameterized like this: data IFin : ℕ -> Set where zero : ∀ {n} -> IFin (suc n) suc : ∀ {n} -> IFin n -> IFin (suc n) data PFin (m : ℕ) : Set where zero : ∀ {n} -> suc n ≡ m -> PFin m suc : ∀ {n} -> suc n ≡ m -> PFin n -> PFin m Conor also mentions this technique at the bottom

Recreating Lisp's `apply` in Haskell using GADTs

走远了吗. 提交于 2019-12-04 05:11:52
As an exercise I'm trying to recreate Lisp's apply in Haskell. I do not intend to use this for any practical purpose, I just think it's a nice opportunity to get more familiar with Haskell's type system and type systems in general. (So I am also not looking for other people's implementations.) My idea is the following: I can use GADTs to "tag" a list with the type of the function it can be applied to. So, I redefine Nil and Cons in a similar way that we would encode list length in the type using a Nat definition, but instead of using Peano numbers the length is in a way encoded in the tagging

Converting an untyped AST for a simple typed language into a GADT

不想你离开。 提交于 2019-12-04 03:04:30
I have an ADT representing the AST for a simple language: data UTerm = UTrue | UFalse | UIf UTerm UTerm UTerm | UZero | USucc UTerm | UIsZero UTerm This data structure can represent invalid terms that don't follow the type rules of the language, like UIsZero UFalse , so I'd like to use a GADT that enforces well-typedness: {-# LANGUAGE GADTs #-} data TTerm a where TTrue :: TTerm Bool TFalse :: TTerm Bool TIf :: TTerm Bool -> TTerm a -> TTerm a -> TTerm a TZero :: TTerm Int TSucc :: TTerm Int -> TTerm Int TIsZero :: TTerm Int -> TTerm Bool My problem is to type check a UTerm and convert it into

GADT's - applications and usefulness?

被刻印的时光 ゝ 提交于 2019-12-03 22:14:12
I'm covering GADT's using learnyouahaskell and I'm interested in their possible uses. I understand that their main characteristic is allowing explicit type setting. Such as: data Users a where GetUserName :: Int -> Users String GetUserId :: String -> Users Int usersFunction :: Users a -> a usersFunction (GetUserName id) | id == 100 = "Bob" | id == 200 = "Phil" | otherwise = "No corresponding user" usersFunction (GetUserId name) | name == "Bob" = 100 | name == "Phil" = 200 | otherwise = 0 main = do print $ usersFunction (GetUserName 100) Apart from adding additional type safety when working

does this GADT actually have type role representational

我只是一个虾纸丫 提交于 2019-12-03 20:42:55
问题 This data type can have type role HCons' representational representational , which allows using coerce to add or remove newtypes applied to the elements, without needing to traverse the list. data HNil' = HNil' data HCons' a b = HCons' a b However the syntax for those lists is not as nice as those with the following GADT data HList (l::[*]) where HNil :: HList '[] HCons :: e -> HList l -> HList (e ': l) I have a class to convert between these two representations, such that Prime (HList [a,b])

What's the closest thing to Haskell GADTs and typeclasses in F#?

我怕爱的太早我们不能终老 提交于 2019-12-03 19:36:23
问题 F# is an ML with OOP. What's the closest it comes to Haskell generalized algebraic data types and typeclasses? 回答1: The answer depends on what problem are you trying to solve. F# does not have typeclasses and GADTs, so there is no direct mapping. However, F# has various mechanisms that you would use to solve problems that you typically solve in Haskell using GADTs and typeclasses: If you want to represent object structures and be able to add new concrete implementations with different