-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantity.ml
More file actions
724 lines (632 loc) · 26.6 KB
/
Copy pathquantity.ml
File metadata and controls
724 lines (632 loc) · 26.6 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
(* SPDX-License-Identifier: MPL-2.0 *)
(* SPDX-FileCopyrightText: 2024-2026 Jonathan D.A. Jewell (hyperpolymath) *)
(** Quantity checking for Quantitative Type Theory (QTT).
AffineScript uses a three-point quantity semiring {0, 1, omega} to track
how many times each variable may be used at runtime:
- [QZero] (0): erased at runtime, compile-time only.
- [QOne] (1): linear — must be used exactly once.
- [QOmega] (omega): unrestricted — any number of uses.
This module implements:
1. The quantity semiring (addition, multiplication, ordering).
2. A usage-counting pass that walks expressions/statements.
3. Compatibility checking between declared quantities and observed usage.
4. A top-level entry point [{!check_function_quantities}] for integration
with the type checker.
Reference: AffineScript SPEC.adoc Section 3.2 — QTT semiring specification. *)
open Ast
(* {1 Quantity semiring} *)
(** Addition in the quantity semiring.
The addition table encodes "use in either context":
{v
+ | 0 1 omega
-----+-----------------
0 | 0 1 omega
1 | 1 omega omega
omega| omega omega omega
v} *)
let q_add (q1 : quantity) (q2 : quantity) : quantity =
match (q1, q2) with
| (QZero, q) | (q, QZero) -> q
| (QOne, QOne) -> QOmega
| (QOmega, _) | (_, QOmega) -> QOmega
(** Multiplication in the quantity semiring.
The multiplication table encodes "use under a context scaled by q":
{v
* | 0 1 omega
-----+-----------------
0 | 0 0 0
1 | 0 1 omega
omega| 0 omega omega
v} *)
let q_mul (q1 : quantity) (q2 : quantity) : quantity =
match (q1, q2) with
| (QZero, _) | (_, QZero) -> QZero
| (QOne, q) | (q, QOne) -> q
| (QOmega, QOmega) -> QOmega
(** Ordering: 0 <= 1 <= omega.
[q_le q1 q2] returns [true] when [q1] is usable wherever [q2] is expected.
This means a more restricted quantity can substitute for a less restricted
one (subquantity). *)
let q_le (q1 : quantity) (q2 : quantity) : bool =
match (q1, q2) with
| (QZero, _) -> true
| (_, QOmega) -> true
| (QOne, QOne) -> true
| _ -> false
(* {1 Usage tracking} *)
(** Usage count for a variable during analysis. *)
type usage =
| UZero (** Not used *)
| UOne (** Used exactly once *)
| UMany (** Used two or more times *)
[@@deriving show, eq]
(** Convert a usage count to its corresponding quantity. *)
let usage_to_quantity (u : usage) : quantity =
match u with
| UZero -> QZero
| UOne -> QOne
| UMany -> QOmega
(** Join two usages — computes the upper bound for branching.
When a variable is used in one branch but not another, we take the
maximum usage across branches. This is correct for [if/match] where
exactly one branch executes. *)
let join_usage (u1 : usage) (u2 : usage) : usage =
match (u1, u2) with
| (UMany, _) | (_, UMany) -> UMany
| (UOne, _) | (_, UOne) -> UOne
| (UZero, UZero) -> UZero
(** Add two usages — computes the sum for sequential composition.
When a variable is used in a sequence of statements, the total usage
is the sum: used once then once more means used many times. *)
let add_usage (u1 : usage) (u2 : usage) : usage =
match (u1, u2) with
| (UZero, u) | (u, UZero) -> u
| _ -> UMany
(** Scale a usage by a quantity — implements the q·u operation that
the QTT-orthodox Let rule needs to scale the value-context Γ₁ by
the binder's quantity q.
Per ADR-002, [let x :^q = e1 in e2] is type-checked under
[q·Γ₁ + Γ₂ ⊢ ...]. The semiring action on usages is:
0 · u = UZero (erased — usage is annihilated)
1 · u = u (linear — usage passes through unchanged)
ω · UZero = UZero (no use stays no use)
ω · UOne = UMany (a single use, replicated, becomes many)
ω · UMany = UMany (many stays many)
The interesting case is [ω · UOne = UMany]: it is the rule that
closes BUG-001. A linear variable consumed in [e1] under an
ω-binder gets its usage promoted to UMany, which the
[check_quantity] step then catches as
[LinearVariableUsedMultiple]. *)
let scale_usage (q : quantity) (u : usage) : usage =
match (q, u) with
| (QZero, _) -> UZero
| (QOne, u) -> u
| (QOmega, UZero) -> UZero
| (QOmega, UOne) -> UMany
| (QOmega, UMany) -> UMany
(* {1 Errors} *)
(** Quantity checking errors with precise descriptions. *)
type quantity_error =
| LinearVariableUnused of ident
(** A parameter declared as linear (1) was never used. *)
| LinearVariableUsedMultiple of ident
(** A parameter declared as linear (1) was used more than once. *)
| ErasedVariableUsed of ident
(** A parameter declared as erased (0) was used at runtime. *)
| QuantityMismatch of ident * quantity * usage
(** General mismatch: variable, declared quantity, actual usage. *)
[@@deriving show]
(** Result type carrying a quantity error paired with a source span. *)
type 'a result = ('a, quantity_error * Span.t) Result.t
(** Pretty-print a quantity using the canonical Option C surface form
from ADR-007. Used in error messages so source vocabulary and
diagnostic vocabulary stay aligned. *)
let canonical_quantity_name (q : quantity) : string =
match q with
| QZero -> "@erased"
| QOne -> "@linear"
| QOmega -> "@unrestricted"
(** Pretty-print a usage count in plain English. *)
let usage_in_words (u : usage) : string =
match u with
| UZero -> "never used"
| UOne -> "used exactly once"
| UMany -> "used multiple times"
(** Format a quantity error for human-readable output. Per ADR-007, all
diagnostic text uses the @linear/@erased/@unrestricted vocabulary,
even when the source program used the :1/:0/:ω sugar form. The
user reads consistent words regardless of which surface they wrote. *)
let format_quantity_error (err : quantity_error) : string =
match err with
| LinearVariableUnused id ->
Printf.sprintf
"@linear binding '%s' must be used exactly once, but was never used"
id.name
| LinearVariableUsedMultiple id ->
Printf.sprintf
"@linear binding '%s' must be used exactly once, but was used multiple times"
id.name
| ErasedVariableUsed id ->
Printf.sprintf
"@erased binding '%s' must not be used at runtime"
id.name
| QuantityMismatch (id, q, u) ->
Printf.sprintf
"quantity mismatch for '%s': declared %s but %s"
id.name (canonical_quantity_name q) (usage_in_words u)
(* {1 Quantity environment} *)
(** Quantity environment: maps variable names to declared quantities and
tracks observed usage counts.
We key by [string] (variable name) rather than [Symbol.symbol_id] so
that the environment is self-contained and does not require the symbol
table to be in a particular state during analysis. *)
type env = {
quantities : (string, quantity) Hashtbl.t;
(** Declared quantity for each tracked variable. *)
usages : (string, usage) Hashtbl.t;
(** Current observed usage count for each tracked variable. *)
errors : (quantity_error * Span.t) list ref;
(** Quantity errors accumulated by nested constructs (lambda params).
[infer_usage_expr] returns [unit], so nested violations are
pushed here and drained at the end of [check_function_quantities]. *)
}
(* ============================================================================
Cmd linearity — type-annotation-based quantity inference (Stage 11)
============================================================================ *)
(** [is_cmd_type te] — true when [te] is an application of the built-in
[Cmd] type constructor (e.g. [Cmd Msg], [Cmd Int]).
The [Cmd] type is intrinsically linear: every let-binding whose declared
type annotation is [Cmd _] is automatically treated as QOne (linear) by
the quantity checker. This means the programmer does not need to write
[@linear] explicitly — the type annotation is sufficient.
Note: this only matches AST-level type annotations supplied by the
programmer. Bindings without a type annotation (e.g. [let cmd = cmd_none])
get QOmega as usual. For full intrinsic linearity without annotation, a
post-typecheck elaboration pass would be needed (deferred). *)
let is_cmd_type (te : type_expr) : bool =
match te with
| TyApp ({ name = "Cmd"; _ }, _) -> true
| _ -> false
(** Infer the quantity of a let-binding from its optional type annotation.
Returns [QOne] if the annotation is [Cmd _]; [QOmega] otherwise.
Explicit [el_quantity] / [sl_quantity] annotations always take precedence
(handled at call sites by checking [Option.value] first). *)
let quantity_of_ty_annotation (te_opt : type_expr option) : quantity =
match te_opt with
| Some te when is_cmd_type te -> QOne
| _ -> QOmega
(** Create a fresh quantity environment. *)
let create_env () : env =
{
quantities = Hashtbl.create 16;
usages = Hashtbl.create 16;
errors = ref [];
}
(** Declare a variable with its quantity annotation in the environment.
Resets the usage counter to [UZero]. *)
let env_declare (env : env) (name : string) (q : quantity) : unit =
Hashtbl.replace env.quantities name q;
Hashtbl.replace env.usages name UZero
(** Record one use of a variable.
Increments the usage counter: UZero -> UOne -> UMany. *)
let env_use (env : env) (name : string) : unit =
match Hashtbl.find_opt env.usages name with
| Some UZero -> Hashtbl.replace env.usages name UOne
| Some UOne -> Hashtbl.replace env.usages name UMany
| Some UMany -> () (* Already saturated *)
| None -> () (* Not a tracked variable (e.g. free/global) *)
(** Snapshot the current usage state — used before analysing branches. *)
let env_snapshot (env : env) : (string, usage) Hashtbl.t =
Hashtbl.copy env.usages
(** Restore a previously snapshotted usage state. *)
let env_restore (env : env) (snapshot : (string, usage) Hashtbl.t) : unit =
Hashtbl.reset env.usages;
Hashtbl.iter (Hashtbl.replace env.usages) snapshot
(** Join the current usage state with a snapshot (for branching).
After analysing branch A, we snapshot, restore, analyse branch B,
then join the B-state with the A-snapshot. The result is the
upper-bound usage across both branches. *)
let env_join (env : env) (other : (string, usage) Hashtbl.t) : unit =
Hashtbl.iter (fun name other_usage ->
let current_usage =
Hashtbl.find_opt env.usages name |> Option.value ~default:UZero
in
Hashtbl.replace env.usages name (join_usage current_usage other_usage)
) other;
(* Handle variables present in current but not in other *)
Hashtbl.iter (fun name current_usage ->
if not (Hashtbl.mem other name) then
Hashtbl.replace env.usages name (join_usage current_usage UZero)
) env.usages
(* {1 check_quantity — per-variable compatibility} *)
(** Check that a single variable's actual usage is compatible with its
declared quantity.
- quantity 0: must be used 0 times (erased).
- quantity 1: must be used exactly 1 time (linear).
- quantity omega: can be used any number of times (unrestricted). *)
let check_quantity (id : ident) (declared : quantity) (actual : usage)
: unit result =
match (declared, actual) with
(* Erased: must not be used *)
| (QZero, UZero) -> Ok ()
| (QZero, _) -> Error (ErasedVariableUsed id, id.span)
(* Linear: must be used exactly once *)
| (QOne, UOne) -> Ok ()
| (QOne, UZero) -> Error (LinearVariableUnused id, id.span)
| (QOne, UMany) -> Error (LinearVariableUsedMultiple id, id.span)
(* Unrestricted: any usage is fine *)
| (QOmega, _) -> Ok ()
(* {1 infer_usage — expression/statement walker} *)
(** Walk an expression and record variable usages in the environment.
This is a recursive descent that mirrors the AST structure.
For branching constructs (if, match), we snapshot/restore/join so
that usage reflects the worst-case across branches. *)
let rec infer_usage_expr (env : env) (expr : expr) : unit =
match expr with
| ExprVar id ->
env_use env id.name
| ExprLit _ -> ()
| ExprApp (func, args) ->
infer_usage_expr env func;
List.iter (infer_usage_expr env) args
| ExprLambda lam ->
(* A lambda may be called zero or many times, so any outer variable
it captures is effectively used QOmega times. We implement this
by walking the body in a temporary copy of the env (shadowing the
lambda's own params), computing the per-variable delta, and then
merging those deltas back scaled by QOmega.
Consequence: a lambda that captures an @linear variable raises
LinearVariableUsedMultiple, because scale_usage QOmega UOne = UMany.
This closes BUG-004.
Additionally: lambda params with explicit quantity annotations are
declared in the env so env_use tracks them, and their usage is
verified against their declared quantity after the body walk.
Violations are pushed to env.errors (since infer_usage_expr is unit)
and drained by check_function_quantities at the top level. *)
let param_names =
List.map (fun (p : param) -> p.p_name.name) lam.elam_params
in
let before_snapshot = env_snapshot env in
(* Save any quantity entries that the param names may shadow, so we
can restore them after the lambda body is analysed. *)
let old_quantities = List.map (fun name ->
(name, Hashtbl.find_opt env.quantities name)
) param_names in
(* Declare each lambda param in the env so env_use can track it.
Annotated params get their declared quantity; unannotated params
get QOmega (unrestricted — no constraint to verify). *)
List.iter (fun (p : param) ->
let q = Option.value p.p_quantity ~default:QOmega in
env_declare env p.p_name.name q
) lam.elam_params;
infer_usage_expr env lam.elam_body;
let after_snapshot = env_snapshot env in
(* Check annotated params against their declared quantities.
Only params with an explicit annotation are verified — unannotated
params default to QOmega which accepts any usage count. *)
List.iter (fun (p : param) ->
match p.p_quantity with
| None -> ()
| Some declared_q ->
let actual =
Hashtbl.find_opt after_snapshot p.p_name.name
|> Option.value ~default:UZero
in
(match check_quantity p.p_name declared_q actual with
| Ok () -> ()
| Error e -> env.errors := e :: !(env.errors))
) lam.elam_params;
(* Restore outer env, then re-apply QOmega-scaled deltas for captured
outer variables only (exclude lambda params). *)
env_restore env before_snapshot;
(* Restore quantity entries that lambda params may have shadowed. *)
List.iter (fun (name, old_q) ->
match old_q with
| Some q -> Hashtbl.replace env.quantities name q
| None -> Hashtbl.remove env.quantities name
) old_quantities;
Hashtbl.iter (fun name after_u ->
if List.mem name param_names then ()
else begin
let before_u =
Hashtbl.find_opt before_snapshot name |> Option.value ~default:UZero
in
let delta =
if equal_usage before_u after_u then UZero
else after_u
in
(* Any use inside a lambda body counts as potentially-unbounded. *)
let scaled = scale_usage QOmega delta in
let merged = add_usage before_u scaled in
Hashtbl.replace env.usages name merged
end
) after_snapshot
| ExprLet lb ->
(* ADR-002 / ADR-007: Let value context is scaled by the binder's
quantity. Concretely:
q·Γ₁ ⊢ e1 : A (Γ₂, x:^q A) ⊢ e2 : B
─────────────────────────────────────
q·Γ₁ + Γ₂ ⊢ let x :^q = e1 in e2 : B
For the usage analysis we implement this as: snapshot the env,
walk e1 (which records usages into the live env), compute the
per-variable delta added by walking e1, scale each delta entry
by q, restore the snapshot, and re-apply the scaled deltas as
additions. Then walk e2 normally.
Stage 11: if no explicit quantity annotation is given but the type
annotation is [Cmd _], infer QOne automatically (intrinsic linearity). *)
let q = match lb.el_quantity with
| Some q -> q
| None -> quantity_of_ty_annotation lb.el_ty
in
let before_value = env_snapshot env in
infer_usage_expr env lb.el_value;
let after_value = env_snapshot env in
env_restore env before_value;
(* Re-apply scaled deltas. *)
Hashtbl.iter (fun name after_u ->
let before_u =
Hashtbl.find_opt before_value name |> Option.value ~default:UZero
in
(* Compute the delta usage added by walking el_value. We model
delta as: how many additional uses occurred during the walk.
Since usage is a 3-point lattice, the simplest sound rule is
"if the walk increased the usage at all, the delta is the
after-value's contribution beyond the before-value." We
implement this by treating any rise from before to after as
the delta usage to scale. *)
let delta =
if equal_usage before_u after_u then UZero
else after_u
in
let scaled = scale_usage q delta in
let merged = add_usage before_u scaled in
Hashtbl.replace env.usages name merged
) after_value;
Option.iter (infer_usage_expr env) lb.el_body
| ExprIf ei ->
(* Condition is always evaluated *)
infer_usage_expr env ei.ei_cond;
(* Branches: take the upper-bound usage *)
let before_branches = env_snapshot env in
infer_usage_expr env ei.ei_then;
let then_usages = env_snapshot env in
env_restore env before_branches;
Option.iter (infer_usage_expr env) ei.ei_else;
(* Join: current state has else-branch usages (or pre-branch if no else) *)
env_join env then_usages
| ExprMatch em ->
infer_usage_expr env em.em_scrutinee;
(* Each arm is a branch — join all of them *)
let before_arms = env_snapshot env in
let arm_snapshots = List.map (fun (arm : match_arm) ->
env_restore env before_arms;
Option.iter (infer_usage_expr env) arm.ma_guard;
infer_usage_expr env arm.ma_body;
env_snapshot env
) em.em_arms in
(* Join all arm snapshots together *)
begin match arm_snapshots with
| [] -> env_restore env before_arms
| first :: rest ->
env_restore env first;
List.iter (env_join env) rest
end
| ExprTuple exprs ->
List.iter (infer_usage_expr env) exprs
| ExprArray exprs ->
List.iter (infer_usage_expr env) exprs
| ExprRecord er ->
List.iter (fun (_id, e_opt) ->
Option.iter (infer_usage_expr env) e_opt
) er.er_fields;
Option.iter (infer_usage_expr env) er.er_spread
| ExprField (e, _) ->
infer_usage_expr env e
| ExprTupleIndex (e, _) ->
infer_usage_expr env e
| ExprIndex (arr, idx) ->
infer_usage_expr env arr;
infer_usage_expr env idx
| ExprRowRestrict (e, _) ->
infer_usage_expr env e
| ExprBlock blk ->
infer_usage_block env blk
| ExprBinary (left, _, right) ->
infer_usage_expr env left;
infer_usage_expr env right
| ExprUnary (_, e) ->
infer_usage_expr env e
| ExprHandle eh ->
infer_usage_expr env eh.eh_body;
List.iter (fun arm ->
match arm with
| HandlerReturn (_pat, body) -> infer_usage_expr env body
| HandlerOp (_op, _pats, body) -> infer_usage_expr env body
) eh.eh_handlers
| ExprResume e_opt ->
Option.iter (infer_usage_expr env) e_opt
| ExprReturn e_opt ->
Option.iter (infer_usage_expr env) e_opt
| ExprTry et ->
infer_usage_block env et.et_body;
Option.iter (fun arms ->
List.iter (fun (arm : match_arm) ->
Option.iter (infer_usage_expr env) arm.ma_guard;
infer_usage_expr env arm.ma_body
) arms
) et.et_catch;
Option.iter (infer_usage_block env) et.et_finally
| ExprUnsafe ops ->
List.iter (fun op ->
match op with
| UnsafeRead e -> infer_usage_expr env e
| UnsafeWrite (e1, e2) ->
infer_usage_expr env e1;
infer_usage_expr env e2
| UnsafeOffset (e1, e2) ->
infer_usage_expr env e1;
infer_usage_expr env e2
| UnsafeTransmute (_, _, e) -> infer_usage_expr env e
| UnsafeForget e -> infer_usage_expr env e
) ops
| ExprVariant _ -> ()
| ExprSpan (e, _) ->
infer_usage_expr env e
(** Walk a block and record variable usages. *)
and infer_usage_block (env : env) (blk : block) : unit =
(* Collect local variables whose quantity must be checked at block end.
For each [StmtLet] whose type annotation infers QOne (i.e. [Cmd _] types),
we declare the bound variable in the env BEFORE processing the statement so
that subsequent uses within the block are tracked via [env_use]. *)
let local_linear_vars = ref [] in
List.iter (fun stmt ->
(match stmt with
| StmtLet sl ->
let q = match sl.sl_quantity with
| Some q -> q
| None -> quantity_of_ty_annotation sl.sl_ty
in
if q = QOne then
(match sl.sl_pat with
| PatVar id ->
env_declare env id.name QOne;
local_linear_vars := id :: !local_linear_vars
| _ -> ())
| _ -> ());
infer_usage_stmt env stmt
) blk.blk_stmts;
Option.iter (infer_usage_expr env) blk.blk_expr;
(* Check that each local linear variable was used exactly once.
Errors are pushed to env.errors for the caller to drain. *)
List.iter (fun id ->
let actual =
Hashtbl.find_opt env.usages id.name |> Option.value ~default:UZero
in
match check_quantity id QOne actual with
| Ok () -> ()
| Error e -> env.errors := e :: !(env.errors)
) !local_linear_vars
(** Walk a statement and record variable usages. *)
and infer_usage_stmt (env : env) (stmt : stmt) : unit =
match stmt with
| StmtLet sl ->
(* Same scaling treatment as ExprLet — see ADR-002 commentary
there. The statement form has no body to walk afterward;
subsequent statements in the enclosing block are walked
independently by infer_usage_block.
Stage 11: infer QOne from [Cmd _] type annotation when no explicit
quantity is given (same rule as ExprLet). *)
let q = match sl.sl_quantity with
| Some q -> q
| None -> quantity_of_ty_annotation sl.sl_ty
in
let before_value = env_snapshot env in
infer_usage_expr env sl.sl_value;
let after_value = env_snapshot env in
env_restore env before_value;
Hashtbl.iter (fun name after_u ->
let before_u =
Hashtbl.find_opt before_value name |> Option.value ~default:UZero
in
let delta =
if equal_usage before_u after_u then UZero
else after_u
in
let scaled = scale_usage q delta in
let merged = add_usage before_u scaled in
Hashtbl.replace env.usages name merged
) after_value
| StmtExpr e ->
infer_usage_expr env e
| StmtAssign (lhs, _, rhs) ->
infer_usage_expr env lhs;
infer_usage_expr env rhs
| StmtWhile (cond, body) ->
(* Loop body may execute multiple times — usage inside is omega-scaled.
We analyse once, then add the usage to itself to model repetition. *)
let before_loop = env_snapshot env in
infer_usage_expr env cond;
infer_usage_block env body;
let after_loop = env_snapshot env in
(* Second pass: any variable used in the loop body is used >=2 times *)
env_restore env before_loop;
infer_usage_expr env cond;
infer_usage_block env body;
env_join env after_loop
| StmtFor (_pat, iter, body) ->
infer_usage_expr env iter;
(* Same loop treatment as while *)
let before_body = env_snapshot env in
infer_usage_block env body;
let after_body = env_snapshot env in
env_restore env before_body;
infer_usage_block env body;
env_join env after_body
(* {1 check_function_quantities — top-level entry point} *)
(** Check that all quantity-annotated parameters in a function declaration
are used the correct number of times.
This is the primary integration point called after type checking.
It:
1. Creates a fresh quantity environment.
2. Declares each parameter with its quantity (defaulting to omega).
3. Walks the function body to count usages.
4. Checks each annotated parameter's usage against its declared quantity.
Returns [Ok ()] if all quantities are satisfied, or
[Error (err, span)] for the first violation found. *)
let check_function_quantities (fd : fn_decl) : unit result =
let env = create_env () in
(* Step 1: declare parameter quantities *)
List.iter (fun (param : param) ->
let q = Option.value param.p_quantity ~default:QOmega in
env_declare env param.p_name.name q
) fd.fd_params;
(* Step 2: walk function body to infer usages *)
begin match fd.fd_body with
| FnBlock blk -> infer_usage_block env blk
| FnExpr e -> infer_usage_expr env e
| FnExtern -> () (* No body to inspect *)
end;
(* Step 3: check each top-level parameter *)
let param_result =
List.fold_left (fun acc (param : param) ->
match acc with
| Error _ -> acc (* Stop at first error *)
| Ok () ->
let declared = Option.value param.p_quantity ~default:QOmega in
let actual =
Hashtbl.find_opt env.usages param.p_name.name
|> Option.value ~default:UZero
in
check_quantity param.p_name declared actual
) (Ok ()) fd.fd_params
in
(* Step 4: drain errors accumulated by nested lambda param checks.
infer_usage_expr is unit, so nested violations are pushed to
env.errors during the body walk above. Report the first one. *)
match param_result with
| Error _ -> param_result
| Ok () ->
match !(env.errors) with
| [] -> Ok ()
| (err, span) :: _ -> Error (err, span)
(** Check quantities for all functions in a program.
Iterates over all top-level function declarations and checks each one.
Returns [Ok ()] if all pass, or the first error found. *)
let check_program_quantities (program : program) : unit result =
List.fold_left (fun acc decl ->
match acc with
| Error _ -> acc
| Ok () ->
match decl with
| TopFn fd -> check_function_quantities fd
| _ -> Ok ()
) (Ok ()) program.prog_decls
(* {1 Backward compatibility aliases}
These maintain the old API surface while the codebase transitions
to the new function names. *)
(** @deprecated Use {!check_function_quantities} instead. *)
let check_function (_symbols : Symbol.t) (fd : fn_decl) : unit result =
check_function_quantities fd
(** @deprecated Use {!check_program_quantities} instead. *)
let check_program (_symbols : Symbol.t) (program : program) : unit result =
check_program_quantities program