gadt

Context in data instances

假如想象 提交于 2019-12-13 21:34:20
问题 I have a datatype which only makes sense if its arguments can be ordered, however I seem to be needing to get deep into some complex and potentially hacky stuff to get it to work (GADTs, mainly). Is what I'm doing (constrained datatypes) considered bad haskell practice, and is there any way around this? For those interested, here's the relevant code: {-# LANGUAGE GADTs #-} {-# LANGUAGE InstanceSigs #-} import Data.List (sort) data OrdTriple a where OrdTriple :: (Ord a) => a -> a -> a ->

OCaml polymorphic recursion errors

烂漫一生 提交于 2019-12-13 03:46:45
问题 Given the following types: type _ task = | Success : 'a -> 'a task | Fail : 'a -> 'a task | Binding : (('a task -> unit) -> unit) -> 'a task | AndThen : ('a -> 'b task) * 'a task -> 'b task | OnError : ('a -> 'b task) * 'a task -> 'b task type _ stack = | NoStack : 'a stack | AndThenStack : ('a -> 'b task) * 'b stack -> 'a stack | OnErrorStack : ('a -> 'b task) * 'b stack -> 'a stack type 'a process = { root: 'a task ; stack: 'a stack } let rec loop : 'a. 'a process -> unit = fun proc ->

Real world use of GADT

我与影子孤独终老i 提交于 2019-12-12 07:07:56
问题 How do I make use of Generalized Algebraic Data Type? The example given in the haskell wikibook is too short to give me an insight of the real possibilities of GADT. 回答1: I have found the "Prompt" monad (from the "MonadPrompt" package) a very useful tool in several places (along with the equivalent "Program" monad from the "operational" package. Combined with GADTs (which is how it was intended to be used), it allows you to make embedded languages very cheaply and very flexibly. There was a

Functions of GADTs

霸气de小男生 提交于 2019-12-11 05:52:32
问题 It's a followup question of Functions to Polymorphic data types Data type Question models a question/answer with a Message (the text of the question) and a function ( String -> a ) that maps user's input to the result of the question: data Question where Simple :: (Typeable a, Show a) => Message -> (String -> a) -> Question This CLI program should first gets the name of the Question , find an instance using getQuestion function and then run the Question and print out the result. {-# LANGUAGE

Writing an interpreter with OCaml GADTs

梦想的初衷 提交于 2019-12-10 17:37:00
问题 I am writing a small interpreter in OCaml and am using GADTs to type my expressions: type _ value = | Bool : bool -> bool value | Int : int -> int value | Symbol : string -> string value | Nil : unit value | Pair : 'a value * 'b value -> ('a * 'b) value and _ exp = | Literal : 'a value -> 'a exp | Var : name -> 'a exp | If : bool exp * 'a exp * 'a exp -> 'a exp and name = string exception NotFound of string type 'a env = (name * 'a) list let bind (n, v, e) = (n, v)::e let rec lookup =

Eliminating subst to prove equality

邮差的信 提交于 2019-12-10 17:15:13
问题 I'm trying to representat mod-n counters as a cut of the interval [0, ..., n-1] into two parts: data Counter : ℕ → Set where cut : (i j : ℕ) → Counter (suc (i + j)) Using this, defining the two crucial operations is straightforward (some proofs omitted for brevity): _+1 : ∀ {n} → Counter n → Counter n cut i zero +1 = subst Counter {!!} (cut zero i) cut i (suc j) +1 = subst Counter {!!} (cut (suc i) j) _-1 : ∀ {n} → Counter n → Counter n cut zero j -1 = subst Counter {!!} (cut j zero) cut (suc

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

大兔子大兔子 提交于 2019-12-10 04:17:23
问题 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

Haskell: Heterogeneous list for data with phantom variable

試著忘記壹切 提交于 2019-12-10 01:59:15
问题 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 =

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

六眼飞鱼酱① 提交于 2019-12-09 16:16:03
问题 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 -

How to make fixed-length vectors instance of Applicative?

不想你离开。 提交于 2019-12-08 20:25:13
问题 I recently learned about promotion and decided to try writing vectors. {-# LANGUAGE DataKinds, GADTs, KindSignatures #-} module Vector where data Nat = Next Nat | Zero data Vector :: Nat -> * -> * where Construct :: t -> Vector n t -> Vector ('Next n) t Empty :: Vector 'Zero t instance Functor (Vector n) where fmap f a = case a of Construct x b -> Construct (f x) (fmap f b) Empty -> Empty So far, everything is working. But I ran into a problem when trying to make Vector instance of