Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f94b01c

Browse files
author
Shati Patel
committed
QL etudes: Address comments + fix sphinx warning
1 parent 56bc8cb commit f94b01c

4 files changed

Lines changed: 28 additions & 25 deletions

File tree

docs/language/learn-ql/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ If you are new to QL, start by looking at the following topics:
2323
introduction-to-ql
2424
about-ql
2525
beginner/ql-tutorials
26+
ql-etudes/river-crossing
2627

2728
QL training and variant analysis examples
2829
******************************************

docs/language/learn-ql/ql-etudes/river-crossing-1.ql

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @name River crossing puzzle (version 1)
33
* @description An "elementary" version of the solution to
4-
* the river crossing problem.
4+
* the river crossing problem. It introduces more explicit and intuitive
5+
* definitions, before tidying them up in the "full" version.
56
*
67
* Note: Parts of this QL file are included in the corresponding .rst file.
78
* Make sure to update the line numbers if you change anything here!
@@ -21,8 +22,8 @@ class Cargo extends string {
2122
class Shore extends string {
2223
Shore() { this = "Left" or this = "Right" }
2324

24-
/** Returns "the other shore". */
25-
Shore flip() {
25+
/** Returns the other shore. */
26+
Shore other() {
2627
this = "Left" and result = "Right"
2728
or
2829
this = "Right" and result = "Left"
@@ -42,20 +43,20 @@ class State extends string {
4243
State() { this = man + "," + goat + "," + cabbage + "," + wolf }
4344

4445
State ferry(Cargo cargo) {
45-
cargo = "Nothing" and result = man.flip() + "," + goat + "," + cabbage + "," + wolf
46+
cargo = "Nothing" and result = man.other() + "," + goat + "," + cabbage + "," + wolf
4647
or
47-
cargo = "Goat" and result = man.flip() + "," + goat.flip() + "," + cabbage + "," + wolf
48+
cargo = "Goat" and result = man.other() + "," + goat.other() + "," + cabbage + "," + wolf
4849
or
49-
cargo = "Cabbage" and result = man.flip() + "," + goat + "," + cabbage.flip() + "," + wolf
50+
cargo = "Cabbage" and result = man.other() + "," + goat + "," + cabbage.other() + "," + wolf
5051
or
51-
cargo = "Wolf" and result = man.flip() + "," + goat + "," + cabbage + "," + wolf.flip()
52+
cargo = "Wolf" and result = man.other() + "," + goat + "," + cabbage + "," + wolf.other()
5253
}
5354

5455
/**
5556
* Holds if predator and prey are on the same shore and the man
56-
* is on the other shore.
57+
* is not present.
5758
*/
58-
predicate eats(Shore predator, Shore prey) { predator = prey and man = predator.flip() }
59+
predicate eats(Shore predator, Shore prey) { predator = prey and man = predator.other() }
5960

6061
/** Holds if nothing gets eaten in this state. */
6162
predicate isSafe() { not (eats(goat, cabbage) or eats(wolf, goat)) }
@@ -93,6 +94,6 @@ class GoalState extends State {
9394
}
9495

9596
from string path
96-
where any(InitialState is).reachesVia(path, _) = any(GoalState gs)
97+
where any(InitialState i).reachesVia(path, _) = any(GoalState g)
9798
select path
9899

docs/language/learn-ql/ql-etudes/river-crossing.ql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class Shore extends string {
2929
this = "Right"
3030
}
3131

32-
/** Returns "the other shore". */
33-
Shore flip() {
32+
/** Returns the other shore. */
33+
Shore other() {
3434
this = "Left" and result = "Right"
3535
or
3636
this = "Right" and result = "Left"
@@ -56,20 +56,20 @@ class State extends string {
5656

5757
/** Returns the state that is reached after ferrying a particular cargo item. */
5858
State ferry(Cargo cargo) {
59-
cargo = "Nothing" and result = renderState(man.flip(), goat, cabbage, wolf)
59+
cargo = "Nothing" and result = renderState(man.other(), goat, cabbage, wolf)
6060
or
61-
cargo = "Goat" and result = renderState(man.flip(), goat.flip(), cabbage, wolf)
61+
cargo = "Goat" and result = renderState(man.other(), goat.other(), cabbage, wolf)
6262
or
63-
cargo = "Cabbage" and result = renderState(man.flip(), goat, cabbage.flip(), wolf)
63+
cargo = "Cabbage" and result = renderState(man.other(), goat, cabbage.other(), wolf)
6464
or
65-
cargo = "Wolf" and result = renderState(man.flip(), goat, cabbage, wolf.flip())
65+
cargo = "Wolf" and result = renderState(man.other(), goat, cabbage, wolf.other())
6666
}
6767

6868
/**
6969
* Holds if predator and prey are on the same shore and the man
70-
* is on the other shore.
70+
* is not present.
7171
*/
72-
predicate eats(Shore predator, Shore prey) { predator = prey and man = predator.flip() }
72+
predicate eats(Shore predator, Shore prey) { predator = prey and man = predator.other() }
7373

7474
/** Holds if nothing gets eaten in this state. */
7575
predicate isSafe() { not (eats(goat, cabbage) or eats(wolf, goat)) }
@@ -110,5 +110,5 @@ class GoalState extends State {
110110
}
111111

112112
from string path
113-
where any(InitialState is).reachesVia(path, _) = any(GoalState gs)
113+
where any(InitialState i).reachesVia(path, _) = any(GoalState g)
114114
select path

docs/language/learn-ql/ql-etudes/river-crossing.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Second, any item can be on one of two shores. Let's call these the "left shore"
4848
Define a class ``Shore`` containing ``"Left"`` and ``"Right"``.
4949

5050
It would be helpful to express "the other shore". You can do this by defining a member predicate
51-
``flip`` in the class ``Shore`` such that ``"Left".flip()`` returns ``"Right"`` and vice versa.
51+
``other`` in the class ``Shore`` such that ``"Left".other()`` returns ``"Right"`` and vice versa.
5252

5353
.. container:: toggle
5454

@@ -74,7 +74,7 @@ temporary variables in the body of a class are called `fields <https://help.semm
7474
*Show/hide code*
7575

7676
.. literalinclude:: river-crossing-1.ql
77-
:lines: 32-42,83
77+
:lines: 33-43,84
7878

7979
We are interested in two particular states, namely the initial state and the goal state.
8080
Assuming that all items start on the left shore and end up on the right shore, define
@@ -87,7 +87,7 @@ Assuming that all items start on the left shore and end up on the right shore, d
8787
*Show/hide code*
8888

8989
.. literalinclude:: river-crossing-1.ql
90-
:lines: 85-93
90+
:lines: 86-94
9191

9292
.. pull-quote::
9393

@@ -114,7 +114,7 @@ The basic act of ferrying moves the man and a cargo item to the other shore,
114114
resulting in a new state.
115115

116116
Write a member predicate (of ``State``) called ``ferry``, that specifies what happens to the state
117-
after ferrying a particular cargo. (Hint: Use the predicate ``flip``.)
117+
after ferrying a particular cargo. (Hint: Use the predicate ``other``.)
118118

119119
.. container:: toggle
120120

@@ -129,7 +129,8 @@ Of course, not all ferrying actions are possible. Add some extra conditions to d
129129
action is "safe", that is, it doesn't lead to a state where the goat or the cabbage get eaten.
130130
For example, follow these steps:
131131

132-
#. Define a predicate ``eats`` that encodes the conditions for when something gets eaten.
132+
#. Define a predicate ``eats`` that encodes the conditions for when a "predator" is able to eat an
133+
unguarded "prey".
133134
#. Define a predicate ``isSafe`` that holds when nothing gets eaten.
134135
#. Define a predicate ``safeFerry`` that restricts ``ferry`` to only include safe ferrying actions.
135136

@@ -175,7 +176,7 @@ for example ``steps <= 7``.
175176
*Show/hide code*
176177

177178
.. literalinclude:: river-crossing-1.ql
178-
:lines: 66-82
179+
:lines: 67-83
179180

180181
However, although this ensures that the solution is finite, it can still contain loops if the upper bound
181182
for ``steps`` is large.

0 commit comments

Comments
 (0)