-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPEC.core.scm
More file actions
289 lines (248 loc) · 11 KB
/
SPEC.core.scm
File metadata and controls
289 lines (248 loc) · 11 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
;;; SPEC.core.scm — Eclexia Core Specification
;; SPDX-License-Identifier: MPL-2.0
;; SPDX-FileCopyrightText: 2025 Jonathan D.A. Jewell
;;
;; This file defines the minimal resource lattice and evaluation semantics
;; for Eclexia's resource-aware computation model.
;;;; ============================================================
;;;; SECTION 1: RESOURCE LATTICE
;;;; ============================================================
;; The resource lattice defines the partial ordering of resource types
;; and their relationships. Resources are first-class values with
;; dimensional analysis at the type level.
(define resource-lattice
'((name . "eclexia-resource-lattice")
(version . "1.0.0")
;; Base resource dimensions (form an abelian group under multiplication)
(base-dimensions
. ((time . ((si-unit . "s")
(description . "Duration/latency")
(exponent . (time . 1))))
(energy . ((si-unit . "J")
(description . "Energy consumption")
(exponent . (mass . 1) (length . 2) (time . -2))))
(carbon . ((si-unit . "kgCO2e")
(description . "Carbon dioxide equivalent emissions")
(exponent . (carbon . 1))))
(memory . ((si-unit . "bit")
(description . "Information/memory usage")
(exponent . (information . 1))))))
;; Derived dimensions (computed from base)
(derived-dimensions
. ((power . ((formula . "energy / time")
(si-unit . "W")
(exponent . (mass . 1) (length . 2) (time . -3))))
(carbon-intensity . ((formula . "carbon / energy")
(si-unit . "gCO2e/kWh")
(description . "Grid carbon intensity")))))
;; Resource bounds form a partial order
(bound-ordering
. ((description . "Resource bounds form a meet-semilattice")
(top . unbounded) ;; No constraint
(bottom . impossible) ;; Unsatisfiable
(meet . "min of bounds") ;; Stricter constraint wins
(join . "max of bounds"))) ;; Looser constraint
;; Constraint operators
(constraint-operators
. ((< . "less than bound")
(<= . "at most bound")
(= . "exactly bound")
(>= . "at least (for provides)")
(> . "greater than (for provides)")))))
;;;; ============================================================
;;;; SECTION 2: CORE TYPE SYSTEM
;;;; ============================================================
(define type-system
'((name . "eclexia-type-system")
(version . "1.0.0")
;; Primitive types
(primitives
. ((Unit . "The unit type, single value ()")
(Bool . "Boolean: true | false")
(Int . "Signed 64-bit integer")
(Float . "IEEE 754 double precision")
(String . "UTF-8 string")))
;; Resource-annotated types
(resource-types
. ((syntax . "T @requires: constraints @provides: guarantees")
(example . "Int @requires: energy < 10J, latency < 100ms")
(semantics . "Type carries resource contract")))
;; Dimensional types
(dimensional-types
. ((energy . "Energy = Float @dimension: J")
(time . "Time = Float @dimension: s")
(carbon . "Carbon = Float @dimension: kgCO2e")
(power . "Power = Energy / Time")))
;; Type algebra
(type-rules
. ((dimensional-addition
. "T @dim:D + T @dim:D → T @dim:D (dimensions must match)")
(dimensional-multiplication
. "T @dim:D1 * T @dim:D2 → T @dim:D1*D2")
(dimensional-division
. "T @dim:D1 / T @dim:D2 → T @dim:D1/D2")
(resource-propagation
. "Calling f @requires:R propagates R to caller")))))
;;;; ============================================================
;;;; SECTION 3: ADAPTIVE EVALUATION SEMANTICS
;;;; ============================================================
(define evaluation-semantics
'((name . "eclexia-evaluation")
(version . "1.0.0")
;; Evaluation context carries resource state
(context
. ((components
. ((budget . "Remaining resource allowances")
(shadow-prices . "Marginal values λ for each resource")
(current-usage . "Accumulated resource consumption")
(environment . "Variable bindings")))
(notation . "Γ; B; λ; U ⊢ e ⇓ v; U'")))
;; Core evaluation rules
(rules
. (
;; Literals evaluate without resource cost
(E-LIT
. ((premise . "literal n")
(conclusion . "Γ; B; λ; U ⊢ n ⇓ n; U")
(note . "Literals consume no resources")))
;; Variables lookup in environment
(E-VAR
. ((premise . "x ∈ dom(Γ)")
(conclusion . "Γ; B; λ; U ⊢ x ⇓ Γ(x); U")))
;; Binary operations check dimension compatibility
(E-BINOP
. ((premises
. ("Γ; B; λ; U ⊢ e1 ⇓ v1; U1"
"Γ; B; λ; U1 ⊢ e2 ⇓ v2; U2"
"compatible-dims(op, dim(v1), dim(v2))"))
(conclusion . "Γ; B; λ; U ⊢ e1 op e2 ⇓ apply-op(op, v1, v2); U2")))
;; Function application with resource tracking
(E-APP
. ((premises
. ("Γ; B; λ; U ⊢ e ⇓ fn(x) body @R; U1"
"Γ; B; λ; U1 ⊢ arg ⇓ v; U2"
"Γ[x↦v]; B; λ; U2 ⊢ body ⇓ result; U3"
"satisfies(U3 - U, R)"))
(conclusion . "Γ; B; λ; U ⊢ e(arg) ⇓ result; U3")))
;; Adaptive function: select solution by weighted cost
(E-ADAPTIVE
. ((premises
. ("Γ; B; λ; U ⊢ e ⇓ adaptive fn solutions @R; U1"
"Γ; B; λ; U1 ⊢ args ⇓ vs; U2"
"S = select-solution(solutions, Γ, B, λ)"
"S ≠ ⊥"
"Γ[params↦vs]; B; λ; U2 ⊢ S.body ⇓ result; U3"
"satisfies(U3 - U2, S.provides)"))
(conclusion . "Γ; B; λ; U ⊢ e(args) ⇓ result; U3")
(note . "Solution selected by minimizing weighted cost")))
;; Constraint violation → deterministic error
(E-VIOLATION
. ((premises
. ("Γ; B; λ; U ⊢ e ⇓ v; U'"
"¬ satisfies(U' - U, @requires R)"))
(conclusion . "Γ; B; λ; U ⊢ e ⇓ ResourceViolation(R); U'")
(note . "Deterministic failure, not exception")))))
;; Solution selection algorithm
(solution-selection
. ((algorithm . "weighted-cost-minimization")
(formula . "cost(S) = Σ λ_r × S.provides[r]")
(selection . "argmin_{S ∈ solutions} cost(S) where S.when = true")
(tie-breaker . "first declared solution")
(no-valid-solution . "ResourceViolation: no solution satisfies constraints")))
;; Shadow price semantics
(shadow-prices
. ((definition . "Marginal value of one additional unit of resource r")
(interpretation
. ("λ_energy = value of 1 more Joule"
"λ_time = value of 1 more millisecond"
"λ_carbon = value of 1 gram less CO2"))
(initialization . "λ = (1.0, 1.0, 1.0) by default")
(update-rule . "LP duality or gradient descent (runtime-dependent)")))))
;;;; ============================================================
;;;; SECTION 4: CONSTRAINT CHECKING
;;;; ============================================================
(define constraint-checking
'((name . "eclexia-constraints")
(version . "1.0.0")
;; Constraint syntax
(constraint-forms
. ((@requires . "Input constraint: must hold at entry")
(@provides . "Output guarantee: will hold at exit")
(@optimize . "Objective: minimize/maximize resource")
(@when . "Guard condition for solution applicability")
(@defer_until . "Delay execution until condition")))
;; Constraint validation rules
(validation
. ((at-call-site
. "Check @requires against current budget B")
(at-return
. "Verify actual usage ≤ @provides")
(static-check
. "Type checker verifies dimensional compatibility")
(dynamic-check
. "Runtime tracks actual consumption")))
;; Violation behavior (deterministic)
(violation-semantics
. ((type . "ResourceViolation")
(contents
. ((resource . "Which resource exceeded")
(constraint . "The violated constraint")
(actual . "Actual usage value")
(limit . "The bound that was exceeded")
(location . "Source location of violation")))
(behavior . "Deterministic error, not exception")
(propagation . "Error propagates up call stack")
(recovery . "No automatic recovery; caller must handle")))))
;;;; ============================================================
;;;; SECTION 5: CONFORMANCE REQUIREMENTS
;;;; ============================================================
(define conformance
'((name . "eclexia-conformance")
(version . "1.0.0")
;; Minimum viable implementation
(minimum-requirements
. ((resource-tracking
. "Must track at least: time, energy")
(constraint-checking
. "Must reject @requires violations")
(dimensional-analysis
. "Must reject dimension mismatches at type check")
(adaptive-selection
. "Must select solution by weighted cost")
(deterministic-errors
. "Constraint violations must be deterministic")))
;; Test corpus categories
(test-corpus
. ((valid
. ((description . "Programs that should succeed")
(categories
. ("resource-typed computation"
"adaptive function selection"
"dimensional arithmetic"
"constraint satisfaction"))))
(invalid
. ((description . "Programs that should fail deterministically")
(categories
. ("resource constraint violation"
"dimension mismatch"
"no valid solution available"
"budget exhaustion"))))))
;; Smoke test command
(smoke-test
. ((command . "cargo test && cargo run -- run examples/hello.ecl")
(success-criteria
. ("All unit tests pass"
"hello.ecl runs without error"
"Output includes: Hello, Economics-as-Code!"))))))
;;;; ============================================================
;;;; SECTION 6: REFERENCE
;;;; ============================================================
(define specification-metadata
'((schema . "eclexia.spec/1.0")
(date . "2026-01-01")
(authority . "SPEC.core.scm is authoritative for semantics")
(implementation . "compiler/ and runtime/ implement this spec")
(related-docs
. ("SPECIFICATION.md - full language specification"
"PROOFS.md - formal correctness proofs"
"THEORY.md - theoretical foundations"))))