Skip to content

Commit 3c3a651

Browse files
authored
Merge pull request #150 from engboris/doc
Update docs
2 parents 861ede0 + a2f8660 commit 3c3a651

File tree

2 files changed

+148
-154
lines changed

2 files changed

+148
-154
lines changed

BASICS.md

Lines changed: 148 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,42 @@ readable.
3838

3939
```stellogen
4040
' single line
41-
(show "Hello, world")
4241
4342
'''
4443
multi
4544
line
4645
'''
4746
```
4847

49-
## Terms and Unification
48+
## Everything is a term!
5049

5150
In stellogen **everything** is a term.
5251

5352
A **term** is either:
5453

5554
* A variable: starts with uppercase (`X`, `Y`, `Var`).
56-
* A function: a sequence of symbols beginning with a lowercase or special
57-
symbol, followed by terms (`(f a X)`, `(:: x t)`).
55+
* A function: a sequence of symbols surrounded by parentheses, beginning with a
56+
lowercase or special symbol, followed by terms:
57+
58+
```
59+
' this program does nothing but you can check the validity of expressions
60+
(f a X)
61+
(:: x t)
62+
63+
' several terms can be on the same line
64+
(add 2 2) (fact 3)
65+
```
5866

5967
In the special case of constants (function without argument) we can omit
60-
parentheses (`f`, `a`, `::`).
68+
parentheses:
69+
70+
```
71+
a
72+
$
73+
a $ :b c
74+
```
75+
76+
## Rays and compatibility
6177

6278
**Unification** = finding substitutions that make two terms identical.
6379

@@ -71,8 +87,6 @@ For example:
7187
'''
7288
```
7389

74-
## Rays and compatibility
75-
7690
A **ray** is a term with polarity:
7791

7892
* `(+f X)` → positive
@@ -85,7 +99,7 @@ Two rays and **compatible** and can interact if they have opposite polarities
8599

86100
```stellogen
87101
(~= (+f X) (-f (h a))) ' => succeeds with {X := (h a)}
88-
(~= (+f X) (+f a)) ' => ❌ (fails because same polarity)
102+
' (~= (+f X) (+f a)) ' => ❌ (fails because same polarity)
89103
```
90104

91105
---
@@ -135,83 +149,103 @@ You can accumulate application of function symbols.
135149

136150
---
137151

138-
## Stars and Constellations
152+
## Make terms interact with Stars and Constellations
139153

140-
* A **star** is a cons list of rays:
154+
Stars and constellations are special terms which can interact with each other.
155+
156+
* A **star** block of rays:
141157

142158
```stellogen
143-
(show [(+f X) (-f (h a)) (+g Y)])
159+
[(+f X) (-f (h a)) (+g Y)]
144160
```
145161

146162
Square brackets are omitted when there is a single ray.
147163

148164
* A **constellation** is a group of stars:
149165

150166
```stellogen
151-
(show { (+f X) [(-f X) (+g a)] })
167+
{ (+f X) [(-f X) (+g a)] }
152168
```
153169

154170
Variables are local to each star. So, in the above example, the `X` in `(+f X)`
155171
and the one in `[(-f X) (+g a)]` are not bound.
156172

157-
---
173+
## Principles of Star Fusion
158174

159-
## Principles of Execution
175+
The idea of **fusion** is that stars can collide along compatible rays and merge.
160176

161-
Execution = stars interacting through **fusion** (Robinson’s resolution rule).
162-
163-
When rays unify:
177+
When rays of two stars unify:
164178

165179
* They disappear (consumed).
166-
* Their substitution applies to the rest of the star.
167-
* Stars merge.
180+
* Their substitution applies to their neighbors rays.
181+
* Their stars merge.
168182

169-
Example of constellation:
183+
Example of interaction:
170184

171185
```stellogen
172-
(:= c { [(+f X) X] (-f a) })
186+
'''
187+
star 1: [(+f X) X]
188+
star 2: (-f a)
189+
190+
' connexion
191+
(-f a) ------ (+f X) X
192+
193+
' annihilation and merge with resolution {X:=a}
194+
X
195+
196+
' propagation
197+
a ' <-- this is the result of execution
198+
'''
173199
```
174200

175-
Fusion along two matching rays: `[(+f X) X]` with `[(-f a)]``{X := a}`
176-
Result: `a`.
201+
Note: this corresponds to the so-called Robinson's resolution rule in formal
202+
logic.
177203

178204
## Focus and Action/State
179205

180-
Before execution, we separate stars into *actions* and *states*.
206+
For execution to even work in the first place, we need to group stars into
207+
*actions* and *states*.
181208

182-
State stars are marked with `@`.
183-
They are the “targets” for interaction.
209+
State stars are marked with `@`. They are the “targets” for interaction.
210+
The other stars are actions.
211+
212+
For example:
184213

185214
```stellogen
215+
' state: @[-c d]
216+
' action: [+a b]
186217
(:= d { [+a b] @[-c d] })
187218
```
188219

189-
**Intuition:** Focus corresponds to distinguishing **data (states)** from
190-
**rules/program (actions)**:
220+
**Intuition:** Focus corresponds to distinguishing **data** from
221+
**rules/program**:
191222

192223
* **States** (`@`) = what you're computing (the data being transformed)
193224
* **Actions** (no `@`) = how you compute (the rules/program that transforms)
194225

195-
Execution duplicates actions and fuses them with state stars until no more
196-
interactions are possible.
197-
The result is a new constellation, like the "normal form" of computation.
198-
199-
You can focus all stars of a constellation with `@`:
226+
You can also focus all stars of a constellation with `@`:
200227

201228
```stellogen
202229
(:= f @{ [a] [b] [c] })
203230
```
204231

205-
## Let's execute constellations!
232+
## Execution of Constellations
233+
234+
Execution = stars interacting through **fusion**.
235+
236+
Execution duplicates actions and fuses them with state stars until no more
237+
interactions are possible. The result is a new constellation.
238+
239+
**Let's execute constellations!**
206240

207241
```stellogen
208242
(:= x [(+f X) X])
209243
(:= y (-f a))
210244
211-
(:= res1 (exec @#x #y)) ' normal execution
245+
(:= res1 (exec @#x #y)) ' normal execution
212246
(show #res1)
213247
214-
(:= res2 (fire @#x #y)) ' actions are used exactly once
248+
(:= res2 (fire @#x #y)) ' actions are used exactly once
215249
(show #res2)
216250
```
217251

@@ -233,7 +267,6 @@ where several equality constraints can be chained after `||`.
233267

234268
This prevents `X` and `Y` from unifying to the same concrete value.
235269

236-
237270
---
238271

239272
## Logic Programming
@@ -287,11 +320,82 @@ This asks: *Who are the children of `b`?*
287320
This is a more strict version of the matching `~=` term which expects
288321
syntactic equality.
289322

323+
```stellogen
324+
(== a a) ' does nothing
325+
' (== a b) ' fails with an error
326+
```
327+
328+
---
329+
330+
## Advanced syntax manipulation
331+
332+
### Parametric definitions
333+
334+
Definitions are actually terms like others so they can be *parametric*:
335+
336+
```stellogen
337+
(:= (initial Q) [(-i W) (+a W Q)])
338+
```
339+
340+
In this case, calling
341+
342+
```stellogen
343+
#(initial q0)
344+
```
345+
346+
will replace `Q` by `q0` in `[(-i W) (+a W Q)]` so we get
347+
348+
```stellogen
349+
[(-i W) (+a W q0)]
350+
```
351+
352+
### Macros
353+
354+
**Macros** work like definitions except that are applied before actual
355+
execution in a preprocessing phase:
356+
357+
```
358+
' replace (spec X Y) by (:= X Y) everywhere in the code
359+
(macro (spec X Y) (:= X Y))
360+
```
361+
362+
Notice that they do not involve any call with `#`, they replace terms.
363+
364+
### Nested phantom constellations
365+
366+
Some terms like `:=`, `show` or `==` are not interactive terms which can
367+
be executed as constellations but they have an effect on the environement.
368+
369+
In fact, they can occur anywhere in the code and produce an effect when
370+
evaluated but they and considered like empty constellations `{}`.
371+
372+
For example:
373+
374+
```stellogen
375+
(exec [(+f X) X] (-f a) (:= x "Hello1"))
376+
(show #x)
377+
(:= y (show "Hello2"))
378+
#y
379+
```
380+
381+
---
382+
383+
## File import
384+
385+
It is possible to import the content of a file through their relative path:
386+
387+
```stellogen
388+
(use "filename")
290389
```
291-
(== a a) ' does nothing
292-
(== a b) ' fails with an error
390+
391+
For macros use:
392+
393+
```stellogen
394+
(use-macros "filename")
293395
```
294396

397+
Because macros are applied before actual execution.
398+
295399
---
296400

297401
## Types as Sets of Tests
@@ -322,8 +426,13 @@ It says that a `Tested` is of type `Test` when their interaction with focus on
322426
A constellation can have one or several types:
323427

324428
```stellogen
429+
' passes the test
325430
(:= 2 (+nat <s s 0>))
326431
(:: 2 nat)
432+
433+
' does not pass the test
434+
(:= 2 (+nat 2)
435+
' (:: 2 nat)
327436
```
328437

329438
Notice that a constellation can have several types providing it passes all

0 commit comments

Comments
 (0)