1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for 'Ord'.
4 module Language.Symantic.Lib.Ord where
8 import Data.Proxy (Proxy(..))
9 import Data.Type.Equality ((:~:)(Refl))
10 import Prelude hiding (Ord(..))
11 import qualified Data.Kind as Kind
12 import qualified Data.Ord as Ord
13 import qualified Data.Text as Text
15 import Language.Symantic.Parsing
16 import Language.Symantic.Typing
17 import Language.Symantic.Compiling
18 import Language.Symantic.Interpreting
19 import Language.Symantic.Transforming
20 import Language.Symantic.Lib.Lambda
21 import Language.Symantic.Lib.Eq (Sym_Eq)
22 import Language.Symantic.Lib.MonoFunctor (TyFam_MonoElement)
25 class Sym_Eq term => Sym_Ord term where
26 compare :: Ord a => term a -> term a -> term Ordering
27 (<) :: Ord a => term a -> term a -> term Bool; infix 4 <
28 (<=) :: Ord a => term a -> term a -> term Bool; infix 4 <=
29 (>) :: Ord a => term a -> term a -> term Bool; infix 4 >
30 (>=) :: Ord a => term a -> term a -> term Bool; infix 4 >=
31 max :: Ord a => term a -> term a -> term a
32 min :: Ord a => term a -> term a -> term a
34 default compare :: (Trans t term, Ord a) => t term a -> t term a -> t term Ordering
35 default (<) :: (Trans t term, Ord a) => t term a -> t term a -> t term Bool
36 default (<=) :: (Trans t term, Ord a) => t term a -> t term a -> t term Bool
37 default (>) :: (Trans t term, Ord a) => t term a -> t term a -> t term Bool
38 default (>=) :: (Trans t term, Ord a) => t term a -> t term a -> t term Bool
39 default max :: (Trans t term, Ord a) => t term a -> t term a -> t term a
40 default min :: (Trans t term, Ord a) => t term a -> t term a -> t term a
42 compare = trans_map2 compare
44 (<=) = trans_map2 (<=)
46 (>=) = trans_map2 (>=)
50 -- * Class 'Sym_Ordering'
51 class Sym_Eq term => Sym_Ordering term where
52 ordering :: Ordering -> term Ordering
53 default ordering :: Trans t term => Ordering -> t term Ordering
54 ordering = trans_lift . ordering
56 type instance Sym_of_Iface (Proxy Ord) = Sym_Ord
57 type instance Sym_of_Iface (Proxy Ordering) = Sym_Ordering
58 type instance TyConsts_of_Iface (Proxy Ord) = Proxy Ord ': TyConsts_imported_by (Proxy Ord)
59 type instance TyConsts_of_Iface (Proxy Ordering) = Proxy Ordering ': TyConsts_imported_by (Proxy Ordering)
60 type instance TyConsts_imported_by (Proxy Ord) = '[]
61 type instance TyConsts_imported_by (Proxy Ordering) =
69 instance Sym_Ord HostI where
70 compare = liftM2 Ord.compare
72 (<=) = liftM2 (Ord.<=)
74 (>=) = liftM2 (Ord.>=)
77 instance Sym_Ordering HostI where
79 instance Sym_Ord TextI where
80 compare = textI2 "compare"
81 (<) = textI_infix "<" (infixN 4)
82 (<=) = textI_infix "<=" (infixN 4)
83 (>) = textI_infix ">" (infixN 4)
84 (>=) = textI_infix ">=" (infixN 4)
87 instance Sym_Ordering TextI where
88 ordering o = TextI $ \_p _v ->
90 instance (Sym_Ord r1, Sym_Ord r2) => Sym_Ord (DupI r1 r2) where
91 compare = dupI2 @Sym_Ord compare
92 (<) = dupI2 @Sym_Ord (<)
93 (<=) = dupI2 @Sym_Ord (<=)
94 (>) = dupI2 @Sym_Ord (>)
95 (>=) = dupI2 @Sym_Ord (>=)
96 min = dupI2 @Sym_Ord min
97 max = dupI2 @Sym_Ord max
98 instance (Sym_Ordering r1, Sym_Ordering r2) => Sym_Ordering (DupI r1 r2) where
99 ordering o = ordering o `DupI` ordering o
102 ( Read_TyNameR TyName cs rs
104 ) => Read_TyNameR TyName cs (Proxy Ord ': rs) where
105 read_TyNameR _cs (TyName "Ord") k = k (ty @Ord)
106 read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k
108 ( Read_TyNameR TyName cs rs
109 , Inj_TyConst cs Ordering
110 ) => Read_TyNameR TyName cs (Proxy Ordering ': rs) where
111 read_TyNameR _cs (TyName "Ordering") k = k (ty @Ordering)
112 read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k
113 instance Show_TyConst cs => Show_TyConst (Proxy Ord ': cs) where
114 show_TyConst TyConstZ{} = "Ord"
115 show_TyConst (TyConstS c) = show_TyConst c
116 instance Show_TyConst cs => Show_TyConst (Proxy Ordering ': cs) where
117 show_TyConst TyConstZ{} = "Ordering"
118 show_TyConst (TyConstS c) = show_TyConst c
120 instance Proj_TyFamC cs TyFam_MonoElement Ordering
122 instance Proj_TyConC cs (Proxy Ord)
123 instance -- Proj_TyConC
124 ( Proj_TyConst cs Ordering
125 , Proj_TyConsts cs (TyConsts_imported_by (Proxy Ordering))
126 ) => Proj_TyConC cs (Proxy Ordering) where
127 proj_TyConC _ (TyConst q :$ TyConst c)
128 | Just Refl <- eq_skind (kind_of_TyConst c) SKiType
129 , Just Refl <- proj_TyConst c (Proxy @Ordering)
131 _ | Just Refl <- proj_TyConst q (Proxy @Bounded) -> Just TyCon
132 | Just Refl <- proj_TyConst q (Proxy @Enum) -> Just TyCon
133 | Just Refl <- proj_TyConst q (Proxy @Eq) -> Just TyCon
134 | Just Refl <- proj_TyConst q (Proxy @Ord) -> Just TyCon
135 | Just Refl <- proj_TyConst q (Proxy @Show) -> Just TyCon
137 proj_TyConC _c _q = Nothing
138 data instance TokenT meta (ts::[*]) (Proxy Ord)
139 = Token_Term_Ord_compare (EToken meta ts)
140 | Token_Term_Ord_le (EToken meta ts)
141 | Token_Term_Ord_lt (EToken meta ts)
142 | Token_Term_Ord_ge (EToken meta ts)
143 | Token_Term_Ord_gt (EToken meta ts)
144 | Token_Term_Ord_min (EToken meta ts)
145 | Token_Term_Ord_max (EToken meta ts)
146 data instance TokenT meta (ts::[*]) (Proxy Ordering)
147 = Token_Term_Ordering Ordering
148 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Ord))
149 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Ordering))
150 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Ord))
151 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Ordering))
154 ( Inj_TyConst cs Bool
155 , Inj_TyConst cs (->)
157 , Inj_TyConst cs Ordering
160 ) => CompileI cs is (Proxy Ord) where
162 :: forall meta ctx ret ls rs.
163 TokenT meta is (Proxy Ord)
164 -> Compiler meta ctx ret cs is ls (Proxy Ord ': rs)
167 Token_Term_Ord_compare tok_a -> compare_from tok_a (ty @Ordering) compare
168 Token_Term_Ord_le tok_a -> compare_from tok_a (ty @Bool) (<=)
169 Token_Term_Ord_lt tok_a -> compare_from tok_a (ty @Bool) (<)
170 Token_Term_Ord_ge tok_a -> compare_from tok_a (ty @Bool) (>=)
171 Token_Term_Ord_gt tok_a -> compare_from tok_a (ty @Bool) (>)
172 Token_Term_Ord_min tok_a -> op2_from tok_a min
173 Token_Term_Ord_max tok_a -> op2_from tok_a max
177 -> Type cs (res::Kind.Type)
178 -> (forall term a. (Sym_Ord term, Ord a) => term a -> term a -> term res)
179 -> Either (Error_Term meta cs is) ret
180 compare_from tok_a ty_res op =
181 -- compare :: Ord a => a -> a -> Ordering
182 -- (<=) :: Ord a => a -> a -> Bool
183 -- (<) :: Ord a => a -> a -> Bool
184 -- (>=) :: Ord a => a -> a -> Bool
185 -- (>) :: Ord a => a -> a -> Bool
186 compile tok_a ctx $ \ty_a (Term x) ->
187 check_TyCon (At (Just tok_a) (ty @Ord :$ ty_a)) $ \TyCon ->
188 k (ty_a ~> ty_res) $ Term $
191 (op::forall term a. (Sym_Ord term, Ord a)
192 => term a -> term a -> term a) =
193 -- min :: Ord a => a -> a -> a
194 -- max :: Ord a => a -> a -> a
195 compile tok_a ctx $ \ty_a (Term x) ->
196 check_TyCon (At (Just tok_a) (ty @Ord :$ ty_a)) $ \TyCon ->
197 k (ty_a ~> ty_a) $ Term $
198 \c -> lam $ \y -> op (x c) y
200 ( Inj_TyConst cs Ordering
201 ) => CompileI cs is (Proxy Ordering) where
202 compileI tok _ctx k =
204 Token_Term_Ordering o -> k (ty @Ordering) $ Term $ \_c -> ordering o
205 instance -- TokenizeT
206 Inj_Token meta ts Ord =>
207 TokenizeT meta ts (Proxy Ord) where
208 tokenizeT _t = mempty
209 { tokenizers_infix = tokenizeTMod []
210 [ tokenize1 "compare" infixN5 Token_Term_Ord_compare
211 , tokenize1 "<" (infixN 4) Token_Term_Ord_lt
212 , tokenize1 "<=" (infixN 4) Token_Term_Ord_le
213 , tokenize1 ">" (infixN 4) Token_Term_Ord_gt
214 , tokenize1 ">=" (infixN 4) Token_Term_Ord_ge
217 instance -- TokenizeT
218 Inj_Token meta ts Ordering =>
219 TokenizeT meta ts (Proxy Ordering) where
220 tokenizeT _t = mempty
221 { tokenizers_infix = tokenizeTMod []
222 [ tokenize0 "LT" infixN5 $ Token_Term_Ordering LT
223 , tokenize0 "EQ" infixN5 $ Token_Term_Ordering EQ
224 , tokenize0 "GT" infixN5 $ Token_Term_Ordering GT
227 instance Gram_Term_AtomsT meta ts (Proxy Ordering) g
228 instance Gram_Term_AtomsT meta ts (Proxy Ord) g