1 {-# LANGUAGE UndecidableInstances #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3 {-# OPTIONS_GHC -fno-warn-orphans #-}
6 -- import qualified Control.Monad.Classes as MC
7 import Control.Monad (Monad(..), forM_)
8 import Data.Bool (Bool(..))
9 import Data.Char (Char)
10 import Data.Either (Either(..))
11 import Data.Either (either)
13 import Data.Function (($), (.), id)
14 import Data.Functor ((<$>))
15 import Data.Maybe (Maybe(..), maybe)
16 import qualified Data.List as L
17 import Data.Semigroup ((<>))
18 import Data.Text (Text)
19 import System.IO (IO, putStrLn)
20 import Text.Show (Show(..))
21 import qualified System.IO as IO
22 import qualified Data.Map.Strict as Map
23 import qualified Control.Monad.Trans.State.Strict as SS
24 import qualified Data.Text as Text
25 import qualified Data.Text.IO as Text
26 import qualified System.Environment as Env
27 import qualified Text.Megaparsec as P
28 import qualified Data.Text as T
29 import qualified Data.Char as Char
31 import qualified Language.Symantic.Grammar as G
32 import Language.Symantic as Sym
33 import qualified Language.Symantic.Lib as Sym
34 import qualified Language.Symantic.Document as D
36 -- import qualified Hcompta.LCC as LCC
37 import qualified Hcompta.LCC.Sym as LCC.Sym
38 import Hcompta.LCC.Megaparsec ()
40 import Control.Applicative (Applicative(..))
41 import Data.Functor (Functor(..))
42 import Data.Functor.Identity (Identity(..))
43 import qualified Control.Monad.Classes.Run as MC
44 import Prelude (error)
52 let mods::Modules SRC SS =
53 either (error . show) id $
54 Sym.deleteDefTermInfix ([] `Mod` "$") `fmap`
56 (`D.ansiIO` IO.stderr) $ docModules mods
57 forM_ args $ \arg -> do
58 ast <- printError $ parseTe mods (Text.pack arg)
59 te <- printError $ readTe ast
62 printError :: Show err => Either err a -> IO a
63 printError (Left err) = error $ show err
64 printError (Right a) = return a
72 Sym.Modules src ss -> d
73 docModules (Sym.Modules mods) =
75 (\p m doc -> docModule p m <> doc)
86 Sym.PathMod -> Sym.Module src ss -> d
87 docModule m Sym.Module
92 go docFixityInfix module_infix <>
93 go docFixityPrefix module_prefix <>
94 go docFixityPostfix module_postfix
96 go :: (fixy -> d) -> ModuleFixy src ss fixy -> d
104 docFixy token_fixity <>
105 D.space <> D.bold (D.yellower "::") <> D.space <>
106 docTokenTerm (t Sym.noSource) <>
116 Sym.Token_Term src ss -> d
118 let n2t = inj_Name2Type @ss in
119 case Sym.readTerm n2t CtxTyZ (G.BinTree0 t) of
120 Right (Sym.TermVT te) ->
121 Sym.docType Sym.config_doc_type
122 { config_Doc_Type_vars_numbering = False
123 } 0 $ Sym.typeOfTerm te
125 docFixityInfix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Infix -> t
126 docFixityInfix = \case
127 Sym.Infix Nothing 5 -> D.empty
132 Sym.AssocB Sym.SideL -> "l"
133 Sym.AssocB Sym.SideR -> "r" in
134 D.magenta $ " infix" <> maybe D.empty docAssoc a <>
135 D.space <> D.bold (D.bluer (D.int p))
136 docFixityPrefix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Unifix -> t
137 docFixityPrefix (Sym.Prefix p) = D.magenta $ " prefix " <> D.bold (D.bluer (D.int p))
138 docFixityPostfix :: (D.Doc_Decoration t, D.Doc_Color t, D.Doc_Text t) => Unifix -> t
139 docFixityPostfix (Sym.Postfix p) = D.magenta $ " postfix " <> D.bold (D.bluer (D.int p))
141 docPathMod :: D.Doc_Text d => PathMod -> d
142 docPathMod (p::Sym.PathMod) =
144 L.intersperse (D.charH '.') $
145 (\(Sym.NameMod n) -> D.textH n) <$> p
150 Sym.PathMod -> Sym.NameTe -> d
151 docPathTe (ms::Sym.PathMod) (Sym.NameTe n) =
153 L.intersperse (D.charH '.') $
154 ((\(Sym.NameMod m) -> D.textH m) <$> ms) <>
155 [(if isOp n then id else D.yellower) $ D.text n]
157 isOp = T.all $ \case '_' -> True; '\'' -> True; c -> Char.isAlphaNum c
162 Gram_Term src ss (P.ParsecT P.Dec Text (SS.StateT (Sym.Imports, Modules src ss) Identity)) =>
163 Sym.Modules src ss ->
165 Either (P.ParseError Char P.Dec) (AST_Term src ss)
167 let imps = importQualifiedAs [] mods in
170 MC.evalStateStrict (imps, mods) $
171 P.runParserT g "" inp
173 g = G.unCF $ Sym.g_term <* G.eoi
176 ( Gram_Term src ss (P.ParsecT P.Dec Text (SS.StateT (Sym.Imports, Modules src ss) Identity))
179 , Inj_Source (TypeVT src) src
180 , Inj_Source (TypeT src '[]) src
181 , Inj_Source (KindK src) src
182 , Inj_Source (AST_Type src) src
187 ( Gram_Term src ss (P.ParsecT P.Dec Text (SS.StateT (Sym.Imports, Modules src ss) Identity))
190 , Syms ss (BetaT View)
194 , Inj_Source (TypeVT src) src
195 , Inj_Source (TypeT src '[]) src
196 , Inj_Source (KindK src) src
197 , Inj_Source (AST_Type src) src
201 Either (Error_Term src) (TermVT src ss '[])
203 let n2t = inj_Name2Type @ss in
204 Sym.readTerm n2t CtxTyZ ast
210 Syms ss (BetaT View) =>
213 evalTe (TermVT (Term q t (TeSym te))) = do
214 putStrLn $ "Type = " <> show (q #> t)
215 case proveConstraint q of
216 Nothing -> putStrLn $ "Cannot prove Constraint: " <> show q
218 Text.putStrLn $ "View = " <> view (betaT $ te CtxTeZ)
219 case proveConstraint $ Sym.tyShow t of
220 Nothing -> putStrLn $ "No Show instance for type: " <> show t
221 Just Dict -> putStrLn $ "Eval = " <> show (eval $ te CtxTeZ)
223 -- dbg :: Show a => String -> a -> a
224 -- dbg msg x = trace (msg ++ " = " ++ show x) x
227 parse :: Text -> IO (Sym.EToken LCC.Meta SS)
230 let mods = Sym.inj_Modules @SS
233 { Sym.tokenizers_infix =
234 ins [] (Sym.tokenize2 "&" (Sym.infixR 0) Sym.Token_Term_App) $
236 Sym.tokenizers_infix mods
242 P.runParserT (Gram.unCF $ Sym.g_term <* Gram.eoi) "" inp
244 Left (err::P.ParseError (P.Token Text) P.Dec) -> error $ P.parseErrorPretty err
245 Right to -> return to
247 del m n = Map.adjust (Map.delete n) m
248 ins m (n, a) = Map.insertWith (<>) m (Map.singleton n a)
250 compile :: Sym.EToken LCC.Meta SS -> IO (Sym.TermVT SRC SS)
252 case Sym.compileWithoutCtx to of
253 Left err -> error $ show err
254 Right te -> return te
257 type instance MC.CanDo (S.StateT (Sym.Tokenizers LCC.Meta is) m)
258 (MC.EffState (Sym.Tokenizers LCC.Meta is)) = 'True
259 instance Monad m => MC.MonadStateN 'MC.Zero (Sym.Tokenizers LCC.Meta is) (S.StateT (Sym.Tokenizers LCC.Meta is) m) where
260 stateN _px = S.StateT . g.state