Skip to content
This repository was archived by the owner on May 7, 2024. It is now read-only.

Commit 81f6bad

Browse files
committed
feat: add macros for creating lambdas with language constructs
1 parent af5972e commit 81f6bad

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/core/lang/macros.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#[macro_export]
2+
macro_rules! lift {
3+
($x:expr) => {{
4+
|vm| (vm, $x)
5+
}};
6+
}
7+
8+
#[macro_export]
9+
macro_rules! nbr {
10+
($x:expr) => {{
11+
|vm| nbr(vm, $x)
12+
}};
13+
}
14+
15+
#[macro_export]
16+
macro_rules! rep {
17+
($init:expr, $fun:expr) => {{
18+
|vm| rep(vm, $init, $fun)
19+
}};
20+
}
21+
22+
#[macro_export]
23+
macro_rules! foldhood {
24+
($init:expr, $aggr:expr, $expr:expr) => {{
25+
|vm| foldhood(vm, $init, $aggr, $expr)
26+
}};
27+
}
28+
29+
#[macro_export]
30+
macro_rules! foldhood_plus {
31+
($init:expr, $aggr:expr, $expr:expr) => {{
32+
|vm| foldhood_plus(vm, $init, $aggr, $expr)
33+
}};
34+
}
35+
36+
#[macro_export]
37+
macro_rules! mux {
38+
($cond:expr, $th:expr, $el:expr) => {{
39+
|vm| mux(vm, $cond, $th, $el)
40+
}};
41+
}
42+
43+
#[macro_export]
44+
macro_rules! mid {
45+
() => {{
46+
|vm| mid(vm)
47+
}};
48+
}

src/core/lang/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod lang;
22
pub mod execution;
33
pub mod builtins;
4+
pub mod macros;
45
mod test;

src/core/lang/test/by_equivalence.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod by_equivalence {
66
use crate::core::lang::test::utils::{assert_equivalence, fully_connected_topology_map};
77
use crate::core::vm::round_vm::RoundVM;
88

9+
910
struct Fixture {
1011
exec_order: Vec<i32>,
1112
nbrs: HashMap<i32, Vec<i32>>,
@@ -22,6 +23,57 @@ mod by_equivalence {
2223
}
2324
}
2425

26+
#[cfg(test)]
27+
mod macros {
28+
use crate::core::lang::test::by_equivalence::by_equivalence::Fixture;
29+
use crate::core::lang::test::utils::assert_equivalence;
30+
use crate::{nbr, mid, rep, lift, foldhood_plus};
31+
use crate::core::lang::lang::{mid, nbr, rep};
32+
use crate::core::lang::builtins::{foldhood_plus, mux};
33+
use crate::core::sensor_id::sensor;
34+
use crate::core::vm::round_vm::RoundVM;
35+
36+
#[test]
37+
fn macros() {
38+
let fixture = Fixture::new();
39+
40+
let program_1 = nbr!(mid!());
41+
let program_2 = |vm| nbr(vm, |vm| mid(vm));
42+
43+
assert_equivalence(fixture.exec_order, fixture.nbrs, program_1, program_2);
44+
}
45+
46+
#[test]
47+
fn gradient() {
48+
let fixture = Fixture::new();
49+
fn is_source(vm: RoundVM) -> (RoundVM, bool) {
50+
let val = vm.local_sense::<bool>(&sensor("source")).unwrap().clone();
51+
(vm, val)
52+
}
53+
54+
55+
let gradient_1 =
56+
rep!(
57+
lift!(f64::INFINITY),
58+
|vm, d| {
59+
mux(
60+
vm,
61+
is_source,
62+
lift!(0.0),
63+
foldhood_plus!(
64+
lift!(f64::INFINITY),
65+
|a, b| a.min(b),
66+
|vm| {
67+
let (vm_, val) = nbr(vm, lift!(d));
68+
(vm_, val + 1.0)
69+
}
70+
)
71+
)
72+
}
73+
);
74+
}
75+
}
76+
2577
#[test]
2678
fn foldhood_multiple_nbrs() {
2779
let fixture = Fixture::new();

0 commit comments

Comments
 (0)