Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion examples/eval/eval.scm
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@
(@ shift `((lambda (n) (+ n 1)) ,`(0)) `(0) s) body)))
((App t1 t2) `(App ,(@ subst j s t1) ,(@ subst j s t2))))))

(define unshift (lambdas (c t)
(match t
((Var n)
(match (@ leb c n)
((True) `(Var ,(- n 1)))
((False) `(Var ,n))))
((Abs body) `(Abs ,(@ unshift `((lambda (n) (+ n 1)) ,c) body)))
((App t1 t2) `(App ,(@ unshift c t1) ,(@ unshift c t2))))))

(define beta (lambdas (body arg)
(@ shift `((lambda (n) (+ n 1)) ,`(0)) `(0)
(@ unshift `(0)
(@ subst `(0) (@ shift `((lambda (n) (+ n 1)) ,`(0)) `(0) arg) body))))

(define whnf (lambdas (fuel t)
Expand Down
18 changes: 2 additions & 16 deletions examples/eval/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use cortex_m_semihosting::{debug, hprintln};
use panic_halt as _;

use encore_vm::error::ExternError;
use encore_vm::ffi::VmCallable;
use encore_vm::value::GlobalAddress;
use encore_vm::vm::Vm;

#[path = "../../common/qemu_clock.rs"]
Expand All @@ -31,19 +29,6 @@ enum OptionNat {
#[ctor(ctors::SOME)] Some(i32),
}

// step : curried 3-argument call (TEST_ADD is a -> b -> fuel -> Option nat)
fn call3<O: encore_vm::ffi::ValueDecode>(
vm: &mut Vm,
global: GlobalAddress,
a: i32,
b: i32,
fuel: i32,
) -> Result<O, ExternError> {
let f1: VmCallable = vm.call_global(global, (a,))?;
let f2: VmCallable = vm.call_closure(&f1, (b,))?;
vm.call_closure(&f2, (fuel,))
}

#[entry]
fn main() -> ! {
qemu_clock::start(cortex_m::Peripherals::take().unwrap().SYST);
Expand All @@ -52,7 +37,8 @@ fn main() -> ! {
vm.set_clock(qemu_clock::now_ns);

for &(a, b, fuel) in &[(1i32, 1i32, 500i32), (1, 2, 1000), (2, 2, 3000)] {
let result: OptionNat = call3(&mut vm, funcs::TEST_ADD, a, b, fuel)
// TEST_ADD : a -> b -> fuel -> Option nat, uncurried into a 3-ary function.
let result: OptionNat = vm.call_global(funcs::TEST_ADD, (a, b, fuel))
.unwrap_or_else(|e| vm_exit_err(e));

match result {
Expand Down
8 changes: 3 additions & 5 deletions examples/fsm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cortex_m_semihosting::{debug, hprintln};
use panic_halt as _;

use encore_vm::error::ExternError;
use encore_vm::ffi::{VmCallable, VmList};
use encore_vm::ffi::VmList;
use encore_vm::vm::Vm;

encore_vm::encore_program!(env!("OUT_DIR"));
Expand Down Expand Up @@ -51,10 +51,8 @@ fn vm_exit_err(e: ExternError) -> ! {
}

fn run_step(vm: &mut Vm, state: i32, event: Event) -> StepResult {
let partial: VmCallable = vm
.call_global(funcs::STEP, (state,))
.unwrap_or_else(|e| vm_exit_err(e));
vm.call_closure(&partial, (event,))
// STEP : State -> Event -> Pair, uncurried into a 2-ary function.
vm.call_global(funcs::STEP, (state, event))
.unwrap_or_else(|e| vm_exit_err(e))
}

Expand Down
Loading