1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
5 -- import qualified Control.Monad.Classes as MC
6 import Control.Monad (Monad(..), forM_)
7 import Data.Char (Char)
8 import Data.Either (Either(..))
9 import Data.Either (either)
11 import Data.Function (($), (.), id)
12 import Data.Maybe (Maybe(..))
13 import Data.Semigroup ((<>))
14 import Data.Text (Text)
15 import System.IO (IO, putStrLn)
16 import Text.Show (Show(..))
17 import qualified Control.Monad.Trans.State.Strict as SS
18 import qualified Data.Text as Text
19 import qualified Data.Text.IO as Text
20 import qualified System.Environment as Env
21 import qualified Text.Megaparsec as P
23 import Language.Symantic.Grammar
24 import Language.Symantic as Sym
25 import qualified Language.Symantic.Lib as Sym
27 -- import qualified Hcompta.LCC as LCC
28 import qualified Hcompta.LCC.Sym as LCC.Sym
29 import Hcompta.LCC.Megaparsec ()
31 import Control.Applicative (Applicative(..))
32 import Data.Functor (Functor(..))
33 import Data.Functor.Identity (Identity(..))
34 import Data.Proxy (Proxy(..))
35 import qualified Control.Monad.Classes.Run as MC
36 import Prelude (error)
44 forM_ args $ \arg -> do
45 ast <- printError $ parseTe @SS @SRC (Text.pack arg)
46 te <- printError $ readTe ast
49 printError :: Show err => Either err a -> IO a
50 printError (Left err) = error $ show err
51 printError (Right a) = return a
56 Gram_Term src ss (P.ParsecT P.Dec Text (SS.StateT (Sym.Imports, Modules src ss) Identity)) =>
58 Either (P.ParseError Char P.Dec) (AST_Term src ss)
60 let mods::Modules src ss =
61 either (error . show) id $
62 Sym.deleteDefTermInfix ([] `Mod` "$") `fmap`
64 let imps = importModulesAs [] mods in
67 MC.evalStateStrict (imps, mods) $
70 g = unCF $ g_term <* eoi
74 ( Gram_Term src ss (P.ParsecT P.Dec Text (SS.StateT (Sym.Imports, Modules src ss) Identity))
77 , Syms ss (BetaT View)
81 , Inj_Source (TypeVT src) src
82 , Inj_Source (TypeT src '[]) src
83 , Inj_Source (KindK src) src
84 , Inj_Source (AST_Type src) src
88 Either (Error_Term src) (TermVT src ss '[])
90 let tys = inj_Name2Type (Proxy @ss) in
91 Sym.readTerm tys CtxTyZ ast
97 Syms ss (BetaT View) =>
100 runTe (TermVT (Term q t (TeSym te))) = do
101 putStrLn $ "Type = " <> show (q #> t)
102 case proveConstraint q of
103 Nothing -> putStrLn $ "Cannot prove Constraint: " <> show q
105 Text.putStrLn $ "View = " <> view (betaT $ te CtxTeZ)
106 case proveConstraint $ Sym.tyShow t of
107 Nothing -> putStrLn $ "No Show instance for type: " <> show t
108 Just Dict -> putStrLn $ "Eval = " <> show (eval $ te CtxTeZ)
110 -- dbg :: Show a => String -> a -> a
111 -- dbg msg x = trace (msg ++ " = " ++ show x) x
114 parse :: Text -> IO (Sym.EToken LCC.Meta SS)
117 let mods = Sym.inj_Modules @SS
120 { Sym.tokenizers_infix =
121 ins [] (Sym.tokenize2 "&" (Sym.infixR 0) Sym.Token_Term_App) $
123 Sym.tokenizers_infix mods
129 P.runParserT (Gram.unCF $ Sym.g_term <* Gram.eoi) "" inp
131 Left (err::P.ParseError (P.Token Text) P.Dec) -> error $ P.parseErrorPretty err
132 Right to -> return to
134 del m n = Map.adjust (Map.delete n) m
135 ins m (n, a) = Map.insertWith (<>) m (Map.singleton n a)
137 compile :: Sym.EToken LCC.Meta SS -> IO (Sym.TermVT SRC SS)
139 case Sym.compileWithoutCtx to of
140 Left err -> error $ show err
141 Right te -> return te
144 type instance MC.CanDo (S.StateT (Sym.Tokenizers LCC.Meta is) m)
145 (MC.EffState (Sym.Tokenizers LCC.Meta is)) = 'True
146 instance Monad m => MC.MonadStateN 'MC.Zero (Sym.Tokenizers LCC.Meta is) (S.StateT (Sym.Tokenizers LCC.Meta is) m) where
147 stateN _px = S.StateT . g.state