-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALib.affine
More file actions
158 lines (123 loc) · 5.99 KB
/
Copy pathALib.affine
File metadata and controls
158 lines (123 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell
//
// aggregate-library (aLib) — AffineScript conformer.
//
// The reference shape of every aLib overlap operation in canonical
// AffineScript. This is the *extension-layer* conformer: AffineScript is one
// cube, but it is presented through six faces (RattleScript / JaffaScript /
// PseudoScript / LucidScript / CafeScripto and the canonical face). Every face
// lowers to exactly these definitions, so conforming once here conforms for
// all six faces at the library level — the same-cube property (invariant-path
// `faces` profile) lifted from syntax to the standard-library overlap.
//
// Operations match specs/<category>/<op>.md one-for-one. Affine annotations on
// the collection operations record where ownership/borrowing make AffineScript
// a *stronger* conformer than a GC'd reference impl (see ALIB-INTEGRATION.md).
//
// Grouped by category with `// === … ===` banners rather than nested modules
// so the file parses under the canonical grammar without relying on a module
// system the stdlib has not yet settled.
// === Arithmetic ===
// Number is modelled as Float (the spec's "Number"); Std-lib overflow/NaN
// behaviour is implementation-defined per each spec's Edge Cases.
/// add: Number, Number -> Number (commutative, associative, identity 0)
fn add(a: Float, b: Float) -> Float { a + b }
/// subtract: Number, Number -> Number (identity 0; not commutative)
fn subtract(a: Float, b: Float) -> Float { a - b }
/// multiply: Number, Number -> Number (commutative, associative, identity 1, zero 0)
fn multiply(a: Float, b: Float) -> Float { a * b }
/// divide: Number, Number -> Number (division by zero is implementation-defined)
fn divide(a: Float, b: Float) -> Float { a / b }
/// modulo: Number, Number -> Number (remainder; sign follows the spec vectors)
fn modulo(a: Float, b: Float) -> Float { a % b }
/// negate: Number -> Number (negate(negate(a)) = a)
fn negate(a: Float) -> Float { 0.0 - a }
/// absolute: Number -> Number (absolute(a) >= 0)
fn absolute(a: Float) -> Float { if a < 0.0 { 0.0 - a } else { a } }
/// max: Number, Number -> Number (commutative, idempotent)
fn max(a: Float, b: Float) -> Float { if a > b { a } else { b } }
/// min: Number, Number -> Number (commutative, idempotent)
fn min(a: Float, b: Float) -> Float { if a < b { a } else { b } }
// === Comparison ===
// Return Bool. Operate on values that are Copy/borrowable (numbers here);
// for affine element types these would take `&T` to avoid consuming.
/// equal: A, A -> Bool (reflexive, symmetric, transitive)
fn equal(a: Float, b: Float) -> Bool { a == b }
/// not_equal: A, A -> Bool (not_equal(a, b) = !equal(a, b))
fn not_equal(a: Float, b: Float) -> Bool { a != b }
/// less_than: A, A -> Bool (irreflexive, transitive)
fn less_than(a: Float, b: Float) -> Bool { a < b }
/// less_equal: A, A -> Bool (reflexive, transitive)
fn less_equal(a: Float, b: Float) -> Bool { a <= b }
/// greater_than: A, A -> Bool (irreflexive, transitive)
fn greater_than(a: Float, b: Float) -> Bool { a > b }
/// greater_equal: A, A -> Bool (reflexive, transitive)
fn greater_equal(a: Float, b: Float) -> Bool { a >= b }
// === Logical ===
/// and: Bool, Bool -> Bool (commutative, associative, identity true, zero false)
fn and(a: Bool, b: Bool) -> Bool { a && b }
/// or: Bool, Bool -> Bool (commutative, associative, identity false, zero true)
fn or(a: Bool, b: Bool) -> Bool { a || b }
/// not: Bool -> Bool (involution: not(not(a)) = a)
fn not(a: Bool) -> Bool { !a }
// === Conditional ===
/// if_then_else: Bool, T, T -> T
/// Both branches have type T; under affine typing exactly one is consumed and
/// the other is dropped — no leak, no double-use.
fn if_then_else<T>(cond: Bool, then_val: T, else_val: T) -> T {
if cond { then_val } else { else_val }
}
// === String ===
/// concat: String, String -> String (associative, identity "")
fn concat(a: String, b: String) -> String { a ++ b }
/// length: String -> Int (borrows; length("") = 0)
fn str_length(s: &String) -> Int { String.length(s) }
/// reverse: String -> String (involution: reverse(reverse(s)) = s)
fn reverse(s: String) -> String { String.reverse(s) }
/// substring: String, Int, Int -> String (s, start, len)
fn substring(s: &String, start: Int, len: Int) -> String {
String.substring(s, start, len)
}
// === Collection ===
// This is where AffineScript conforms *more strongly* than a GC reference:
// ownership makes "consumed exactly once" and "no iterator invalidation"
// type-level guarantees rather than runtime conventions.
/// map: [T], (T -> U) -> [U]
/// Affine: `list` is moved; each element is consumed exactly once by `f`.
/// The source is inaccessible after the call (no use-after-map).
fn map<T: affine, U>(list: [T], f: T -> U) -> [U] {
let result = [];
for x in list { // list moved; x consumed once
result = result ++ [f(x)];
}
result
}
/// filter: [T], (&T -> Bool) -> [T]
/// Affine: predicate *borrows* (`&T`) so it can inspect without consuming;
/// kept elements are moved into the result, dropped elements are released.
fn filter<T: affine>(list: [T], pred: &T -> Bool) -> [T] {
let result = [];
for x in list {
if pred(&x) { result = result ++ [x]; } // x moved into result, else dropped
}
result
}
/// fold: [T], Acc, ((Acc, T) -> Acc) -> Acc
/// Affine: accumulator threaded by ownership; each element consumed once.
fn fold<T: affine, Acc>(list: [T], acc: Acc, f: (Acc, T) -> Acc) -> Acc {
let state = acc;
for x in list { state = f(state, x); }
state
}
/// contains: &[T], &T -> Bool (requires Eq; borrows both, consumes neither)
fn contains<T: Eq>(list: &[T], x: &T) -> Bool {
for y in list { // borrowing iteration
if y == x { return true; }
}
false
}
/// length: &[T] -> Int (borrows; length([]) = 0)
fn length<T>(list: &[T]) -> Int { Array.length(list) }
/// empty: &[T] -> Bool (empty(xs) = (length(xs) == 0))
fn empty<T>(list: &[T]) -> Bool { Array.length(list) == 0 }