cli: add explicit static function parameters
Bind typed source functions with exact effect contracts during deterministic specialization; retain nominal operation identity and ordinary AIR verification. Add spec/RFC/grammar, checked Quick Reference examples, source/AIR conformance, package/hosted CLI tests, scheduler/GC coverage and documentation budgets.
This commit is contained in:
1 parent
57603860df
commit
f02f4bd55b
72 files changed
+969
-25
No files matched your search
@@ -0,0 +1,10 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module demo.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] {
|
||||
return F(move value);
|
||||
}
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn identity>(42u64); }
|
||||
@@ -0,0 +1,3 @@
|
||||
module app.algorithm
|
||||
import app.policy;
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E0006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn unused<F: fn(u64) -> u64 effects [nonexistent]>() -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
struct Key { id: u64 }
|
||||
fn hash(key: ref<Key>) -> u64 effects [] { return key.id; }
|
||||
fn observe<K, H: fn(ref<K>) -> u64 effects []>(key: ref<K>) -> u64 effects [] { return H(key); }
|
||||
fn main() -> u64 effects [] { let key = Key { id: 42 }; return observe::<Key, fn hash>(ref key); }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn unused<F: fn(ref<u64>) -> ref<u64> effects []>() -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn run<F: fn(u64) -> u64 effects []>(x: u64) -> u64 effects [] { return F(x); }
|
||||
fn main() -> u64 effects [] { return run::<fn own.take>(42); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
struct Bad<F: fn(u64) -> u64 effects []> { stored: F }
|
||||
fn main() -> u64 effects [] { let bad = Bad::<fn identity> { stored: 42 }; return 42; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// equivalent: static_functions/copy.air
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn identity>(42u64); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
struct Policy<T, F: fn(Policy<T, fn F>) -> u64 effects []> { value: T }
|
||||
fn selected<T>(value: Policy<T, fn selected::<T>>) -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { let value = Policy::<u64, fn selected::<u64>> { value: 42 }; return selected::<u64>(value); }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn unused<F: fn(u64) -> u64 effects [io.write, io.write]>() -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn run<F: fn(u64) -> u64 effects [mem.alloc]>() -> u64 effects [mem.alloc] { return F(42u64); }
|
||||
fn main() -> u64 effects [mem.alloc] { return run::<fn identity>(); }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected() -> u64 effects [task.wait, task.spawn] { return 42; }
|
||||
fn run<F: fn() -> u64 effects [task.spawn, task.wait]>() -> u64 effects [task.spawn, task.wait] { return F(); }
|
||||
fn main() -> u64 effects [task.spawn, task.wait] { return run::<fn selected>(); }
|
||||
@@ -0,0 +1,10 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// grant: out=io.write
|
||||
// stdout: 42
|
||||
module test.static_functions
|
||||
fn write(out: cap<io.write>, data: slice<u8>) -> void effects [io.write] { io.stdout_write(out, data) or trap; }
|
||||
fn run<F: fn(cap<io.write>, slice<u8>) -> void effects [io.write]>(out: cap<io.write>) -> u64 effects [io.write] { F(out, b"42"); return 42; }
|
||||
fn main(out: cap<io.write>) -> u64 effects [io.write] { return run::<fn write>(out); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected(value: u64) -> u64 effects [io.write] { return value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn selected>(42u64); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn bad<F: fn(u64) -> u64 effects []>() -> u64 effects [] { let value = F; return 42; }
|
||||
fn main() -> u64 effects [] { return bad::<fn identity>(); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn F(value: u64) -> u64 effects [] { return value; }
|
||||
fn unused<F: fn(u64) -> u64 effects []>() -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn forward<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return apply::<T, fn F>(move value); }
|
||||
fn main() -> u64 effects [] { return forward::<u64, fn identity>(42u64); }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<F: fn(u64) -> u64 effects []>() -> u64 effects [] { return 42; }
|
||||
fn run<F: fn(u64) -> u64 effects []>() -> u64 effects [] { return apply::<fn F::<u64>>(); }
|
||||
fn main() -> u64 effects [] { return run::<fn identity>(); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn id<T>(value: T) -> T effects [] { return move value; }
|
||||
fn main() -> u64 effects [] { return id::<fn identity>(42u64); }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn identity<T>(value: T) -> T effects [] { return move value; }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn identity::<u64>>(42u64); }
|
||||
@@ -0,0 +1,10 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// module: app.policy=source/static_functions/policy.aji.inc
|
||||
// module: app.algorithm=source/static_functions/algorithm.aji.inc
|
||||
module test.static_functions
|
||||
import app.policy;
|
||||
import app.algorithm;
|
||||
fn main() -> u64 effects [] { return app.algorithm.apply::<u64, fn app.policy.identity>(42u64); }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn unused<A, B, C, D, E, F, G, H, I: fn(u64) -> u64 effects []>() -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E2101 E2101
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn set(value: mut<u64>) -> void effects [] { *value = 42; }
|
||||
fn apply<F: fn(mut<u64>) -> void effects []>(value: mut<u64>) -> void effects [] { F(value); }
|
||||
fn main() -> u64 effects [] { var value: u64 = 0; ref loan = value; apply::<fn set>(mut value); return *loan; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn bad<F: fn(u64) -> u64 effects []>() -> u64 effects [] { let F = 42u64; return F; }
|
||||
fn main() -> u64 effects [] { return bad::<fn identity>(); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E3101
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected(out: cap<io.write>) -> u64 effects [io.write] { return 42; }
|
||||
fn run<F: fn(cap<io.write>) -> u64 effects [io.write]>(out: cap<io.write>) -> u64 effects [] { return F(out); }
|
||||
fn main(out: cap<io.write>) -> u64 effects [] { return run::<fn selected>(out); }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn set(value: mut<u64>) -> void effects [] { *value = 42; }
|
||||
fn apply<T, F: fn(mut<T>) -> void effects []>(value: mut<T>) -> void effects [] { F(value); }
|
||||
fn main() -> u64 effects [] { var value: u64 = 0; apply::<u64, fn set>(mut value); return value; }
|
||||
@@ -0,0 +1,10 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
struct Policy<T, F: fn(T) -> T effects []> { value: T }
|
||||
fn read<T>(value: Policy<T, fn apply::<T, fn identity>>) -> T effects [] { return value.value; }
|
||||
fn main() -> u64 effects [] { let p = Policy::<u64, fn apply::<u64, fn identity>> { value: 42 }; return read::<u64>(p); }
|
||||
@@ -0,0 +1,12 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// grant: heap=mem.alloc:8
|
||||
module test.static_functions
|
||||
fn identity(value: own<u64>) -> own<u64> effects [] { return move value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
||||
let input = own.new(42u64, heap) or trap;
|
||||
let output = apply::<own<u64>, fn identity>(move input); return own.take(move output);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E3002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected(value: own<u64>) -> u64 effects [] { return 42; }
|
||||
fn run<F: fn(own<u64>) -> u64 effects []>(value: own<u64>) -> u64 effects [] { return F(move value); }
|
||||
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let value = own.new(42u64, heap) or trap; return run::<fn selected>(move value); }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E2002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: own<u64>) -> own<u64> effects [] { return move value; }
|
||||
fn apply<F: fn(own<u64>) -> own<u64> effects []>(value: own<u64>) -> u64 effects [] {
|
||||
let output = F(move value); own.take(move value); return own.take(move output);
|
||||
}
|
||||
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] { let value = own.new(42u64, heap) or trap; return apply::<fn identity>(move value); }
|
||||
@@ -0,0 +1,2 @@
|
||||
module app.policy
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn other(value: u64) -> u64 effects [] { return value; }
|
||||
struct Policy<T, F: fn(T) -> T effects []> { value: T }
|
||||
fn read(value: Policy<u64, fn identity>) -> u64 effects [] { return value.value; }
|
||||
fn main() -> u64 effects [] { let value = Policy::<u64, fn other> { value: 42 }; return read(value); }
|
||||
@@ -0,0 +1,11 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T, n: usize) -> T effects [] {
|
||||
if n == 0usize { return F(move value); }
|
||||
return apply::<T, fn F>(move value, n - 1usize);
|
||||
}
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn identity>(42u64, 4usize); }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
struct Policy<F: fn(Policy<fn F>) -> u64 effects []> { value: u64 }
|
||||
fn selected(value: Policy<fn selected>) -> u64 effects [] { return value.value; }
|
||||
fn apply<F: fn(Policy<fn F>) -> u64 effects []>(value: Policy<fn F>) -> u64 effects [] { return F(value); }
|
||||
fn main() -> u64 effects [] { let value = Policy::<fn selected> { value: 42 }; return apply::<fn selected>(value); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn apply<F: fn(u64) -> u64 effects []>() -> u64 effects [] { return F::<u64>(42u64); }
|
||||
fn main() -> u64 effects [] { return apply::<fn identity>(); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7006
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn bad<F: fn(u64) -> u64 effects []>(F: u64) -> u64 effects [] { return 42; }
|
||||
fn main() -> u64 effects [] { return bad::<fn identity>(0u64); }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, u64>(42u64); }
|
||||
@@ -0,0 +1,16 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
struct Policy<T, F: fn(T) -> T effects []> { value: T }
|
||||
enum Result<T, F: fn(T) -> T effects []> { ok(Policy<T, fn F>), none }
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn read<T, F: fn(T) -> T effects []>(value: Policy<T, fn F>) -> T effects [] {
|
||||
let (item) = struct.unpack(move value); return F(move item);
|
||||
}
|
||||
fn main() -> u64 effects [] {
|
||||
let value = Policy::<u64, fn identity> { value: 42 };
|
||||
let result = Result::<u64, fn identity>::ok(value);
|
||||
match result { Result::<u64, fn identity>::ok(item) => { return read::<u64, fn identity>(item); }, Result::<u64, fn identity>::none => { trap; } }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// module: app.policy=source/static_functions/policy.aji.inc
|
||||
// module: app.algorithm=source/static_functions/algorithm.aji.inc
|
||||
module test.static_functions
|
||||
import app.algorithm;
|
||||
fn main() -> u64 effects [] { return app.algorithm.apply::<u64, fn app.policy.identity>(42u64); }
|
||||
@@ -0,0 +1,6 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn unknown>(42u64); }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: reject E7002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
struct Policy<T, F: fn(T) -> T effects []> { value: T }
|
||||
fn unused<T>(value: Policy<T, fn missing>) -> void effects [] { trap; }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,11 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// schedules: 12
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn run<F: fn(u64) -> u64 effects []>() -> u64 effects [task.spawn, task.wait] {
|
||||
scope s { let t = task.spawn(s, F, 42u64); return task.join(move t); }
|
||||
}
|
||||
fn main() -> u64 effects [task.spawn, task.wait] { return run::<fn identity>(); }
|
||||
@@ -0,0 +1,12 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// schedules: 12
|
||||
module test.static_functions
|
||||
fn identity(value: u64) -> u64 effects [] { return value; }
|
||||
fn worker<T: Copy>(value: T) -> T effects [] { return value; }
|
||||
fn run<F: fn(u64) -> u64 effects []>() -> u64 effects [task.spawn, task.wait] {
|
||||
scope s { let t = task.spawn(s, F, 42u64); return task.join(move t); }
|
||||
}
|
||||
fn main() -> u64 effects [task.spawn, task.wait] { return run::<fn worker::<u64>>(); }
|
||||
@@ -0,0 +1,9 @@
|
||||
// expect: reject E2101
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn read(value: ref<u64>) -> u64 effects [] { return *value; }
|
||||
fn run<F: fn(ref<u64>) -> u64 effects []>() -> u64 effects [task.spawn, task.wait] {
|
||||
var value: u64 = 42; scope s { ref loan = value; let t = task.spawn(s, F, loan); value = 0; return task.join(move t); }
|
||||
}
|
||||
fn main() -> u64 effects [task.spawn, task.wait] { return run::<fn read>(); }
|
||||
@@ -0,0 +1,11 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
// schedules: 12
|
||||
module test.static_functions
|
||||
fn read(value: ref<u64>) -> u64 effects [] { return *value; }
|
||||
fn run<F: fn(ref<u64>) -> u64 effects []>() -> u64 effects [task.spawn, task.wait] {
|
||||
var value: u64 = 42; var result: u64 = 0; { ref loan = value; scope s { let t = task.spawn(s, F, loan); result = task.join(move t); } } value = 0; return result;
|
||||
}
|
||||
fn main() -> u64 effects [task.spawn, task.wait] { return run::<fn read>(); }
|
||||
@@ -0,0 +1,10 @@
|
||||
// expect: reject E4002
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18; docs/spec/04-concurrency.md §35
|
||||
module test.static_functions
|
||||
fn worker(value: ref<file<write>>) -> u64 effects [] { return 42; }
|
||||
fn run<F: fn(ref<file<write>>) -> u64 effects []>(value: ref<file<write>>) -> u64 effects [task.spawn, task.wait] {
|
||||
scope s { let t = task.spawn(s, F, value); return task.join(move t); }
|
||||
}
|
||||
fn bad(value: ref<file<write>>) -> u64 effects [task.spawn, task.wait] { return run::<fn worker>(value); }
|
||||
fn main() -> u64 effects [] { return 42; }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected() -> u64 effects [] { return 42; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn selected>(42u64); }
|
||||
@@ -0,0 +1,8 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
struct Key { id: u64 }
|
||||
fn selected(key: mut<Key>) -> u64 effects [] { return key.id; }
|
||||
fn run<F: fn(ref<Key>) -> u64 effects []>(key: ref<Key>) -> u64 effects [] { return F(key); }
|
||||
fn main() -> u64 effects [] { let key = Key { id: 42 }; return run::<fn selected>(ref key); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected(value: u32) -> u64 effects [] { return value as u64; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn selected>(42u64); }
|
||||
@@ -0,0 +1,7 @@
|
||||
// expect: reject E7005
|
||||
// phase: 6
|
||||
// spec: docs/spec/15-source-language.md §18
|
||||
module test.static_functions
|
||||
fn selected(value: u64) -> u32 effects [] { return 42; }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn main() -> u64 effects [] { return apply::<u64, fn selected>(42u64); }
|
||||
@@ -0,0 +1,23 @@
|
||||
; expect: run
|
||||
; result: 42
|
||||
; phase: 6
|
||||
; spec: docs/spec/15-source-language.md §18; docs/spec/07a-air-instructions.md call
|
||||
module test.static_functions
|
||||
air 0
|
||||
lang 0.1
|
||||
features [effects]
|
||||
fn @identity(%value: u64) -> u64
|
||||
effects []
|
||||
entry:
|
||||
ret %value
|
||||
fn @apply(%value: u64) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%result = call @identity(%value)
|
||||
ret %result
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%value = const.u64 42
|
||||
%result = call @apply(%value)
|
||||
ret %result
|
||||
@@ -0,0 +1,17 @@
|
||||
; expect: reject E3101
|
||||
; phase: 6
|
||||
; spec: docs/spec/15-source-language.md §18; docs/spec/05-effects-and-capabilities.md
|
||||
module test.static_functions
|
||||
air 0
|
||||
lang 0.1
|
||||
features [effects]
|
||||
fn @selected() -> u64
|
||||
effects [io.write]
|
||||
entry:
|
||||
%result = const.u64 42
|
||||
ret %result
|
||||
fn @apply() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%result = call @selected()
|
||||
ret %result
|
||||
@@ -0,0 +1,17 @@
|
||||
; expect: reject E3002
|
||||
; phase: 6
|
||||
; spec: docs/spec/15-source-language.md §18; docs/spec/03-memory-model.md
|
||||
module test.static_functions
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, ownership, resources]
|
||||
fn @selected(%value: own<u64>) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%result = const.u64 42
|
||||
ret %result
|
||||
fn @apply(%value: own<u64>) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%result = call @selected(%value)
|
||||
ret %result
|
||||
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Static operations through source, AIR, packages and hosted execution; fuel bounds runs."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
REPO = Path(__file__).resolve().parents[2]
|
||||
AJI = REPO / "zig-out/bin/aji"
|
||||
checks = 0
|
||||
|
||||
|
||||
def cli(*args, code=0):
|
||||
global checks
|
||||
run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True)
|
||||
assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr)
|
||||
checks += 1
|
||||
return run.stdout
|
||||
|
||||
|
||||
example = REPO / "examples/std/static_functions.aji"
|
||||
cli("check", example)
|
||||
assert json.loads(cli("run", "--fuel", "1000000", example))["value"] == 42
|
||||
cli("report", example)
|
||||
cli("explore", "--max-runs", "8", example)
|
||||
canonical = cli("lower", example)
|
||||
assert canonical == cli("lower", example)
|
||||
assert "call @identity" in canonical and "fn_ref" not in canonical
|
||||
|
||||
with tempfile.TemporaryDirectory(prefix="aji-static-functions-") as directory:
|
||||
root = Path(directory)
|
||||
air = root / "result.air"
|
||||
air.write_text(canonical)
|
||||
cli("check", air)
|
||||
assert json.loads(cli("run", "--fuel", "1000000", air))["value"] == 42
|
||||
result = root / "hosted.json"
|
||||
assert cli("run", "--profile", "hosted", "--fuel", "1000000", "--result-file", result, example) == ""
|
||||
assert json.loads(result.read_text())["value"] == 42
|
||||
entry, algo, policy, manifest = (root / name for name in ("main.aji", "algorithm.aji", "policy.aji", "aji.toml"))
|
||||
manifest.write_text('[package]\nname = "static"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.algorithm = "algorithm.aji"\napp.policy = "policy.aji"\n')
|
||||
algo.write_text('''module app.algorithm
|
||||
struct Policy<T, F: fn(T) -> T effects []> { value: T }
|
||||
fn apply<T, F: fn(T) -> T effects []>(value: T) -> T effects [] { return F(move value); }
|
||||
fn read<T, F: fn(T) -> T effects []>(value: Policy<T, fn F>) -> T effects [] {
|
||||
let (item) = struct.unpack(move value); return apply::<T, fn F>(move item);
|
||||
}
|
||||
''')
|
||||
policy_text = '''module app.policy
|
||||
fn identity<T: Copy>(value: T) -> T effects [] { return value; }
|
||||
fn other<T: Copy>(value: T) -> T effects [] { return value; }
|
||||
'''
|
||||
policy.write_text(policy_text)
|
||||
imports = "import app.algorithm;\nimport app.policy;\n"
|
||||
body = '''fn main() -> u64 effects [] {
|
||||
let value = app.algorithm.Policy::<u64, fn app.policy.identity::<u64>> { value: 42 };
|
||||
return app.algorithm.read::<u64, fn app.policy.identity::<u64>>(value);
|
||||
}
|
||||
'''
|
||||
entry.write_text("module app.entry\n" + imports + body)
|
||||
assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest))["value"] == 42
|
||||
cli("report", "--manifest", manifest)
|
||||
cli("explore", "--max-runs", "8", "--manifest", manifest)
|
||||
package_air = cli("lower", "--manifest", manifest)
|
||||
entry.write_text("module app.entry\nimport app.policy;\nimport app.algorithm;\n" + body)
|
||||
assert cli("lower", "--manifest", manifest) == package_air
|
||||
# Equal layouts and signatures do not erase operation identity.
|
||||
entry.write_text("module app.entry\n" + imports + body.replace("read::<u64, fn app.policy.identity", "read::<u64, fn app.policy.other"))
|
||||
error = json.loads(cli("check", "--manifest", manifest, code=1))
|
||||
assert error["code"] == "E7005" and Path(error["file"]).name == "main.aji", error
|
||||
assert "identity" in error["found"] and "other" in error["expected"]
|
||||
# Selecting an operation cannot bypass direct-import visibility.
|
||||
entry.write_text("module app.entry\nimport app.algorithm;\n" + body)
|
||||
error = json.loads(cli("check", "--manifest", manifest, code=1))
|
||||
assert error["code"] == "E7002" and Path(error["file"]).name == "main.aji"
|
||||
entry.write_text("module app.entry\n" + imports + body)
|
||||
# Template-body failures retain the operation's provider filename.
|
||||
policy.write_text(policy_text.replace("return value;", "assert(false); return value;"))
|
||||
record = json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, code=4))
|
||||
assert record["source_location"]["file"] == "policy.aji"
|
||||
policy.write_text(policy_text.replace("return value;", "return missing(value);"))
|
||||
error = json.loads(cli("check", "--manifest", manifest, code=1))
|
||||
assert error["code"] == "E7002" and error["file"] == "policy.aji"
|
||||
# Contracts reject consistently at all CLI entry points, before execution.
|
||||
policy.write_text("module app.policy\nfn identity<T>(value: T) -> bool effects [] { return true; }\n")
|
||||
for command in ("check", "lower", "report", "run", "explore"):
|
||||
error = json.loads(cli(command, "--manifest", manifest, code=1))
|
||||
assert error["code"] == "E7005" and Path(error["file"]).name == "main.aji", error
|
||||
assert error["parameter"] == "F" and error["expected"] == "fn(u64) -> u64 effects []"
|
||||
assert error["found"] == "fn(u64) -> bool effects []"
|
||||
# Recursive function-argument resolution is bounded before body expansion.
|
||||
params = "T, F: fn(T) -> T effects []"
|
||||
declaration = f"fn apply<{params}>(value: T) -> T effects [] {{ return F(move value); }}\n"
|
||||
entry.write_text("module app.entry\nfn identity(value: u64) -> u64 effects [] { return value; }\n" + declaration +
|
||||
"fn main() -> u64 effects [] { return apply::<u64, fn identity>(42u64); }\n")
|
||||
assert json.loads(cli("run", "--fuel", "1000000", entry))["value"] == 42
|
||||
argument = "fn identity"
|
||||
for _ in range(70):
|
||||
argument = f"fn apply::<u64, {argument}>"
|
||||
entry.write_text("module app.entry\nfn identity(value: u64) -> u64 effects [] { return value; }\n" + declaration +
|
||||
f"fn main() -> u64 effects [] {{ return apply::<u64, {argument}>(42u64); }}\n")
|
||||
error = json.loads(cli("check", entry, code=1))
|
||||
assert error["code"] == "E7006", error
|
||||
|
||||
print(f"static function CLI integration passed ({checks} checks)")
|
||||
Reference in new issue
Block a user