1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=6 #-}
4 -- | Symantic for 'Integer'.
5 module Language.Symantic.Lib.Integer where
8 import qualified Data.Text as Text
9 import Data.Type.Equality ((:~:)(Refl))
11 import Language.Symantic.Parsing
12 import Language.Symantic.Parsing.Grammar
13 import Language.Symantic.Typing
14 import Language.Symantic.Compiling
15 import Language.Symantic.Interpreting
16 import Language.Symantic.Transforming.Trans
18 -- * Class 'Sym_Integer'
19 class Sym_Integer term where
20 integer :: Integer -> term Integer
21 default integer :: Trans t term => Integer -> t term Integer
22 integer = trans_lift . integer
24 type instance Sym_of_Iface (Proxy Integer) = Sym_Integer
25 type instance Consts_of_Iface (Proxy Integer) = Proxy Integer ': Consts_imported_by Integer
26 type instance Consts_imported_by Integer =
36 instance Sym_Integer HostI where
38 instance Sym_Integer TextI where
39 integer a = TextI $ \_p _v ->
41 instance (Sym_Integer r1, Sym_Integer r2) => Sym_Integer (DupI r1 r2) where
42 integer x = integer x `DupI` integer x
45 ( Read_TypeNameR Type_Name cs rs
46 , Inj_Const cs Integer
47 ) => Read_TypeNameR Type_Name cs (Proxy Integer ': rs) where
48 read_typenameR _cs (Type_Name "Integer") k = k (ty @Integer)
49 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
50 instance Show_Const cs => Show_Const (Proxy Integer ': cs) where
51 show_const ConstZ{} = "Integer"
52 show_const (ConstS c) = show_const c
55 ( Proj_Const cs Integer
56 , Proj_Consts cs (Consts_imported_by Integer)
57 ) => Proj_ConC cs (Proxy Integer) where
58 proj_conC _ (TyConst q :$ TyConst c)
59 | Just Refl <- eq_skind (kind_of_const c) SKiType
60 , Just Refl <- proj_const c (Proxy @Integer)
62 _ | Just Refl <- proj_const q (Proxy @Enum) -> Just Con
63 | Just Refl <- proj_const q (Proxy @Eq) -> Just Con
64 | Just Refl <- proj_const q (Proxy @Integral) -> Just Con
65 | Just Refl <- proj_const q (Proxy @Num) -> Just Con
66 | Just Refl <- proj_const q (Proxy @Ord) -> Just Con
67 | Just Refl <- proj_const q (Proxy @Real) -> Just Con
68 | Just Refl <- proj_const q (Proxy @Show) -> Just Con
70 proj_conC _c _q = Nothing
71 data instance TokenT meta (ts::[*]) (Proxy Integer)
72 = Token_Term_Integer Integer
73 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Integer))
74 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Integer))
76 Inj_Const (Consts_of_Ifaces is) Integer =>
77 CompileI is (Proxy Integer) where
80 Token_Term_Integer i -> k (ty @Integer) $ TermO $ \_c -> integer i
82 -- Inj_Token meta ts Integer =>
83 TokenizeT meta ts (Proxy Integer)
84 instance -- Gram_Term_AtomsT
90 , Inj_Token meta ts Integer
91 ) => Gram_Term_AtomsT meta ts (Proxy Integer) g where
93 [ rule "term_integer" $
95 (\i meta -> ProTok $ inj_etoken meta $ Token_Term_Integer $ read i)
96 <$> some (choice $ char <$> ['0'..'9'])