Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into merge
Browse files Browse the repository at this point in the history
  • Loading branch information
vkobinski committed Jun 18, 2024
2 parents 50c9f65 + dc20905 commit ff1a2bb
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 113 deletions.
8 changes: 7 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"recommendations": [
"rust-lang.rust-analyzer"
"rust-lang.rust-analyzer",
"editorconfig.editorconfig",
"streetsidesoftware.code-spell-checker",
"mkhl.direnv",
"tamasfe.even-better-toml",
"jnoortheen.nix-ide",
"foxundermoon.shell-format"
]
}
4 changes: 0 additions & 4 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@ build:
cd crates/benda; \
maturin develop

<<<<<<< HEAD
run: build
python3 tmp/quicksort.py
python3 tmp/quicksort.py
=======
run_examples:
python -m examples.quicksort
>>>>>>> upstream/master
6 changes: 0 additions & 6 deletions crates/placeholder/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions crates/placeholder/src/main.rs

This file was deleted.

4 changes: 4 additions & 0 deletions examples/future/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# benda/examples/future

Examples of code that we intend to support in the future. Mainly about translating
a subset of Python code to Bend/HVM.
Empty file added examples/future/__init__.py
Empty file.
22 changes: 5 additions & 17 deletions examples/fib.py → examples/future/fib.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
from typing import TypeVar
# from benda import bjit
from benda import bjit, bend

T = TypeVar("T")


def bend(fn: T) -> T:
"""
Decorator that hints the transpiler that a call to the recursive function
should be translated to an inline `bend` block.
This decorator is intended to be defined internally by the benda library.
"""
return fn

# @bjit


@bjit
def fib_recursive(n: int) -> int:
"""
Calculates the nth Fibonacci number using a recursive approach.
Expand All @@ -28,16 +17,15 @@ def fib_recursive(n: int) -> int:
case _:
return fib_recursive(n - 2) + fib_recursive(n - 1)

# @bjit


@bjit
def fib_iterative(n: int) -> int:
"""
Calculates the nth Fibonacci number using an iterative (tail-recursive) approach.
"""

# This decorator does nothing, it just hints the benda transpiler to
# use a `bend` block for the recursive computation.
# This decorator hints the Benda transpiler to use a `bend` block for the
# recursive computation.
@bend
def go(a: int, b: int, n: int) -> int:
if n == 0:
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.py → examples/future/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def simple() -> u24:
if __name__ == "__main__":
translated_simple = bjit(simple)
print(simple())
print(translated_simple())
print(translated_simple())
12 changes: 8 additions & 4 deletions examples/tree.py → examples/future/tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from dataclasses import dataclass
import random

from benda import bjit, u24


@dataclass
class MyLeaf:
value: u24
Expand All @@ -26,14 +29,15 @@ def sum_tree(tree: MyTree) -> u24:
return sum_tree(left) + sum_tree(right)


def gen_tree(depth: int, n: int) -> MyTree:
def gen_tree(depth: int) -> MyTree:
n = random.randint(0, 1000)
if depth == 0:
return MyLeaf(value=n)
else:
return MyNode(left=gen_tree(depth-1, n-1), right=gen_tree(depth-1, n+1))
return MyNode(left=gen_tree(depth-1), right=gen_tree(depth-1))


if __name__ == "__main__":
tree = gen_tree(4, 10)
tree = gen_tree(4)
print(tree)
print(sum_tree(tree))
print(sum_tree(tree))
33 changes: 33 additions & 0 deletions examples/quicksort.bend
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
type MyTree = Leaf | (Node lft val rgt)

# Parallel QuickSort
<<<<<<< HEAD
(Sort List/Nil) = MyTree/Leaf
(Sort(List/Cons head tail)) =
((Part head tail) λmin λmax
Expand All @@ -15,6 +16,28 @@ type MyTree = Leaf | (Node lft val rgt)
# 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)))
=======
(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))

# --- Test ---
>>>>>>> upstream/master

# Generates a random list with xorshift
(Rnd 0 state) = List/Nil
Expand All @@ -25,12 +48,22 @@ let state = (^ state(<< state 5))
(List/Cons state(Rnd(- n 1) state))

# Sums all elements in a concatenation tree
<<<<<<< HEAD
(Sum MyTree/Leaf) = 0
(Sum(MyTree/Node lft val rgt)) = (+ val(+ (Sum lft)(Sum rgt)))

# Sorts and sums n random numbers
# (Main) =
# (Sum(Sort(Rnd 10 20)))
=======
(Sum List/Nil) = 0
(Sum (List/Cons head tail)) = (+ head (Sum tail))

# Sorts and sums n random numbers
# Result: 12741879
(Main) =
(Sum (Sort (Rnd 0x100 1)))
>>>>>>> upstream/master

# Use an argument from cli
# (Main n) = (Sum (Sort (Rnd (<< 1 n) 1)))
115 changes: 40 additions & 75 deletions examples/quicksort.py
Original file line number Diff line number Diff line change
@@ -1,104 +1,69 @@
from typing import TypeVar, Generic
from dataclasses import dataclass
from typing import TypeVar
import random

from .quicksort_mock import u24, List, List_Cons, List_Nil, mock_sort

T = TypeVar("T")

# --> Our Target

# import benda
# from benda import u24

# book = benda.load_book_from_file("./quicksort.bend")
# List_Nil = book.adts.List.Nil
# List_Cons = book.adts.List.Cons
# MyTree_Leaf = book.adts.MyTree.Leaf
# MyTree_Node = book.adts.MyTree.Node


# -> Mock

u24 = int


@dataclass
class List_Nil:
pass


@dataclass
class List_Cons(Generic[T]):
value: T
tail: "List[T]"


List = List_Nil | List_Cons[T]


@dataclass
class MyTree_Leaf:
pass


@dataclass
class MyTree_Node(Generic[T]):
value: T
left: "MyTree[T]"
right: "MyTree[T]"


MyTree = MyTree_Leaf | MyTree_Node[T]

def gen_list(n: int, max_value: int = 0xffffff) -> list[u24]:
"""Generates an array of random u24 numbers with the `random` package"""
result: list[u24] = []
for _ in range(n):
result.append(u24(random.randint(0, max_value)))
return result

def gen_list(n: int, max_value: int) -> List[u24]:
if n <= 0:
return List_Nil()
else:
value = random.randint(0, max_value)
return List_Cons(u24(value), gen_list(n-1, max_value))

def to_cons_list(xs: list[int]) -> List[u24]:
"""Converts a Python list to a Bend cons-list"""
result = List_Nil()

def print_tree(tree: MyTree[u24]):
match tree:
case MyTree_Leaf():
pass
case MyTree_Node(value, left, right):
print(value)
print_tree(left)
print_tree(right)
hi = len(xs)
if hi == 0:
return result

while hi > 0:
hi -= 1
result = List_Cons(u24(xs[hi]), result)

def list_to_tree(list: List[u24]) -> MyTree[u24]:
match list:
case List_Nil():
return MyTree_Leaf()
case List_Cons(value, tail):
return MyTree_Node(value, MyTree_Leaf(), list_to_tree(tail))
return result


def mock_sum(tree: MyTree[u24]) -> u24:
match tree:
case MyTree_Leaf():
return u24(0)
case MyTree_Node(value, left, right):
return value + mock_sum(left) + mock_sum(right)
def from_cons_list(xs: List[u24]) -> list[u24]:
"""Converts a Bend cons-list to a Python list"""
result: list[u24] = []
while True:
match xs:
case List_Nil():
return result
case List_Cons(value, tail):
result.append(value)
xs = tail


def main():
numbers = gen_list(7, 1000)
print(numbers)
data = gen_list(10, 1000)
print("Data: ", data)

# tree = benda.run(book.defs.Sort, [numbers])
# tree = book.defs.Sort(numbers)
expected = sorted(data)
print("Expected:", expected)

tree = list_to_tree(numbers)
print("Values:")
print_tree(tree)
cons_list = to_cons_list(data)

# result = book.run(book.defs.Sum, [tree])
# sorted_res = book.defs.Sort(cons_list)
# sorted_arr = from_cons_list(sorted_res)
# print("Result: ", sorted_arr)

result = mock_sum(tree)
print("Result:", result)
mocked_sorted = mock_sort(cons_list)
mocked_sorted_arr = from_cons_list(mocked_sorted)
print("Mocked: ", mocked_sorted_arr)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit ff1a2bb

Please sign in to comment.