-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into _update-deps/runtimeverification/haskell-…
…backend
- Loading branch information
Showing
20 changed files
with
845 additions
and
117 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 @@ | ||
from .generate import GenContext, generate |
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,109 @@ | ||
from __future__ import annotations | ||
|
||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from pyk.cli.utils import dir_path | ||
from pyk.kore.internal import KoreDefn | ||
|
||
if TYPE_CHECKING: | ||
from argparse import Namespace | ||
from collections.abc import Iterable | ||
|
||
|
||
def main() -> None: | ||
import logging | ||
import sys | ||
|
||
from pyk.cli.utils import LOG_FORMAT | ||
|
||
args = sys.argv | ||
level, args = _extract_log_level(args) | ||
logging.basicConfig(level=level, format=LOG_FORMAT) | ||
|
||
klean(args) | ||
|
||
|
||
def _extract_log_level(args: list[str]) -> tuple[int, list[str]]: | ||
from pyk.cli.args import KCLIArgs | ||
from pyk.cli.utils import loglevel | ||
|
||
parser = KCLIArgs().logging_args | ||
ns, rest = parser.parse_known_args(args) | ||
level = loglevel(ns) | ||
return level, rest | ||
|
||
|
||
def klean(args: Iterable[str]) -> None: | ||
from .generate import generate | ||
|
||
ns = _parse_args(args) | ||
defn = _load_defn(ns.definition_dir) | ||
if ns.rules: | ||
defn = defn.filter_rewrites(ns.rules) | ||
defn = defn.project_to_rewrites() | ||
generate( | ||
defn=defn, | ||
output_dir=ns.output_dir, | ||
context={ | ||
'package_name': ns.package_name, | ||
'library_name': ns.library_name, | ||
}, | ||
) | ||
|
||
|
||
def _parse_args(args: Iterable[str]) -> Namespace: | ||
parser = _parser() | ||
args = list(args)[1:] | ||
ns = parser.parse_args(args) | ||
|
||
if ns.library_name is None: | ||
ns.library_name = ''.join(word.capitalize() for word in ns.package_name.split('-')) | ||
|
||
if ns.output_dir is None: | ||
ns.output_dir = Path() | ||
|
||
return ns | ||
|
||
|
||
def _parser() -> ArgumentParser: | ||
parser = ArgumentParser(description='Generate a Lean 4 project from a K definition') | ||
parser.add_argument('definition_dir', metavar='DEFN_DIR', type=dir_path, help='definition directory') | ||
parser.add_argument('package_name', metavar='PKG_NAME', help='name of the generated Lean 4 package (in kebab-case)') | ||
parser.add_argument( | ||
'-o', | ||
'--output', | ||
dest='output_dir', | ||
metavar='DIR', | ||
type=dir_path, | ||
help='output directory (default: .)', | ||
) | ||
parser.add_argument( | ||
'-l', | ||
'--library', | ||
dest='library_name', | ||
metavar='NAME', | ||
help='name of the generated Lean library (default: package name in PascalCase)', | ||
) | ||
parser.add_argument( | ||
'-r', | ||
'--rule', | ||
dest='rules', | ||
metavar='LABEL', | ||
action='append', | ||
help='labels of rules to include (default: all)', | ||
) | ||
return parser | ||
|
||
|
||
def _load_defn(definition_dir: Path) -> KoreDefn: | ||
from ..kore.parser import KoreParser | ||
|
||
kore_text = (definition_dir / 'definition.kore').read_text() | ||
definition = KoreParser(kore_text).definition() | ||
return KoreDefn.from_definition(definition) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,75 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING, TypedDict | ||
|
||
from .k2lean4 import K2Lean4 | ||
from .model import Module | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Callable, Iterable | ||
|
||
from ..kore.internal import KoreDefn | ||
|
||
|
||
class GenContext(TypedDict): | ||
package_name: str | ||
library_name: str | ||
|
||
|
||
def generate( | ||
defn: KoreDefn, | ||
context: GenContext, | ||
*, | ||
output_dir: str | Path | None = None, | ||
) -> Path: | ||
output_dir = Path(output_dir) if output_dir is not None else Path('.') | ||
|
||
k2lean4 = K2Lean4(defn) | ||
genmodel = { | ||
'Sorts': (k2lean4.sort_module, ['Prelude']), | ||
'Func': (k2lean4.func_module, ['Sorts']), | ||
'Inj': (k2lean4.inj_module, ['Sorts']), | ||
'Rewrite': (k2lean4.rewrite_module, ['Func', 'Inj']), | ||
} | ||
|
||
modules = _gen_modules(context['library_name'], genmodel) | ||
res = _gen_template(output_dir, context) | ||
_write_modules(res, modules) | ||
return res | ||
|
||
|
||
def _gen_template(output_dir: Path, context: GenContext) -> Path: | ||
from cookiecutter.generate import generate_files | ||
|
||
template_dir = Path(__file__).parent / 'template' | ||
gen_res = generate_files( | ||
repo_dir=str(template_dir), | ||
output_dir=str(output_dir), | ||
context={'cookiecutter': context}, | ||
) | ||
|
||
res = Path(gen_res) | ||
assert res.is_dir() | ||
return res | ||
|
||
|
||
def _gen_modules( | ||
library_name: str, | ||
genmodel: dict[str, tuple[Callable[[], Module], list[str]]], | ||
) -> dict[Path, Module]: | ||
def gen_module(generator: Callable[[], Module], imports: Iterable[str]) -> Module: | ||
imports = tuple(f'{library_name}.{importt}' for importt in imports) | ||
module = generator() | ||
module = Module(imports=imports, commands=module.commands) | ||
return module | ||
|
||
return { | ||
Path(library_name) / f'{name}.lean': gen_module(generator, imports) | ||
for name, (generator, imports) in genmodel.items() | ||
} | ||
|
||
|
||
def _write_modules(output_dir: Path, modules: dict[Path, Module]) -> None: | ||
for path, module in modules.items(): | ||
(output_dir / path).write_text(str(module)) |
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,4 @@ | ||
{ | ||
"package_name": null, | ||
"library_name": null | ||
} |
1 change: 1 addition & 0 deletions
1
pyk/src/pyk/klean/template/{{ cookiecutter.package_name }}/.gitignore
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 @@ | ||
/.lake |
9 changes: 9 additions & 0 deletions
9
pyk/src/pyk/klean/template/{{ cookiecutter.package_name }}/lakefile.toml
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,9 @@ | ||
name = "{{ cookiecutter.package_name }}" | ||
version = "0.1.0" | ||
defaultTargets = ["{{ cookiecutter.library_name }}"] | ||
weakLeanArgs = [ | ||
"-D maxHeartbeats=10000000" | ||
] | ||
|
||
[[lean_lib]] | ||
name = "{{ cookiecutter.library_name }}" |
1 change: 1 addition & 0 deletions
1
pyk/src/pyk/klean/template/{{ cookiecutter.package_name }}/lean-toolchain
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 @@ | ||
stable |
1 change: 1 addition & 0 deletions
1
...c/pyk/klean/template/{{ cookiecutter.package_name }}/{{ cookiecutter.library_name }}.lean
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 @@ | ||
import {{ cookiecutter.library_name }}.Rewrite |
Oops, something went wrong.