]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Type/Map.hs
Integer, Integral, Num
[haskell/symantic.git] / Language / Symantic / Type / Map.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE OverloadedStrings #-}
6 {-# LANGUAGE PatternSynonyms #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# OPTIONS_GHC -fno-warn-orphans #-}
10 module Language.Symantic.Type.Map where
11
12 import Data.Map.Strict as Map
13 import Data.Proxy
14 import Data.Type.Equality ((:~:)(Refl))
15
16 import Language.Symantic.Type.Root
17 import Language.Symantic.Type.Type0
18 import Language.Symantic.Type.Type1
19 import Language.Symantic.Type.Type2
20
21 -- * Type 'Type_Map'
22 -- | The 'Map' type.
23 type Type_Map = Type_Type2 (Proxy Map)
24
25 pattern Type_Map
26 :: root k -> root a
27 -> Type_Type2 (Proxy Map) root (Map k a)
28 pattern Type_Map k a
29 = Type_Type2 Proxy k a
30
31 instance Unlift_Type1 Type_Map where
32 unlift_type1 (Type_Type2 px a b) k =
33 k ( Type_Type1 (Proxy::Proxy (Map a)) b
34 , Lift_Type1 (\(Type_Type1 _ b') -> Type_Type2 px a b')
35 )
36 instance
37 Constraint_Type Eq root =>
38 Constraint_Type Eq (Type_Map root) where
39 constraint_type c (Type_Type2 _ k a)
40 | Just Dict <- constraint_type c k
41 , Just Dict <- constraint_type c a
42 = Just Dict
43 constraint_type _c _ = Nothing
44 instance
45 Constraint_Type Ord root =>
46 Constraint_Type Ord (Type_Map root) where
47 constraint_type c (Type_Type2 _ k a)
48 | Just Dict <- constraint_type c k
49 , Just Dict <- constraint_type c a
50 = Just Dict
51 constraint_type _c _ = Nothing
52 instance
53 Constraint_Type Ord root =>
54 Constraint_Type Monoid (Type_Map root) where
55 constraint_type _c (Type_Type2 _ k _a)
56 | Just Dict <- constraint_type (Proxy::Proxy Ord) k
57 = Just Dict
58 constraint_type _c _ = Nothing
59 instance Constraint_Type Num (Type_Map root)
60 instance Constraint_Type Integral (Type_Map root)
61 instance Constraint_Type1 Functor (Type_Map root) where
62 constraint_type1 _c Type_Type2{} = Just Dict
63 instance Constraint_Type1 Traversable (Type_Map root) where
64 constraint_type1 _c Type_Type2{} = Just Dict
65 instance Constraint_Type1 Foldable (Type_Map root) where
66 constraint_type1 _c Type_Type2{} = Just Dict
67 instance -- Eq_Type
68 Eq_Type root =>
69 Eq_Type (Type_Map root) where
70 eq_type (Type_Type2 _px1 k1 a1) (Type_Type2 _px2 k2 a2)
71 | Just Refl <- k1 `eq_type` k2
72 , Just Refl <- a1 `eq_type` a2
73 = Just Refl
74 eq_type _ _ = Nothing
75 instance -- Eq_Type1
76 Eq_Type root =>
77 Eq_Type1 (Type_Map root) where
78 eq_type1 (Type_Type2 _px1 k1 _a1) (Type_Type2 _px2 k2 _a2)
79 | Just Refl <- k1 `eq_type` k2
80 = Just Refl
81 eq_type1 _ _ = Nothing
82 instance -- String_from_Type
83 String_from_Type root =>
84 String_from_Type (Type_Map root) where
85 string_from_type (Type_Type2 _ k a) =
86 "Map (" ++ string_from_type k ++ ")"
87 ++ " (" ++ string_from_type a ++ ")"
88
89 -- | Inject 'Type_Map' within a root type.
90 type_map
91 :: forall root h_k h_a.
92 (Lift_Type_Root Type_Map root)
93 => root h_k -> root h_a
94 -> root (Map h_k h_a)
95 type_map k a = lift_type_root (Type_Type2 Proxy k a
96 ::Type_Map root (Map h_k h_a))