Skip to content

Commit da354f0

Browse files
committed
docs and compatibility
1 parent fde45c7 commit da354f0

21 files changed

+217
-62
lines changed

README.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
# LODA Python Module
1+
# LODA Python
22

3-
This Python module contains an implementation of the [LODA Language](https://loda-lang.org/). You can use it to read, write and evaluate LODA programs to integer sequences, and to use machine learning to find new programs.
3+
This Python package contains an implementation of the [LODA Language](https://loda-lang.org/):
4+
an assembly language and computational model for finding integer sequence programs.
5+
6+
This Python package allows you to read and write LODA programs, to evaluate
7+
them to integer sequences, to search for matches in the
8+
[OEIS](https://www.oeis.org/) database,
9+
and to use machine learning tools from [Tensorflow](https://www.tensorflow.org/)
10+
to find new integer sequence programs.
411

512
## Getting Started
613

7-
Install the dependencies for the LODA Python module:
14+
You need Python 3.7 or higher. To install the dependencies for LODA, run these commands:
815

916
```bash
1017
python3 -m venv env
@@ -18,4 +25,4 @@ To execute the tests, run the following command:
1825
nose2 tests -v
1926
```
2027

21-
Check out [sample.py](sample.py) and the [documentation](https://loda-lang.org/loda-python/) to find out how to use the LODA Python module.
28+
Check out [sample.py](sample.py) and the [documentation](https://loda-lang.org/loda-python/) to find out how to use the LODA Python package.

fibonacci.asm

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; A000045: Fibonacci numbers.
2+
mov $3,1
3+
lpb $0
4+
sub $0,1
5+
mov $2,$1
6+
add $1,$3
7+
mov $3,$2
8+
lpe
9+
mov $0,$1

loda/__init__.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
"""Python implementation of the [LODA Language](https://loda-lang.org/):
2-
an assembly language and computational model for finding integer sequence programs.
1+
"""
2+
Python implementation of the [LODA Language](https://loda-lang.org/):
3+
an assembly language and computational model for integer sequences.
34
4-
This Python package allows you to read and write LODA programs, to evaluate
5-
them to integer sequences, to search for matches in the
6-
[On-Line Encyclopedia of Integer Sequences®](https://www.oeis.org/) (OEIS®),
7-
and to use machine learning from [Tensorflow](https://www.tensorflow.org/)
8-
to find new integer sequence programs.
5+
.. include:: ./documentation.md
96
"""

loda/documentation.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
This Python package allows you to read and write LODA programs, to evaluate
2+
them to integer sequences, to search for matches in the
3+
[OEIS](https://www.oeis.org/) database,
4+
and to use machine learning from [Tensorflow](https://www.tensorflow.org/)
5+
to generate new integer sequence programs.
6+
7+
## Installation
8+
9+
You need Python 3.7 or higher. To install the dependencies for LODA, run these commands:
10+
11+
```bash
12+
python3 -m venv env
13+
source env/bin/activate
14+
pip install -r requirements.txt
15+
```
16+
17+
## Getting Started
18+
19+
LODA programs are stored in `*.asm` files. Below you can find the example program `fibonacci.asm` which
20+
computes the Fibonacci numbers. For a comprehensive overview of the language, see the [LODA Language Specification](https://loda-lang.org/spec/).
21+
22+
```asm
23+
; A000045: Fibonacci numbers.
24+
mov $3,1
25+
lpb $0
26+
sub $0,1
27+
mov $2,$1
28+
add $1,$3
29+
mov $3,$2
30+
lpe
31+
mov $0,$1
32+
```
33+
34+
Check out the [sub-modules](#header-submodules) for working with LODA programs.
35+
36+
## Development
37+
38+
To execute the tests, run the following command:
39+
40+
```bash
41+
nose2 tests -v
42+
```

loda/lang/__init__.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
"""LODA Language core. Model and (de-)serialization of programs.
1+
"""
2+
Load and save programs.
3+
4+
This module contains the in-memory representation of LODA programs. You can use it to load and save programs,
5+
and to inspect and manipulate their structure programmatically. You can load a program from an `*.asm` file
6+
as follows:
7+
8+
>>> from loda.lang import Program
9+
>>>
10+
>>> with open("fibonacci.asm", "r") as file:
11+
>>> program = Program(file.read())
12+
>>> print(program)
13+
14+
To save it, just write the string representation of the program to another file.
15+
To inspect and manipulate programs, see the `loda.lang.program.Program` class.
216
"""
317

418
from .operand import Operand

loda/lang/operand.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Operand model and serialization."""
4+
35
from enum import Enum
46

57

68
class Operand:
7-
"""Operand model and (de-)serialization.
8-
9+
"""
910
Operands consist of two members: `type` (enum) and `value` (int).
1011
The operand's type can be either `CONSTANT`, `DIRECT` memory access,
1112
or `INDIRECT` memory access. Constants are plain integers. Direct

loda/lang/operation.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Operation model and serialization."""
4+
35
from enum import Enum
46
from .operand import Operand
57

68

79
class Operation:
8-
"""Operation model and (de-)serialization.
9-
10+
"""
1011
Operations have the following structure: `<type> <target>,<source> ; <comment>`
1112
where the operation type is an enum consisting of three-letter operation names, and
1213
target and source are `loda.lang.operand.Operand`s. Depending on their type,

loda/lang/program.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Program model and serialization."""
4+
35
from .operation import Operation
46

57

68
class Program:
7-
"""Program model and (de-)serialization.
9+
"""
10+
Programs are essentially a list of `Operation`s.
811
912
>>> # Constructing programs from operations:
1013
>>> p1 = Program()
@@ -21,7 +24,7 @@ class Program:
2124
div $0,$2
2225
"""
2326

24-
operations: list[Operation]
27+
operations: list
2528
"""Operations of this program."""
2629

2730
def __init__(self, program_str=None):

loda/ml/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
"""Machine learning integration for LODA using Tensorflow/Keras."""
1+
"""
2+
Machine learning integration.
3+
"""

loda/ml/keras.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class Model(tf.keras.Model):
1111

12-
def __init__(self, vocabulary: list[str],
12+
def __init__(self, vocabulary: list,
1313
embedding_dim: int = 256, num_rnn_units: int = 1024):
1414

1515
super().__init__(self)
@@ -75,10 +75,10 @@ def __init__(self, model: Model, temperature: float = 1.0):
7575
dense_shape=[model.get_vocab_size()])
7676
self.prediction_mask = tf.sparse.to_dense(sparse_mask)
7777

78-
def ids_to_tokens_str(self, ids) -> list[str]:
78+
def ids_to_tokens_str(self, ids) -> list:
7979
return [t.numpy().decode("utf-8") for t in self.model.ids_to_tokens(ids)]
8080

81-
def ids_to_programs(self, ids) -> list[Program]:
81+
def ids_to_programs(self, ids) -> list:
8282
return util.split_program(util.tokens_to_program(self.ids_to_tokens_str(ids)))
8383

8484
def program_to_input_ids(self, program: Program, num_lanes: int = 1):

loda/ml/util.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from loda.lang import Operand, Operation, Program
22
from loda.oeis import ProgramCache
33

4-
import typing
54
import random
65

76

8-
def program_to_tokens(program: Program) -> typing.Tuple[list[str], list[str]]:
7+
def program_to_tokens(program: Program):
98
tokens = []
109
vocab = set()
1110
for op in program.operations:
@@ -18,7 +17,7 @@ def program_to_tokens(program: Program) -> typing.Tuple[list[str], list[str]]:
1817
vocab.add(type)
1918
vocab.add(target)
2019
vocab.add(source)
21-
return (tokens, sorted(vocab))
20+
return tokens, sorted(vocab)
2221

2322

2423
def tokens_to_operation(type_tok: str, target_tok: str, source_tok: str):
@@ -31,7 +30,7 @@ def tokens_to_operation(type_tok: str, target_tok: str, source_tok: str):
3130
return Operation() # nop
3231

3332

34-
def tokens_to_program(tokens: list[str]) -> Program:
33+
def tokens_to_program(tokens: list) -> Program:
3534
i = 0
3635
program = Program()
3736
while i+2 < len(tokens):
@@ -90,7 +89,7 @@ def merge_programs(program_cache: ProgramCache, num_programs: int = -1, num_ops_
9089
return merged, num_samples, sample_size
9190

9291

93-
def split_program(program: Program) -> list[Program]:
92+
def split_program(program: Program) -> list:
9493
splitted = []
9594
next = Program()
9695
for op in program.operations:

loda/oeis/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
Search integer sequences from the
3-
[On-Line Encyclopedia of Integer Sequences®](https://www.oeis.org/) (OEIS®).
2+
[OEIS](https://www.oeis.org/) integration.
3+
4+
This module contains and integration with the [On-Line Encyclopedia of Integer Sequences®](https://www.oeis.org/).
45
"""
56

67
from .prefix_index import PrefixIndex

loda/oeis/prefix_index.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Prefix index for searching integer sequences."""
4+
35
import copy
46
import os.path
57
import re
@@ -105,7 +107,7 @@ def refine_match(self, match: Match, term: int) -> bool:
105107
match.end_index = new_end
106108
return new_start < new_end
107109

108-
def get_match_ids(self, match: Match) -> list[int]:
110+
def get_match_ids(self, match: Match) -> list:
109111
ids = [self.__index[i].id for i in range(
110112
match.start_index, match.end_index)]
111113
ids.extend(match.finished_ids)

loda/oeis/program_cache.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Program cache for integer sequence programs."""
4+
35
import os.path
46

57
from loda.lang import Program

loda/oeis/sequence.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
"""Integer sequence model."""
4+
35
import functools
46

57

loda/runtime/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
"""Runtime of the LODA language. Includes an interpreter for evaluating programs."""
1+
"""
2+
Evaluate programs to integer sequences.
3+
4+
.. include:: ./documentation.md
5+
"""
26

37
from .interpreter import Interpreter
48
from .operations import *

loda/runtime/documentation.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
You can evaluate LODA programs to an integer sequence as follows:
2+
3+
```python
4+
from loda.lang import Program
5+
from loda.runtime import Interpreter
6+
7+
with open("fibonacci.asm", "r") as file:
8+
program = Program(file.read())
9+
interpreter = Interpreter()
10+
print(interpreter.eval_to_seq(program, num_terms=10))
11+
```
12+
13+
The result is a pair, where the first entry is the list of computed Fibonacci numbers:
14+
15+
```python
16+
([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], 305)
17+
```

0 commit comments

Comments
 (0)