1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-03 09:41:10 +03:00
juvix/test/Isabelle/Positive.hs

56 lines
1.5 KiB
Haskell
Raw Normal View History

Translate function bodies to Isabelle/HOL (#2868) * Closes #2813 Implements a translation from Juvix functions to Isabelle/HOL functions. This extends the previous Juvix -> Isabelle translation which could handle only type signatures. Checklist --------- - [x] Basic translation - [x] Polymorphism - [x] Arithmetic operators - [x] Numeric literals - [x] List literals - [x] Comparison operators - [x] Boolean operators - [x] `if` translated to Isabelle `if` - [x] `true` and `false` translated to Isabelle `True` and `False` - [x] `zero` and `suc` translated to Isabelle `0` and `Suc` - [x] `Maybe` translated to Isabelle `option` - [x] Pairs translated to Isabelle tuples - [x] Quote Isabelle identifier names (e.g. cannot begin with `_`) - [x] Rename variables to avoid clashes (in Isabelle/HOL pattern variables don't shadow function identifiers) - [x] Common stdlib functions (`map`, `filter`, etc) translated to corresponding Isabelle functions - [x] Multiple assignments in a single `let` - [x] CLI - [x] Test - The test is very fragile, similar to the markdown test. It just compares the result of translation to Isabelle against a predefined expected output file. Limitations ----------- The translation is not designed to be completely correct under all circumstances. There are aspects of the Juvix language that cannot be straightforwardly translated to Isabelle/HOL, and these are not planned to ever be properly handled. There are other aspects that are difficult but not impossible to translate, and these are left for future work. Occasionally, the generated code may need manual adjustments to type-check in Isabelle/HOL. In particular: * Higher-rank polymorphism or functions on types cannot be translated as these features are not supported by Isabelle/HOL. Juvix programs using these features will not be correctly translated (the generated output may need manual adjustment). * In cases where Juvix termination checking diverges from Isabelle/HOL termination checking, providing a termination proof manually may be necessary. Non-terminating Juvix functions cannot be automatically translated and need to be manually modelled in Isabelle/HOL in a different way (e.g. as relations). * Comments (including judoc) are ignored. This is left for future work. * Traits are not translated to Isabelle/HOL type classes / locales. This is left for future work. * Mutually recursive functions are not correctly translated. This is left for future work. * Record creation, update, field access and pattern matching are not correctly translated. This is left for future work. * Named patterns are not correctly translated. This is left for future work. * Side conditions in patterns are not supported. This is left for future work. * If a Juvix function in the translated module has the same name as some function from the Isabelle/HOL standard library, there will be a name clash in the generated code. --------- Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-07-19 10:40:07 +03:00
module Isabelle.Positive where
import Base
import Juvix.Compiler.Backend.Isabelle.Data.Result
import Juvix.Compiler.Backend.Isabelle.Pretty
data PosTest = PosTest
{ _name :: String,
_dir :: Path Abs Dir,
_file :: Path Abs File,
_expectedFile :: Path Abs File
}
makeLenses ''PosTest
root :: Path Abs Dir
root = relToProject $(mkRelDir "tests/positive/Isabelle")
posTest :: String -> Path Rel Dir -> Path Rel File -> Path Rel File -> PosTest
posTest _name rdir rfile efile =
let _dir = root <//> rdir
_file = _dir <//> rfile
_expectedFile = _dir <//> efile
in PosTest {..}
testDescr :: PosTest -> TestDescr
testDescr PosTest {..} =
TestDescr
{ _testName = _name,
_testRoot = _dir,
_testAssertion = Steps $ \step -> do
entryPoint <- testDefaultEntryPointIO _dir _file
step "Translate"
PipelineResult {..} <- snd <$> testRunIO entryPoint upToIsabelle
let thy = _pipelineResult ^. resultTheory
comments = _pipelineResult ^. resultComments
Translate function bodies to Isabelle/HOL (#2868) * Closes #2813 Implements a translation from Juvix functions to Isabelle/HOL functions. This extends the previous Juvix -> Isabelle translation which could handle only type signatures. Checklist --------- - [x] Basic translation - [x] Polymorphism - [x] Arithmetic operators - [x] Numeric literals - [x] List literals - [x] Comparison operators - [x] Boolean operators - [x] `if` translated to Isabelle `if` - [x] `true` and `false` translated to Isabelle `True` and `False` - [x] `zero` and `suc` translated to Isabelle `0` and `Suc` - [x] `Maybe` translated to Isabelle `option` - [x] Pairs translated to Isabelle tuples - [x] Quote Isabelle identifier names (e.g. cannot begin with `_`) - [x] Rename variables to avoid clashes (in Isabelle/HOL pattern variables don't shadow function identifiers) - [x] Common stdlib functions (`map`, `filter`, etc) translated to corresponding Isabelle functions - [x] Multiple assignments in a single `let` - [x] CLI - [x] Test - The test is very fragile, similar to the markdown test. It just compares the result of translation to Isabelle against a predefined expected output file. Limitations ----------- The translation is not designed to be completely correct under all circumstances. There are aspects of the Juvix language that cannot be straightforwardly translated to Isabelle/HOL, and these are not planned to ever be properly handled. There are other aspects that are difficult but not impossible to translate, and these are left for future work. Occasionally, the generated code may need manual adjustments to type-check in Isabelle/HOL. In particular: * Higher-rank polymorphism or functions on types cannot be translated as these features are not supported by Isabelle/HOL. Juvix programs using these features will not be correctly translated (the generated output may need manual adjustment). * In cases where Juvix termination checking diverges from Isabelle/HOL termination checking, providing a termination proof manually may be necessary. Non-terminating Juvix functions cannot be automatically translated and need to be manually modelled in Isabelle/HOL in a different way (e.g. as relations). * Comments (including judoc) are ignored. This is left for future work. * Traits are not translated to Isabelle/HOL type classes / locales. This is left for future work. * Mutually recursive functions are not correctly translated. This is left for future work. * Record creation, update, field access and pattern matching are not correctly translated. This is left for future work. * Named patterns are not correctly translated. This is left for future work. * Side conditions in patterns are not supported. This is left for future work. * If a Juvix function in the translated module has the same name as some function from the Isabelle/HOL standard library, there will be a name clash in the generated code. --------- Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-07-19 10:40:07 +03:00
step "Checking against expected output file"
expFile :: Text <- readFile _expectedFile
assertEqDiffText "Compare to expected output" (ppPrint comments thy <> "\n") expFile
Translate function bodies to Isabelle/HOL (#2868) * Closes #2813 Implements a translation from Juvix functions to Isabelle/HOL functions. This extends the previous Juvix -> Isabelle translation which could handle only type signatures. Checklist --------- - [x] Basic translation - [x] Polymorphism - [x] Arithmetic operators - [x] Numeric literals - [x] List literals - [x] Comparison operators - [x] Boolean operators - [x] `if` translated to Isabelle `if` - [x] `true` and `false` translated to Isabelle `True` and `False` - [x] `zero` and `suc` translated to Isabelle `0` and `Suc` - [x] `Maybe` translated to Isabelle `option` - [x] Pairs translated to Isabelle tuples - [x] Quote Isabelle identifier names (e.g. cannot begin with `_`) - [x] Rename variables to avoid clashes (in Isabelle/HOL pattern variables don't shadow function identifiers) - [x] Common stdlib functions (`map`, `filter`, etc) translated to corresponding Isabelle functions - [x] Multiple assignments in a single `let` - [x] CLI - [x] Test - The test is very fragile, similar to the markdown test. It just compares the result of translation to Isabelle against a predefined expected output file. Limitations ----------- The translation is not designed to be completely correct under all circumstances. There are aspects of the Juvix language that cannot be straightforwardly translated to Isabelle/HOL, and these are not planned to ever be properly handled. There are other aspects that are difficult but not impossible to translate, and these are left for future work. Occasionally, the generated code may need manual adjustments to type-check in Isabelle/HOL. In particular: * Higher-rank polymorphism or functions on types cannot be translated as these features are not supported by Isabelle/HOL. Juvix programs using these features will not be correctly translated (the generated output may need manual adjustment). * In cases where Juvix termination checking diverges from Isabelle/HOL termination checking, providing a termination proof manually may be necessary. Non-terminating Juvix functions cannot be automatically translated and need to be manually modelled in Isabelle/HOL in a different way (e.g. as relations). * Comments (including judoc) are ignored. This is left for future work. * Traits are not translated to Isabelle/HOL type classes / locales. This is left for future work. * Mutually recursive functions are not correctly translated. This is left for future work. * Record creation, update, field access and pattern matching are not correctly translated. This is left for future work. * Named patterns are not correctly translated. This is left for future work. * Side conditions in patterns are not supported. This is left for future work. * If a Juvix function in the translated module has the same name as some function from the Isabelle/HOL standard library, there will be a name clash in the generated code. --------- Co-authored-by: Paul Cadman <git@paulcadman.dev>
2024-07-19 10:40:07 +03:00
}
allTests :: TestTree
allTests =
testGroup
"Isabelle positive tests"
(map (mkTest . testDescr) tests)
tests :: [PosTest]
tests =
[ posTest
"Test Isabelle translation"
$(mkRelDir ".")
$(mkRelFile "Program.juvix")
$(mkRelFile "isabelle/Program.thy")
]