git.iko.soy/type-level-sets
on master help sign in
type-level-sets/example.hs
31 lines 963 B history raw
1{-# LANGUAGE DataKinds, TypeOperators, TypeFamilies, MultiParamTypeClasses #-}
3import GHC.TypeLits
4import Data.Type.Map
6-- Specify that key-value pairs on Ints combine to an Int
7type instance Combine Int Int = Int
8-- Specify that Int values for matching keys should be added
9instance Combinable Int Int where
10 combine x y = x + y
12foo :: Map '["x" :-> Int, "z" :-> Bool, "w" :-> Int]
13foo = Ext (Var :: (Var "x")) 2
14 $ Ext (Var :: (Var "z")) True
15 $ Ext (Var :: (Var "w")) 5
16 $ Empty
18foo' :: Map (AsMap '["z" :-> Bool, "x" :-> Int, "w" :-> Int])
19foo' = asMap foo
21bar :: Map '["y" :-> Int, "w" :-> Int]
22bar = Ext (Var :: (Var "y")) 3 $
23 Ext (Var :: (Var "w")) 1 $
24 Empty
26-- GHC can easily infer this type, so an explicit signature not necessary
27-- foobar :: Map '["w" :-> Int, "x" :-> Int, "y" :-> Integer, "z" :-> Int]
28foobar = foo `union` bar
30foobarToFoo :: Map '["w" :-> Int, "x" :-> Int, "z" :-> Bool]
31foobarToFoo = submap foobar