quox/tests/Tests/FromPTerm.idr

108 lines
3.2 KiB
Idris
Raw Normal View History

2023-03-10 15:52:29 -05:00
module Tests.FromPTerm
2023-03-31 17:43:25 -04:00
import Quox.Parser.FromParser
2023-03-10 15:52:29 -05:00
import Quox.Parser
2023-03-13 14:33:09 -04:00
import TypingImpls
import Tests.Parser as TParser
2023-03-31 17:43:25 -04:00
import Quox.EffExtra
2023-05-01 21:06:25 -04:00
import TAP
import AstExtra
2023-05-14 13:58:46 -04:00
import PrettyExtra
2023-03-10 15:52:29 -05:00
2023-03-13 14:33:09 -04:00
import System.File
2023-03-10 15:52:29 -05:00
import Derive.Prelude
%language ElabReflection
%hide TParser.Failure
%hide TParser.ExpectedFail
2024-05-28 12:39:21 -04:00
PError = Parser.Error
FPError = FromParser.Error
2023-03-10 15:52:29 -05:00
public export
data Failure =
2024-05-28 12:39:21 -04:00
ParseError PError
| FromParser FPError
| WrongResult String
2023-03-10 15:52:29 -05:00
| ExpectedFail String
2023-03-13 14:33:09 -04:00
%runElab derive "FileError" [Show]
2023-03-10 15:52:29 -05:00
export
ToInfo Failure where
toInfo (ParseError err) = toInfo err
2023-03-13 14:33:09 -04:00
toInfo (FromParser err) =
[("type", "FromParserError"),
2023-05-14 13:58:46 -04:00
("got", prettyStr $ prettyError True err)]
2023-03-10 15:52:29 -05:00
toInfo (WrongResult got) =
[("type", "WrongResult"), ("got", got)]
toInfo (ExpectedFail got) =
[("type", "ExpectedFail"), ("got", got)]
parameters {c : Bool} {auto _ : Show b}
(grm : FileName -> Grammar c a)
2024-05-28 12:39:21 -04:00
(fromP : a -> Either FPError b)
2023-03-10 15:52:29 -05:00
(inp : String)
2024-05-28 12:39:21 -04:00
parsesWith : String -> (b -> Bool) -> Test
parsesWith label p = test label $ do
pres <- mapFst ParseError $ lexParseWith (grm "test") inp
res <- mapFst FromParser $ fromP pres
unless (p res) $ Left $ WrongResult $ show res
2023-05-01 21:06:25 -04:00
2024-05-28 12:39:21 -04:00
%macro
parseMatch : {default (ltrim inp) label : String} -> TTImp -> Elab Test
parseMatch {label} pat =
parsesWith label <$> check `(\case ~(pat) => True; _ => False)
2023-03-10 15:52:29 -05:00
2024-05-28 12:39:21 -04:00
parseFails : {default "\{ltrim inp} # fails" label : String} -> Test
parseFails {label} = test label $ do
pres <- mapFst ParseError $ lexParseWith (grm "test") inp
either (const $ Right ()) (Left . ExpectedFail . show) $ fromP pres
2023-03-10 15:52:29 -05:00
2024-05-28 12:39:21 -04:00
runFromParser : Definitions -> Eff FromParserPure a -> Either FPError a
runFromParser defs = map val . fromParserPure [<] 0 defs initStack
2023-03-31 17:43:25 -04:00
2023-03-10 15:52:29 -05:00
export
tests : Test
tests = "PTerm → Term" :- [
"dimensions" :-
2024-05-28 12:39:21 -04:00
let fromPDim = runFromParser empty . fromPDimWith [< "𝑖", "𝑗"]
2023-03-10 15:52:29 -05:00
in [
note "dim ctx: [𝑖, 𝑗]",
2023-05-01 21:06:25 -04:00
parseMatch dim fromPDim "𝑖" `(B (VS VZ) _),
parseMatch dim fromPDim "𝑗" `(B VZ _),
2023-03-10 15:52:29 -05:00
parseFails dim fromPDim "𝑘",
2023-05-01 21:06:25 -04:00
parseMatch dim fromPDim "0" `(K Zero _),
parseMatch dim fromPDim "1" `(K One _)
2023-03-10 15:52:29 -05:00
],
"terms" :-
2023-11-02 13:14:22 -04:00
let defs = fromList [("f", mkDef GAny (^NAT) (^Zero))]
-- doesn't have to be well typed yet, just well scoped
2024-05-28 12:39:21 -04:00
fromPTerm = runFromParser defs .
2023-03-31 17:43:25 -04:00
fromPTermWith [< "𝑖", "𝑗"] [< "A", "x", "y", "z"]
2023-03-10 15:52:29 -05:00
in [
note "dim ctx: [𝑖, 𝑗]; term ctx: [A, x, y, z]",
2023-05-01 21:06:25 -04:00
parseMatch term fromPTerm "x" `(E $ B (VS $ VS VZ) _),
2023-03-31 17:43:25 -04:00
parseFails term fromPTerm "𝑖",
2023-05-21 14:09:34 -04:00
parseMatch term fromPTerm "f" `(E $ F "f" {}),
2023-05-01 21:06:25 -04:00
parseMatch term fromPTerm "λ w ⇒ w"
`(Lam (S _ $ Y $ E $ B VZ _) _),
parseMatch term fromPTerm "λ w ⇒ x"
2024-05-28 11:00:01 -04:00
`(Lam (S _ $ Y $ E $ B (VS $ VS $ VS VZ) _) _),
2023-05-01 21:06:25 -04:00
parseMatch term fromPTerm "λ x ⇒ x"
`(Lam (S _ $ Y $ E $ B VZ _) _),
parseMatch term fromPTerm "λ a b ⇒ f a b"
`(Lam (S _ $ Y $
Lam (S _ $ Y $
2023-05-21 14:09:34 -04:00
E $ App (App (F "f" {}) (E $ B (VS VZ) _) _) (E $ B VZ _) _) _) _),
2023-05-01 21:06:25 -04:00
parseMatch term fromPTerm "f @𝑖" $
2023-05-21 14:09:34 -04:00
`(E $ DApp (F "f" {}) (B (VS VZ) _) _),
parseFails term fromPTerm "λ x ⇒ x¹"
],
todo "everything else"
2023-03-10 15:52:29 -05:00
]