66[ HVM ] : https://github.com/HigherOrderCo/hvm
77[ Bend ] : https://github.com/HigherOrderCo/bend
88
9- This is in conceptual stage.
9+ [ Read the Docs] ( /docs/FFI.md )
10+
11+ This is in MVP stage.
1012
1113## Example
1214
1315``` py
14- from dataclasses import dataclass
15- from benda import bjit, u24
16-
17- @dataclass
18- class Leaf :
19- value: u24 # native HVM machine integer
20-
21- @dataclass
22- class Node :
23- left: ' Tree'
24- right: ' Tree'
25-
26- Tree = Node | Leaf
27-
28- # The `bjit` decorator will introspect and translate the function to HVM/Bend
29- # code, replacing it with a wrapper that converts the Python-level types of the
30- # inputs and result value, Numba-style.
31-
32- @bjit
33- def sum_tree (tree : Tree) -> u24:
34- match tree:
35- case Leaf(value = value):
36- return value
37- case Node(left = left, right = right):
38- return sum_tree(left) + sum_tree(right)
39- case _:
40- raise TypeError (" Invalid type for tree" )
41-
42- # Alternatively, you can opt to use Python big integers and other primitives,
43- # they will be translated to the equivalent representations automatically.
44-
45- @dataclass
46- class Leaf2 :
47- value: int
16+ import benda
17+ import random
18+
19+ book = benda.load_book("""
20+ (Sort List/Nil) = List/Nil
21+ (Sort(List/Cons head tail)) =
22+ ((Part head tail) λmin λmax
23+ let lft=(Sort min)
24+ let rgt=(Sort max)
25+ (Concat lft(List/Cons head rgt)))
26+
27+ # Partitions a list in two halves, less-than-p and greater-than-p
28+ (Part p List/Nil) = λt(t List/Nil List/Nil)
29+ (Part p(List/Cons head tail)) = (Push(> head p) head(Part p tail))
30+
31+ # Pushes a value to the first or second list of a pair
32+ (Push 0 x pair) = (pair λmin λmax λp(p(List/Cons x min) max))
33+ (Push _ x pair) = (pair λmin λmax λp(p min(List/Cons x max)))
34+
35+ (Concat List/Nil tail) = tail
36+ (Concat(List/Cons head tail) xs2) =
37+ (List/Cons head(Concat tail xs2))
38+ """ )
39+
40+ List = book.adts.List
41+
42+ def gen_list (n : int , max_value : int = 0x ffffff ) -> list[int ]:
43+ result: list[int ] = []
44+ for _ in range (n):
45+ result.append(random.randint(0 , max_value))
46+ return result
47+
48+
49+ def to_cons_list (xs : list[int ]):
50+ result = List.Nil()
51+
52+ hi = len (xs)
53+ if hi == 0 :
54+ return result
55+
56+ while hi > 0 :
57+ hi -= 1
58+ result = List.Cons(xs[hi], result)
59+
60+ return result
61+
62+ def print_cons_list (list ):
63+ while True :
64+ match list :
65+ case List.Cons.type(value, tail):
66+ print (value, end = " , " )
67+ list = tail
68+ case List.Nil.type():
69+ break
70+
71+
72+ data = gen_list(5 , 1000 )
73+ cons_list = to_cons_list(data)
74+ book.set_cmd(benda.BendRuntime.Cuda)
75+ sorted_list = book.defs.Sort(cons_list)
76+ sorted_list = sorted_list.to_adt(book.adts.List)
77+ print_cons_list(sorted_list)
78+ ```
79+
80+ ## Install
81+
82+ To install the current release:
83+ ```
84+ $ pip install benda
4885```
4986
5087## Development
5188
89+ Dependencies:
90+
91+ - Python 3.11+
92+ - Rust
93+ - C compiler
94+ - maturin
95+
96+ ### Getting dependencies with Nix (optional)
97+
5298- Install Nix with [ Determinate Nix Installer]
5399
54100 ``` sh
@@ -58,8 +104,20 @@ class Leaf2:
58104
59105- You can run ` nix develop ` to enter a shell with the dependencies installed.
60106
61- - You can use [ ` direnv ` ] [ direnv ] to automatically load the environment when you
62- enter the project directory.
107+ ### Building
108+
109+ - Create and activate a Python virtual environment.
110+ - e.g. with
111+ ```
112+ python -m venv .venv
113+ source .venv/bin/activate
114+ ```
115+
116+ - Run `make` to build the project and install the `benda` package in the virtual
117+ environment.
118+
119+ <!-- - You can use [`direnv`][direnv] to automatically load the environment when you
120+ enter the project directory. -->
63121
64122[Determinate Nix Installer]: https://install.determinate.systems
65123[direnv]: https://direnv.net
0 commit comments