1 {-# LANGUAGE RankNTypes #-}
2 {-# OPTIONS_GHC -Wno-partial-fields #-}
4 module Symantic.Parser where
6 import Data.Either (Either (..))
7 import Data.Eq (Eq (..))
8 import Data.Function (($))
9 import Data.Semigroup (Semigroup (..))
10 import Data.String (String)
11 import Data.Void (Void)
13 import Symantic.Syntaxes.Classes (Syntax)
14 import Text.Read (Read (..), reads)
15 import Text.Show (Show (..))
17 import Symantic.Compiler
18 import Symantic.Semantics (PerSyntax)
22 data Parser syns prov = Parser
24 ( -- MT.State (TokenTerm prov a)
27 Either (PerSyntax syns (ErrorParser prov)) (TermVT syns prov vs)
32 -- * Data family 'ErrorParser'
33 data family ErrorParser prov (syn :: Syntax)
35 ---- * Type 'SomeParsingError'
36 -- data SomeParsingError
39 -- -- Derivable (SomeParsingError syn sem),
42 -- SomeParsingError (SomeParsingError syn)
44 ----type instance Derived (SomeParsingError sem) = sem
45 ----instance Derivable (SomeParsingError sem) where
46 ---- derive (SomeParsingError x) = derive x
48 ---- ** Type 'SomeParsingError'
50 ---- TODO: neither data families nor data instances
51 ---- can have phantom roles with GHC-9's RoleAnnotations,
52 ---- hence 'SomeParsingError.Coerce.coerce' cannot be used on them for now.
53 ---- https://gitlab.haskell.org/ghc/ghc/-/issues/8177
54 ---- https://gitlab.haskell.org/ghc/ghc/-/wikis/roles#proposal-roles-for-type-families
55 -- data family SomeParsingError (syn :: Syntax) :: Semantic -> Semantic
56 ----type instance Derived (SomeParsingError syn sem) = sem
58 ---- | Convenient utility to pattern-match a 'SomeParsingError'.
59 ----pattern SomeParsingError :: Typeable syn => SomeParsingError syn sem a -> SomeParsingError sem a
60 ----pattern SomeParsingError x <- (unSomeParsingError -> Maybe.Just x)
62 ---- | @(unSomeParsingError sd :: 'Maybe' ('SomeParsingError' syn sem a))@
63 ---- extract the data-constructor from the given 'SomeParsingError' @(sd)@
64 ---- iif. it belongs to the @('SomeParsingError' syn sem a)@ data-instance.
65 -- unSomeParsingError ::
68 -- SomeParsingError sem a ->
69 -- Maybe (SomeParsingError syn sem a)
70 -- unSomeParsingError (SomeParsingError (constr :: SomeParsingError actualSyn sem a)) =
71 -- case typeRep @syn `eqTypeRep` typeRep @actualSyn of
72 -- Maybe.Just HRefl -> Maybe.Just constr
73 -- Maybe.Nothing -> Maybe.Nothing
75 data TermVT syns prov vs = forall a vs'.
77 { termVTType :: Ty prov vs' a
78 , unTermVT :: OpenTerm syns vs a
86 | BinTree2 (BinTree a) (BinTree a)
90 type TermAST prov = BinTree (TokenTerm prov)
94 = TokenTermAbst String (TyT prov '[]) (TermAST prov)
96 | TokenTermAtom String
99 safeRead :: Read a => String -> Either ErrMsg a
100 safeRead s = case reads s of
102 _ -> Left $ "Read error: " <> s
105 instance ( forall sem. syns sem => Functor sem
106 ) => Functor (Parser syns) where
107 fmap f (Parser esa) = Parser $
110 Right (Sem sem) -> Right (Sem (f <$> sem))
111 a <$ Parser esa = Parser $
114 Right (Sem sem) -> Right (Sem (a <$ sem))
115 instance ( forall sem. syns sem => Applicative sem
116 , Applicative (Sem syns)
117 , forall err. syns (Either err)
118 , syns (Parser syns) -- FIXME: what constraint is still missing to still require that?
119 ) => Applicative (Parser syns) where
120 pure a = Parser (Right (Sem (pure a)))
121 liftA2 f (Parser a) (Parser b) = Parser (liftA2 (liftA2 f) a b)
122 (<*>) (Parser f) (Parser a) = Parser (liftA2 (<*>) f a)
123 (*>) (Parser f) (Parser a) = Parser (liftA2 (*>) f a)
124 (<*) (Parser f) (Parser a) = Parser (liftA2 (<*) f a)
125 instance ( forall sem. syns sem => Monad sem
126 , forall err. syns (Either err)
127 , syns (Parser syns) -- FIXME: what constraint is still missing to still require that?
128 ) => Monad (Parser syns) where
129 (>>=) (Parser efsa) f = Parser (efsa >>= \(Sem sa) -> sa >>= unParser . f)
134 -- | /Typing context/
135 -- accumulating at each /lambda abstraction/
136 -- the 'Type' of the introduced variable.
137 -- It is built top-down from the closest
138 -- including /lambda abstraction/ to the farest.
139 -- It determines the 'Type's of 'CtxTe'.
140 data CtxTy prov (as :: [Type]) void where
141 CtxTyZ :: CtxTy prov '[] Void
142 CtxTyS :: NameTe -> Ty prov '[] a -> CtxTy prov as Void -> CtxTy prov (a ': as) Void
146 instance LenVar CtxTy where
148 lenVar (CtxTyS _n _ty x) = LenS (lenVar x)