gadt

type level integers in ocaml

混江龙づ霸主 提交于 2019-11-30 09:46:35
Could anyone give me suggestions/advice on making type level integers in OCaml (3.12) supporting addition and subtraction operations on them? For example, if I have numbers represented like this: type zero type 'a succ type pos1 = zero succ type pos2 = zero succ succ ... I need a way to define function on types like this: val add: pos2 -> pos1 -> pos3 Little background: I'm trying to port some haskell code for operations on physical dimensions and i need the ability to define operations on dimension types (record of 7 type level ints representing exponents of 7 basic SI units). I need to do it

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

心不动则不痛 提交于 2019-11-30 09:36:28
F# is an ML with OOP. What's the closest it comes to Haskell generalized algebraic data types and typeclasses? Tomas Petricek 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 behaviour, then you can often use standard OO and interfaces. If you want to write generic numeric

Real world use of GADT

扶醉桌前 提交于 2019-11-29 19:05:45
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. mokus 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 pretty good article in the Monad Reader issue 15 called "Adventures in Three Monads" that had a good

type level integers in ocaml

只愿长相守 提交于 2019-11-29 14:35:28
问题 Could anyone give me suggestions/advice on making type level integers in OCaml (3.12) supporting addition and subtraction operations on them? For example, if I have numbers represented like this: type zero type 'a succ type pos1 = zero succ type pos2 = zero succ succ ... I need a way to define function on types like this: val add: pos2 -> pos1 -> pos3 Little background: I'm trying to port some haskell code for operations on physical dimensions and i need the ability to define operations on

Type inference with GADTs - a0 is untouchable

眉间皱痕 提交于 2019-11-29 06:10:34
Lets say I have this program {-# LANGUAGE GADTs #-} data My a where A :: Int -> My Int B :: Char -> My Char main :: IO () main = do let x = undefined :: My a case x of A v -> print v -- print x compiles fine. But when I comment in the print x , I get: gadt.hs: line 13, column 12: Couldn't match type ‘a0’ with ‘()’ ‘a0’ is untouchable inside the constraints (a1 ~ GHC.Types.Int) bound by a pattern with constructor Main.A :: GHC.Types.Int -> Main.My GHC.Types.Int, in a case alternative at /home/niklas/src/hs/gadt-binary.hs:13:5-7 Expected type: GHC.Types.IO a0 Actual type: GHC.Types.IO () In the

Total real-time persistent queues

三世轮回 提交于 2019-11-29 03:27:56
Okasaki describes persistent real-time queues which can be realized in Haskell using the type data Queue a = forall x . Queue { front :: [a] , rear :: [a] , schedule :: [x] } where incremental rotations maintain the invariant length schedule = length front - length rear More details If you're familiar with the queues involved, you can skip this section. The rotation function looks like rotate :: [a] -> [a] -> [a] -> [a] rotate [] (y : _) a = y : a rotate (x : xs) (y : ys) a = x : rotate xs ys (y : a) and it's called by a smart constructor exec :: [a] -> [a] -> [x] -> Queue a exec f r (_ : s) =

Creating GADT expression in OCaml

纵饮孤独 提交于 2019-11-28 11:27:44
There is my toy GADT expression: type _ expr = | Num : int -> int expr | Add : int expr * int expr -> int expr | Sub : int expr * int expr -> int expr | Mul : int expr * int expr -> int expr | Div : int expr * int expr -> int expr | Lt : int expr * int expr -> bool expr | Gt : int expr * int expr -> bool expr | And : bool expr * bool expr -> bool expr | Or : bool expr * bool expr -> bool expr Evaluation function: let rec eval : type a. a expr -> a = function | Num n -> n | Add (a, b) -> (eval a) + (eval b) | Sub (a, b) -> (eval a) - (eval b) | Mul (a, b) -> (eval a) * (eval b) | Div (a, b) ->

Simplifying a GADT with Uniplate

冷暖自知 提交于 2019-11-28 08:27:36
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 Int -> Expression Int Mul :: Expression Int -> Expression Int -> Expression Int Eq :: Expression Int

Type inference with GADTs - a0 is untouchable

回眸只為那壹抹淺笑 提交于 2019-11-27 22:12:14
问题 Lets say I have this program {-# LANGUAGE GADTs #-} data My a where A :: Int -> My Int B :: Char -> My Char main :: IO () main = do let x = undefined :: My a case x of A v -> print v -- print x compiles fine. But when I comment in the print x , I get: gadt.hs: line 13, column 12: Couldn't match type ‘a0’ with ‘()’ ‘a0’ is untouchable inside the constraints (a1 ~ GHC.Types.Int) bound by a pattern with constructor Main.A :: GHC.Types.Int -> Main.My GHC.Types.Int, in a case alternative at /home

Odd ghc error message, “My brain just exploded”?

余生长醉 提交于 2019-11-27 22:00:29
When I try to pattern-match a GADT in an proc syntax (with Netwire and Vinyl): sceneRoot = proc inputs -> do let (Identity camera :& Identity children) = inputs returnA -< (<*>) (map (rGet draw) children) . pure I get the (rather odd) compiler error, from ghc-7.6.3 My brain just exploded I can't handle pattern bindings for existential or GADT data constructors. Instead, use a case-expression, or do-notation, to unpack the constructor. In the pattern: Identity cam :& Identity childs I get a similar error when I put the pattern in the proc (...) pattern. Why is this? Is it unsound, or just