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 Text.Read (Read (..), reads)
14 import Text.Show (Show (..))
16 import Symantic.Compiler
17 import Symantic.Semantics (PerSyntax)
21 data Parser syns meta = Parser
23 ( -- MT.State (TokenTerm meta a)
26 Either (PerSyntax syns (ErrorParser meta)) (TermVT syns meta vs)
31 -- * Data family 'ErrorParser'
32 data family ErrorParser meta (syn :: Syntax)
34 ---- * Type 'SomeParsingError'
35 -- data SomeParsingError
38 -- -- Derivable (SomeParsingError syn sem),
41 -- SomeParsingError (SomeParsingError syn)
43 ----type instance Derived (SomeParsingError sem) = sem
44 ----instance Derivable (SomeParsingError sem) where
45 ---- derive (SomeParsingError x) = derive x
47 ---- ** Type 'SomeParsingError'
49 ---- TODO: neither data families nor data instances
50 ---- can have phantom roles with GHC-9's RoleAnnotations,
51 ---- hence 'SomeParsingError.Coerce.coerce' cannot be used on them for now.
52 ---- https://gitlab.haskell.org/ghc/ghc/-/issues/8177
53 ---- https://gitlab.haskell.org/ghc/ghc/-/wikis/roles#proposal-roles-for-type-families
54 -- data family SomeParsingError (syn :: Syntax) :: Semantic -> Semantic
55 ----type instance Derived (SomeParsingError syn sem) = sem
57 ---- | Convenient utility to pattern-match a 'SomeParsingError'.
58 ----pattern SomeParsingError :: Typeable syn => SomeParsingError syn sem a -> SomeParsingError sem a
59 ----pattern SomeParsingError x <- (unSomeParsingError -> Maybe.Just x)
61 ---- | @(unSomeParsingError sd :: 'Maybe' ('SomeParsingError' syn sem a))@
62 ---- extract the data-constructor from the given 'SomeParsingError' @(sd)@
63 ---- iif. it belongs to the @('SomeParsingError' syn sem a)@ data-instance.
64 -- unSomeParsingError ::
67 -- SomeParsingError sem a ->
68 -- Maybe (SomeParsingError syn sem a)
69 -- unSomeParsingError (SomeParsingError (constr :: SomeParsingError actualSyn sem a)) =
70 -- case typeRep @syn `eqTypeRep` typeRep @actualSyn of
71 -- Maybe.Just HRefl -> Maybe.Just constr
72 -- Maybe.Nothing -> Maybe.Nothing
74 data TermVT syns meta vs = forall a vs'.
76 { termVTType :: Ty meta vs' a
77 , unTermVT :: OpenTerm syns vs a
85 | BinTree2 (BinTree a) (BinTree a)
88 type TermAST meta = BinTree (TokenTerm meta)
90 = TokenTermAbst String (TyT meta '[]) (TermAST meta)
92 | TokenTermAtom String
95 safeRead :: Read a => String -> Either ErrMsg a
96 safeRead s = case reads s of
98 _ -> Left $ "Read error: " <> s
101 instance ( forall sem. syns sem => Functor sem
102 ) => Functor (Parser syns) where
103 fmap f (Parser esa) = Parser $
106 Right (Sem sem) -> Right (Sem (f <$> sem))
107 a <$ Parser esa = Parser $
110 Right (Sem sem) -> Right (Sem (a <$ sem))
111 instance ( forall sem. syns sem => Applicative sem
112 , Applicative (Sem syns)
113 , forall err. syns (Either err)
114 , syns (Parser syns) -- FIXME: what constraint is still missing to still require that?
115 ) => Applicative (Parser syns) where
116 pure a = Parser (Right (Sem (pure a)))
117 liftA2 f (Parser a) (Parser b) = Parser (liftA2 (liftA2 f) a b)
118 (<*>) (Parser f) (Parser a) = Parser (liftA2 (<*>) f a)
119 (*>) (Parser f) (Parser a) = Parser (liftA2 (*>) f a)
120 (<*) (Parser f) (Parser a) = Parser (liftA2 (<*) f a)
121 instance ( forall sem. syns sem => Monad sem
122 , forall err. syns (Either err)
123 , syns (Parser syns) -- FIXME: what constraint is still missing to still require that?
124 ) => Monad (Parser syns) where
125 (>>=) (Parser efsa) f = Parser (efsa >>= \(Sem sa) -> sa >>= unParser . f)
130 -- | /Typing context/
131 -- accumulating at each /lambda abstraction/
132 -- the 'Type' of the introduced variable.
133 -- It is built top-down from the closest
134 -- including /lambda abstraction/ to the farest.
135 -- It determines the 'Type's of 'CtxTe'.
136 data CtxTy meta (as :: [Type]) void where
137 CtxTyZ :: CtxTy meta '[] Void
138 CtxTyS :: NameTe -> Ty meta '[] a -> CtxTy meta as Void -> CtxTy meta (a ': as) Void
142 instance LenVar CtxTy where
144 lenVar (CtxTyS _n _ty x) = LenS (lenVar x)