1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for 'Bool'.
4 module Language.Symantic.Lib.Bool where
7 import Prelude hiding ((&&), not, (||))
8 import qualified Data.Bool as Bool
9 import qualified Data.Text as Text
11 import Language.Symantic
12 import Language.Symantic.Lib.Function ()
15 type instance Sym Bool = Sym_Bool
16 class Sym_Bool term where
17 bool :: Bool -> term Bool
18 not :: term Bool -> term Bool
19 (&&) :: term Bool -> term Bool -> term Bool; infixr 3 &&
20 (||) :: term Bool -> term Bool -> term Bool; infixr 2 ||
21 xor :: term Bool -> term Bool -> term Bool; infixr 2 `xor`
22 xor x y = (x || y) && not (x && y)
24 default bool :: Sym_Bool (UnT term) => Trans term => Bool -> term Bool
25 default not :: Sym_Bool (UnT term) => Trans term => term Bool -> term Bool
26 default (&&) :: Sym_Bool (UnT term) => Trans term => term Bool -> term Bool -> term Bool
27 default (||) :: Sym_Bool (UnT term) => Trans term => term Bool -> term Bool -> term Bool
35 instance Sym_Bool Eval where
38 (&&) = liftM2 (Bool.&&)
39 (||) = liftM2 (Bool.||)
40 instance Sym_Bool View where
41 bool o = View $ \_p _v -> Text.pack (show o)
43 (&&) = viewInfix "&&" (infixR 3)
44 (||) = viewInfix "||" (infixR 2)
45 xor = viewInfix "`xor`" (infixR 2)
46 instance (Sym_Bool r1, Sym_Bool r2) => Sym_Bool (Dup r1 r2) where
47 bool b = bool b `Dup` bool b
48 not = dup1 @Sym_Bool not
49 (&&) = dup2 @Sym_Bool (&&)
50 (||) = dup2 @Sym_Bool (||)
51 xor = dup2 @Sym_Bool xor
54 instance (Sym_Lambda term, Sym_Bool term) => Sym_Bool (BetaT term) where
58 instance ClassInstancesFor Bool where
59 proveConstraintFor _ (TyApp _ (TyConst _ _ q) c)
60 | Just HRefl <- proj_ConstKiTy @_ @Bool c
62 _ | Just Refl <- proj_Const @Bounded q -> Just Dict
63 | Just Refl <- proj_Const @Enum q -> Just Dict
64 | Just Refl <- proj_Const @Eq q -> Just Dict
65 | Just Refl <- proj_Const @Ord q -> Just Dict
66 | Just Refl <- proj_Const @Show q -> Just Dict
68 proveConstraintFor _c _q = Nothing
69 instance TypeInstancesFor Bool
72 instance Gram_Term_AtomsFor src ss g Bool
73 instance (Source src, Inj_Sym ss Bool) => ModuleFor src ss Bool where
74 moduleFor = ["Bool"] `moduleWhere`
75 [ "False" := teBool False
76 , "True" := teBool True
78 , "and" `withInfixR` 3 := teBool_and
79 , "or" `withInfixR` 2 := teBool_or
80 , "xor" `withInfixR` 2 := teBool_xor
84 tyBool :: Source src => Inj_Len vs => Type src vs Bool
85 tyBool = tyConst @(K Bool) @Bool
88 teBool :: Source src => Inj_Sym ss Bool => Bool -> Term src ss ts '[] (() #> Bool)
89 teBool b = Term noConstraint tyBool $ teSym @Bool $ bool b
91 teBool_not :: TermDef Bool '[] (() #> (Bool -> Bool))
92 teBool_not = Term noConstraint (tyBool ~> tyBool) $ teSym @Bool $ lam1 not
94 teBool_and, teBool_or, teBool_xor :: TermDef Bool '[] (() #> (Bool -> Bool -> Bool))
95 teBool_and = Term noConstraint (tyBool ~> tyBool ~> tyBool) $ teSym @Bool $ lam2 (&&)
96 teBool_or = Term noConstraint (tyBool ~> tyBool ~> tyBool) $ teSym @Bool $ lam2 (||)
97 teBool_xor = Term noConstraint (tyBool ~> tyBool ~> tyBool) $ teSym @Bool $ lam2 xor