feat: Add Chapter 6

This commit is contained in:
Glen Whitney 2021-01-19 22:09:21 -08:00
parent d5a19e0d69
commit 76456b2c21
3 changed files with 89 additions and 0 deletions

View File

@ -32,6 +32,8 @@ for any of them if you want to try them that way. Just remember to add a
Hopefully these examples are helpful to your exploration and understanding of
Stratego -- certainly creating them was to mine.
Additional notes on particular sections or tests follow.
**/
module manual-suite
language Spoofax-Propositional-Language
@ -91,3 +93,71 @@ test sec5_3_2_impl_nf_ex [[
False())),
Impl(Atom("p"),False())),
False())
/** md
### Chapter 6
Many of the examples in
[Chapter 6](http://www.metaborg.org/en/latest/source/langdev/meta/lang/stratego/strategoxt/06-rules-and-strategies.html)
are in the form of interactive "Stratego shell" sessions. For this repository, we
have to convert them to rule definitions in a Stratego file (`chap6.str`) and
test cases for each of the executed examples.
Also, there are some examples for a Plus-Times signature; we transpose these
to Or-And for the sake of keeping everything within the SPL language project.
To illustrate extending a rule, we need to give the rule we're going to extend
a different identifier and add both definitions to that (otherwise our original
rule `SwapArgs` would already have been extended).
The `chap6.str` file also includes a solution to the exercise in
[Section 6.2](http://www.metaborg.org/en/latest/source/langdev/meta/lang/stratego/strategoxt/06-rules-and-strategies.html#what-is-a-strategy).
On the other hand, it is not clear whether the Spoofax Eclipse IDE has a "debug"
operator for Stratego, or if so how it would work, so the repository does not
attempt to implement the "debug" example.
The remainder of the chapter (from section 6.3 on) consists of general information
about Stratego syntax and information about the Stratego compiler. As it's not
clear if the latter applies to the Spoofax Eclipse IDE, there are no further test
cases in this repository beyond Section 6.2.
**/
test sec6_1_swap_ex [[
p | 0
]] run SwapArgs to Or(False(), Atom("p"))
test sec6_1_swap_fails [[
p & 0
]] run SwapArgs fails
test sec6_1_swap_subterm_fails [[
(1 | p) & 0
]] run SwapArgs fails
test sec6_1_swap_only_outer_ex [[
(1 | p) | 0
]] run SwapArgs to Or(False(), Or(True(), Atom("p")))
test sec6_1_swapboth_or_ex [[
p | 0
]] run SwapBoth to Or(False(), Atom("p"))
test sec6_1_swapboth_and_ex [[
p & 0
]] run SwapBoth to And(False(), Atom("p"))
test sec6_2_invoke_rule_ex [[
1 -> p
]] run DefI to Or(Not(True()), Atom("p"))
test sec6_2_innermost_ex [[
p = q
]] run dnf4 to Or(Or(And(Not(Atom("p")),Not(Atom("q"))),
And(Not(Atom("p")),Atom("p"))),
Or(And(Atom("q"),Not(Atom("q"))),
And(Atom("q"),Atom("p"))))
test sec6_2_dnf_exercise_ex [[
p = q
]] run better-dnf to Or(And(Not(Atom("p")),Not(Atom("q"))),
And(Atom("q"),Atom("p")))

18
trans/chap6.str Normal file
View File

@ -0,0 +1,18 @@
module chap6
imports prop-laws prop-eval2 signatures/-
rules
SwapArgs : Or(e1,e2) -> Or(e2,e1)
SwapBoth : Or(e1,e2) -> Or(e2,e1)
SwapBoth : And(e1,e2) -> And(e2,e1)
ExMid: And(Not(x),x) -> False()
ExMid: And(x,Not(x)) -> False()
strategies
better-dnf =
innermost(DefI <+ DefE <+ ExMid <+ DAOL <+ DAOR <+ DN <+ DMA <+ DMO); eval-down

View File

@ -13,6 +13,7 @@ imports
prop-laws
prop-eval2
prop-desugar
chap6
rules // Debugging