Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bend ADTS in Python #10

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
77222a1
Represent Benda in Python
vkobinski Jun 15, 2024
0bdd2ca
Bend ADTS in Python
vkobinski Jun 15, 2024
f5d0c63
Bend Types
vkobinski Jun 15, 2024
8eace62
Bend IO
vkobinski Jun 16, 2024
513cbfb
fmt
vkobinski Jun 16, 2024
807cb9a
fmt
vkobinski Jun 16, 2024
1681edd
fmt
vkobinski Jun 16, 2024
b5479e2
Fixed bug
vkobinski Jun 16, 2024
d5611df
Bend has IO now
vkobinski Jun 17, 2024
4009e0c
__str__ functions
vkobinski Jun 17, 2024
50c9f65
Print Term
vkobinski Jun 18, 2024
ff1a2bb
Merge remote-tracking branch 'upstream/master' into merge
vkobinski Jun 18, 2024
5d8d1ca
Working match with one case
vkobinski Jun 20, 2024
1405203
Remove CtrEnum
vkobinski Jun 20, 2024
65acef4
Running quicksort example
vkobinski Jun 21, 2024
f084a40
Syntax
vkobinski Jun 21, 2024
f037280
Pattern Matching with desestructuring
vkobinski Jun 24, 2024
77cc0d3
fmt
vkobinski Jun 24, 2024
e6454e8
Fixed Term parsing
vkobinski Jun 24, 2024
f64b130
Fixed Term parsing
vkobinski Jun 24, 2024
00037de
Merge branch 'master' of https://github.com/vkobinski/benda-main
vkobinski Jun 24, 2024
46390b6
Nix
vkobinski Jun 24, 2024
656135a
to_list() for HVM result
vkobinski Jun 24, 2024
500d2cb
New example
vkobinski Jun 24, 2024
24cc6f4
Added all sorts from Bend repo
vkobinski Jun 24, 2024
184c179
Sat Solver example
vkobinski Jun 25, 2024
d408a30
HVM output
vkobinski Jun 26, 2024
286d395
TermParser
vkobinski Jun 26, 2024
e6d6e37
Term parser
vkobinski Jun 26, 2024
b4f6a9d
Fixed type
vkobinski Jun 26, 2024
834c50f
Refactoring
vkobinski Jun 26, 2024
3cc5276
Writing FFI documentation
vkobinski Jun 28, 2024
9c8cb8a
Adjusting examples
vkobinski Jun 28, 2024
d9fadb7
Fixed to_list
vkobinski Jun 28, 2024
fcc857d
Docs
vkobinski Jun 28, 2024
30a97f4
Superpositions
vkobinski Jun 28, 2024
01f00ca
Fixed examples
vkobinski Jun 29, 2024
636fa25
Refactor
vkobinski Jun 29, 2024
75375c0
Refactoring
vkobinski Jun 29, 2024
a14d533
Bend algorithm addition
vkobinski Jul 4, 2024
58bc444
Bend dpll
vkobinski Jul 5, 2024
13db0f1
flake update
vkobinski Jul 5, 2024
deaca8b
flake
vkobinski Jul 5, 2024
16f4601
Flake lock
vkobinski Jul 5, 2024
75f1f6b
Solved TODOs
vkobinski Jul 6, 2024
f8593af
Refactoring
vkobinski Jul 6, 2024
7cc5300
Refactoring
vkobinski Jul 6, 2024
7d41d42
Documentation and load_book
vkobinski Jul 7, 2024
3f15ff4
removed result
vkobinski Jul 7, 2024
3d1df1b
Fixed examples
vkobinski Jul 8, 2024
8694b93
Fixed examples
vkobinski Jul 8, 2024
c0fd358
Documentation
vkobinski Jul 8, 2024
f923f90
Documentation
vkobinski Jul 8, 2024
e702f23
Documentation
vkobinski Jul 8, 2024
99f63bd
Update README.md
steinerkelvin Jul 8, 2024
6aa07af
Merge branch 'master' of https://github.com/vkobinski/benda-main
vkobinski Jul 8, 2024
686fd71
Documentation
vkobinski Jul 9, 2024
9d08667
Benda package
vkobinski Jul 9, 2024
c3c70cd
Package
vkobinski Jul 9, 2024
b8dd273
Package
vkobinski Jul 9, 2024
4406f91
Package
vkobinski Jul 9, 2024
d01949e
Packaging
vkobinski Jul 9, 2024
ab05d80
Package
vkobinski Jul 9, 2024
e3d642a
Package
vkobinski Jul 9, 2024
1898aad
Package
vkobinski Jul 9, 2024
492a0ba
Updated Bend dependency
vkobinski Jul 12, 2024
dca076c
Refactoring
vkobinski Jul 12, 2024
6722efc
Refactoring
vkobinski Jul 16, 2024
ea2e367
Refactoring
vkobinski Jul 16, 2024
c7a2b31
Refactoring
vkobinski Jul 16, 2024
70cdeae
Writing tests
vkobinski Jul 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ __pycache__
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.vscode/
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ rust-fmt:
cargo fmt --all

build:
cd crates/benda; \
maturin develop --release

debug:
cd crates/benda; \
maturin develop

run_examples:
python -m examples.quicksort
python -m examples.radix_sort
bend run examples/radix_sort.bend
python -m examples.insertion_sort
bend run examples/insertion_sort.bend
python -m examples.bitonic_sort
bend run examples/bitonic_sort.bend
python -m examples.bubble_sort
bend run examples/bubble_sort.bend
132 changes: 95 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,95 @@
[HVM]: https://github.com/HigherOrderCo/hvm
[Bend]: https://github.com/HigherOrderCo/bend

This is in conceptual stage.
[Read the Docs](/docs/FFI.md)

This is in MVP stage.

## Example

```py
from dataclasses import dataclass
from benda import bjit, u24

@dataclass
class Leaf:
value: u24 # native HVM machine integer

@dataclass
class Node:
left: 'Tree'
right: 'Tree'

Tree = Node | Leaf

# The `bjit` decorator will introspect and translate the function to HVM/Bend
# code, replacing it with a wrapper that converts the Python-level types of the
# inputs and result value, Numba-style.

@bjit
def sum_tree(tree: Tree) -> u24:
match tree:
case Leaf(value=value):
return value
case Node(left=left, right=right):
return sum_tree(left) + sum_tree(right)
case _:
raise TypeError("Invalid type for tree")

# Alternatively, you can opt to use Python big integers and other primitives,
# they will be translated to the equivalent representations automatically.

@dataclass
class Leaf2:
value: int
import benda
import random

book = benda.load_book("""
(Sort List/Nil) = List/Nil
(Sort(List/Cons head tail)) =
((Part head tail) λmin λmax
let lft=(Sort min)
let rgt=(Sort max)
(Concat lft(List/Cons head rgt)))

# Partitions a list in two halves, less-than-p and greater-than-p
(Part p List/Nil) = λt(t List/Nil List/Nil)
(Part p(List/Cons head tail)) = (Push(> head p) head(Part p tail))

# Pushes a value to the first or second list of a pair
(Push 0 x pair) = (pair λmin λmax λp(p(List/Cons x min) max))
(Push _ x pair) = (pair λmin λmax λp(p min(List/Cons x max)))

(Concat List/Nil tail) = tail
(Concat(List/Cons head tail) xs2) =
(List/Cons head(Concat tail xs2))
""")

List = book.adts.List

def gen_list(n: int, max_value: int = 0xffffff) -> list[int]:
result: list[int] = []
for _ in range(n):
result.append(random.randint(0, max_value))
return result


def to_cons_list(xs: list[int]):
result = List.Nil()

hi = len(xs)
if hi == 0:
return result

while hi > 0:
hi -= 1
result = List.Cons(xs[hi], result)

return result

def print_cons_list(list):
while True:
match list:
case List.Cons.type(value, tail):
print(value, end=", ")
list = tail
case List.Nil.type():
break


data = gen_list(5, 1000)
cons_list = to_cons_list(data)
book.set_cmd(benda.BendRuntime.Cuda)
sorted_list = book.defs.Sort(cons_list)
sorted_list = sorted_list.to_adt(book.adts.List)
print_cons_list(sorted_list)
```

## Install

To install the current release:
```
$ pip install benda
```

## Development

Dependencies:

- Python 3.11+
- Rust
- C compiler
- maturin

### Getting dependencies with Nix (optional)

- Install Nix with [Determinate Nix Installer]

```sh
Expand All @@ -58,8 +104,20 @@ class Leaf2:

- You can run `nix develop` to enter a shell with the dependencies installed.

- You can use [`direnv`][direnv] to automatically load the environment when you
enter the project directory.
### Building

- Create and activate a Python virtual environment.
- e.g. with
```
python -m venv .venv
source .venv/bin/activate
```

- Run `make` to build the project and install the `benda` package in the virtual
environment.

<!-- - You can use [`direnv`][direnv] to automatically load the environment when you
enter the project directory. -->

[Determinate Nix Installer]: https://install.determinate.systems
[direnv]: https://direnv.net
Loading