type-level-computation

Proof of associativity law for type-level set

不羁岁月 提交于 2020-01-02 07:55:10
问题 I'm trying to prove that type-level function Union is associative, but I'm not sure how it should be done. I proved right identity and associativity laws for type-level append function and right identity for union: data SList (i :: [k]) where SNil :: SList '[] SSucc :: SList t -> SList (h ': t) appRightId :: SList xs -> xs :~: (xs :++ '[]) appRightId SNil = Refl appRightId (SSucc xs) = case appRightId xs of Refl -> Refl appAssoc :: SList xs -> Proxy ys -> Proxy zs -> (xs :++ (ys :++ zs)) :~:

Haskell type family instance with type constraints

女生的网名这么多〃 提交于 2020-01-02 02:33:11
问题 I am trying to represent expressions with type families, but I cannot seem to figure out how to write the constraints that I want, and I'm starting to feel like it's just not possible. Here is my code: class Evaluable c where type Return c :: * evaluate :: c -> Return c data Negate n = Negate n instance (Evaluable n, Return n ~ Int) => Evaluable (Negate n) where type Return (Negate n) = Return n evaluate (Negate n) = negate (evaluate n) This all compiles fine, but it doesn't express exactly

How can I extract this polymorphic recursion function?

折月煮酒 提交于 2020-01-02 01:18:08
问题 I'm doing so fairly fun stuff with GHC 7.8, but have ran in to a bit of a problem. I have the following: mkResultF :: Eq k => Query kvs ('KV k v) -> k -> ResultF (Reverse kvs) (Maybe v) mkResultF Here key = ResultComp (pure . lookup key) mkResultF q@(There p) key = case mkResultF p key of ResultFId a -> pure a ResultComp c -> ResultComp $ \foo -> case c foo of ResultFId a -> pure a ResultComp c -> ResultComp $ \foo -> case c foo of ResultFId a -> pure a Clearly there is something to abstract

Experience reports using indexed monads in production?

萝らか妹 提交于 2020-01-01 02:48:06
问题 In a previous question I discovered the existence of Conor McBride's Kleisli arrows of Outrageous Fortune while looking for ways of encoding Idris examples in Haskell. My efforts to understand McBride's code and make it compile in Haskell led to this gist: https://gist.github.com/abailly/02dcc04b23d4c607f33dca20021bcd2f While searching on Hackage, I discovered several implementations of these concepts, notably by (guess who?) Edward Kmett and Gabriel Gonzalez. What experience do people have

Model a serial format in the type system, like Servant

不打扰是莪最后的温柔 提交于 2019-12-24 08:59:55
问题 I'm working on an API integration that ignores the existence of XML or JSON in favor of just appending character data. (The Metro2 format, if interested) I'm simplifying, but imagine that a person needs to be serialized like this: At pos 0, 4 chars: Number of bytes in the message At pos 5: 6 chars: "PERSON" hard coded At pos 11: 20 chars: Name, left-aligned and space-padded At pos 21: 8 chars: Birthday, YYYYMMDD At pos 29: 3 chars: Age, right-aligned and zero-padded Numeric fields are always

Type variables in context not fixed?

谁都会走 提交于 2019-12-24 07:08:00
问题 I'm currently experimenting with typelevel code. I've got one instance with a type variable that only occurs in the context, and not in the instance itself. Somehow the compiler doesn't like that, but I can't figure out why not. Adding a functional dependency to HasRecipe effect pot target -> deps works for that error, but then the incorrect deps are inferred at the test in the last line of the code. Error: • Could not deduce (HasRecipe target pot effect deps0) from the context: (HasRecipe

working with proofs involving CmpNat and singletons in Haskell

允我心安 提交于 2019-12-24 00:39:33
问题 I'm trying to create some functions to work with the following type. The following code uses the singletons and constraints libraries on GHC-8.4.1: {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-#

Type safe lookup on heterogeneous lists in Haskell

混江龙づ霸主 提交于 2019-12-23 07:59:20
问题 I want to develop a type safe lookup function for the following data type: data Attr (xs :: [(Symbol,*)]) where Nil :: Attr '[] (:*) :: KnownSymbol s => (Proxy s, t) -> Attr xs -> Attr ('(s , t) ': xs) The obvious lookup function would be like: lookupAttr :: (KnownSymbol s, Lookup s env ~ 'Just t) => Proxy s -> Attr env -> t lookupAttr s ((s',t) :* env') = case sameSymbol s s' of Just Refl -> t Nothing -> lookupAttr s env' where Lookup type family is defined in singletons library. This

scala generic type hierarchy with different attribute name

寵の児 提交于 2019-12-23 05:06:52
问题 I use https://pureconfig.github.io/ to load configuration values. For example for each table in a database, I store (db: String, table: String) .However, I need to denote specific tables. Therefore, each one has a separate trait. I.e.: trait Thing trait ThingWithStuff extends Thing { def value:String } trait FooThing extends Thing{ def fooThing: ThingWithStuff } trait BarThing extends Thing{ def barThing: ThingWithStuff } They all have a different attribute name with the same type which in

Aux Pattern for higher-kinded types

筅森魡賤 提交于 2019-12-21 20:08:39
问题 EDIT: Here is a much simpler formulation of the problem, using Foo as an example of the Aux pattern which does work: // Foo is a simple Aux-pattern type trait Foo[A, B] { type Out } object Foo { type Aux[A, B, C] = Foo[A, B] { type Out = C } // One instance, turning Int+String into Boolean implicit val instance: Foo.Aux[Int, String, Boolean] = null } // Wrapper is exactly the same but contains a higher-kinded type trait Wrapper[A, B] { type Contract[_] } object Wrapper { type Aux[A, B, C[_]]