]> Git — Sourcephile - haskell/symantic.git/blob - symantic-lib/Language/Symantic/Lib/Int.hs
Backtrack (try) the grammar only when necessary to get better error messages.
[haskell/symantic.git] / symantic-lib / Language / Symantic / Lib / Int.hs
1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-# OPTIONS_GHC -fconstraint-solver-iterations=7 #-}
4 -- | Symantic for 'Int'.
5 module Language.Symantic.Lib.Int where
6
7 import Data.Proxy
8 import qualified Data.Text as Text
9 import Data.Type.Equality ((:~:)(Refl))
10
11 import Language.Symantic.Parsing
12 import Language.Symantic.Typing
13 import Language.Symantic.Compiling
14 import Language.Symantic.Interpreting
15 import Language.Symantic.Transforming
16
17 -- * Class 'Sym_Int'
18 class Sym_Int term where
19 int :: Int -> term Int
20 default int :: Trans t term => Int -> t term Int
21 int = trans_lift . int
22
23 type instance Sym_of_Iface (Proxy Int) = Sym_Int
24 type instance TyConsts_of_Iface (Proxy Int) = Proxy Int ': TyConsts_imported_by Int
25 type instance TyConsts_imported_by Int =
26 [ Proxy Bounded
27 , Proxy Enum
28 , Proxy Eq
29 , Proxy Integral
30 , Proxy Num
31 , Proxy Ord
32 , Proxy Real
33 , Proxy Show
34 ]
35
36 instance Sym_Int HostI where
37 int = HostI
38 instance Sym_Int TextI where
39 int a = TextI $ \_p _v ->
40 Text.pack (show a)
41 instance (Sym_Int r1, Sym_Int r2) => Sym_Int (DupI r1 r2) where
42 int x = int x `DupI` int x
43
44 instance
45 ( Read_TyNameR TyName cs rs
46 , Inj_TyConst cs Int
47 ) => Read_TyNameR TyName cs (Proxy Int ': rs) where
48 read_TyNameR _cs (TyName "Int") k = k (ty @Int)
49 read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k
50 instance Show_TyConst cs => Show_TyConst (Proxy Int ': cs) where
51 show_TyConst TyConstZ{} = "Int"
52 show_TyConst (TyConstS c) = show_TyConst c
53
54 instance -- Proj_TyConC
55 ( Proj_TyConst cs Int
56 , Proj_TyConsts cs (TyConsts_imported_by Int)
57 ) => Proj_TyConC cs (Proxy Int) where
58 proj_TyConC _ (TyConst q :$ TyConst c)
59 | Just Refl <- eq_skind (kind_of_TyConst c) SKiType
60 , Just Refl <- proj_TyConst c (Proxy @Int)
61 = case () of
62 _ | Just Refl <- proj_TyConst q (Proxy @Bounded) -> Just TyCon
63 | Just Refl <- proj_TyConst q (Proxy @Enum) -> Just TyCon
64 | Just Refl <- proj_TyConst q (Proxy @Eq) -> Just TyCon
65 | Just Refl <- proj_TyConst q (Proxy @Integral) -> Just TyCon
66 | Just Refl <- proj_TyConst q (Proxy @Num) -> Just TyCon
67 | Just Refl <- proj_TyConst q (Proxy @Ord) -> Just TyCon
68 | Just Refl <- proj_TyConst q (Proxy @Real) -> Just TyCon
69 | Just Refl <- proj_TyConst q (Proxy @Show) -> Just TyCon
70 _ -> Nothing
71 proj_TyConC _c _q = Nothing
72 data instance TokenT meta (ts::[*]) (Proxy Int)
73 = Token_Term_Int Int
74 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Int))
75 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Int))
76 instance -- CompileI
77 Inj_TyConst (TyConsts_of_Ifaces is) Int =>
78 CompileI is (Proxy Int) where
79 compileI tok _ctx k =
80 case tok of
81 Token_Term_Int i -> k (ty @Int) $ TermO $ \_c -> int i
82 instance TokenizeT meta ts (Proxy Int)
83 instance Gram_Term_AtomsT meta ts (Proxy Int) g