-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Juvix to Isabelle/HOL translation (#2752)
* Closes #2689 * Adds the command `juvix isabelle program.juvix` which translates a given file to an Isabelle/HOL theory. * Currently, only a single module is translated. * By default translates types and function signatures. Translating function signatures can be disabled with the `--only-types` option. Blocked by: - #2763 --------- Co-authored-by: Jonathan Cubides <[email protected]>
- Loading branch information
1 parent
44cdd84
commit ce938ef
Showing
20 changed files
with
688 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Commands.Isabelle where | ||
|
||
import Commands.Base | ||
import Commands.Isabelle.Options | ||
import Juvix.Compiler.Backend.Isabelle.Data.Result | ||
import Juvix.Compiler.Backend.Isabelle.Language | ||
import Juvix.Compiler.Backend.Isabelle.Pretty | ||
|
||
runCommand :: | ||
(Members '[EmbedIO, TaggedLock, App] r) => | ||
IsabelleOptions -> | ||
Sem r () | ||
runCommand opts = do | ||
let inputFile = opts ^. isabelleInputFile | ||
res <- runPipeline opts inputFile upToIsabelle | ||
let thy = res ^. resultTheory | ||
outputDir <- fromAppPathDir (opts ^. isabelleOutputDir) | ||
if | ||
| opts ^. isabelleStdout -> do | ||
renderStdOut (ppOutDefault thy) | ||
putStrLn "" | ||
| otherwise -> do | ||
ensureDir outputDir | ||
let file :: Path Rel File | ||
file = | ||
relFile | ||
( unpack (thy ^. theoryName . namePretty) | ||
<.> isabelleFileExt | ||
) | ||
absPath :: Path Abs File | ||
absPath = outputDir <//> file | ||
writeFileEnsureLn absPath (ppPrint thy <> "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Commands.Isabelle.Options where | ||
|
||
import CommonOptions | ||
import Juvix.Compiler.Pipeline.EntryPoint | ||
|
||
data IsabelleOptions = IsabelleOptions | ||
{ _isabelleInputFile :: Maybe (AppPath File), | ||
_isabelleOutputDir :: AppPath Dir, | ||
_isabelleStdout :: Bool, | ||
_isabelleOnlyTypes :: Bool | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''IsabelleOptions | ||
|
||
parseIsabelle :: Parser IsabelleOptions | ||
parseIsabelle = do | ||
_isabelleOutputDir <- | ||
parseGenericOutputDir | ||
( value "isabelle" | ||
<> showDefault | ||
<> help "Isabelle/HOL output directory." | ||
<> action "directory" | ||
) | ||
_isabelleStdout <- | ||
switch | ||
( long "stdout" | ||
<> help "Write the output to stdout instead of a file." | ||
) | ||
_isabelleOnlyTypes <- | ||
switch | ||
( long "only-types" | ||
<> help "Translate types only. Omit function signatures." | ||
) | ||
_isabelleInputFile <- optional (parseInputFile FileExtJuvix) | ||
pure IsabelleOptions {..} | ||
|
||
instance EntryPointOptions IsabelleOptions where | ||
applyOptions IsabelleOptions {..} e = e {_entryPointIsabelleOnlyTypes = _isabelleOnlyTypes} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Juvix.Compiler.Backend.Isabelle.Data.Result where | ||
|
||
import Juvix.Compiler.Backend.Isabelle.Language | ||
|
||
data Result = Result | ||
{ _resultTheory :: Theory, | ||
_resultModuleId :: ModuleId | ||
} | ||
|
||
makeLenses ''Result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
module Juvix.Compiler.Backend.Isabelle.Language | ||
( module Juvix.Compiler.Backend.Isabelle.Language, | ||
module Juvix.Compiler.Internal.Data.Name, | ||
module Juvix.Prelude, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Internal.Data.Name | ||
import Juvix.Prelude | ||
|
||
data Type | ||
= TyVar Var | ||
| TyFun FunType | ||
| TyInd IndApp | ||
deriving stock (Eq) | ||
|
||
data Var = Var | ||
{ _varName :: Name | ||
} | ||
deriving stock (Eq) | ||
|
||
data FunType = FunType | ||
{ _funTypeLeft :: Type, | ||
_funTypeRight :: Type | ||
} | ||
deriving stock (Eq) | ||
|
||
data Inductive | ||
= IndBool | ||
| IndNat | ||
| IndInt | ||
| IndList | ||
| IndString | ||
| IndUser Name | ||
deriving stock (Eq) | ||
|
||
data IndApp = IndApp | ||
{ _indAppInductive :: Inductive, | ||
_indAppParams :: [Type] | ||
} | ||
deriving stock (Eq) | ||
|
||
makeLenses ''Var | ||
makeLenses ''FunType | ||
makeLenses ''IndApp | ||
|
||
data Statement | ||
= StmtDefinition Definition | ||
| StmtFunction Function | ||
| StmtSynonym Synonym | ||
| StmtDatatype Datatype | ||
| StmtRecord Record | ||
|
||
data Definition = Definition | ||
{ _definitionName :: Name, | ||
_definitionType :: Type | ||
} | ||
|
||
data Function = Function | ||
{ _functionName :: Name, | ||
_functionType :: Type | ||
} | ||
|
||
data Synonym = Synonym | ||
{ _synonymName :: Name, | ||
_synonymType :: Type | ||
} | ||
|
||
data Datatype = Datatype | ||
{ _datatypeName :: Name, | ||
_datatypeParams :: [Var], | ||
_datatypeConstructors :: [Constructor] | ||
} | ||
|
||
data Constructor = Constructor | ||
{ _constructorName :: Name, | ||
_constructorArgTypes :: [Type] | ||
} | ||
|
||
data Record = Record | ||
{ _recordName :: Name, | ||
_recordParams :: [Var], | ||
_recordFields :: [RecordField] | ||
} | ||
|
||
data RecordField = RecordField | ||
{ _recordFieldName :: Name, | ||
_recordFieldType :: Type | ||
} | ||
|
||
makeLenses ''Definition | ||
makeLenses ''Function | ||
makeLenses ''Synonym | ||
makeLenses ''Datatype | ||
makeLenses ''Constructor | ||
makeLenses ''Record | ||
makeLenses ''RecordField | ||
|
||
data Theory = Theory | ||
{ _theoryName :: Name, | ||
_theoryImports :: [Name], | ||
_theoryStatements :: [Statement] | ||
} | ||
|
||
makeLenses ''Theory | ||
|
||
instance HasAtomicity Var where | ||
atomicity _ = Atom | ||
|
||
instance HasAtomicity Type where | ||
atomicity = \case | ||
TyVar {} -> Atom | ||
TyFun {} -> Aggregate funFixity | ||
TyInd IndApp {..} | ||
| null _indAppParams -> Atom | ||
| otherwise -> Aggregate appFixity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Juvix.Compiler.Backend.Isabelle.Pretty where | ||
|
||
import Juvix.Compiler.Backend.Isabelle.Pretty.Base | ||
import Juvix.Compiler.Backend.Isabelle.Pretty.Options | ||
import Juvix.Data.PPOutput | ||
import Juvix.Prelude | ||
import Prettyprinter.Render.Terminal qualified as Ansi | ||
|
||
ppOutDefault :: (PrettyCode c) => c -> AnsiText | ||
ppOutDefault = mkAnsiText . PPOutput . doc defaultOptions | ||
|
||
ppOut :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> AnsiText | ||
ppOut o = mkAnsiText . PPOutput . doc (project o) | ||
|
||
ppTrace' :: (CanonicalProjection a Options, PrettyCode c) => a -> c -> Text | ||
ppTrace' opts = Ansi.renderStrict . reAnnotateS stylize . layoutPretty defaultLayoutOptions . doc (project opts) | ||
|
||
ppTrace :: (PrettyCode c) => c -> Text | ||
ppTrace = ppTrace' traceOptions | ||
|
||
ppPrint :: (PrettyCode c) => c -> Text | ||
ppPrint = show . ppOutDefault |
Oops, something went wrong.