1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for 'Bool'.
4 module Language.Symantic.Compiling.Bool where
7 import qualified Data.Bool as Bool
10 import Data.Text (Text)
11 import qualified Data.Text as Text
12 import Data.Type.Equality ((:~:)(Refl))
13 import Prelude hiding ((&&), not, (||))
15 import Language.Symantic.Parsing
16 import Language.Symantic.Typing
17 import Language.Symantic.Compiling.Term
18 import Language.Symantic.Interpreting
19 import Language.Symantic.Transforming.Trans
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)
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
35 bool = trans_lift . bool
37 (&&) = trans_map2 (&&)
38 (||) = trans_map2 (||)
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 =
53 instance Sym_Bool HostI where
56 (&&) = liftM2 (Bool.&&)
57 (||) = liftM2 (Bool.||)
58 instance Sym_Bool TextI where
59 bool a = TextI $ \_p _v ->
63 let p' = Precedence 9 in
64 paren p p' $ "not " <> x p' v
65 (&&) = textI_infix "&&" (Precedence 6)
66 (||) = textI_infix "||" (Precedence 5)
67 xor = textI_infix "`xor`" (Precedence 5)
68 instance (Sym_Bool r1, Sym_Bool r2) => Sym_Bool (DupI r1 r2) where
69 bool x = bool x `DupI` bool x
70 not = dupI1 (Proxy @Sym_Bool) not
71 (&&) = dupI2 (Proxy @Sym_Bool) (&&)
72 (||) = dupI2 (Proxy @Sym_Bool) (||)
73 xor = dupI2 (Proxy @Sym_Bool) xor
75 instance Const_from Text cs => Const_from Text (Proxy Bool ': cs) where
76 const_from "Bool" k = k (ConstZ kind)
77 const_from s k = const_from s $ k . ConstS
78 instance Show_Const cs => Show_Const (Proxy Bool ': cs) where
79 show_const ConstZ{} = "Bool"
80 show_const (ConstS c) = show_const c
84 , Proj_Consts cs (Consts_imported_by Bool)
85 ) => Proj_ConC cs (Proxy Bool) where
86 proj_conC _ (TyConst q :$ TyConst c)
87 | Just Refl <- eq_skind (kind_of_const c) SKiType
88 , Just Refl <- proj_const c (Proxy::Proxy Bool)
90 _ | Just Refl <- proj_const q (Proxy::Proxy Bounded) -> Just Con
91 | Just Refl <- proj_const q (Proxy::Proxy Enum) -> Just Con
92 | Just Refl <- proj_const q (Proxy::Proxy Eq) -> Just Con
93 | Just Refl <- proj_const q (Proxy::Proxy Ord) -> Just Con
95 proj_conC _c _q = Nothing
96 data instance TokenT meta (ts::[*]) (Proxy Bool)
97 = Token_Term_Bool Bool
101 | Token_Term_Bool_xor
102 deriving instance Eq_Token meta ts => Eq (TokenT meta ts (Proxy Bool))
103 deriving instance Show_Token meta ts => Show (TokenT meta ts (Proxy Bool))
104 instance -- Term_fromI
105 ( Inj_Const (Consts_of_Ifaces is) Bool
106 , Inj_Const (Consts_of_Ifaces is) (->)
107 ) => Term_fromI is (Proxy Bool) where
108 term_fromI tok _ctx k =
110 Token_Term_Bool b -> k (ty @Bool) $ TermLC $ \_c -> bool b
111 Token_Term_Bool_not -> op1_from not
112 Token_Term_Bool_and -> op2_from (&&)
113 Token_Term_Bool_or -> op2_from (||)
114 Token_Term_Bool_xor -> op2_from xor
117 (op::forall term. Sym_Bool term => term Bool -> term Bool) =
118 k (ty @Bool ~> ty @Bool) $ TermLC $ \_c -> lam op
120 (op::forall term. Sym_Bool term => term Bool -> term Bool -> term Bool) =
121 k (ty @Bool ~> ty @Bool ~> ty @Bool) $ TermLC $ \_c -> lam $ lam . op