on master help sign in
11 files changed+180−38
since splitsnapshots
unifiedsplit
.gitignore+3 CHANGELOG.md+23 example/Main.hs+5−3 src/TypeFun/Constraint.hs+4−1 src/TypeFun/Data/Eq.hs+3−1 src/TypeFun/Data/List.hs+76−17 src/TypeFun/Data/Maybe.hs+11−2 src/TypeFun/Data/Peano.hs+22−4 stack.yaml+9 stack.yaml.lock+12 type-fun.cabal+12−10
.gitignore+3
@@ -1,2 +1,5 @@
11dist
22TAGS
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+5−3
@@ -18,9 +18,10 @@ sublistConstr p = elemConstr p + 1
1818prefixConstr :: (IsPrefixOf '[Int, Bool] l) => proxy l -> Int
1919prefixConstr p = sublistConstr p + 1
2020
21−-- | This function has narrower constraint __Int is inside of l and it
22−-- is a first argument__ and uses inside more wide function
23−indexConstr :: (('Just 'Z) ~ (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
2425indexConstr p = elemConstr p + 1
2526
2627uniqConstr :: (UniqElements l) => proxy l -> Int
@@ -36,4 +37,5 @@ main = do
3637 print $ prefixConstr p2
3738 print $ prefixConstr p3
3839 print $ uniqConstr p2
40+ print $ peanoVal (Proxy :: Proxy (FromNat 100))
3941 -- print $ uniqConstr p3 -- < ElementIsNotUniqInList Int '[Int, Bool, Int]
src/TypeFun/Constraint.hs+4−1
@@ -1,7 +1,10 @@
1−module TypeFun.Constraint where
1+module TypeFun.Constraint
2+ ( AllSatisfy
3+ ) where
24
35import GHC.Exts
46
7+-- | Apply constraint for each element of list
58type family AllSatisfy (c :: k -> Constraint) (s :: [k]) :: Constraint where
69 AllSatisfy c '[] = ()
710 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+76−17
@@ -9,11 +9,17 @@ module TypeFun.Data.List
99 , Drop
1010 , Take
1111 , Delete
12+ , Remove
13+ , (:++:)
1214 -- * Elements lookup
1315 , IndexOf
14− , IndexOf'
16+ , IndexOfMay
17+ , IndexOfMay'
18+ , IndicesOfMay
1519 , IndicesOf
1620 , Index
21+ , IndexMay
22+ , IndicesMay
1723 , Indices
1824 , Elem
1925 , NotElem
@@ -24,13 +30,19 @@ module TypeFun.Data.List
2430 , IsPrefixOf
2531 , IsNotPrefixOf
2632 , IsPrefixOfBool
33+ -- * Set operations
2734 , Union
35+ , UnionList
2836 , AppendUniq
2937 , Intersect
38+ , Substract
3039 -- * Uniqueness checking
3140 , ElementIsUniq
3241 , UniqElements
3342 , UniqElements'
43+ -- * Unsafe helpers
44+ , appendId
45+ , subListId
3446 ) where
3547
3648import Data.Type.Bool
@@ -39,6 +51,7 @@ import GHC.TypeLits
3951import TypeFun.Data.Eq
4052import TypeFun.Data.Maybe
4153import TypeFun.Data.Peano
54+import Unsafe.Coerce
4255
4356------------------------------------------------------------------------
4457-- NOTE: Errors type classes. These type classes are not in separate --
@@ -58,6 +71,8 @@ class ListIsPrefixOf a b
5871
5972class ElementIsNotUniqInList a s
6073
74+class IndexNotFoundInList a s
75+
6176----------------------
6277-- main module code --
6378----------------------
@@ -83,34 +98,62 @@ type family Delete (a :: k) (s :: [k]) :: [k] where
8398 Delete a (a ': as) = Delete a as
8499 Delete a (b ': as) = b ': (Delete a as)
85100
86−type IndexOf a s = IndexOf' Z a s
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)
110+
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
87126
88−type family IndexOf' (acc :: N) (a :: k) (s :: [k]) :: Maybe N where
89− IndexOf' acc a '[] = 'Nothing
90− IndexOf' acc a (a ': as) = 'Just acc
91− IndexOf' acc a (b ': as) = IndexOf' (S acc) a as
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)
92130
93−type family IndicesOf (a :: [k]) (b :: [k]) :: [Maybe N] where
131+type family IndicesOf (a :: [k]) (b :: [k]) :: [N] where
94132 IndicesOf '[] bs = '[]
95133 IndicesOf (a ': as) bs = (IndexOf a bs) ': (IndicesOf as bs)
96134
97−type family Index (idx :: N) (s :: [k]) :: Maybe k where
98− Index idx '[] = 'Nothing
99− Index Z (a ': as) = 'Just a
100− Index (S idx) (a ': as) = Index idx as
135+type Index idx s = FromJust (IndexMay idx s)
101136
102−type family Indices (idxs :: [N]) (a :: [k]) :: [Maybe k] where
103− Indices '[] as = '[]
104− Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
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)
105145
146+type family Indices (idxs :: [N]) (a :: [k]) :: [k] where
147+ Indices '[] as = '[]
148+ Indices (i ': idxs) as = (Index i as) ': (Indices idxs as)
106149
107150-- | Generates unresolvable constraint if fists element is not
108151-- contained inside of second
109−type Elem a s = NothingToConstr (IndexOf a s)
152+type Elem a s = NothingToConstr (IndexOfMay a s)
110153 (ElementNotFoundInList a s)
111154
112155-- | Reverse of 'Elem'
113−type NotElem a s = JustToConstr (IndexOf a s)
156+type NotElem a s = JustToConstr (IndexOfMay a s)
114157 (ElementIsInList a s)
115158
116159-- | Count elements in a list
@@ -125,6 +168,12 @@ type family SubList (a :: [k]) (b :: [k]) :: Constraint where
125168 SubList '[] bs = ()
126169 SubList (a ': as) bs = (Elem a bs, SubList as bs)
127170
171+subListId
172+ :: forall proxy l r
173+ . proxy l
174+ -> (SubList l l => r) -> r
175+subListId _ = unsafeCoerce id
176+
128177type family NotSubList (a :: [k]) (b :: [k]) :: Constraint where
129178 NotSubList '[] bs = ()
130179 NotSubList (a ': as) bs = (NotElem a bs, NotSubList as bs)
@@ -150,6 +199,10 @@ type family Union (a :: [k]) (b :: [k]) :: [k] where
150199 Union '[] bs = bs
151200 Union (a ': as) bs = Union as (AppendUniq a bs)
152201
202+type family UnionList (l :: [[k]]) :: [k] where
203+ UnionList '[] = '[]
204+ UnionList (a ': as) = Union a (UnionList as)
205+
153206-- | Append element to list if element is not already presented in
154207type family AppendUniq (a :: k) (s :: [k]) :: [k] where
155208 AppendUniq a (a ': bs) = a ': bs
@@ -158,7 +211,13 @@ type family AppendUniq (a :: k) (s :: [k]) :: [k] where
158211
159212-- | Calculates intersection between two lists. Order of elements is
160213-- taken from first list
161−type Intersect a b = (CatMaybes (Indices (CatMaybes (IndicesOf a b)) b))
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
162221
163222-- | Checks that element 'a' occurs in a list just once
164223type ElementIsUniq a s = If (Equal (S Z) (Count a s))
src/TypeFun/Data/Maybe.hs+11−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 = ()
@@ -17,7 +22,11 @@ type family JustToConstr (a :: Maybe k) (c :: Constraint) :: Constraint where
1722 JustToConstr 'Nothing c = ()
1823 JustToConstr ('Just a) c = c
1924
25+-- | Like 'catMaybes' for type lists
2026type family CatMaybes (l :: [Maybe k]) :: [k] where
2127 CatMaybes '[] = '[]
2228 CatMaybes (('Just a) ': as) = a ': (CatMaybes as)
2329 CatMaybes ('Nothing ': as) = CatMaybes as
30+
31+type family FromJust (a :: Maybe k) :: k where
32+ FromJust ('Just a) = a
src/TypeFun/Data/Peano.hs+22−4
@@ -1,13 +1,31 @@
1−module TypeFun.Data.Peano where
1+module TypeFun.Data.Peano
2+ ( N(..)
3+ , KnownPeano(..)
4+ , ToNat
5+ , FromNat
6+ , (:+:)
7+ , (:-:)
8+ , (:*:)
9+ ) where
210
3−import Data.Typeable
4−import GHC.Generics (Generic)
5−import GHC.TypeLits
11+import Data.Typeable
12+import GHC.Generics (Generic)
13+import GHC.TypeLits
614
715data N = Z | S N
816 deriving ( Eq, Ord, Read, Show
917 , Generic, Typeable )
1018
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+
1129type family ToNat (a :: N) :: Nat where
1230 ToNat Z = 0
1331 ToNat (S a) = 1 + (ToNat a)
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+12−10
@@ -1,29 +1,31 @@
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
2323 , DeriveGeneric
2424 , FlexibleInstances
2525 , MultiParamTypeClasses
2626 , PolyKinds
27+ , RankNTypes
28+ , ScopedTypeVariables
2729 , TypeFamilies
2830 , TypeOperators
2931 , UndecidableInstances
@@ -34,15 +36,15 @@ library
3436 , TypeFun.Data.Maybe
3537 , TypeFun.Data.Peano
3638
37− build-depends: base >=4.7 && <4.9
38−
39+ build-depends: base >= 4.7 && < 5
3940
4041test-suite example
4142 type: exitcode-stdio-1.0
4243 default-language: Haskell2010
4344 hs-source-dirs: example
4445 main-is: Main.hs
45− default-extensions: DataKinds
46+ default-extensions: ConstraintKinds
47+ , DataKinds
4648 , GADTs
4749 , TemplateHaskell
4850 , TypeFamilies