type-families

Type abstraction in GHC Haskell

荒凉一梦 提交于 2019-12-30 09:05:05
问题 I'd love to get the following example to type-check: {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} module Foo where f :: Int -> (forall f. Functor f => Secret f) -> Int f x _ = x g :: (forall f. Functor f => Secret f) -> Int g = f 4 type family Secret (f :: * -> *) :: * where Secret f = Int I get it that it's probably not possible to infer and check g s type (even though in this specific case it's obvious

How to make lenses for records with type-families [duplicate]

旧巷老猫 提交于 2019-12-29 09:10:16
问题 This question already has answers here : How to derive instances for records with type-families (2 answers) Closed 2 years ago . Here's what I've got, which is not compiling: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FunctionalDependencies #-} import Data.Text as T import Data.Int (Int64) import Control.Lens type family Incoming validationResult

Infer constraints for both if and else of type equality

守給你的承諾、 提交于 2019-12-24 05:29:35
问题 I am trying to fill the hole in the following snippet import Data.Proxy import GHC.TypeLits import Data.Type.Equality import Data.Type.Bool import Unsafe.Coerce ifThenElse :: forall (a :: Nat) (b :: Nat) x l r. (KnownNat a, KnownNat b, x ~ If (a==b) l r) => Proxy a -> Proxy b -> Either (x :~: l) (x :~: r) ifThenElse pa pb = case sameNat pa pb of Just Refl -> Left Refl Nothing -> Right $ unsafeCoerce Refl -- This was the hole Is it possible? Edit: Checked the source of sameNat and it turns out

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 #-} {-#

Associated data families and overlapping instances

余生长醉 提交于 2019-12-23 16:49:25
问题 I'd like a 'generic' map data structure that can be efficiently specialized by providing custom instances, much like in the GHC manual section on type families. {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} module MapKey where import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map class MapKey k where data MMap k :: * -> * instance {-# OVERLAPPING #-} MapKey () where newtype MMap () v = UnitMap (Maybe v) instance {-#

How can I move this instance method definition to class default?

流过昼夜 提交于 2019-12-23 15:19:32
问题 Consider this class, and an example instance. The purpose is to provide a type level switch that allows to convert the base type — Int , in this case, — to a proven predicate subtype, for future use. Granted, the class is somewhat contrived, but I extracted it from actual code, and I would populate it with more useful methods, save that I am stuck. {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeFamilyDependencies #-} {-# LANGUAGE TypeApplications #-} module Collection where import Data.Tagged

Type ambiguity in Haskell type families

旧巷老猫 提交于 2019-12-23 12:38:47
问题 I am trying put together the following class Domain and its instance TrivialDomain {-# LANGUAGE TypeFamilies #-} data Transition = Transition class Domain d where type Set d type Engine d :: * -> * top :: Engine d (Set d) -- ... complement :: Set d -> Engine d (Set d) exclude :: Set d -> Set d -> Engine d (Set d) -- ... data TrivialDomain = TrivialDomain instance Domain TrivialDomain where type Set TrivialDomain = [Int] type Engine TrivialDomain = IO top = return [0..10] -- ... complement a =

ghc 7.10.2 type families extension doesn't work

倖福魔咒の 提交于 2019-12-23 10:09:23
问题 I'm getting parse error on input ‘where’ when trying the following example in GHC 7.10.2: {-# LANGUAGE TypeFamilies #-} type family F a :: * type instance where F (Maybe Int) = Int F (Maybe Bool) = Bool F (Maybe a) = String Same question was asked two years ago about GHC 7.4.2. I used type families fine with GHC 7.6.* (can't remember last digit) this year. Is it a problem with GHC 7.10.2? According to the User's Guide, type families are available. My GHC 7.10.2 and cabal 1.22.6.0 where

Using type families and Generics to find an Id value

北慕城南 提交于 2019-12-22 08:35:07
问题 This question is related to this one, where I wanted to avoid the boilerplate of extracting an Id value from a data structure, but in a type-safe manner. I'll repeat the relevant details of the problem here: suppose you have a type Id : newtype Id = Id { _id :: Int } And you want to define a function getId that extracts this Id from any structure that contains at least one Id value: class Identifiable e where getId :: e -> Id Now the problem is how to define such a class in a type-safe manner

Is there a general way to apply constraints to a type application?

本秂侑毒 提交于 2019-12-21 04:49:48
问题 A comment by user 2426021684 led me to investigate whether it was possible to come up with a type function F such that F c1 c2 fa demonstrates that for some f and a : fa ~ f a c1 f c2 a It turns out that the simplest form of this is quite easy. However, I found it rather difficult to work out how to write a poly-kinded version. Fortunately, I managed to find a way as I was writing this question. 回答1: First, some boilerplate: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE ConstraintKinds #-} {-#