on master help sign in
12 files changed+331−51
since splitsnapshots
unifiedsplit
.gitignore+5 CHANGELOG.md+23 example/Main.hs+6−3 src/TypeFun/Constraint.hs+10 src/TypeFun/Data/Eq.hs+3−1 src/TypeFun/Data/List.hs+183−23 src/TypeFun/Data/List/Internal.hs−11 src/TypeFun/Data/Maybe.hs+16−2 src/TypeFun/Data/Peano.hs+48 stack.yaml+9 stack.yaml.lock+12 type-fun.cabal+16−11
.gitignorenew file+5
@@ -0,0 +1,5 @@
1+dist
2+TAGS
3+.cabal-sandbox
4+cabal.sandbox.config
5+.stack-work
CHANGELOG.mdnew file+23
@@ -0,0 +1,23 @@
1+# TODO
2+* Add doctests
3+* Add TyFun from singletons (or just depend from)?
4+
5+# CHANGELOG
6+
7+## 0.1.2
8+### Added KnownPeano
9+
10+## 0.1.1
11+### Fixed
12+* Compilable with ghc-8.0.1
13+
14+## 0.1.0
15+### Added
16+* Type family `Substract` to remove elements of one list from another
17+* Unsafe helper functions to relax constraints in some safe points. But we use
18+ `unsafeCoerce` so this functions are potentially just break constants
19+### Changed
20+* Compilable under ghc-7.8
21+
22+## 0.0.1
23+First version
example/Main.hs+6−3
@@ -2,6 +2,7 @@ module Main where
22
33import Data.Proxy
44import TypeFun.Data.List
5+import TypeFun.Data.Peano
56
67-- | This function has wider constraint __Int is inside l__
78elemConstr :: (Elem Int l) => proxy l -> Int
@@ -17,9 +18,10 @@ sublistConstr p = elemConstr p + 1
1718prefixConstr :: (IsPrefixOf '[Int, Bool] l) => proxy l -> Int
1819prefixConstr p = sublistConstr p + 1
1920
20−-- | This function has narrower constraint __Int is inside of l and it
21−-- is a first argument__ and uses inside more wide function
22−indexConstr :: (('Just 0) ~ (IndexOf Int l)) => proxy l -> Int
21+-- | This function has narrower constraint
22+-- __Int is inside of l and it is a first argument__
23+-- and uses inside more wide function
24+indexConstr :: (('Just 'Z) ~ (IndexOfMay Int l)) => proxy l -> Int
2325indexConstr p = elemConstr p + 1
2426
2527uniqConstr :: (UniqElements l) => proxy l -> Int
@@ -35,4 +37,5 @@ main = do
3537 print $ prefixConstr p2
3638 print $ prefixConstr p3
3739 print $ uniqConstr p2
40+ print $ peanoVal (Proxy :: Proxy (FromNat 100))
3841 -- print $ uniqConstr p3 -- < ElementIsNotUniqInList Int '[Int, Bool, Int]
src/TypeFun/Constraint.hsnew file+10
@@ -0,0 +1,10 @@
1+module TypeFun.Constraint
2+ ( AllSatisfy
3+ ) where
4+
5+import GHC.Exts
6+
7+-- | Apply constraint for each element of list
8+type family AllSatisfy (c :: k -> Constraint) (s :: [k]) :: Constraint where
9+ AllSatisfy c '[] = ()
10+ AllSatisfy c (a ': as) = (c a, AllSatisfy c as)
src/TypeFun/Data/Eq.hs+3−1
@@ -1,4 +1,6 @@
1−module TypeFun.Data.Eq where
1+module TypeFun.Data.Eq
2+ ( Equal
3+ ) where
24
35type family Equal (a :: k) (b :: k) :: Bool where
46 Equal a a = 'True
src/TypeFun/Data/List.hs+183−23
@@ -1,46 +1,183 @@
11-- | Collection of type families on type lists. This module differs
2−-- from __Data.Singletons.Prelude.List__ because it polykinded and
3−-- works with __[*]__ as well
4−
5−module TypeFun.Data.List where
2+-- from __Data.Singletons.Prelude.List__ because works with __[*]__
3+-- and relies on GHC's type equality (~). The rest of required
4+-- operations like 'Reverse' or '(:++:)' you could find in singletons
5+
6+module TypeFun.Data.List
7+ ( -- * Primitive operations on lists
8+ Length
9+ , Drop
10+ , Take
11+ , Delete
12+ , Remove
13+ , (:++:)
14+ -- * Elements lookup
15+ , IndexOf
16+ , IndexOfMay
17+ , IndexOfMay'
18+ , IndicesOfMay
19+ , IndicesOf
20+ , Index
21+ , IndexMay
22+ , IndicesMay
23+ , Indices
24+ , Elem
25+ , NotElem
26+ , Count
27+ -- * Operations with lists
28+ , SubList
29+ , NotSubList
30+ , IsPrefixOf
31+ , IsNotPrefixOf
32+ , IsPrefixOfBool
33+ -- * Set operations
34+ , Union
35+ , UnionList
36+ , AppendUniq
37+ , Intersect
38+ , Substract
39+ -- * Uniqueness checking
40+ , ElementIsUniq
41+ , UniqElements
42+ , UniqElements'
43+ -- * Unsafe helpers
44+ , appendId
45+ , subListId
46+ ) where
647
748import Data.Type.Bool
849import GHC.Exts
950import GHC.TypeLits
1051import TypeFun.Data.Eq
11−import TypeFun.Data.List.Internal
1252import TypeFun.Data.Maybe
53+import TypeFun.Data.Peano
54+import Unsafe.Coerce
55+
56+------------------------------------------------------------------------
57+-- NOTE: Errors type classes. These type classes are not in separate --
58+-- module because in this case user could import them and instantiate --
59+-- by mistake. The name of typeclass is an error message in fact. We --
60+-- have not any way to fail constraint explicitly, so this hack was --
61+-- introduced. --
62+------------------------------------------------------------------------
63+
64+class ElementNotFoundInList a s
65+
66+class ElementIsInList a s
67+
68+class ListIsNotPrefixOf a b
69+
70+class ListIsPrefixOf a b
71+
72+class ElementIsNotUniqInList a s
73+
74+class IndexNotFoundInList a s
75+
76+----------------------
77+-- main module code --
78+----------------------
79+
80+
81+type family Length (a :: [k]) :: N where
82+ Length '[] = 'Z
83+ Length (a ': as) = 'S (Length as)
84+
85+type family Drop (c :: N) (s :: [k]) :: [k] where
86+ Drop 'Z s = s
87+ Drop ('S c) '[] = '[]
88+ Drop ('S c) (a ': as) = Drop c as
89+
90+type family Take (c :: N) (s :: [k]) :: [k] where
91+ Take 'Z s = '[]
92+ Take ('S c) '[] = '[]
93+ Take ('S c) (a ': as) = a ': (Take c as)
94+
95+-- | Remove first argument type from anywhere in a list.
96+type family Delete (a :: k) (s :: [k]) :: [k] where
97+ Delete a '[] = '[]
98+ Delete a (a ': as) = Delete a as
99+ Delete a (b ': as) = b ': (Delete a as)
100+
101+-- | Remove index from the list
102+type family Remove (i :: N) (a :: [k]) :: [k] where
103+ Remove i '[] = '[]
104+ Remove 'Z (a ': as) = as
105+ Remove ('S i) (a ': as) = a ': (Remove i as)
106+
107+type family (:++:) (a :: [k]) (b :: [k]) :: [k] where
108+ '[] :++: b = b
109+ (a ': as) :++: b = a ': (as :++: b)
13110
14−type family Length (a :: [k]) :: Nat where
15− Length '[] = 0
16− Length (a ': as) = 1 + (Length as)
111+appendId
112+ :: forall proxy l r
113+ . proxy l
114+ -> (l ~ (l :++: '[]) => r)
115+ -> r
116+appendId = unsafeCoerce id
117+
118+type IndexOf a s = FromJust (IndexOfMay a s)
119+
120+type IndexOfMay a s = IndexOfMay' Z a s
121+
122+type family IndexOfMay' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
123+ IndexOfMay' acc a '[] = 'Nothing
124+ IndexOfMay' acc a (a ': as) = 'Just acc
125+ IndexOfMay' acc a (b ': as) = IndexOfMay' (S acc) a as
126+
127+type family IndicesOfMay (a :: [k]) (b :: [k]) :: [Maybe N] where
128+ IndicesOfMay '[] bs = '[]
129+ IndicesOfMay (a ': as) bs = (IndexOfMay a bs) ': (IndicesOfMay as bs)
130+
131+type family IndicesOf (a :: [k]) (b :: [k]) :: [N] where
132+ IndicesOf '[] bs = '[]
133+ IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
134+
135+type Index idx s = FromJust (IndexMay idx s)
136+
137+type family IndexMay (idx :: N) (s :: [k]) :: Maybe k where
138+ IndexMay idx '[] = 'Nothing
139+ IndexMay Z (a ': as) = 'Just a
140+ IndexMay (S idx) (a ': as) = IndexMay idx as
141+
142+type family IndicesMay (idxs :: [N]) (a :: [k]) :: [Maybe k] where
143+ IndicesMay '[] as = '[]
144+ IndicesMay (i ': idxs) as = (IndexMay i as) ': (IndicesMay idxs as)
145+
146+type family Indices (idxs :: [N]) (a :: [k]) :: [k] where
147+ Indices '[] as = '[]
148+ Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
17149
18150-- | Generates unresolvable constraint if fists element is not
19151-- contained inside of second
20−type Elem a s = NothingToConstr (IndexOf a s)
152+type Elem a s = NothingToConstr (IndexOfMay a s)
21153 (ElementNotFoundInList a s)
22154
23155-- | Reverse of 'Elem'
24−type NotElem a s = JustToConstr (IndexOf a s)
156+type NotElem a s = JustToConstr (IndexOfMay a s)
25157 (ElementIsInList a s)
26158
159+-- | Count elements in a list
160+type family Count (a :: k) (s :: [k]) :: N where
161+ Count a '[] = 'Z
162+ Count a (a ': as) = 'S (Count a as)
163+ Count a (b ': as) = Count a as
164+
27165-- | Constanints that first argument is a sublist of second. Reduces
28166-- to __(Elem a1 b, Elem a2 b, Elem a3 b, ...)__
29167type family SubList (a :: [k]) (b :: [k]) :: Constraint where
30168 SubList '[] bs = ()
31169 SubList (a ': as) bs = (Elem a bs, SubList as bs)
32170
171+subListId
172+ :: forall proxy l r
173+ . proxy l
174+ -> (SubList l l => r) -> r
175+subListId _ = unsafeCoerce id
176+
33177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
34178 NotSubList '[] bs = ()
35179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
36180
37−type IndexOf a s = IndexOf' 0 a s
38−
39−type family IndexOf' (acc :: Nat) (a :: k) (s :: [k]) :: Maybe Nat where
40− IndexOf' acc a '[] = 'Nothing
41− IndexOf' acc a (a ': as) = 'Just acc
42− IndexOf' acc a (b ': as) = IndexOf' (acc + 1) a as
43−
44181type IsPrefixOf a b = ( If (IsPrefixOfBool a b)
45182 (() :: Constraint)
46183 (ListIsNotPrefixOf a b)
@@ -56,15 +193,38 @@ type family IsPrefixOfBool (a :: [k]) (b :: [k]) :: Bool where
56193 IsPrefixOfBool (a ': as) (a ': bs) = IsPrefixOfBool as bs
57194 IsPrefixOfBool as bs = 'False
58195
59−type ElementIsUniq a s = If (Equal 1 (Count a s))
196+-- | Appends elements from first list to second if they are not
197+-- presented in.
198+type family Union (a :: [k]) (b :: [k]) :: [k] where
199+ Union '[] bs = bs
200+ Union (a ': as) bs = Union as (AppendUniq a bs)
201+
202+type family UnionList (l :: [[k]]) :: [k] where
203+ UnionList '[] = '[]
204+ UnionList (a ': as) = Union a (UnionList as)
205+
206+-- | Append element to list if element is not already presented in
207+type family AppendUniq (a :: k) (s :: [k]) :: [k] where
208+ AppendUniq a (a ': bs) = a ': bs
209+ AppendUniq a (b ': bs) = b ': (AppendUniq a bs)
210+ AppendUniq a '[] = '[a]
211+
212+-- | Calculates intersection between two lists. Order of elements is
213+-- taken from first list
214+type Intersect a b = (Indices (CatMaybes (IndicesOfMay a b)) b)
215+
216+-- | Removes from first list all elements occured in second
217+type family Substract (a :: [k]) (b :: [k]) :: [k] where
218+ Substract '[] b = '[]
219+ Substract a '[] = a
220+ Substract a (b ': bs) = Substract (Delete b a) bs
221+
222+-- | Checks that element 'a' occurs in a list just once
223+type ElementIsUniq a s = If (Equal (S Z) (Count a s))
60224 (() :: Constraint)
61225 (ElementIsNotUniqInList a s)
62226
63−type family Count (a :: k) (s :: [k]) :: Nat where
64− Count a '[] = 0
65− Count a (a ': as) = 1 + (Count a as)
66− Count a (b ': as) = Count a as
67−
227+-- | Checks that all elements in list are unique
68228type UniqElements a = UniqElements' a a
69229
70230type family UniqElements' (a :: [k]) (self :: [k]) :: Constraint where
src/TypeFun/Data/List/Internal.hsdeleted−11
@@ -1,11 +0,0 @@
1−module TypeFun.Data.List.Internal where
2−
3−class ElementNotFoundInList a s
4−
5−class ElementIsInList a s
6−
7−class ListIsNotPrefixOf a b
8−
9−class ListIsPrefixOf a b
10−
11−class ElementIsNotUniqInList a s
src/TypeFun/Data/Maybe.hs+16−2
@@ -1,4 +1,10 @@
1−module TypeFun.Data.Maybe where
1+module TypeFun.Data.Maybe
2+ ( MaybeCase
3+ , NothingToConstr
4+ , JustToConstr
5+ , CatMaybes
6+ , FromJust
7+ ) where
28
39import GHC.Exts
410
@@ -8,7 +14,6 @@ type family MaybeCase (a :: Maybe k)
814 MaybeCase 'Nothing nothing just = nothing
915 MaybeCase ('Just k) nothing just = just k
1016
11−
1217type family NothingToConstr (a :: Maybe k) (c :: Constraint) :: Constraint where
1318 NothingToConstr 'Nothing c = c
1419 NothingToConstr ('Just a) c = ()
@@ -16,3 +21,12 @@ type family NothingToConstr (a :: Maybe k) (c :: Constraint) :: Constraint where
1621type family JustToConstr (a :: Maybe k) (c :: Constraint) :: Constraint where
1722 JustToConstr 'Nothing c = ()
1823 JustToConstr ('Just a) c = c
24+
25+-- | Like 'catMaybes' for type lists
26+type family CatMaybes (l :: [Maybe k]) :: [k] where
27+ CatMaybes '[] = '[]
28+ CatMaybes (('Just a) ': as) = a ': (CatMaybes as)
29+ CatMaybes ('Nothing ': as) = CatMaybes as
30+
31+type family FromJust (a :: Maybe k) :: k where
32+ FromJust ('Just a) = a
src/TypeFun/Data/Peano.hsnew file+48
@@ -0,0 +1,48 @@
1+module TypeFun.Data.Peano
2+ ( N(..)
3+ , KnownPeano(..)
4+ , ToNat
5+ , FromNat
6+ , (:+:)
7+ , (:-:)
8+ , (:*:)
9+ ) where
10+
11+import Data.Typeable
12+import GHC.Generics (Generic)
13+import GHC.TypeLits
14+
15+data N = Z | S N
16+ deriving ( Eq, Ord, Read, Show
17+ , Generic, Typeable )
18+
19+-- | @since 0.1.2
20+class KnownPeano (p :: N) where
21+ peanoVal :: proxy p -> Integer
22+
23+instance KnownPeano Z where
24+ peanoVal _ = 0
25+
26+instance (KnownPeano n) => KnownPeano (S n) where
27+ peanoVal _ = succ $ peanoVal (Proxy :: Proxy n)
28+
29+type family ToNat (a :: N) :: Nat where
30+ ToNat Z = 0
31+ ToNat (S a) = 1 + (ToNat a)
32+
33+type family FromNat (a :: Nat) :: N where
34+ FromNat 0 = Z
35+ FromNat a = S (FromNat (a - 1))
36+
37+type family (:+:) (a :: N) (b :: N) :: N where
38+ Z :+: b = b
39+ (S a) :+: b = a :+: (S b)
40+
41+type family (:-:) (a :: N) (b :: N) :: N where
42+ a :-: Z = a
43+ (S a) :-: (S b) = a :-: b
44+
45+type family (:*:) (a :: N) (b :: N) :: N where
46+ Z :*: b = Z
47+ a :*: Z = Z
48+ (S a) :*: b = b :+: (a :*: b)
stack.yamlnew file+9
@@ -0,0 +1,9 @@
1+nix:
2+ packages:
3+ - zlib
4+ - postgresql
5+
6+resolver: lts-13.21
7+
8+packages:
9+ - .
stack.yaml.locknew file+12
@@ -0,0 +1,12 @@
1+# This file was autogenerated by Stack.
2+# You should not edit this file by hand.
3+# For more information, please see the documentation at:
4+# https://docs.haskellstack.org/en/stable/lock_files
5+
6+packages: []
7+snapshots:
8+- completed:
9+ size: 498180
10+ url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/13/21.yaml
11+ sha256: eff2de19a6d4691ccbf6edc1fba858f1918683047dce0f09adede874bbd2a8f3
12+ original: lts-13.21
type-fun.cabal+16−11
@@ -1,45 +1,50 @@
1−-- Initial type-fun.cabal generated by cabal init. For further
2−-- documentation, see http://haskell.org/cabal/users-guide/
3−
41name: type-fun
5−version: 0.0.1
2+version: 0.1.2
63synopsis: Collection of widely reimplemented type families
7−-- description:
84license: BSD3
95license-file: LICENSE
106author: Aleksey Uimanov
117maintainer: s9gf4ult@gmail.com
12−-- copyright:
138category: Dependent Types
149build-type: Simple
15−-- extra-source-files:
10+extra-source-files: CHANGELOG.md
1611cabal-version: >=1.10
12+homepage: https://github.com/s9gf4ult/type-fun
13+source-repository head
14+ type: git
15+ location: git@github.com:s9gf4ult/type-fun.git
1716
1817library
1918 default-language: Haskell2010
2019 hs-source-dirs: src
2120 default-extensions: ConstraintKinds
2221 , DataKinds
22+ , DeriveDataTypeable
23+ , DeriveGeneric
2324 , FlexibleInstances
2425 , MultiParamTypeClasses
2526 , PolyKinds
27+ , RankNTypes
28+ , ScopedTypeVariables
2629 , TypeFamilies
2730 , TypeOperators
2831 , UndecidableInstances
2932
30− exposed-modules: TypeFun.Data.Eq
33+ exposed-modules: TypeFun.Constraint
34+ , TypeFun.Data.Eq
3135 , TypeFun.Data.List
3236 , TypeFun.Data.Maybe
37+ , TypeFun.Data.Peano
3338
34− build-depends: base >=4.7 && <4.9
35−
39+ build-depends: base >= 4.7 && < 5
3640
3741test-suite example
3842 type: exitcode-stdio-1.0
3943 default-language: Haskell2010
4044 hs-source-dirs: example
4145 main-is: Main.hs
42− default-extensions: DataKinds
46+ default-extensions: ConstraintKinds
47+ , DataKinds
4348 , GADTs
4449 , TemplateHaskell
4550 , TypeFamilies