? users online
  • Logout
    • Open hangout
    • Open chat for current file
<div class="notebook">

<div class="nb-cell markdown" name="md1">
# Law - Blawx - s(CASP) 
(Workbook authored by David Pratten.)

This workbook takes Blawx's "Rocks Paper Scissors Law" example and explores the way that Blawx captures law in its block-language and thence translates the law into s(CASP) language.
</div>

<div class="nb-cell markdown" name="md3">
We start this SWISH notebook by initialising s(CASP). By default the language understood is vanilla SWI-Prolog.
</div>

<div class="nb-cell program" data-background="true" name="p1">
% Load s(CASP)
:- use_module(library(scasp)).

% Suppress known warnings 
:- style_check(-discontiguous).
:- style_check(-singleton).
:- set_prolog_flag(scasp_unknown, fail).
</div>

<div class="nb-cell markdown" name="md4">
We incorporate a minimal legal theory expected by Blawx. See the first few sections of the notebook [here](https://swish.swi-prolog.org/p/r34v2.swinb) for some introduction to Blawx's legal theory.
</div>

<div class="nb-cell program" data-background="true" name="p3">
% Source: https://github.com/Lexpedite/blawx/blob/main/blawx/ldap.py

legally_holds(R,C) :-
    according_to(R,C). 

% The according_to(X,Y) rule is required to be defined below. 
% However, we will only be able to do this once we have
% created the categories and attributes over which it will operate.
</div>

<div class="nb-cell markdown" name="md2">
### The Rock Paper Scissors Act

The Rock Paper Scissors Act is adapted from https://github.com/Lexpedite/blawx/blob/main/blawx/static/blawx/examples/rps.yaml. Here it is in its entirety ...

---
```
Rock Paper Scissors Act

Players
1. A game of rock paper scissors has two players.
2. There are three signs:
  (a) Rock,
  (b) Paper, and
  (c) Scissors.

Defeating Relationships
3. The signs are related in the following ways:
  (a) Rock beats Scissors,
  (b) Scissors beats Paper, and
  (c) Paper beats Rock.

Winner
4. The winner of a game is the player who throws
a sign that beats the sign of the other player.

```

### Encoding the Act in Blawx and thence to s(CASP)

From here on we will follow Blawx's encoding of the Act, and its translation to s(CASP), step by step.

---

```
Rock Paper Scissors Act
1. Players A game of rock paper scissors has two players.
```
which may be translated into blawx like this:
</div>

<div class="nb-cell html" name="htm1">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s1.png">
</div>

<div class="nb-cell markdown" name="md10">
and by Blawx into s(CASP) like this ...
</div>

<div class="nb-cell program" data-background="true" name="p5">
% Generated s(CASP)

% The blawx_category rule is used by Blawx but not by s(CASP).
blawx_category(game). 

% The "#pred" rules are used by s(CASP) to respond to 
% queries in natural language. These rules are generated by
% Blawx but do not represent any logic or relationships.
#pred game(X) :: '@(X) is a game'.
#pred according_to(R,game(X)) :: 'according to @(R), @(X) is a game'.
#pred legally_holds(_,game(X)) :: 'it legally holds that @(X) is a game'.

blawx_category(player).
#pred player(X) :: '@(X) is a player'.
#pred according_to(R,player(X)) :: 'according to @(R), @(X) is a player'.
#pred legally_holds(_,player(X)) :: 'it legally holds that @(X) is a player'.

% The blawx_attribute rule is used by Blawx but not by s(CASP).
blawx_attribute(game,player,player).

% The blawx_attribute_nlg rule is used by Blawx but not by s(CASP).
blawx_attribute_nlg(player,vo,"","played in","").

#pred player(Y,X) :: '@(X) played in @(Y)'.
#pred according_to(R,player(Y,X)) :: 'according to @(R), @(X) played in @(Y)'.
#pred legally_holds(_,player(Y,X)) :: 'it legally holds that @(X) played in @(Y)'.
</div>

<div class="nb-cell markdown" name="md6">
the rest of the act continues in the same fashion:
- A section of the act, followed by
- The encoding in Blawx, and finally,
- The translation into s(CASP). 

---
```
2 There are three signs:
```
</div>

<div class="nb-cell html" name="htm3">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s2.png">
</div>

<div class="nb-cell program" data-background="true" name="p6">
% Generated s(CASP)

blawx_category(sign).
#pred sign(X) :: '@(X) is a sign'.
#pred according_to(R,sign(X)) :: 'according to @(R), @(X) is a sign'.
#pred legally_holds(_,sign(X)) :: 'it legally holds that @(X) is a sign'.
</div>

<div class="nb-cell markdown" name="md7">
---

```
a) Rock,
b) Paper,
c) Scissors.
```
</div>

<div class="nb-cell html" name="htm2">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s2a.png">
</div>

<div class="nb-cell html" name="htm4">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s2b.png">
</div>

<div class="nb-cell html" name="htm5">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s2c.png">
</div>

<div class="nb-cell program" data-background="true" name="p2">
% Generated s(CASP)

sign(rock).
sign(paper).
sign(scissors).
</div>

<div class="nb-cell markdown" name="md13">
---
```
3 Defeating Relationships. The signs are related in the following ways:
```
</div>

<div class="nb-cell html" name="htm7">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s3.png">
</div>

<div class="nb-cell program" data-background="true" name="p10">
% Generated s(CASP)

blawx_attribute(sign,beats,sign).
blawx_attribute_nlg(beats,ov,"","beats","").
#pred beats(X,Y) :: '@(X) beats @(Y)'.
#pred according_to(R,beats(X,Y)) :: 'according to @(R), @(X) beats @(Y)'.
#pred legally_holds(_,beats(X,Y)) :: 'it legally holds that @(X) beats @(Y)'.
</div>

<div class="nb-cell markdown" name="md9">
---
```
a) Rock beats Scissors,
b) Scissors beats Paper, and
c) Paper beats Rock.
```
</div>

<div class="nb-cell html" name="htm6">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s3a.png">
</div>

<div class="nb-cell html" name="htm8">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s3b.png">
</div>

<div class="nb-cell html" name="htm9">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s3c.png">
</div>

<div class="nb-cell program" data-background="true" name="p11">
% Generated s(CASP)

beats(rock,scissors).
beats(paper,rock).
beats(scissors,paper).
</div>

<div class="nb-cell markdown" name="md8">
---
```
4 Winner The winner of a game is the player who throws a sign 
that beats the sign of the other player.
```
</div>

<div class="nb-cell html" name="htm10">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s4.1.png">
</div>

<div class="nb-cell program" data-background="true" name="p8">
% Generated s(CASP)

blawx_attribute(game,winner,player).
blawx_attribute_nlg(winner,ov,"the winner of","is","").
#pred winner(X,Y) :: 'the winner of @(X) is @(Y)'.
#pred according_to(R,winner(X,Y)) :: 'according to @(R), the winner of @(X) is @(Y)'.
#pred legally_holds(_,winner(X,Y)) :: 'it legally holds that the winner of @(X) is @(Y)'.

blawx_attribute(player,throw,sign).
blawx_attribute_nlg(throw,ov,"","threw","").
#pred throw(X,Y) :: '@(X) threw @(Y)'.
#pred according_to(R,throw(X,Y)) :: 'according to @(R), @(X) threw @(Y)'.
#pred legally_holds(_,throw(X,Y)) :: 'it legally holds that @(X) threw @(Y)'.
</div>

<div class="nb-cell html" name="htm11">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/rps-s4.2.png">
</div>

<div class="nb-cell program" data-background="true" name="p9">
% Generated s(CASP)

according_to(sec_4_section,  winner(Game,Player1)) :-
game(Game),
player(Player1),
player(Player2),
player(Game,Player1),
player(Game,Player2),
throw(Player1,Throw1),
throw(Player2,Throw2),
beats(Throw1,Throw2).

% End of the s(CASP) encoding of the Rocks Paper Scissors Law.
</div>

<div class="nb-cell markdown" name="md11">
### bobjane Test
Included along with the "Rock Paper Scissors Law" example are a few test cases.  Here we will look at the "bobjane" test.

The test begins with some facts and then asks a question.

```
Bob and Jane play a game of Rock Paper Scissors called testgame. 
Jane throws Rock, Bob throws Scissors. 
```
</div>

<div class="nb-cell html" name="htm12">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/blawx_test_test_bobjane_particulars.png">
</div>

<div class="nb-cell program" data-background="true" name="p7">
% Generated s(CASP).

game(testgame).
player(bob).
player(jane).
player(testgame,bob).
player(testgame,jane).
throw(jane,rock).
throw(bob,scissors).
</div>

<div class="nb-cell markdown" name="md12">
---
```
Who won?
```
</div>

<div class="nb-cell html" name="htm13">
<img title="Blawx blocks encoding" src="https://davidpratten.com/wp-content/uploads/2022/08/blawx_test_test_bobjane_query.png">
</div>

<div class="nb-cell query" name="q2">
? legally_holds(_,  winner(testgame,Winner)).
</div>

</div>