-
Notifications
You must be signed in to change notification settings - Fork 159
Factor out CTermShow class for simpler node printing #4808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
bb36563
pyk/{kast/manip,cli/pyk,__main__}: remove unused and barely tested op…
ehildenb bb405ab
pyk/cterm/cterm: add CTerm.constraint
ehildenb 99d40d2
pyk/cterm/show: introduce CTermShow class for displaying cterms directly
ehildenb 879af57
cterm/show: adjust CTermShow.show to return list of strings, adjust o…
ehildenb 39e0050
pyk/kcfg/{show,tui},pyk/proof/{show,tui}: use CTermShow in all NodePr…
ehildenb fcd55fa
pyk/kcfg/show: leave setting max_width up to consumer of print info
ehildenb f53b981
Merge remote-tracking branch 'upstream/develop' into node-print-updates
ehildenb 8da24e3
Merge remote-tracking branch 'upstream/develop' into node-print-updates
ehildenb 0093fc8
pyk/{kcfg/show,kcfg/tui,proof/show}: adjust KCFGShow to not use KPrint
ehildenb b4e3c87
pyk/proof/{show,tui},pyk/__main__: lower KPrint to KDefinition in sho…
ehildenb aa034ef
pyk/{kcfg/tui,proof/tui}: completely removed usage of KPrint
ehildenb 9ca3b3b
pyk/{cterm/show,kcfg/show,kcfg/tui}: move boolify and minimize to cla…
ehildenb fa66e6c
pyk/cterm/show, pyk/kcfg/tui: enable dynamic minimization in tui
ehildenb aeb983e
pyk/proof/tui: correctly thread through cterm_show
ehildenb 16e8a81
pyk/cterm/show: remove boolify flag
ehildenb 54f1983
pyk/cterm/show: break collections directly under cells by default
ehildenb 7803d04
pyk/cterm/show: switch sorting collections and minimizing to being on…
ehildenb 4d92be4
pyk/cterm/show: remove custom cterm adjustment for printing
ehildenb 1d71048
pyk/cterm/show: do minimization late, to get maximal minimization
ehildenb e2375be
pyk/cterm/show: split CTermShow.show into CTermShow.show_config and C…
ehildenb 6be28fa
pyk/cterm/show: add option to omit cells to CTermShow
ehildenb 8229fd0
Merge remote-tracking branch 'upstream/develop' into node-print-updates
ehildenb 4cbf389
pyk/kcfg/tui: correct signature for ctermshow
ehildenb 6b64d4d
pyk/{kcfg/show,proof/show,__main__}: remove minimize option for NodeP…
ehildenb d504ee4
pyk/cterm/show: rename omit_cells => omit_labels
ehildenb 551b6e5
Merge remote-tracking branch 'upstream/develop' into node-print-updates
ehildenb a473fa7
pyk/src/tests/unit: rename mock file
ehildenb e49ed8a
pyk/cterm/show: reorder functions
ehildenb d2731bf
pyk/{cterm,kast,kcfg}: abstract away actual provider of pretty printer
ehildenb 8fe4216
pyk/tests/unit/test_kcfg: correct way of constructing CTermShow
ehildenb 17e15f2
pyk/{cterm,kast,kcfg}: switch to using single-entry printer
ehildenb 1dfc6d5
cterm/show: add dataclass annotation
ehildenb 3fd3bc8
pyk/cterm/show: make display options private
ehildenb 508855f
Merge branch 'develop' into node-print-updates
ehildenb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,99 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING | ||
|
||
from ..kast.inner import KApply, KSort, KToken, flatten_label, top_down | ||
from ..kast.manip import free_vars, minimize_term | ||
from ..kast.prelude.k import DOTS | ||
from .cterm import CTerm | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Callable, Iterable | ||
from typing import Final | ||
|
||
from ..kast.inner import KInner | ||
|
||
_LOGGER: Final = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class CTermShow: | ||
_printer: Callable[[KInner], str] | ||
_minimize: bool | ||
_break_cell_collections: bool | ||
_omit_labels: tuple[str, ...] | ||
|
||
def __init__( | ||
self, | ||
printer: Callable[[KInner], str], | ||
minimize: bool = True, | ||
break_cell_collections: bool = True, | ||
omit_labels: Iterable[str] = (), | ||
): | ||
self._printer = printer | ||
self._minimize = minimize | ||
self._break_cell_collections = break_cell_collections | ||
self._omit_labels = tuple(omit_labels) | ||
|
||
def print_lines(self, kast: KInner) -> list[str]: | ||
return self._printer(kast).split('\n') | ||
|
||
def let( | ||
tothtamas28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self, | ||
minimize: bool | None = None, | ||
break_cell_collections: bool | None = None, | ||
omit_labels: Iterable[str] | None = None, | ||
) -> CTermShow: | ||
return CTermShow( | ||
self._printer, | ||
minimize=(self._minimize if minimize is None else minimize), | ||
break_cell_collections=( | ||
self._break_cell_collections if break_cell_collections is None else break_cell_collections | ||
), | ||
omit_labels=(self._omit_labels if omit_labels is None else omit_labels), | ||
) | ||
|
||
def show(self, cterm: CTerm) -> list[str]: | ||
ret_strs: list[str] = [] | ||
ret_strs.extend(self.show_config(cterm)) | ||
ret_strs.extend(self.show_constraints(cterm)) | ||
return ret_strs | ||
|
||
def show_config(self, cterm: CTerm) -> list[str]: | ||
if self._break_cell_collections: | ||
cterm = CTerm(top_down(self._break_cells_visitor, cterm.config), cterm.constraints) | ||
if self._omit_labels: | ||
cterm = CTerm(top_down(self._omit_labels_visitor, cterm.config), cterm.constraints) | ||
if self._minimize: | ||
cterm = CTerm(minimize_term(cterm.config, keep_vars=free_vars(cterm.constraint)), cterm.constraints) | ||
return self.print_lines(cterm.config) | ||
|
||
def show_constraints(self, cterm: CTerm) -> list[str]: | ||
ret_strs: list[str] = [] | ||
for constraint in cterm.constraints: | ||
constraint_strs = self.print_lines(constraint) | ||
if len(constraint_strs) > 0: | ||
constraint_strs = [f'#And {cstr}' for cstr in constraint_strs] | ||
ret_strs.append(constraint_strs[0]) | ||
ret_strs.extend([f' {constraint_str}' for constraint_str in constraint_strs[1:]]) | ||
return ret_strs | ||
|
||
def _break_cells_visitor(self, kast: KInner) -> KInner: | ||
if ( | ||
type(kast) is KApply | ||
and kast.is_cell | ||
and len(kast.args) == 1 | ||
and type(kast.args[0]) is KApply | ||
and kast.args[0].label.name in {'_Set_', '_List_', '_Map_'} | ||
): | ||
items = flatten_label(kast.args[0].label.name, kast.args[0]) | ||
printed = KToken('\n'.join(map(self._printer, items)), KSort(kast.label.name[1:-1])) | ||
return KApply(kast.label, [printed]) | ||
return kast | ||
|
||
def _omit_labels_visitor(self, kast: KInner) -> KInner: | ||
if type(kast) == KApply and kast.label.name in self._omit_labels: | ||
return DOTS | ||
return kast |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.