]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Lib/Bool.hs
Fix Lib.Text.
[haskell/symantic.git] / Language / Symantic / Lib / Bool.hs
1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for 'Bool'.
4 module Language.Symantic.Lib.Bool where
5
6 import Control.Monad
7 import qualified Data.Bool as Bool
8 import Data.Proxy
9 import qualified Data.Text as Text
10 import Data.Type.Equality ((:~:)(Refl))
11 import Prelude hiding ((&&), not, (||))
12
13 import Language.Symantic.Parsing
14 import Language.Symantic.Parsing.Grammar
15 import Language.Symantic.Typing
16 import Language.Symantic.Compiling
17 import Language.Symantic.Interpreting
18 import Language.Symantic.Transforming.Trans
19 import Language.Symantic.Lib.Lambda
20
21 -- * Class 'Sym_Bool'
22 class Sym_Bool term where
23 bool :: Bool -> term Bool
24 not :: term Bool -> term Bool
25 (&&) :: term Bool -> term Bool -> term Bool
26 (||) :: term Bool -> term Bool -> term Bool
27 xor :: term Bool -> term Bool -> term Bool
28 xor x y = (x || y) && not (x && y)
29
30 default bool :: Trans t term => Bool -> t term Bool
31 default not :: Trans t term => t term Bool -> t term Bool
32 default (&&) :: Trans t term => t term Bool -> t term Bool -> t term Bool
33 default (||) :: Trans t term => t term Bool -> t term Bool -> t term Bool
34
35 bool = trans_lift . bool
36 not = trans_map1 not
37 (&&) = trans_map2 (&&)
38 (||) = trans_map2 (||)
39
40 infixr 2 ||
41 infixr 2 `xor`
42 infixr 3 &&
43
44 type instance Sym_of_Iface (Proxy Bool) = Sym_Bool
45 type instance Consts_of_Iface (Proxy Bool) = Proxy Bool ': Consts_imported_by Bool
46 type instance Consts_imported_by Bool =
47 [ Proxy Bounded
48 , Proxy Enum
49 , Proxy Eq
50 , Proxy Ord
51 , Proxy Show
52 ]
53
54 instance Sym_Bool HostI where
55 bool = HostI
56 not = liftM Bool.not
57 (&&) = liftM2 (Bool.&&)
58 (||) = liftM2 (Bool.||)
59 instance Sym_Bool TextI where
60 bool o = TextI $ \_p _v ->
61 Text.pack (show o)
62 not = textI1 "not"
63 (&&) = textI_infix "&&" (infixR 3)
64 (||) = textI_infix "||" (infixR 2)
65 xor = textI_infix "`xor`" (infixR 2)
66 instance (Sym_Bool r1, Sym_Bool r2) => Sym_Bool (DupI r1 r2) where
67 bool b = bool b `DupI` bool b
68 not = dupI1 (Proxy @Sym_Bool) not
69 (&&) = dupI2 (Proxy @Sym_Bool) (&&)
70 (||) = dupI2 (Proxy @Sym_Bool) (||)
71 xor = dupI2 (Proxy @Sym_Bool) xor
72
73 instance
74 ( Read_TypeNameR Type_Name cs rs
75 , Inj_Const cs Bool
76 ) => Read_TypeNameR Type_Name cs (Proxy Bool ': rs) where
77 read_typenameR _cs (Type_Name "Bool") k = k (ty @Bool)
78 read_typenameR _rs raw k = read_typenameR (Proxy @rs) raw k
79 instance Show_Const cs => Show_Const (Proxy Bool ': cs) where
80 show_const ConstZ{} = "Bool"
81 show_const (ConstS c) = show_const c
82
83 instance -- Proj_ConC
84 ( Proj_Const cs Bool
85 , Proj_Consts cs (Consts_imported_by Bool)
86 ) => Proj_ConC cs (Proxy Bool) where
87 proj_conC _ (TyConst q :$ TyConst c)
88 | Just Refl <- eq_skind (kind_of_const c) SKiType
89 , Just Refl <- proj_const c (Proxy @Bool)
90 = case () of
91 _ | Just Refl <- proj_const q (Proxy @Bounded) -> Just Con
92 | Just Refl <- proj_const q (Proxy @Enum) -> Just Con
93 | Just Refl <- proj_const q (Proxy @Eq) -> Just Con
94 | Just Refl <- proj_const q (Proxy @Ord) -> Just Con
95 | Just Refl <- proj_const q (Proxy @Show) -> Just Con
96 _ -> Nothing
97 proj_conC _c _q = Nothing
98 data instance TokenT meta (ts::[*]) (Proxy Bool)
99 = Token_Term_Bool Bool
100 | Token_Term_Bool_not
101 | Token_Term_Bool_and
102 | Token_Term_Bool_or
103 | Token_Term_Bool_xor
104 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Bool))
105 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Bool))
106 instance -- CompileI
107 ( Inj_Const (Consts_of_Ifaces is) Bool
108 , Inj_Const (Consts_of_Ifaces is) (->)
109 ) => CompileI is (Proxy Bool) where
110 compileI tok _ctx k =
111 case tok of
112 Token_Term_Bool b -> k (ty @Bool) $ TermO $ \_c -> bool b
113 Token_Term_Bool_not -> op1_from not
114 Token_Term_Bool_and -> op2_from (&&)
115 Token_Term_Bool_or -> op2_from (||)
116 Token_Term_Bool_xor -> op2_from xor
117 where
118 op1_from
119 (op::forall term. Sym_Bool term => term Bool -> term Bool) =
120 k (ty @Bool ~> ty @Bool) $ TermO $ \_c -> lam op
121 op2_from
122 (op::forall term. Sym_Bool term => term Bool -> term Bool -> term Bool) =
123 k (ty @Bool ~> ty @Bool ~> ty @Bool) $ TermO $ \_c -> lam $ lam . op
124 instance -- TokenizeT
125 Inj_Token meta ts Bool =>
126 TokenizeT meta ts (Proxy Bool) where
127 tokenizeT _t = mempty
128 { tokenizers_infix = tokenizeTMod []
129 [ tokenize0 "False" infixN5 $ Token_Term_Bool False
130 , tokenize0 "True" infixN5 $ Token_Term_Bool True
131 , tokenize0 "not" infixN5 Token_Term_Bool_not
132 , tokenize0 "and" (infixR 3) Token_Term_Bool_and
133 , tokenize0 "or" (infixR 2) Token_Term_Bool_or
134 , tokenize0 "xor" (infixR 2) Token_Term_Bool_xor
135 ]
136 }
137 instance Gram_Term_AtomsT meta ts (Proxy Bool) g