gadt

Pattern matching on a GADT fails

。_饼干妹妹 提交于 2019-12-08 03:43:45
问题 I was playing around a bit more with ReasonML and found pattern matching on type t from the following example to not be working with the error Error: This pattern matches values of type t(float) but a pattern was expected which matches values of type t(int) Type float is not compatible with type int type t('a) = | One: t(int) | Two: t(float); let x = fun | One => None | Two => None; Now on some level this makes sense to me if this was about the return type of a function. I found an answer (I

How to parse string into GADT

浪子不回头ぞ 提交于 2019-12-07 08:46:17
问题 I am trying trying to implement Combinatory Logic in Haskell, and I would like to write to parser for the language. I am having trouble getting a parser to work via Parsec. The basic problem is that I need a way to ensure that the objects returned by the parser are well typed. Does anyone have any creative ideas on how to do this? {-# Language GeneralizedNewtypeDeriving #-} import qualified Data.Map as Map import qualified Text.ParserCombinators.Parsec as P import Text.Parsec.Token (parens)

Why won't GHC reduce my type family?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 02:31:55
问题 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

Transform a GADT without constraints to another GADT with constraints when such constraints hold

♀尐吖头ヾ 提交于 2019-12-06 19:58:23
问题 Can we transform a GADT without a given constraint on its constructors to a GADT that does have the said constraint? I want to do this because I want to get a deep-embedding of Arrows and do some interesting things with the representation that (for now) seem to require Typeable . (One reason) data DSL a b where Id :: DSL a a Comp :: DSL b c -> DSL a b -> DSL a c -- Other constructors for Arrow(Loop,Apply,etc) data DSL2 a b where Id2 :: (Typeable a, Typeable b) => DSL2 a a Comp2 :: (Typeable a

Recreating Lisp's `apply` in Haskell using GADTs

牧云@^-^@ 提交于 2019-12-05 23:46:31
问题 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

Weaken GADTs type constraints to deal with unpredictable data

隐身守侯 提交于 2019-12-05 21:17:49
问题 I am trying to make use of GADTs to have well constrained types, but some dependencies are impossible to handle during compilation time – for example user input. Let's consider following AVL tree definition: data Zero data S a data AVL depth where Nil :: AVL Zero LNode :: AVL n -> Int -> AVL (S n) -> AVL (S (S n)) RNode :: AVL (S n) -> Int -> AVL n -> AVL (S (S n)) MNode :: AVL n -> Int -> AVL n -> AVL (S n) Magic of GADTs ensures that every AVL tree is well balanced. I can define some basic

How can I programatically produce this datatype from the other?

痞子三分冷 提交于 2019-12-05 20:50:50
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 programming techniques? Here's another example to be clear about the type of mapping I want to produce.

How to parse string into GADT

为君一笑 提交于 2019-12-05 15:52:11
I am trying trying to implement Combinatory Logic in Haskell, and I would like to write to parser for the language. I am having trouble getting a parser to work via Parsec. The basic problem is that I need a way to ensure that the objects returned by the parser are well typed. Does anyone have any creative ideas on how to do this? {-# Language GeneralizedNewtypeDeriving #-} import qualified Data.Map as Map import qualified Text.ParserCombinators.Parsec as P import Text.Parsec.Token (parens) import Text.ParserCombinators.Parsec ((<|>)) import Control.Applicative ((<$>), (<*>), (*>), (<*)) data

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) ->

GADTs for a DSL: swings and roundabouts?

Deadly 提交于 2019-12-05 07:32:56
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 evaluating? (The expressions are a lot chunkier than a typical DSL.) For one thing, you can't derive Show