instant-generics 0.3.7 → 0.4
raw patch · 9 files changed
+108/−71 lines, 9 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
- Generics.Instant.Base: data Su :: * -> *
- Generics.Instant.Base: data Ze :: *
- Generics.Instant.Base: instance Read a => Read (CEq c p p a)
- Generics.Instant.Base: instance Show a => Show (CEq c p q a)
- Generics.Instant.Functions.Empty: instance [overlap ok] Empty a => Empty (CEq c p p a)
- Generics.Instant.Functions.Empty: instance [overlap ok] HasRec a => HasRec (CEq c p q a)
- Generics.Instant.Functions.Eq: instance [overlap ok] GEq' a => GEq' (CEq c p q a)
- Generics.Instant.Functions.Show: instance [overlap ok] (GShow' a, Constructor c) => GShow' (CEq c p q a)
- Generics.Instant.Instances: instance Constructor List_Cons_
- Generics.Instant.Instances: instance Constructor List_Nil_
- Generics.Instant.Instances: instance Constructor Maybe_Just_
- Generics.Instant.Instances: instance Constructor Maybe_Nothing_
- Generics.Instant.Instances: instance Constructor Tuple_Pair_
- Generics.Instant.Instances: instance Representable a => Representable (CEq c p q a)
+ Generics.Instant.Base: Su :: Nat -> Nat
+ Generics.Instant.Base: Ze :: Nat
+ Generics.Instant.Base: data Nat
+ Generics.Instant.Base: instance Read a => Read (CEq k k1 c p p a)
+ Generics.Instant.Base: instance Show a => Show (CEq k k1 c p q a)
+ Generics.Instant.Functions.Empty: instance [overlap ok] Empty a => Empty (CEq k k1 c p p a)
+ Generics.Instant.Functions.Empty: instance [overlap ok] HasRec a => HasRec (CEq k k1 c p q a)
+ Generics.Instant.Functions.Enum: class GEnum a
+ Generics.Instant.Functions.Enum: genum :: (Representable a, GEnum (Rep a)) => [a]
+ Generics.Instant.Functions.Enum: genum' :: GEnum a => [a]
+ Generics.Instant.Functions.Enum: instance [overlap ok] (GEnum f, GEnum g) => GEnum (f :*: g)
+ Generics.Instant.Functions.Enum: instance [overlap ok] (GEnum f, GEnum g) => GEnum (f :+: g)
+ Generics.Instant.Functions.Enum: instance [overlap ok] GEnum (CEq k k1 c p q a)
+ Generics.Instant.Functions.Enum: instance [overlap ok] GEnum Int
+ Generics.Instant.Functions.Enum: instance [overlap ok] GEnum U
+ Generics.Instant.Functions.Enum: instance [overlap ok] GEnum a => GEnum (CEq k k1 c p p a)
+ Generics.Instant.Functions.Enum: instance [overlap ok] GEnum a => GEnum (Rec a)
+ Generics.Instant.Functions.Enum: instance [overlap ok] GEnum a => GEnum (Var a)
+ Generics.Instant.Functions.Eq: instance [overlap ok] GEq' a => GEq' (CEq k k1 c p q a)
+ Generics.Instant.Functions.Show: instance [overlap ok] (GShow' a, Constructor k c) => GShow' (CEq k k1 c p q a)
+ Generics.Instant.Instances: instance Constructor * List_Cons_
+ Generics.Instant.Instances: instance Constructor * List_Nil_
+ Generics.Instant.Instances: instance Constructor * Maybe_Just_
+ Generics.Instant.Instances: instance Constructor * Maybe_Nothing_
+ Generics.Instant.Instances: instance Constructor * Tuple_Pair_
+ Generics.Instant.Instances: instance Representable a => Representable (CEq * * c p q a)
Files
- examples/Test.hs +3/−53
- instant-generics.cabal +2/−1
- src/Generics/Instant/Base.hs +6/−5
- src/Generics/Instant/Functions.hs +2/−0
- src/Generics/Instant/Functions/Empty.hs +1/−0
- src/Generics/Instant/Functions/Enum.hs +81/−0
- src/Generics/Instant/Functions/Eq.hs +4/−3
- src/Generics/Instant/Functions/Show.hs +5/−5
- src/Generics/Instant/TH.hs +4/−4
examples/Test.hs view
@@ -15,59 +15,6 @@ import Generics.Instant.TH import Generics.Instant.Functions --------------------------------------------------------------------------------- --- Generic enum - -class GEnum a where - genum' :: [a] - -instance GEnum U where - genum' = [U] - -instance (GEnum a) => GEnum (Rec a) where - genum' = map Rec genum' - -instance (GEnum a) => GEnum (Var a) where - genum' = map Var genum' - -instance (GEnum a) => GEnum (CEq c p p a) where genum' = map C genum' -instance GEnum (CEq c p q a) where genum' = [] - -instance (GEnum f, GEnum g) => GEnum (f :+: g) where - genum' = map L genum' ||| map R genum' - -instance (GEnum f, GEnum g) => GEnum (f :*: g) where - genum' = diag (map (\x -> map (\y -> x :*: y) genum') genum') - - -instance GEnum Int where - genum' = [0..9] - - --- Dispatcher -genum :: (Representable a, GEnum (Rep a)) => [a] -genum = map to genum' - - --- Utilities -infixr 5 ||| - -(|||) :: [a] -> [a] -> [a] -[] ||| ys = ys -(x:xs) ||| ys = x : ys ||| xs - -diag :: [[a]] -> [a] -diag = concat . foldr skew [] . map (map (\x -> [x])) - -skew :: [[a]] -> [[a]] -> [[a]] -skew [] ys = ys -skew (x:xs) ys = x : combine (++) xs ys - -combine :: (a -> a -> a) -> [a] -> [a] -> [a] -combine _ xs [] = xs -combine _ [] ys = ys -combine f (x:xs) (y:ys) = f x y : combine f xs ys - ------------------------------------------------------------------------------- -- Simple Datatype ------------------------------------------------------------------------------- @@ -190,6 +137,9 @@ -- Example 2: vectors + +data Ze +data Su n -- Vec has a parameter 'a' and an index 'n' data Vec a :: * -> * where
instant-generics.cabal view
@@ -1,7 +1,7 @@ category: Generics copyright: (c) 2011 Universiteit Utrecht, 2012 University of Oxford name: instant-generics -version: 0.3.7 +version: 0.4 license: BSD3 license-file: LICENSE author: José Pedro Magalhães @@ -41,5 +41,6 @@ Generics.Instant.Functions, Generics.Instant.Functions.Show, Generics.Instant.Functions.Empty, + Generics.Instant.Functions.Enum, Generics.Instant.Functions.Eq ghc-options: -Wall
src/Generics/Instant/Base.hs view
@@ -4,6 +4,8 @@ {-# LANGUAGE GADTs #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE EmptyDataDecls #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE PolyKinds #-} ----------------------------------------------------------------------------- -- | @@ -45,7 +47,7 @@ Z, U(..), (:+:)(..), (:*:)(..), CEq(..), C, Var(..), Rec(..) , Constructor(..), Fixity(..), Associativity(..) , Representable(..) - , X, Ze, Su + , X, Nat(..) ) where infixr 5 :+: @@ -98,8 +100,7 @@ -} -- Type family for representing existentially-quantified variables -type family X c n a +type family X c (n :: Nat) (a :: k1) :: k2 --- Type-level natural numbers -data Ze :: * -data Su :: * -> * +-- Natural numbers, to be used at the type level +data Nat = Ze | Su Nat
src/Generics/Instant/Functions.hs view
@@ -14,10 +14,12 @@ module Generics.Instant.Functions ( module Generics.Instant.Functions.Empty, + module Generics.Instant.Functions.Enum, module Generics.Instant.Functions.Show, module Generics.Instant.Functions.Eq ) where import Generics.Instant.Functions.Empty +import Generics.Instant.Functions.Enum import Generics.Instant.Functions.Show import Generics.Instant.Functions.Eq
src/Generics/Instant/Functions/Empty.hs view
@@ -4,6 +4,7 @@ {-# LANGUAGE OverlappingInstances #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE PolyKinds #-} ----------------------------------------------------------------------------- -- |
+ src/Generics/Instant/Functions/Enum.hs view
@@ -0,0 +1,81 @@+{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE UndecidableInstances #-} +{-# LANGUAGE ConstraintKinds #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverlappingInstances #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE PolyKinds #-} + +----------------------------------------------------------------------------- +-- | +-- Module : Generics.Instant.Functions.Enum +-- Copyright : (c) 2010, Universiteit Utrecht +-- License : BSD3 +-- +-- Maintainer : generics@haskell.org +-- Stability : experimental +-- Portability : non-portable +-- +-- Generically enumerate values +-- +----------------------------------------------------------------------------- + +module Generics.Instant.Functions.Enum ( + GEnum(..), genum + ) where + +import Generics.Instant.Base +import Generics.Instant.Instances () + +-- Generic enum (worker) +class GEnum a where + genum' :: [a] + +instance GEnum U where + genum' = [U] + +instance (GEnum a) => GEnum (Rec a) where + genum' = map Rec genum' + +instance (GEnum a) => GEnum (Var a) where + genum' = map Var genum' + +instance (GEnum a) => GEnum (CEq c p p a) where genum' = map C genum' +instance GEnum (CEq c p q a) where genum' = [] + +instance (GEnum f, GEnum g) => GEnum (f :+: g) where + genum' = map L genum' ||| map R genum' + +instance (GEnum f, GEnum g) => GEnum (f :*: g) where + genum' = diag (map (\x -> map (\y -> x :*: y) genum') genum') + + +instance GEnum Int where + genum' = [0..9] + + +-- Dispatcher +genum :: (Representable a, GEnum (Rep a)) => [a] +genum = map to genum' + + +-- Utilities +infixr 5 ||| + +(|||) :: [a] -> [a] -> [a] +[] ||| ys = ys +(x:xs) ||| ys = x : ys ||| xs + +diag :: [[a]] -> [a] +diag = concat . foldr skew [] . map (map (\x -> [x])) + +skew :: [[a]] -> [[a]] -> [[a]] +skew [] ys = ys +skew (x:xs) ys = x : combine (++) xs ys + +combine :: (a -> a -> a) -> [a] -> [a] -> [a] +combine _ xs [] = xs +combine _ [] ys = ys +combine f (x:xs) (y:ys) = f x y : combine f xs ys
src/Generics/Instant/Functions/Eq.hs view
@@ -2,6 +2,7 @@ {-# LANGUAGE TypeOperators #-} {-# LANGUAGE OverlappingInstances #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE PolyKinds #-} ----------------------------------------------------------------------------- -- | @@ -29,15 +30,15 @@ instance GEq' U where geq' U U = True - + instance (GEq' a, GEq' b) => GEq' (a :+: b) where geq' (L x) (L x') = geq' x x' geq' (R x) (R x') = geq' x x' geq' _ _ = False - + instance (GEq' a, GEq' b) => GEq' (a :*: b) where geq' (a :*: b) (a' :*: b') = geq' a a' && geq' b b' - + instance (GEq' a) => GEq' (CEq c p q a) where geq' (C a) (C a') = geq' a a'
src/Generics/Instant/Functions/Show.hs view
@@ -3,6 +3,7 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverlappingInstances #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE PolyKinds #-} ----------------------------------------------------------------------------- -- | @@ -31,24 +32,23 @@ instance GShow' U where gshow' U = "" - + instance (GShow' a, GShow' b) => GShow' (a :+: b) where gshow' (L x) = gshow' x gshow' (R x) = gshow' x - + instance (GShow' a, GShow' b) => GShow' (a :*: b) where gshow' (a :*: b) = gshow' a `space` gshow' b - + instance (GShow' a, Constructor c) => GShow' (CEq c p q a) where gshow' c@(C a) | gshow' a == "" = paren $ conName c - | otherwise = paren $ (conName c) `space` gshow' a + | otherwise = paren $ (conName c) `space` gshow' a instance GShow a => GShow' (Var a) where gshow' (Var x) = gshow x instance GShow a => GShow' (Rec a) where gshow' (Rec x) = gshow x - class GShow a where gshow :: a -> String
src/Generics/Instant/TH.hs view
@@ -336,8 +336,8 @@ do let genTypeEqs ((EqualP t1 t2):r) | otherwise = case genTypeEqs r of - (t1s,t2s) -> ( ConT ''(:*:) `AppT` (substTyVar vsN t1) `AppT` t1s - , ConT ''(:*:) `AppT` (substTyVar vsN t2) `AppT` t2s) + (t1s,t2s) -> ( ConT '(,) `AppT` (substTyVar vsN t1) `AppT` t1s + , ConT '(,) `AppT` (substTyVar vsN t2) `AppT` t2s) genTypeEqs (_:r) = genTypeEqs r -- other constraints are ignored genTypeEqs [] = baseEqs @@ -368,8 +368,8 @@ -- Generate a type-level natural from an Int int2TLNat :: Int -> Type -int2TLNat 0 = ConT ''Ze -int2TLNat n = ConT ''Su `AppT` int2TLNat (n-1) +int2TLNat 0 = ConT 'Ze +int2TLNat n = ConT 'Su `AppT` int2TLNat (n-1) -- Generate the mobility rules for the existential type families genExTyFamInsts :: Dec -> Q [Dec]