Types and widths
Types come before names. string name, never name: string.
Everyday types#
fn nihil_agit() → void {
print "done"
}
main {
let string name ← "Marcus"
let i32 aetas ← 30
let bool ready ← true
let list<int> empty ← empty
let int ∪ none missing ← null
print name, aetas, ready, empty, missing
nihil_agit()
}| Type | Is |
|---|---|
string | text |
i32 | a signed integer |
bool | true / false — true / false |
void | the return type of a function that yields no value |
none | absence — the other half of an optional |
Do not confuse void with empty. void is a type, used as a return
annotation. empty is a value: the empty collection, which is why it seeds
lists and tensors above. Binding empty to something that is not a collection
is a compile error.
Numeric widths#
Name the width in type position. These are the numeric types and the raw-bit types — the same list as Math in the ether.
faber convert --to en — English reader surfacemain {
let i32 signed ← -7
let u8 byte ← 255
let f64 wide ← 1.5
print signed, byte, wide
}faber convert --to la — canonical Faberincipit {
sit i32 signed ← -7
sit u8 byte ← 255
sit f64 wide ← 1.5
nota signed, byte, wide
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด i32 signed ← -7
กำหนด u8 byte ← 255
กำหนด f64 wide ← 1.5
บันทึก signed, byte, wide
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 i32 signed ← -7
设 u8 byte ← 255
设 f64 wide ← 1.5
显示 signed, byte, wide
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 i32 signed ← -7
設為 u8 byte ← 255
設為 f64 wide ← 1.5
註記 signed, byte, wide
}faber convert --to vi — Vietnamesebắt_đầu {
đặt i32 signed ← -7
đặt u8 byte ← 255
đặt f64 wide ← 1.5
ghi_chú signed, byte, wide
}faber convert --to ar — Arabicبداية {
ليكن i32 signed ← -7
ليكن u8 byte ← 255
ليكن f64 wide ← 1.5
اعرض signed, byte, wide
}faber convert --to hi — Hindiआरंभ {
बैठा i32 signed ← -7
बैठा u8 byte ← 255
बैठा f64 wide ← 1.5
दिखाओ signed, byte, wide
}| Family | Widths |
|---|---|
| Signed | i8 i16 i32 i64 |
| Unsigned | u8 u16 u32 u64 |
| Decimal | d64 |
| Unbounded integer | inf |
| Floating | f16 bf16 f32 f64 |
| Raw bits | b8 b16 b32 b64 |
Raw bits are not numbers. A b8 takes bit operators at its own width and has no
arithmetic; x + x on a b8 is rejected, and a number converts to it only with
an explicit ↦ to the same width.
Lists and tables#
main {
let list<int> numeri ← [1, 2, 3]
let map<string, int> aetates ← { "Marcus": 30, "Julia": 28 }
print numeri[0]
print aetates["Marcus"]
print numeri.length()
}list<T> holds many of one type. A map maps keys to values — the
{ "key": value } form above is inline JSON
ascribed to a map type, not a separate map literal, which is why it uses :
where Faber's typed construction uses =. Both take
type holes and unions as their parameters:
faber convert --to en — English reader surfacemain {
let list<string ∪ int> mixed ← [1, "two", 3]
let _ inferred ← [1, 2, 3]
print mixed, inferred
}faber convert --to la — canonical Faberincipit {
sit lista<textus ∪ numerus> mixed ← [1, "two", 3]
sit _ inferred ← [1, 2, 3]
nota mixed, inferred
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด รายการ<ข้อความ ∪ จํานวน> mixed ← [1, "two", 3]
กำหนด _ inferred ← [1, 2, 3]
บันทึก mixed, inferred
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 列表<文本 ∪ 整数> mixed ← [1, "two", 3]
设 _ inferred ← [1, 2, 3]
显示 mixed, inferred
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 列表<文字 ∪ 整數> mixed ← [1, "two", 3]
設為 _ inferred ← [1, 2, 3]
註記 mixed, inferred
}faber convert --to vi — Vietnamesebắt_đầu {
đặt danh_sách<văn_bản ∪ số> mixed ← [1, "two", 3]
đặt _ inferred ← [1, 2, 3]
ghi_chú mixed, inferred
}faber convert --to ar — Arabicبداية {
ليكن قائمة<نص ∪ عدد> mixed ← [1, "two", 3]
ليكن _ inferred ← [1, 2, 3]
اعرض mixed, inferred
}faber convert --to hi — Hindiआरंभ {
बैठा सूची<पाठ ∪ संख्या> mixed ← [1, "two", 3]
बैठा _ inferred ← [1, 2, 3]
दिखाओ mixed, inferred
}Tensors#
A tensor carries an element type and a shape. A flat list converts to a
shaped tensor with ↦ (flat ↦ tf32[2, 3]); empty is the empty collection.
faber convert --to en — English reader surfacemain {
let list<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
let tensor<f32, [2, 3]> m ← flat ↦ tensor<f32, [2, 3]>
print m.mean()
}faber convert --to la — canonical Faberincipit {
sit lista<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
sit tensor<f32, [2, 3]> m ← flat ↦ tensor<f32, [2, 3]>
nota m.media()
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด รายการ<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
กำหนด เทนเซอร์<f32, [2, 3]> m ← flat ↦ เทนเซอร์<f32, [2, 3]>
บันทึก m.ค่าเฉลี่ย()
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 列表<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
设 张量<f32, [2, 3]> m ← flat ↦ 张量<f32, [2, 3]>
显示 m.均值()
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 列表<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
設為 張量<f32, [2, 3]> m ← flat ↦ 張量<f32, [2, 3]>
註記 m.平均值()
}faber convert --to vi — Vietnamesebắt_đầu {
đặt danh_sách<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
đặt ten_xo<f32, [2, 3]> m ← flat ↦ ten_xo<f32, [2, 3]>
ghi_chú m.trung_bình()
}faber convert --to ar — Arabicبداية {
ليكن قائمة<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
ليكن موتر<f32, [2, 3]> m ← flat ↦ موتر<f32, [2, 3]>
اعرض m.المتوسط()
}faber convert --to hi — Hindiआरंभ {
बैठा सूची<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
बैठा टेंसर<f32, [2, 3]> m ← flat ↦ टेंसर<f32, [2, 3]>
दिखाओ m.माध्य()
}Shape sugar#
tf32[2, 3] is sugar for tensor<f32, [2, 3]> — same type, shorter to read in
a signature.
faber convert --to en — English reader surfacefn medium(tf32[2, 3] m) → f32 {
return m.mean()
}
main {
let list<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
let tf32[2, 3] m ← flat ↦ tensor<f32, [2, 3]>
print medium(m)
}faber convert --to la — canonical Faberfunctio medium(tf32[2, 3] m) → f32 {
redde m.media()
}
incipit {
sit lista<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
sit tf32[2, 3] m ← flat ↦ tensor<f32, [2, 3]>
nota medium(m)
}faber convert --to th-TH — Thaiฟังก์ชัน medium(tf32[2, 3] m) → f32 {
คืน m.ค่าเฉลี่ย()
}
เริ่ม {
กำหนด รายการ<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
กำหนด tf32[2, 3] m ← flat ↦ เทนเซอร์<f32, [2, 3]>
บันทึก medium(m)
}faber convert --to zh-Hans — Simplified Chinese函数 medium(tf32[2, 3] m) → f32 {
返回 m.均值()
}
入口 {
设 列表<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
设 tf32[2, 3] m ← flat ↦ 张量<f32, [2, 3]>
显示 medium(m)
}faber convert --to zh-Hant — Traditional Chinese函式 medium(tf32[2, 3] m) → f32 {
傳回 m.平均值()
}
入口 {
設為 列表<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
設為 tf32[2, 3] m ← flat ↦ 張量<f32, [2, 3]>
註記 medium(m)
}faber convert --to vi — Vietnamesehàm medium(tf32[2, 3] m) → f32 {
trả m.trung_bình()
}
bắt_đầu {
đặt danh_sách<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
đặt tf32[2, 3] m ← flat ↦ ten_xo<f32, [2, 3]>
ghi_chú medium(m)
}faber convert --to ar — Arabicدالة medium(tf32[2, 3] m) → f32 {
أعد m.المتوسط()
}
بداية {
ليكن قائمة<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
ليكن tf32[2, 3] m ← flat ↦ موتر<f32, [2, 3]>
اعرض medium(m)
}faber convert --to hi — Hindiफलन medium(tf32[2, 3] m) → f32 {
लौटाओ m.माध्य()
}
आरंभ {
बैठा सूची<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
बैठा tf32[2, 3] m ← flat ↦ टेंसर<f32, [2, 3]>
दिखाओ medium(m)
}Vectors#
vector<T, size N> is a fixed-width vector, with vf32[N] as its sugar; the long form needs the size marker (vector<f32, 4> is retired). Build one
by converting a literal — the conversion is what fixes the width.
faber convert --to en — English reader surfacemain {
let vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ vector<f32, size 4>
print v
}faber convert --to la — canonical Faberincipit {
sit vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ vector<f32, magnitudo 4>
nota v
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ เวกเตอร์<f32, ขนาด 4>
บันทึก v
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ 向量<f32, 维度 4>
显示 v
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ 向量<f32, 尺寸 4>
註記 v
}faber convert --to vi — Vietnamesebắt_đầu {
đặt vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ vectơ<f32, kích_thước 4>
ghi_chú v
}faber convert --to ar — Arabicبداية {
ليكن vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ متجه<f32, حجم 4>
اعرض v
}faber convert --to hi — Hindiआरंभ {
बैठा vf32[4] v ← [1.0, 2.0, 3.0, 4.0] ↦ सदिश<f32, आकार 4>
दिखाओ v
}Naming a type#
type gives a type another name.
faber convert --to en — English reader surfacetype Nomen = string
type Puncta = list<int>
main {
let Nomen n ← "Marcus"
let Puncta p ← [1, 2, 3]
print n, p
}faber convert --to la — canonical Fabertypus Nomen = textus
typus Puncta = lista<numerus>
incipit {
sit Nomen n ← "Marcus"
sit Puncta p ← [1, 2, 3]
nota n, p
}faber convert --to th-TH — Thaiชนิดนามแฝง Nomen = ข้อความ
ชนิดนามแฝง Puncta = รายการ<จํานวน>
เริ่ม {
กำหนด Nomen n ← "Marcus"
กำหนด Puncta p ← [1, 2, 3]
บันทึก n, p
}faber convert --to zh-Hans — Simplified Chinese类型 Nomen = 文本
类型 Puncta = 列表<整数>
入口 {
设 Nomen n ← "Marcus"
设 Puncta p ← [1, 2, 3]
显示 n, p
}faber convert --to zh-Hant — Traditional Chinese型別 Nomen = 文字
型別 Puncta = 列表<整數>
入口 {
設為 Nomen n ← "Marcus"
設為 Puncta p ← [1, 2, 3]
註記 n, p
}faber convert --to vi — Vietnamesekiểu_tên Nomen = văn_bản
kiểu_tên Puncta = danh_sách<số>
bắt_đầu {
đặt Nomen n ← "Marcus"
đặt Puncta p ← [1, 2, 3]
ghi_chú n, p
}faber convert --to ar — Arabicنمط Nomen = نص
نمط Puncta = قائمة<عدد>
بداية {
ليكن Nomen n ← "Marcus"
ليكن Puncta p ← [1, 2, 3]
اعرض n, p
}faber convert --to hi — Hindiप्रकार Nomen = पाठ
प्रकार Puncta = सूची<संख्या>
आरंभ {
बैठा Nomen n ← "Marcus"
बैठा Puncta p ← [1, 2, 3]
दिखाओ n, p
}Classes#
class (en class) declares a class. Fields are type-first, like everything
else, and each one is let (fixed after construction) or mut (writable).
For a labeled value with no declared class, use a
record: { x = 1.5, y = 2 }, whose fields are
read-only.
faber convert --to en — English reader surfaceclass Persona {
let string nomen
let int aetas
}
main {
let _ p ← Persona { nomen = "Marcus", aetas = 30 }
print p.nomen, p.aetas
}faber convert --to la — canonical Fabergenus Persona {
sit textus nomen
sit numerus aetas
}
incipit {
sit _ p ← Persona { nomen = "Marcus", aetas = 30 }
nota p.nomen, p.aetas
}faber convert --to th-TH — Thaiชนิด Persona {
กำหนด ข้อความ nomen
กำหนด จํานวน aetas
}
เริ่ม {
กำหนด _ p ← Persona { nomen = "Marcus", aetas = 30 }
บันทึก p.nomen, p.aetas
}faber convert --to zh-Hans — Simplified Chinese类 Persona {
设 文本 nomen
设 整数 aetas
}
入口 {
设 _ p ← Persona { nomen = "Marcus", aetas = 30 }
显示 p.nomen, p.aetas
}faber convert --to zh-Hant — Traditional Chinese類型 Persona {
設為 文字 nomen
設為 整數 aetas
}
入口 {
設為 _ p ← Persona { nomen = "Marcus", aetas = 30 }
註記 p.nomen, p.aetas
}faber convert --to vi — Vietnamesekiểu Persona {
đặt văn_bản nomen
đặt số aetas
}
bắt_đầu {
đặt _ p ← Persona { nomen = "Marcus", aetas = 30 }
ghi_chú p.nomen, p.aetas
}faber convert --to ar — Arabicصنف Persona {
ليكن نص nomen
ليكن عدد aetas
}
بداية {
ليكن _ p ← Persona { nomen = "Marcus", aetas = 30 }
اعرض p.nomen, p.aetas
}faber convert --to hi — Hindiवर्ग Persona {
बैठा पाठ nomen
बैठा संख्या aetas
}
आरंभ {
बैठा _ p ← Persona { nomen = "Marcus", aetas = 30 }
दिखाओ p.nomen, p.aetas
}Related: Bindings · Conversions · Types and values