Rust
HIR projection. The package product path compiles this through Cargo.
Part of the HIR lane. Every panel below is compiler output.
How to read it#
Close to one-for-one with the source. That is the point of this emitter: generated Rust is meant to be read and reviewed, so it keeps the shape of the Faber it came from rather than expanding into something unrecognisable.
Typed tensors#
Builds two shaped matrices, multiplies them, and reduces the product to a scalar. Exercises shape-bearing types and a reduction.
Faber source
faber format --locale en — English reader surfacemain {
const list<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
const list<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
const tensor<f32, []> seed ← vacua
const tensor<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
const tensor<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
const tensor<f32, [2, 4]> product ← a.matmul(b)
const f32 mean ← product.media()
print mean
}faber format --locale la — canonical Faberincipit {
fixum lista<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
fixum lista<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
fixum tensor<f32, []> seed ← vacua
fixum tensor<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
fixum tensor<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
fixum tensor<f32, [2, 4]> product ← a.matmul(b)
fixum f32 mean ← product.media()
nota mean
}faber format --locale th-TH — Thaiเริ่ม {
คงที่ รายการ<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
คงที่ รายการ<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
คงที่ เทนเซอร์<f32, []> seed ← เซตว่าง
คงที่ เทนเซอร์<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
คงที่ เทนเซอร์<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
คงที่ เทนเซอร์<f32, [2, 4]> product ← a.matmul(b)
คงที่ f32 mean ← product.media()
บันทึก mean
}faber format --locale zh-Hans — Simplified Chinese入口 {
常量 列表<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
常量 列表<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
常量 张量<f32, []> seed ← 空集
常量 张量<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
常量 张量<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
常量 张量<f32, [2, 4]> product ← a.matmul(b)
常量 f32 mean ← product.media()
显示 mean
}faber format --locale zh-Hant — Traditional Chinese入口 {
定值 列表<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
定值 列表<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
定值 張量<f32, []> seed ← 空集
定值 張量<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
定值 張量<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
定值 張量<f32, [2, 4]> product ← a.matmul(b)
定值 f32 mean ← product.media()
註記 mean
}faber format --locale vi — Vietnamesebắt_đầu {
hằng danh_sách<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
hằng danh_sách<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
hằng ten_xo<f32, []> seed ← tập_rỗng
hằng ten_xo<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
hằng ten_xo<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
hằng ten_xo<f32, [2, 4]> product ← a.matmul(b)
hằng f32 mean ← product.media()
ghi_chú mean
}faber format --locale ar — Arabicبداية {
ثابت قائمة<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
ثابت قائمة<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
ثابت موتر<f32, []> seed ← فارغ
ثابت موتر<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
ثابت موتر<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
ثابت موتر<f32, [2, 4]> product ← a.matmul(b)
ثابت f32 mean ← product.media()
اعرض mean
}faber format --locale hi — Hindiआरंभ {
स्थिर सूची<f32> flat_a ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
स्थिर सूची<f32> flat_b ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0]
स्थिर टेंसर<f32, []> seed ← खाली
स्थिर टेंसर<f32, [2, 3]> a ← seed.strue(flat_a, [2, 3])
स्थिर टेंसर<f32, [3, 4]> b ← seed.strue(flat_b, [3, 4])
स्थिर टेंसर<f32, [2, 4]> product ← a.matmul(b)
स्थिर f32 mean ← product.media()
दिखाओ mean
}Rust — 10 lines in, 14 out (1.4×)
// Generated by radix - do not edit
// Requires the faber language-runtime crate (add to Cargo.toml):
// faber = { path = "../faber" } # adjust path for your layout
fn main() {
let flat_a: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
let flat_b: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0];
let seed: faber::Tensor<f32> /* tensor<fractus<f32>, []> */ = faber::Tensor::vacua();
let a: faber::Tensor<f32> /* tensor<fractus<f32>, [2, 3]> */ = faber::Tensor::<f32>::structa(flat_a, &{ let t28 = &vec![2, 3]; t28.iter().copied().map(|value| value as i64).collect::<Vec<i64>>() }).expect("tensor structa element count does not match shape");
let b: faber::Tensor<f32> /* tensor<fractus<f32>, [3, 4]> */ = faber::Tensor::<f32>::structa(flat_b, &{ let t35 = &vec![3, 4]; t35.iter().copied().map(|value| value as i64).collect::<Vec<i64>>() }).expect("tensor structa element count does not match shape");
let product: faber::Tensor<f32> /* tensor<fractus<f32>, [2, 4]> */ = { let t40 = &a; t40.matmul(&(b)) }.expect("tensor matmul failed");
let mean: f32 = { let t44 = &product; let c44: f32 = t44.planata().iter().map(|x| *x as f32).sum(); c44 / t44.element_count() as f32 };
println!("{}", faber::display_fractus(mean));
}The error channel#
A function that may fail, and a caller that catches. Shows how the ⇥ channel becomes each target's own error idiom.
Faber source
faber format --locale en — English reader surfacefn divide(int a, int b) → int ⇥ string {
if b ≡ 0 {
throw "division by zero"
}
return a / b
}
main {
do {
print divide(10, 2)
}
catch err {
warn err
}
}faber format --locale la — canonical Faberfunctio divide(numerus a, numerus b) → numerus ⇥ textus {
si b ≡ 0 {
iace "division by zero"
}
redde a / b
}
incipit {
fac {
nota divide(10, 2)
}
cape err {
mone err
}
}faber format --locale th-TH — Thaiฟังก์ชัน divide(จำนวน a, จำนวน b) → จำนวน ⇥ ข้อความ {
ถ้า b ≡ 0 {
โยน "division by zero"
}
คืน a / b
}
เริ่ม {
ทำ {
บันทึก divide(10, 2)
}
จับ err {
เตือน err
}
}faber format --locale zh-Hans — Simplified Chinese函数 divide(整数 a, 整数 b) → 整数 ⇥ 文本 {
如果 b ≡ 0 {
抛错 "division by zero"
}
返回 a / b
}
入口 {
执行 {
显示 divide(10, 2)
}
捕获 err {
警告 err
}
}faber format --locale zh-Hant — Traditional Chinese函式 divide(整數 a, 整數 b) → 整數 ⇥ 文字 {
若 b ≡ 0 {
拋出 "division by zero"
}
傳回 a / b
}
入口 {
執行 {
註記 divide(10, 2)
}
捕捉 err {
警告 err
}
}faber format --locale vi — Vietnamesehàm divide(số a, số b) → số ⇥ văn_bản {
nếu b ≡ 0 {
ném "division by zero"
}
trả a / b
}
bắt_đầu {
làm {
ghi_chú divide(10, 2)
}
bắt err {
cảnh_báo err
}
}faber format --locale ar — Arabicدالة divide(عدد a, عدد b) → عدد ⇥ نص {
إذا b ≡ 0 {
ارم "division by zero"
}
أعد a / b
}
بداية {
افعل {
اعرض divide(10, 2)
}
التقط err {
نبه err
}
}faber format --locale hi — Hindiफलन divide(संख्या a, संख्या b) → संख्या ⇥ पाठ {
यदि b ≡ 0 {
इधरफेंको "division by zero"
}
लौटाओ a / b
}
आरंभ {
करो {
दिखाओ divide(10, 2)
}
पकड़ो err {
चेताओ err
}
}Rust — 13 lines in, 22 out (1.7×)
// Generated by radix - do not edit
// Requires the faber language-runtime crate (add to Cargo.toml):
// faber = { path = "../faber" } # adjust path for your layout
fn divide(a: i64, b: i64) -> Result<i64, String> {
if b == 0 {
return Err(String::from("division by zero"));
};
return Ok((i64::checked_div(a, b).expect("numerus division failed")));
}
fn main() {
match { let ok: Result<(), String> = 'fac_result: {
loop {
println!("{}", match divide(10, 2) { Ok(fac_value) => fac_value, Err(fac_err) => break 'fac_result Err(fac_err) });
break;
}Ok(())
}; ok } { Ok(ok) => ok, Err(err) => {
let err = err;
eprintln!("{}", err);
()} };
}Collections and iteration#
A list folded to a total with itera ex. The plainest possible read on how loops lower.
Faber source
faber format --locale en — English reader surfacefn summa(list<int> numeri) → int {
var int total ← 0
for from numeri const n {
total ← total + n
}
return total
}
main {
const list<int> valores ← [1, 2, 3, 4, 5]
print summa(valores)
}faber format --locale la — canonical Faberfunctio summa(lista<numerus> numeri) → numerus {
varia numerus total ← 0
itera ex numeri fixum n {
total ← total + n
}
redde total
}
incipit {
fixum lista<numerus> valores ← [1, 2, 3, 4, 5]
nota summa(valores)
}faber format --locale th-TH — Thaiฟังก์ชัน summa(รายการ<จำนวน> numeri) → จำนวน {
แปร จำนวน total ← 0
วน ออก numeri คงที่ n {
total ← total + n
}
คืน total
}
เริ่ม {
คงที่ รายการ<จำนวน> valores ← [1, 2, 3, 4, 5]
บันทึก summa(valores)
}faber format --locale zh-Hans — Simplified Chinese函数 summa(列表<整数> numeri) → 整数 {
变量 整数 total ← 0
遍历 取自 numeri 常量 n {
total ← total + n
}
返回 total
}
入口 {
常量 列表<整数> valores ← [1, 2, 3, 4, 5]
显示 summa(valores)
}faber format --locale zh-Hant — Traditional Chinese函式 summa(列表<整數> numeri) → 整數 {
變值 整數 total ← 0
遍歷 取自 numeri 定值 n {
total ← total + n
}
傳回 total
}
入口 {
定值 列表<整數> valores ← [1, 2, 3, 4, 5]
註記 summa(valores)
}faber format --locale vi — Vietnamesehàm summa(danh_sách<số> numeri) → số {
biến số total ← 0
lặp từ numeri hằng n {
total ← total + n
}
trả total
}
bắt_đầu {
hằng danh_sách<số> valores ← [1, 2, 3, 4, 5]
ghi_chú summa(valores)
}faber format --locale ar — Arabicدالة summa(قائمة<عدد> numeri) → عدد {
متغير عدد total ← 0
كرر من numeri ثابت n {
total ← total + n
}
أعد total
}
بداية {
ثابت قائمة<عدد> valores ← [1, 2, 3, 4, 5]
اعرض summa(valores)
}faber format --locale hi — Hindiफलन summa(सूची<संख्या> numeri) → संख्या {
चर संख्या total ← 0
दोहराओ सेवन numeri स्थिर n {
total ← total + n
}
लौटाओ total
}
आरंभ {
स्थिर सूची<संख्या> valores ← [1, 2, 3, 4, 5]
दिखाओ summa(valores)
}Rust — 12 lines in, 17 out (1.4×)
// Generated by radix - do not edit
// Requires the faber language-runtime crate (add to Cargo.toml):
// faber = { path = "../faber" } # adjust path for your layout
fn summa(numeri: Vec<i64>) -> i64 {
let mut total: i64 = 0;
for i1000002 in &(numeri) {
let n = i1000002.clone();
total = i64::checked_add(total, n).expect("numerus overflow");
};
return total;
}
fn main() {
let valores: Vec<i64> = vec![1, 2, 3, 4, 5];
println!("{}", summa(valores.clone()));
}---