Skip to content

Commit 4c925c3

Browse files
committed
Provide language specs and install instructions
1 parent 86ef44f commit 4c925c3

File tree

3 files changed

+211
-13
lines changed

3 files changed

+211
-13
lines changed

README.mkd

Lines changed: 211 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ yourself entertained, you can find some exercises on that website, too.
1616
Execute `runtests.sh` to run the tests.
1717

1818

19+
Installation
20+
------------
21+
22+
1. Install the [Haskell platform]. This will give you [GHC] and
23+
the [cabal-install] build tool.
24+
25+
2. Clone this project and build it with `cabal`:
26+
27+
git clone git://github.com/eugenkiss/loopgotowhile.git
28+
cd loopgotowhile-site
29+
cabal install
30+
31+
Now you can use this project as a library in Haskell source files.
32+
33+
[GHC]: http://www.haskell.org/ghc/
34+
[Haskell platform]: http://hackage.haskell.org/platform/
35+
[cabal-install]: http://hackage.haskell.org/trac/hackage/wiki/CabalInstall
36+
37+
1938
Background
2039
----------
2140

@@ -120,38 +139,225 @@ whose domain are the natural numbers to the power of *n*, where *n* is the
120139
number of arguments. Also, every variable is by default initialized to `0` and
121140
arguments, if any, are stored in `x1,...,xn`. That means, providing the
122141
argument list `4,87,3` is the same as prepending `x1 := 4; x2 := 87, x3 := 3`
123-
to the top of your program.
142+
to the top of your program. Also, note that each language is case sensitive!
124143

125144

126145
### Loop
127146

147+
An exemplary (extended and pointless) Loop program looks as follows:
148+
149+
x4 := abc + y * 4;
150+
IF hg != 7 || hg != 7 THEN
151+
LOOP x4 - (x4 + 1) DO
152+
x0 := 1
153+
END
154+
ELSE
155+
x0 := counter
156+
END
157+
158+
128159
#### Syntax
129160

161+
The syntactical components are
162+
163+
- Variables: `x0`,`x1`,`x2`,`x3`,`x4`,...
164+
- Constants: `0`,`1`,`2`,`3`,`4`,...
165+
- Operators: `+` and `-`
166+
- Separators: `;` and `:=`
167+
- Keywords: `LOOP`,`DO` and `END`
168+
169+
Let `xi` and `xj` be variables and let `c` be a constant. Then
170+
171+
xi := xj + c
172+
173+
and
174+
175+
xi := xj - c
176+
177+
are Loop programs.
178+
179+
180+
Let `P1` and `P2` be Loop programs. Then
181+
182+
P1; P2
183+
184+
is a Loop Program.
185+
186+
187+
Let `P` be a Loop program and let `xi` be a variable. Then
188+
189+
LOOP xi DO P END
190+
191+
is a Loop Program.
192+
193+
130194
#### Sematics
131195

196+
Let `P` be a Loop Program. `P` computes a function *f: ℕ^k → ℕ* like so: At the
197+
beginning of the computation the arguments *n1,...,nk ∈ ℕ* are to be found in
198+
the variables *x1,...,xk*. All other variables have the starting value 0. `P`
199+
is executed as follows:
200+
201+
- By executing the program `xi := xj + c` `xi` is assigned the value of `xj + c`.
202+
- By executing the program `xi := xj - c` `xi` is assigned the value of `xj - c`
203+
if the value is non-negative. Otherwise `xi` is assigned 0.
204+
- By executing the program `P1; P2` at first `P1` is executed and after that
205+
`P2`.
206+
- The execution of the program `LOOP xi DO P' END` happens as follows: The
207+
Program `P'` is executed as often as the value of `xi` has been at the
208+
Beginning. That means, assignments to `xi` inside `P'` do *not* influence the
209+
number of repetitions.
210+
211+
The result of `P`'s execution is the value of `x0` or put in another way
212+
*f(n1,...,nk) = Value of x0 after execution*.
213+
214+
A function *f: ℕ^k → ℕ* is called *Loop-computable* if there exists a Loop
215+
program that computes *f* as described above.
216+
217+
132218
#### Extensions
133219

220+
Variables can be named arbitrarily. The only restrictions are that they begin
221+
with a letter and are not a keyword, e.g. `counter4` is a valid variable
222+
identifier. Apart from that the following Loop programs are all valid and have
223+
the respective intuitive semantics:
224+
225+
- `xi := xj` (stands for `xi := xj + 0`)
226+
- `xi := c` (stands for `xi := xj + c` where `xj` is an unused variable)
227+
- `xi := aexp` where `aexp` is an arbitrary arithmetic expression consisting of
228+
variables, constants, (optional) parantheses and operators
229+
(`+`,`-`,`*`,`/`,`^` and `%`) like for example `y * (hg + 8 / x8) - 2 % 4`.
230+
- `LOOP aexp DO P END` where `aexp` is an arbitrary arithmetic expression.
231+
- `IF xi = 0 THEN P END`
232+
- `IF !(xi < xj && !(xk != 3)) THEN P1 ELSE P2 END`
233+
- `IF bexp THEN P END` where `bexp` is an arbitrary "boolean" expression
234+
consisting of variables, constants, (optional) parantheses, relational
235+
operators (`<`,`<=`,`>`,`>=`,`=` and `!=`) and boolean operators (`&&`,`||`
236+
and `!`).
237+
- `IF bexp THEN P1 ELSE P2 END`
238+
239+
Also, you are allowed to insert comments in your source code. The syntax is
240+
similar to Java's comment syntax, i.e. `//` introduces a comment
241+
whose scope ends at the next line and `/* */` can be used for
242+
multiline comments.
243+
134244

135245
### Goto
136246

247+
An exemplary (extended and pointless) Goto program looks as follows:
248+
249+
M1: x4 := abc + y * 4;
250+
Cool: IF hg != 7 || hg != 7 THEN GOTO M1 ELSE HALT END;
251+
GOTO Cool;
252+
M42: HALT
253+
254+
137255
#### Syntax
138256

257+
A Goto program is a succession
258+
259+
M1: A1;
260+
M2: A2;
261+
262+
Mk: Ak
263+
264+
where `Mi` is a so called *label* and `Ai` is an instruction. Possible
265+
instructions are:
266+
267+
- Value assignment: `xi := xj ± c` (`xi`, `xj` are variables, `c` is a constant)
268+
- Jump: `GOTO Mi`
269+
- Conditional Jump: `IF xi = c THEN GOTO Mj`
270+
- Stop instruction: `HALT`
271+
272+
The last instruction is either a stop instruction or a jump.
273+
274+
139275
#### Sematics
140276

141-
The behaviour for using an unspecified label in a GOTO statement is undefined.
277+
The execution of a Goto program starts with the first instruction. The
278+
execution of instructions of each type is as follows:
279+
280+
- `xi := xj ± c`: The value of `xi` becomes `xj ± c` and the next instruction
281+
is executed.
282+
- `GOTO Mi`: Proceed with the instruction with label `Mi`.
283+
- `IF xi = c THEN GOTO Mj`: If the value of `xi` is equal to `c` proceed with
284+
the instruction with label `Mj`. Otherwise the next instruction is executed.
285+
- `HALT`: Stop the execution of the program.
286+
287+
A jump to a label that is not existent in the program is not defined.
288+
289+
Let `P` be a Goto Program. `P` computes a function *f: ℕ^k → ℕ* like so: At the
290+
beginning of the computation the arguments *n1,...,nk ∈ ℕ* are to be found in
291+
the variables *x1,...,xk*. All other variables have the starting value 0.
292+
293+
*f(n1,...,nk)* is the value of `x0` after execution if `P` terminates.
294+
Otherwise *f(n1,...,nk)* is undefined.
295+
296+
A function *f: ℕ^k → ℕ* is called *Goto-computable* if there exists a Goto
297+
program that computes *f* as described above.
298+
142299

143300
#### Extensions
144301

145-
optional labels, can be named like identifiers,
146-
HALT can come inside if body, rest like loope
302+
The program doesn't have to end with a jump or a stop instruction. Labels can
303+
be named arbitrarily or can be omitted altogether. Note, tough, that labels
304+
must be unique. `IF` statements must be completed with the lexeme `END` because
305+
they can contain several statements. Furthermore, a `HALT` statement may appear
306+
in the body of an `IF`.
307+
308+
Apart from that, all extensions from the Loop language - except for the `LOOP`
309+
construct which is not present in the extended Goto language - apply to the
310+
extended Goto language.
147311

148312

149313
### While
150314

315+
The While language is an extension of the Loop language. An exemplary (extended
316+
and pointless) While program looks as follows:
317+
318+
x4 := abc + y * 4;
319+
IF hg != 7 || hg != 7 THEN
320+
WHILE !(x4 = x4 + 1) DO
321+
x0 := 1
322+
END
323+
ELSE
324+
x0 := counter
325+
END
326+
327+
151328
#### Syntax
152329

330+
Apart from the `LOOP` construct which is not part of the While language the
331+
syntax is the same as that of Loop. Additionally, a new keyword (`WHILE`) with
332+
the accompanying syntactical construct, namely the `WHILE` loop, is introduced.
333+
334+
Let `P` be a While program and let `xi` be a variable. Then
335+
336+
WHILE xi != 0 DO P END
337+
338+
is a While program.
339+
340+
153341
#### Sematics
154342

343+
The execution of `WHILE xi != 0 DO P END` happens so, that the program `P` is
344+
executed as long as the value of `xi` is not equal to 0.
345+
346+
Let `P` be a While Program. `P` computes a function *f: ℕ^k → ℕ* like so: At
347+
the beginning of the computation the arguments *n1,...,nk ∈ ℕ* are to be found
348+
in the variables *x1,...,xk*. All other variables have the starting value 0.
349+
350+
*f(n1,...,nk)* is the value of `x0` after execution if `P` terminates.
351+
Otherwise *f(n1,...,nk)* is undefined.
352+
353+
A function *f: ℕ^k → ℕ* is called *While-computable* if there exists a While
354+
program that computes *f* as described above.
355+
356+
155357
#### Extensions
156358

157-
while head can have bexp, rest like loope
359+
Apart from the `LOOP` related extensions - since the While language has no
360+
`LOOP` construct - the Loop extensions are all valid While extensions.
361+
Additionally, the head of a `WHILE` loop can have an arbitrary boolean
362+
expression, e.g. `WHILE xyz != 9 || u = 8 DO P END` is a valid (extended) While
363+
program.

TODO

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
- finish readme
2-
- Language Specifications from GTI Skript
3-
41
- Implement suggestions
52

63

7-
84
How to improve the code?
95
========================
106

test

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ END
4444

4545
Where c is a previously unused variable that represents a "boolean value".
4646

47-
--------------------------------------------
48-
49-
Some extended Goto programs are not at all optimized so that a simple
50-
division operation like x0 := x1 / x2 takes ridiculously long time to compute.
5147

5248
-------------------
5349
Function definition

0 commit comments

Comments
 (0)