1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DeriveFunctor #-}
4 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
5 {-# LANGUAGE PolyKinds #-}
6 {-# LANGUAGE UndecidableInstances #-}
7 {-# OPTIONS_GHC -fno-warn-orphans #-}
8 module Language.Symantic.Compiling.Term.Grammar where
10 import Control.Applicative (Alternative(..))
11 import Control.Arrow (left)
12 import Control.Monad (foldM, void, (=<<))
13 import qualified Data.Char as Char
14 import qualified Data.Function as Fun
15 import qualified Data.List as List
16 import Data.Map.Strict (Map)
17 import qualified Data.Map.Strict as Map
18 import Data.Monoid ((<>))
19 import Data.Proxy (Proxy(..))
20 import Data.Text (Text)
21 import qualified Data.Text as Text
22 import Prelude hiding (mod, not, any)
24 import Language.Symantic.Parsing
25 import Language.Symantic.Parsing.Grammar
26 import Language.Symantic.Parsing.EBNF
27 import Language.Symantic.Typing
30 newtype Term_Name = Term_Name Text
31 deriving (Eq, Ord, Show)
34 -- | Proto 'EToken'. It's almost like a free monad,
35 -- but has a third constructor ('ProTokPi')
36 -- to require a type argument.
38 -- NOTE: this type may one day be removed
39 -- if proper type inferencing is done.
40 -- In the meantime it is used to require
41 -- term or type arguments needed to build
42 -- the 'EToken's of polymorphic terms.
44 = ProTokLam (EToken meta ts -> ProTok meta ts)
45 -- ^ Require a term argument.
46 | ProTokPi (EToken meta '[Proxy Token_Type] -> ProTok meta ts)
47 -- ^ Require a type argument.
48 | ProTok (EToken meta ts)
49 -- ^ No need for any argument.
51 -- | Declared here and not in @Compiling.Lambda@
52 -- to be able to use 'Token_Term_Var' in 'protok'.
53 data instance TokenT meta (ts::[*]) (Proxy (->))
54 = Token_Term_Abst Term_Name (EToken meta '[Proxy Token_Type]) (EToken meta ts)
55 | Token_Term_App (EToken meta ts) (EToken meta ts)
56 | Token_Term_Let Term_Name (EToken meta ts) (EToken meta ts)
57 | Token_Term_Var Term_Name
58 | Token_Term_Compose (EToken meta ts) (EToken meta ts)
62 = TokenizeR meta ts ts
64 -- ** Type 'Tokenizers'
65 data Tokenizers meta ts
67 { tokenizers_prefix :: Map Mod_Path (Map Term_Name (Term_ProTok Unifix meta ts))
68 , tokenizers_infix :: Map Mod_Path (Map Term_Name (Term_ProTok Infix meta ts))
69 , tokenizers_postfix :: Map Mod_Path (Map Term_Name (Term_ProTok Unifix meta ts))
71 instance Monoid (Tokenizers meta ts) where
72 mempty = Tokenizers Map.empty Map.empty Map.empty
75 (Map.unionWith Map.union
77 (tokenizers_prefix y))
78 (Map.unionWith Map.union
81 (Map.unionWith Map.union
82 (tokenizers_postfix x)
83 (tokenizers_postfix y))
85 data Term_ProTok fixy meta ts
87 { term_protok :: meta -> ProTok meta ts
91 tokenizers :: forall meta ts. Tokenize meta ts => Tokenizers meta ts
92 tokenizers = tokenizeR (Proxy @ts)
96 -> Either Error_Term_Gram (EToken meta ts)
97 unProTok (ProTok t) = Right t
98 unProTok _ = Left Error_Term_Gram_Term_incomplete
101 :: Inj_Token meta ts (->)
103 -> Tokenizers meta ts
104 -> Either Error_Term_Gram
105 ( Maybe (Term_ProTok Unifix meta ts)
106 , Term_ProTok Infix meta ts
107 , Maybe (Term_ProTok Unifix meta ts)
109 protok (mod `Mod` tn) (Tokenizers pres ins posts) = do
110 let pre = Map.lookup mod pres >>= Map.lookup tn
111 let post = Map.lookup mod posts >>= Map.lookup tn
112 in_ <- var_or_err $ Map.lookup mod ins >>= Map.lookup tn
113 return (pre, in_, post)
115 var_or_err (Just x) = Right x
118 [] -> Right (var infixN5)
119 _ -> Left $ Error_Term_Gram_Undefined_term
122 { term_protok = \meta -> ProTok $ inj_etoken meta $ Token_Term_Var tn
127 :: Inj_Token meta ts (->)
129 -> [Either (EToken meta '[Proxy Token_Type]) (EToken meta ts)]
130 -> Either Error_Term_Gram (ProTok meta ts)
136 ProTokPi g -> Right $ g typ
137 _ -> Left Error_Term_Gram_Cannot_apply_type
140 ProTokLam f -> Right $ f te
141 ProTok tok@(EToken e) -> Right $
142 ProTok $ inj_etoken (meta_of e) $
143 Token_Term_App tok te
144 _ -> Left Error_Term_Gram_Cannot_apply_term
146 -- ** Class 'TokenizeR'
147 class TokenizeR meta (ts::[*]) (rs::[*]) where
148 tokenizeR :: Proxy rs -> Tokenizers meta ts
149 instance TokenizeR meta ts '[] where
150 tokenizeR _rs = mempty
152 ( TokenizeT meta ts t
153 , TokenizeR meta ts rs
154 ) => TokenizeR meta ts (t ': rs) where
156 tokenizeR (Proxy @rs) `mappend`
159 -- ** Class 'TokenizeT'
160 class TokenizeT meta ts t where
161 tokenizeT :: Proxy t -> Tokenizers meta ts
162 -- tokenizeT _t = [] `Mod` []
163 tokenizeT _t = mempty
167 -> [(Term_Name, Term_ProTok fix meta ts)]
168 -> Map Mod_Path (Map Term_Name (Term_ProTok fix meta ts))
169 tokenizeTMod mod tbl = Map.singleton mod $ Map.fromList tbl
172 :: Inj_Token meta ts t
173 => Text -> fixity -> TokenT meta ts (Proxy t)
174 -> (Term_Name, Term_ProTok fixity meta ts)
175 tokenize0 n term_fixity tok =
176 (Term_Name n,) Term_ProTok
177 { term_protok = \meta -> ProTok $ inj_etoken meta $ tok
181 :: Inj_Token meta ts t
183 -> (EToken meta ts -> TokenT meta ts (Proxy t))
184 -> (Term_Name, Term_ProTok fixity meta ts)
185 tokenize1 n term_fixity tok =
186 (Term_Name n,) Term_ProTok
187 { term_protok = \meta ->
189 ProTok $ inj_etoken meta $ tok a
193 :: Inj_Token meta ts t
195 -> (EToken meta ts -> EToken meta ts -> TokenT meta ts (Proxy t))
196 -> (Term_Name, Term_ProTok fixity meta ts)
197 tokenize2 n term_fixity tok =
198 (Term_Name n,) Term_ProTok
199 { term_protok = \meta ->
200 ProTokLam $ \a -> ProTokLam $ \b ->
201 ProTok $ inj_etoken meta $ tok a b
206 :: Inj_Token meta ts t
208 -> (EToken meta ts -> EToken meta ts -> EToken meta ts -> TokenT meta ts (Proxy t))
209 -> (Term_Name, Term_ProTok fixity meta ts)
210 tokenize3 n term_fixity tok =
211 (Term_Name n,) Term_ProTok
212 { term_protok = \meta ->
213 ProTokLam $ \a -> ProTokLam $ \b -> ProTokLam $ \c ->
214 ProTok $ inj_etoken meta $ tok a b c
219 type Mod_Path = [Mod_Name]
220 newtype Mod_Name = Mod_Name Text
221 deriving (Eq, Ord, Show)
222 data Mod a = Mod Mod_Path a
223 deriving (Eq, Functor, Ord, Show)
225 -- * Class 'Gram_Term_Name'
237 ) => Gram_Term_Name g where
238 mod_path :: CF g Mod_Path
239 mod_path = rule "mod_path" $
244 mod_name :: CF g Mod_Name
245 mod_name = rule "mod_name" $
246 (Mod_Name . Text.pack <$>) $
250 <*. (any `but` term_idname_tail))
252 identG = (:) <$> headG <*> many (cf_of_term term_idname_tail)
253 headG = unicat $ Unicat Char.UppercaseLetter
255 term_mod_name :: CF g (Mod Term_Name)
256 term_mod_name = rule "term_mod_name" $
259 parens term_mod_opname
260 term_name :: CF g Term_Name
261 term_name = rule "term_name" $
266 term_mod_idname :: CF g (Mod Term_Name)
267 term_mod_idname = rule "term_mod_idname" $
269 <$> option [] (mod_path <* char '.')
271 term_idname :: CF g Term_Name
272 term_idname = rule "term_idname" $
273 (Term_Name . Text.pack <$>) $
277 <*. (any `but` term_idname_tail)
279 identG = (:) <$> headG <*> many (cf_of_term term_idname_tail)
280 headG = unicat $ Unicat_Letter
281 term_idname_tail :: Terminal g Char
282 term_idname_tail = rule "term_idname_tail" $
283 unicat Unicat_Letter <+>
285 term_keywords :: Reg rl g String
286 term_keywords = rule "term_keywords" $
287 choice $ string <$> ["in", "let"]
289 term_mod_opname :: CF g (Mod Term_Name)
290 term_mod_opname = rule "term_mod_opname" $
292 <$> option [] (mod_path <* char '.')
294 term_opname :: CF g Term_Name
295 term_opname = rule "term_opname" $
296 (Term_Name . Text.pack <$>) $
300 <*. (any `but` term_opname_ok)
302 symG = some $ cf_of_term $ term_opname_ok
303 term_opname_ok :: Terminal g Char
304 term_opname_ok = rule "term_opname_ok" $
311 koG = choice (char <$> ['(', ')', '`', '\'', ',', '[', ']'])
312 term_keysyms :: Reg rl g String
313 term_keysyms = rule "term_keysyms" $
314 choice $ string <$> ["\\", "->", "="]
316 deriving instance Gram_Term_Name g => Gram_Term_Name (CF g)
317 instance Gram_Term_Name EBNF
318 instance Gram_Term_Name RuleDef
320 -- * Class 'Gram_Term_Type'
333 ) => Gram_Term_Type meta g where
335 :: CF g (Term_Name, TokType meta)
336 term_abst_decl = rule "term_abst_decl" $
342 deriving instance Gram_Term_Type meta g => Gram_Term_Type meta (CF g)
343 instance Gram_Term_Type meta EBNF
344 instance Gram_Term_Type meta RuleDef
346 -- * Class 'Gram_Error'
347 class Gram_Error g where
348 term_unError :: CF g (Either Error_Term_Gram a) -> CF g a
349 deriving instance Gram_Error g => Gram_Error (CF g)
350 instance Gram_Error EBNF where
351 term_unError (CF (EBNF g)) = CF $ EBNF g
352 instance Gram_Error RuleDef where
353 term_unError (CF (RuleDef (EBNF g))) =
354 CF $ RuleDef $ EBNF $ g
356 -- ** Type 'Error_Term_Gram'
358 = Error_Term_Gram_Fixity Error_Fixity
359 | Error_Term_Gram_Cannot_apply_term
360 | Error_Term_Gram_Cannot_apply_type
361 | Error_Term_Gram_Undefined_term
362 | Error_Term_Gram_Term_incomplete
365 -- * Class 'Gram_Term'
377 , Gram_Term_AtomsR meta ts ts g
379 , Gram_Term_Type meta g
381 ) => Gram_Term ts meta g where
382 -- | Wrap 'term_abst'. Useful to modify body's scope.
384 :: CF g [(Term_Name, TokType meta)]
385 -> CF g (EToken meta ts)
386 -> CF g ([(Term_Name, TokType meta)], EToken meta ts)
387 term_abst_args_body args body = (,) <$> args <*> body
388 term_tokenizers :: CF g (Tokenizers meta ts -> a) -> CF g a
391 :: Inj_Tokens meta ts '[Proxy (->)]
392 => CF g (EToken meta ts)
393 termG = rule "term" $
400 :: Inj_Tokens meta ts '[Proxy (->)]
401 => CF g (EToken meta ts)
402 term_operators = rule "term_operators" $
405 left Error_Term_Gram_Fixity <$>
408 (term_unError $ metaG $ term_tokenizers $ op_prefix <$> term_op_prefix)
409 (term_unError $ metaG $ term_tokenizers $ op_infix <$> term_op_infix)
410 (term_unError $ metaG $ term_tokenizers $ op_postfix <$> term_op_postfix)
412 bqG :: Gram_Terminal g' => g' Char
414 op_infix name toks meta = do
415 (_pre, in_, _post) <- protok name toks
417 (term_fixity in_,) $ \ma mb -> do
420 unProTok =<< term_protok in_ meta `protok_app` [Right a, Right b]
421 op_prefix name toks meta = do
422 (pre, _in_, _post) <- protok name toks
425 Right $ (term_fixity p,) $ (=<<) $ \a ->
426 unProTok =<< term_protok p meta `protok_app` [Right a]
427 Nothing -> Left $ Error_Term_Gram_Fixity Error_Fixity_NeedPrefix
428 op_postfix name toks meta = do
429 (_pre, _in_, post) <- protok name toks
432 Right $ (term_fixity p,) $ (=<<) $ \a ->
433 unProTok =<< term_protok p meta `protok_app` [Right a]
434 Nothing -> Left $ Error_Term_Gram_Fixity Error_Fixity_NeedPostfix
435 term_op_postfix :: CF g (Mod Term_Name)
436 term_op_postfix = rule "term_op_postfix" $
438 bqG *> term_mod_idname <+> -- <* (cf_of_term $ Gram.Term (pure ' ') `but` bqG)
440 term_op_infix :: CF g (Mod Term_Name)
441 term_op_infix = rule "term_op_infix" $
443 between bqG bqG term_mod_idname <+>
445 term_op_prefix :: CF g (Mod Term_Name)
446 term_op_prefix = rule "term_op_prefix" $
448 term_mod_idname <* bqG <+>
451 :: Inj_Tokens meta ts '[Proxy (->)]
452 => CF g (EToken meta ts)
453 term_app = rule "term_app" $
455 (\a as -> unProTok =<< protok_app a as)
459 :: Inj_Tokens meta ts '[Proxy (->)]
460 => CF g (Either (EToken meta '[Proxy Token_Type])
462 term_atom = rule "term_atom" $
463 (Left <$ char '@' <*> typeG) <+>
464 (Right <$> term_unError (unProTok <$> term_atom_proto))
466 :: Inj_Tokens meta ts '[Proxy (->)]
467 => CF g (ProTok meta ts)
470 term_atomsR (Proxy @ts) <>
471 [ metaG $ ((\(_, in_, _) -> term_protok in_) <$>) $ term_unError $ term_tokenizers $
472 protok <$> term_mod_name
473 , ProTok <$> term_group
476 :: Inj_Tokens meta ts '[Proxy (->)]
477 => CF g (EToken meta ts)
478 term_group = rule "term_group" $ parens termG
480 :: Inj_Tokens meta ts '[Proxy (->)]
481 => CF g (EToken meta ts)
482 term_abst = rule "term_abst" $
485 List.foldr (\(x, ty_x) ->
487 Token_Term_Abst x ty_x) te xs) <$>) $
489 (symbol "\\" *> some term_abst_decl <* symbol "->")
492 :: Inj_Tokens meta ts '[Proxy (->)]
493 => CF g (EToken meta ts)
494 term_let = rule "term_let" $
496 (\name args bound body meta ->
500 (\(x, ty_x) -> inj_etoken meta . Token_Term_Abst x ty_x) bound args
504 <*> many term_abst_decl
511 ( Gram_Term ts meta g
512 , Gram_Term_AtomsR meta ts ts (CF g)
513 ) => Gram_Term ts meta (CF g)
515 Gram_Term_AtomsR meta ts ts EBNF =>
516 Gram_Term ts meta EBNF where
517 term_tokenizers (CF (EBNF g)) = CF $ EBNF g
519 Gram_Term_AtomsR meta ts ts RuleDef =>
520 Gram_Term ts meta RuleDef where
521 term_tokenizers (CF (RuleDef (EBNF g))) =
522 CF $ RuleDef $ EBNF $ g
524 -- ** Class 'Gram_Term_AtomsR'
525 class Gram_Term_AtomsR meta (ts::[*]) (rs::[*]) g where
526 term_atomsR :: Proxy rs -> [CF g (ProTok meta ts)]
527 instance Gram_Term_AtomsR meta ts '[] g where
530 ( Gram_Term_AtomsT meta ts t g
531 , Gram_Term_AtomsR meta ts rs g
532 ) => Gram_Term_AtomsR meta ts (t ': rs) g where
534 term_atomsT (Proxy @t) <>
535 term_atomsR (Proxy @rs)
537 -- ** Class 'Gram_Term_AtomsT'
538 class Gram_Term_AtomsT meta ts t g where
539 term_atomsT :: Proxy t -> [CF g (ProTok meta ts)]
544 ( Gram_Term '[Proxy (->), Proxy Integer] () g
553 , void (term_abst_decl::CF g (Term_Name, TokType ()))
558 , void $ cf_of_term term_idname_tail
559 , void $ cf_of_reg term_keywords
560 , void term_mod_opname
562 , void $ cf_of_term term_opname_ok
563 , void $ cf_of_reg term_keysyms
565 ue :: CF g (EToken () '[Proxy (->), Proxy Integer]) -> CF g ()
567 -- uf :: CF g (ProTok () '[Proxy (->)]) -> CF g ()
569 ug :: CF g (Either (EToken () '[Proxy Token_Type])
570 (EToken () '[Proxy (->), Proxy Integer])) -> CF g ()