1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for 'Alternative'.
4 module Language.Symantic.Lib.Alternative where
6 import Control.Applicative (Alternative)
7 import Control.Monad (liftM2)
9 import Data.Type.Equality ((:~:)(Refl))
10 import Prelude hiding (Functor(..), (<$>), id, const)
11 import qualified Control.Applicative as Alternative
12 import qualified Data.Function as Fun
14 import Language.Symantic.Parsing hiding (Alter(..))
15 import Language.Symantic.Typing
16 import Language.Symantic.Compiling
17 import Language.Symantic.Lib.Lambda
18 import Language.Symantic.Lib.Functor (Sym_Functor(..))
19 import Language.Symantic.Interpreting
20 import Language.Symantic.Transforming
22 -- * Class 'Sym_Alternative'
23 class Sym_Functor term => Sym_Alternative term where
24 empty :: Alternative f => term (f a)
25 (<|>) :: Alternative f => term (f a) -> term (f a) -> term (f a)
28 default empty :: (Trans t term, Alternative f) => t term (f a)
29 default (<|>) :: (Trans t term, Alternative f)
30 => t term (f a) -> t term (f a) -> t term (f a)
32 empty = trans_lift empty
33 (<|>) = trans_map2 (<|>)
35 type instance Sym_of_Iface (Proxy Alternative) = Sym_Alternative
36 type instance TyConsts_of_Iface (Proxy Alternative) = Proxy Alternative ': TyConsts_imported_by (Proxy Alternative)
37 type instance TyConsts_imported_by (Proxy Alternative) = '[]
39 instance Sym_Alternative HostI where
40 empty = HostI Alternative.empty
41 (<|>) = liftM2 (Alternative.<|>)
42 instance Sym_Alternative TextI where
43 empty = textI0 "empty"
44 (<|>) = textI_infix "<|>" (infixL 3)
45 instance (Sym_Alternative r1, Sym_Alternative r2) => Sym_Alternative (DupI r1 r2) where
46 empty = dupI0 @Sym_Alternative empty
47 (<|>) = dupI2 @Sym_Alternative (<|>)
50 ( Read_TyNameR TyName cs rs
51 , Inj_TyConst cs Alternative
52 ) => Read_TyNameR TyName cs (Proxy Alternative ': rs) where
53 read_TyNameR _cs (TyName "Alternative") k = k (ty @Alternative)
54 read_TyNameR _rs raw k = read_TyNameR (Proxy @rs) raw k
55 instance Show_TyConst cs => Show_TyConst (Proxy Alternative ': cs) where
56 show_TyConst TyConstZ{} = "Alternative"
57 show_TyConst (TyConstS c) = show_TyConst c
59 instance Proj_TyConC cs (Proxy Alternative)
60 data instance TokenT meta (ts::[*]) (Proxy Alternative)
61 = Token_Term_Alternative_empty (EToken meta '[Proxy Token_Type]) (EToken meta '[Proxy Token_Type])
62 | Token_Term_Alternative_alt (EToken meta ts)
63 deriving instance (Eq meta, Eq_Token meta ts) => Eq (TokenT meta ts (Proxy Alternative))
64 deriving instance (Show meta, Show_Token meta ts) => Show (TokenT meta ts (Proxy Alternative))
67 ( Read_TyName TyName cs
68 , Inj_TyConst cs Alternative
72 ) => CompileI cs is (Proxy Alternative) where
75 Token_Term_Alternative_empty tok_ty_f tok_ty_a ->
76 -- empty :: Alternative f => f a
77 compile_Type tok_ty_f $ \(ty_f::Type cs f) ->
78 compile_Type tok_ty_a $ \(ty_a::Type cs a) ->
80 (At Nothing $ SKiType `SKiArrow` SKiType)
81 (At (Just tok_ty_f) $ kind_of ty_f) $ \Refl ->
82 check_TyCon (At (Just tok_ty_f) (ty @Alternative :$ ty_f)) $ \TyCon ->
84 (At Nothing $ SKiType)
85 (At (Just tok_ty_a) $ kind_of ty_a) $ \Refl ->
86 k (ty_f :$ ty_a) $ Term $
88 Token_Term_Alternative_alt tok_fl ->
89 -- (<|>) :: Alternative f => f a -> f a -> f a
90 compile tok_fl ctx $ \ty_fa (Term fl) ->
91 check_TyCon1 (ty @Alternative)
92 (At (Just tok_fl) ty_fa) $ \Refl TyCon _ty_f _ty_a ->
93 k (ty_fa ~> ty_fa) $ Term $
94 \c -> lam $ \fr -> (<|>) (fl c) fr
96 Inj_Token meta ts Alternative =>
97 TokenizeT meta ts (Proxy Alternative) where
99 { tokenizers_infix = tokenizeTMod []
100 [ (TeName "empty",) ProTok_Term
101 { protok_term = \meta -> ProTokPi $ \f -> ProTokPi $ \a ->
102 ProTok $ inj_EToken meta $ Token_Term_Alternative_empty f a
103 , protok_fixity = infixN5
105 , tokenize1 "<|>" (infixL 3) Token_Term_Alternative_alt
108 instance Gram_Term_AtomsT meta ts (Proxy Alternative) g