aver-lang 0.16.1

VM and transpiler for Aver, a statically-typed language designed for AI-assisted development
Documentation
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
/// Aver expressions → Dafny expression strings.
use crate::ast::*;
use crate::codegen::CodegenContext;
use crate::codegen::common::{expr_to_dotted_name, is_user_type, resolve_module_call};

/// Dafny reserved words.
const DAFNY_RESERVED: &[&str] = &[
    "abstract",
    "allocated",
    "as",
    "assert",
    "assume",
    "bool",
    "break",
    "by",
    "calc",
    "case",
    "char",
    "class",
    "codatatype",
    "colemma",
    "constructor",
    "copredicate",
    "datatype",
    "decreases",
    "default",
    "else",
    "ensures",
    "exists",
    "expect",
    "export",
    "extends",
    "false",
    "forall",
    "fresh",
    "function",
    "ghost",
    "if",
    "import",
    "in",
    "include",
    "int",
    "invariant",
    "is",
    "iterator",
    "label",
    "lemma",
    "map",
    "match",
    "method",
    "modifies",
    "modify",
    "module",
    "multiset",
    "nat",
    "new",
    "newtype",
    "null",
    "object",
    "old",
    "opened",
    "predicate",
    "print",
    "provides",
    "reads",
    "real",
    "refines",
    "requires",
    "return",
    "returns",
    "reveal",
    "reveals",
    "seq",
    "set",
    "static",
    "string",
    "then",
    "this",
    "trait",
    "true",
    "twostate",
    "type",
    "unchanged",
    "var",
    "while",
    "witness",
    "yield",
    "yields",
];

/// Convert an Aver identifier to a valid Dafny name. Aver allows
/// underscore-prefixed names like `_pmy` (idiomatic "intentionally
/// unused"); Dafny rejects identifiers that begin with `_` AND have
/// further characters. Pure `_` (Dafny's wildcard) is fine. We rewrite
/// `_pmy` → `aver_pmy` but leave bare `_` untouched.
pub fn aver_name_to_dafny(name: &str) -> String {
    let stripped = name.trim_start_matches('_');
    let normalized = if stripped.is_empty() {
        // Pure `_` (or `__`...): Dafny treats this as wildcard.
        name.to_string()
    } else if stripped.len() < name.len() {
        format!("aver_{}", stripped)
    } else {
        name.to_string()
    };
    crate::codegen::common::escape_reserved_word(&normalized, DAFNY_RESERVED, "_")
}

/// Emit a Dafny expression from an Aver Spanned<Expr>.
pub fn emit_expr(expr: &Spanned<Expr>, ctx: &CodegenContext) -> String {
    match &expr.node {
        Expr::Literal(lit) => emit_literal(lit),
        Expr::Ident(name) | Expr::Resolved { name, .. } => aver_name_to_dafny(name),
        Expr::Attr(obj, field) => {
            if let Expr::Ident(type_name) = &obj.node {
                if type_name == "Option" && field == "None" {
                    return "Option.None".to_string();
                }
                // Oracle v1: `BranchPath.Root` — nullary value,
                // matches the `const BranchPath_Root` in the prelude.
                if type_name == "BranchPath" && field == "Root" {
                    return "BranchPath_Root".to_string();
                }
            }
            // Module-qualified call/access: must be checked before
            // `is_user_type` because Aver allows `module Enemy` to coexist
            // with `record Enemy`. If the head is a known module prefix,
            // route through the renamed Dafny module (`Aver_Enemy.fn`).
            if let Some(full_dotted) = expr_to_dotted_name_spanned(expr)
                && let Some((prefix, bare)) = resolve_module_call(&full_dotted, ctx)
            {
                if let Some(dot_pos) = bare.find('.') {
                    let type_name = &bare[..dot_pos];
                    let variant = &bare[dot_pos + 1..];
                    if is_user_type(type_name, ctx) {
                        return format!("{}.{}", type_name, variant);
                    }
                }
                let bare_dafny = aver_name_to_dafny(bare);
                if !ctx.modules.is_empty() {
                    return format!("{}.{}", super::dafny_module_name(prefix), bare_dafny);
                }
                return bare_dafny;
            }
            if let Expr::Ident(type_name) = &obj.node
                && is_user_type(type_name, ctx)
            {
                return format!("{}.{}", type_name, field);
            }
            let obj_str = emit_expr(obj, ctx);
            format!("{}.{}", obj_str, aver_name_to_dafny(field))
        }
        Expr::FnCall(fn_expr, args) => emit_fn_call(fn_expr, args, ctx),
        Expr::BinOp(op, left, right) => {
            // Narrow special case: `0 - x` where the right-hand side is
            // a `Float`-typed expression becomes `(-x)`. Dafny rejects
            // `0 - 2.5` because the literal `0` is `int` and mixing is
            // a type mismatch against `real`; the split_here fix from
            // the Oracle PR removed the blanket rewrite, so reintroduce
            // it only for Float operands where Dafny actually needs it.
            if matches!(op, BinOp::Sub)
                && matches!(left.node, Expr::Literal(Literal::Int(0)))
                && right_is_float_shape(right)
            {
                let r = emit_expr(right, ctx);
                return format!("(-{})", r);
            }
            let l = emit_expr(left, ctx);
            let r = emit_expr(right, ctx);
            let op_str = match op {
                BinOp::Add => "+",
                BinOp::Sub => "-",
                BinOp::Mul => "*",
                BinOp::Div => "/",
                BinOp::Eq => "==",
                BinOp::Neq => "!=",
                BinOp::Lt => "<",
                BinOp::Gt => ">",
                BinOp::Lte => "<=",
                BinOp::Gte => ">=",
            };
            format!("({} {} {})", l, op_str, r)
        }
        Expr::Match { subject, arms, .. } => emit_match(subject, arms, ctx),
        Expr::Constructor(name, arg) => emit_constructor(name, arg.as_deref(), ctx),
        Expr::ErrorProp(_) => {
            // ? operator requires early-return semantics (Err propagation).
            // Dafny pure functions cannot express this; functions using ? are
            // skipped at the top-level emission stage.  If we get here, emit
            // a marker that makes the generated Dafny obviously wrong rather
            // than silently modelling a different program.
            "/* ERROR: ? operator not supported in Dafny pure functions */".to_string()
        }
        Expr::InterpolatedStr(parts) => emit_interpolated_str(parts, ctx),
        Expr::List(elems) => {
            let items: Vec<String> = elems.iter().map(|e| emit_expr(e, ctx)).collect();
            format!("[{}]", items.join(", "))
        }
        Expr::Tuple(elems) | Expr::IndependentProduct(elems, _) => {
            // Oracle v1: plain `!` lifts to a Dafny tuple — schedule
            // invariance is a compiler-level claim, no extra machinery at
            // the expression site. `?!` also passes through the tuple form
            // here (typechecker models `?!` as the Ok-short-circuit, so
            // body-type is the unwrapped tuple); proper Result-fold
            // emission for `?!` with explicit Err propagation is deferred
            // — it needs a coordinated rewrite with the enclosing
            // `Result.Ok(...)` wrapper the typechecker forces on function
            // returns.
            let items: Vec<String> = elems.iter().map(|e| emit_expr(e, ctx)).collect();
            format!("({})", items.join(", "))
        }
        Expr::MapLiteral(entries) => {
            if entries.is_empty() {
                "map[]".to_string()
            } else if entries
                .iter()
                .all(|(_, v)| crate::codegen::common::is_unit_expr_spanned(v))
            {
                // Map<T, Unit> literal → set literal
                let items: Vec<String> = entries.iter().map(|(k, _)| emit_expr(k, ctx)).collect();
                format!("{{{}}}", items.join(", "))
            } else {
                let items: Vec<String> = entries
                    .iter()
                    .map(|(k, v)| format!("{} := {}", emit_expr(k, ctx), emit_expr(v, ctx)))
                    .collect();
                format!("map[{}]", items.join(", "))
            }
        }
        Expr::RecordCreate { type_name, fields } => {
            let field_strs: Vec<String> = fields
                .iter()
                .map(|(name, expr)| {
                    format!("{} := {}", aver_name_to_dafny(name), emit_expr(expr, ctx))
                })
                .collect();
            // Dotted names (`Terminal.Size`, `Tcp.Connection`) are
            // rendered as underscored datatype names in Dafny — same
            // mapping `toplevel::render_type` applies for `Type::Named`.
            let dafny_type_name = type_name.replace('.', "_");
            format!("{}({})", dafny_type_name, field_strs.join(", "))
        }
        Expr::RecordUpdate { base, updates, .. } => {
            let base_str = emit_expr(base, ctx);
            let update_strs: Vec<String> = updates
                .iter()
                .map(|(name, expr)| {
                    format!("{} := {}", aver_name_to_dafny(name), emit_expr(expr, ctx))
                })
                .collect();
            format!("{}.({})", base_str, update_strs.join(", "))
        }
        Expr::TailCall(inner) => {
            let TailCallData {
                target: name, args, ..
            } = inner.as_ref();
            let arg_strs: Vec<String> = args.iter().map(|a| emit_expr(a, ctx)).collect();
            format!("{}({})", aver_name_to_dafny(name), arg_strs.join(", "))
        }
    }
}

/// Helper to extract dotted name from a Spanned<Expr>.
fn expr_to_dotted_name_spanned(expr: &Spanned<Expr>) -> Option<String> {
    expr_to_dotted_name(&expr.node)
}

/// True when an expression is syntactically Float-shaped: a float
/// literal, an explicit `as real` cast, or a float-returning builtin.
/// Used by the `0 - x` → `(-x)` rewrite for Dafny `real` arithmetic.
fn right_is_float_shape(expr: &Spanned<Expr>) -> bool {
    match &expr.node {
        Expr::Literal(Literal::Float(_)) => true,
        Expr::FnCall(callee, _) => {
            matches!(
                &callee.node,
                Expr::Attr(obj, field)
                    if field == "toFloat"
                        || (matches!(&obj.node, Expr::Ident(n) if n == "Float" || n == "Int")
                            && (field == "sqrt" || field == "pow" || field == "abs"))
            )
        }
        // A parenthesised `e as real` lands here as a nested cast node
        // after the parser / resolver pass. The shape we actually see
        // at this codegen point is a FnCall into a cast builtin or a
        // float literal embedded in a constructor arg; both covered
        // above. Be permissive — if in doubt treat as float for this
        // rewrite so we don't regress the `0 - float_expr` shape.
        _ => false,
    }
}

fn emit_literal(lit: &Literal) -> String {
    match lit {
        Literal::Int(n) => n.to_string(),
        Literal::Float(f) => {
            let s = f.to_string();
            if s.contains('.') {
                format!("{} as real", s)
            } else {
                format!("{}.0 as real", s)
            }
        }
        Literal::Str(s) => {
            format!(
                "\"{}\"",
                crate::codegen::common::escape_string_literal_unicode(s)
            )
        }
        Literal::Bool(b) => b.to_string(),
        Literal::Unit => "()".to_string(),
    }
}

fn emit_fn_call(fn_expr: &Spanned<Expr>, args: &[Spanned<Expr>], ctx: &CodegenContext) -> String {
    use crate::codegen::builtins::recognize_builtin;
    use crate::codegen::common::is_unit_expr_spanned;

    let dotted = expr_to_dotted_name_spanned(fn_expr);

    // Map<T, Unit> set operations: intercept before generic builtin path
    if let Some(name) = dotted.as_deref()
        && name == "Map.set"
        && args.len() == 3
        && is_unit_expr_spanned(&args[2])
    {
        let m = emit_expr(&args[0], ctx);
        let k = emit_expr(&args[1], ctx);
        return format!("({} + {{{}}})", m, k);
    }

    if let Some(builtin) = dotted.as_deref().and_then(recognize_builtin) {
        let a: Vec<String> = args.iter().map(|e| emit_expr(e, ctx)).collect();
        return emit_dafny_builtin(builtin, &a);
    }

    // Oracle v1: BranchPath.* constructor calls map onto the underscore-
    // named prelude functions (Dafny's dotted notation collides with
    // record-member access on the BranchPath datatype).
    if let Some(name) = dotted.as_deref() {
        let a: Vec<String> = args.iter().map(|e| emit_expr(e, ctx)).collect();
        match name {
            "BranchPath.child" if a.len() == 2 => {
                return format!("BranchPath_child({}, {})", a[0], a[1]);
            }
            "BranchPath.parse" if a.len() == 1 => {
                return format!("BranchPath_parse({})", a[0]);
            }
            _ => {}
        }
    }

    // Not a recognized builtin — generic function call
    let fn_name = emit_expr(fn_expr, ctx);
    let arg_strs: Vec<String> = args.iter().map(|e| emit_expr(e, ctx)).collect();
    format!("{}({})", fn_name, arg_strs.join(", "))
}

fn emit_dafny_builtin(b: crate::codegen::builtins::Builtin, a: &[String]) -> String {
    use crate::codegen::builtins::Builtin::*;
    match b {
        // Constructors
        ResultOk => format!("Result.Ok({})", a.first().map(|s| s.as_str()).unwrap_or("")),
        ResultErr => format!(
            "Result.Err({})",
            a.first().map(|s| s.as_str()).unwrap_or("")
        ),
        OptionSome => format!(
            "Option.Some({})",
            a.first().map(|s| s.as_str()).unwrap_or("")
        ),

        // Combinators
        ResultWithDefault => format!("ResultWithDefault({}, {})", a[0], a[1]),
        OptionWithDefault => format!("OptionWithDefault({}, {})", a[0], a[1]),
        OptionToResult => format!("OptionToResult({}, {})", a[0], a[1]),

        // Int
        IntAbs => format!("(if {} >= 0 then {} else -{})", a[0], a[0], a[0]),
        IntToFloat => format!("({} as real)", a[0]),
        IntToString | StringFromInt => format!("IntToString({})", a[0]),
        IntFromString | IntParse => format!("IntFromString({})", a[0]),
        IntMin => format!("(if {} <= {} then {} else {})", a[0], a[1], a[0], a[1]),
        IntMax => format!("(if {} >= {} then {} else {})", a[0], a[1], a[0], a[1]),
        IntRem | IntMod => format!("Result<int, string>.Ok(({} % {}))", a[0], a[1]),

        // Float
        FloatAbs => format!("(if {} >= 0.0 then {} else -{})", a[0], a[0], a[0]),
        FloatSqrt => format!("FloatSqrt({})", a[0]),
        FloatPow => format!("FloatPow({}, {})", a[0], a[1]),
        FloatRound | FloatFloor | FloatCeil | FloatToInt => format!("FloatToInt({})", a[0]),
        FloatToString | StringFromFloat => format!("FloatToString({})", a[0]),
        FloatFromString | FloatParse => format!("FloatFromString({})", a[0]),
        FloatPi => "FloatPi()".to_string(),
        FloatMin => format!("(if {} <= {} then {} else {})", a[0], a[1], a[0], a[1]),
        FloatMax => format!("(if {} >= {} then {} else {})", a[0], a[1], a[0], a[1]),
        FloatSin => format!("FloatSin({})", a[0]),
        FloatCos => format!("FloatCos({})", a[0]),
        FloatAtan2 => format!("FloatAtan2({}, {})", a[0], a[1]),

        // String
        StringLen => format!("|{}|", a[0]),
        StringConcat => format!("({} + {})", a[0], a[1]),
        StringCharAt => format!("StringCharAt({}, {})", a[0], a[1]),
        StringChars => format!("StringChars({})", a[0]),
        StringSlice => format!("{}[{}..{}]", a[0], a[1], a[2]),
        StringContains => format!("StringContains({}, {})", a[0], a[1]),
        StringStartsWith => format!("StringStartsWith({}, {})", a[0], a[1]),
        StringEndsWith => format!("StringEndsWith({}, {})", a[0], a[1]),
        StringTrim => format!("StringTrim({})", a[0]),
        StringSplit => format!("StringSplit({}, {})", a[0], a[1]),
        StringJoin => format!("StringJoin({}, {})", a[1], a[0]), // Aver: join(list, sep)
        StringReplace => format!("StringReplace({}, {}, {})", a[0], a[1], a[2]),
        StringRepeat => format!("StringRepeat({}, {})", a[0], a[1]),
        StringIndexOf => format!("StringIndexOf({}, {})", a[0], a[1]),
        StringToUpper => format!("StringToUpper({})", a[0]),
        StringToLower => format!("StringToLower({})", a[0]),
        StringFromBool => format!("StringFromBool({})", a[0]),
        StringByteLength => format!("StringByteLength({})", a[0]),

        // Bool
        BoolOr => format!("({} || {})", a[0], a[1]),
        BoolAnd => format!("({} && {})", a[0], a[1]),
        BoolNot => format!("(!{})", a[0]),

        // Char/Byte
        CharToCode => format!("CharToCode({})", a[0]),
        CharFromCode => format!("CharFromCode({})", a[0]),
        ByteToHex => format!("ByteToHex({})", a[0]),
        ByteFromHex => format!("ByteFromHex({})", a[0]),

        // List
        // An empty-list literal has no element-type context in Dafny
        // (seq<?>), which makes `|[]|` fail resolver with "type of this
        // expression is underspecified". The length is trivially 0
        // regardless of element type, so short-circuit the emission.
        ListLen => {
            if a[0].trim() == "[]" {
                "0".to_string()
            } else {
                format!("|{}|", a[0])
            }
        }
        ListHead => format!("ListHead({})", a[0]),
        ListTail => format!("ListTail({})", a[0]),
        ListPrepend => format!("[{}] + {}", a[0], a[1]),
        ListTake => format!("ListTake({}, {})", a[0], a[1]),
        ListDrop => format!("ListDrop({}, {})", a[0], a[1]),
        ListConcat => format!("({} + {})", a[0], a[1]),
        ListReverse => format!("ListReverse({})", a[0]),
        ListContains => format!("({} in {})", a[1], a[0]),
        ListFind => format!("ListFind({}, {})", a[0], a[1]),
        ListAny => format!("ListAny({}, {})", a[0], a[1]),
        ListZip => format!("ListZip({}, {})", a[0], a[1]),

        // Vector (maps to seq in Dafny — same as List but with indexed access)
        VectorNew => format!("seq({}, _ => {})", a[0], a[1]),
        VectorGet => format!(
            "if 0 <= {} < |{}| then Some({}[{}]) else None",
            a[1], a[0], a[0], a[1]
        ),
        VectorSet => format!(
            "if 0 <= {} < |{}| then Some({}[{} := {}]) else None",
            a[1], a[0], a[0], a[1], a[2]
        ),
        VectorLen => format!("|{}|", a[0]),
        VectorFromList => a[0].clone(),
        VectorToList => a[0].clone(),

        // Map
        MapEmpty => "map[]".to_string(),
        MapGet => format!("MapGet({}, {})", a[0], a[1]),
        MapSet => format!("{}[{} := {}]", a[0], a[1], a[2]),
        MapHas => format!("({} in {})", a[1], a[0]),
        MapRemove => format!("({} - {{{}}})", a[0], a[1]),
        MapKeys => format!("MapKeys({})", a[0]),
        MapValues => format!("MapValues({})", a[0]),
        MapEntries => format!("MapEntries({})", a[0]),
        MapLen => format!("|{}|", a[0]),
        MapFromList => format!("MapFromList({})", a[0]),
    }
}

fn emit_match(subject: &Spanned<Expr>, arms: &[MatchArm], ctx: &CodegenContext) -> String {
    // Check if this is a list-pattern match (EmptyList / Cons arms)
    if has_list_patterns(arms) {
        return emit_list_match(subject, arms, ctx);
    }

    // Bool match: `true -> ..., false -> ...` → `if subj then ... else ...`
    if is_bool_match(arms) {
        return emit_bool_match(subject, arms, ctx);
    }

    // Scalar match (int literals, wildcards) → if-then-else chain.
    // This helps Dafny's verifier see guards for termination proofs.
    if should_emit_as_if_chain(arms) {
        return emit_if_chain(subject, arms, ctx);
    }

    let subj = emit_expr(subject, ctx);
    let mut lines = Vec::new();
    lines.push(format!("match {}", subj));

    for arm in arms {
        let pat = emit_pattern(&arm.pattern);
        let body = emit_expr(&arm.body, ctx);
        lines.push(format!("  case {} => {}", pat, body));
    }

    format!("({})", lines.join(" "))
}

/// Should we emit this match as an if-then-else chain?
/// Yes for matches on scalar values (int, bool, string literals) and wildcards.
fn should_emit_as_if_chain(arms: &[MatchArm]) -> bool {
    arms.iter().all(|arm| {
        matches!(
            arm.pattern,
            Pattern::Literal(_) | Pattern::Wildcard | Pattern::Ident(_)
        )
    })
}

/// Check if arms form a bool match: `true -> ..., false -> ...` (in either order).
fn is_bool_match(arms: &[MatchArm]) -> bool {
    if arms.len() != 2 {
        return false;
    }
    let has_true = arms
        .iter()
        .any(|a| matches!(&a.pattern, Pattern::Literal(Literal::Bool(true))));
    let has_false = arms
        .iter()
        .any(|a| matches!(&a.pattern, Pattern::Literal(Literal::Bool(false))));
    has_true && has_false
}

/// Emit a bool match as `if subject then true_body else false_body`.
fn emit_bool_match(subject: &Spanned<Expr>, arms: &[MatchArm], ctx: &CodegenContext) -> String {
    let subj = emit_expr(subject, ctx);
    let true_arm = arms
        .iter()
        .find(|a| matches!(&a.pattern, Pattern::Literal(Literal::Bool(true))))
        .unwrap();
    let false_arm = arms
        .iter()
        .find(|a| matches!(&a.pattern, Pattern::Literal(Literal::Bool(false))))
        .unwrap();
    let true_body = emit_expr(&true_arm.body, ctx);
    let false_body = emit_expr(&false_arm.body, ctx);
    format!("(if {} then {} else {})", subj, true_body, false_body)
}

/// Emit a match as a Dafny if-then-else chain.
fn emit_if_chain(subject: &Spanned<Expr>, arms: &[MatchArm], ctx: &CodegenContext) -> String {
    let subj = emit_expr(subject, ctx);
    emit_if_chain_inner(&subj, arms, 0, ctx)
}

fn emit_if_chain_inner(subj: &str, arms: &[MatchArm], idx: usize, ctx: &CodegenContext) -> String {
    if idx >= arms.len() {
        return "/* unreachable */".to_string();
    }

    let arm = &arms[idx];
    let body = emit_expr(&arm.body, ctx);

    match &arm.pattern {
        Pattern::Wildcard | Pattern::Ident(_) => {
            if let Pattern::Ident(name) = &arm.pattern {
                format!("(var {} := {}; {})", aver_name_to_dafny(name), subj, body)
            } else {
                body
            }
        }
        Pattern::Literal(lit) => {
            let rest = emit_if_chain_inner(subj, arms, idx + 1, ctx);

            let lit_str = emit_literal(lit);
            format!("(if {} == {} then {} else {})", subj, lit_str, body, rest)
        }
        _ => {
            let pat = emit_pattern(&arm.pattern);
            format!("/* unsupported pattern: {} */ {}", pat, body)
        }
    }
}

/// Check if any arm uses list patterns (EmptyList or Cons).
fn has_list_patterns(arms: &[MatchArm]) -> bool {
    arms.iter()
        .any(|arm| matches!(arm.pattern, Pattern::EmptyList | Pattern::Cons(_, _)))
}

/// Emit a match on a list (seq) as if-then-else with seq operations.
fn emit_list_match(subject: &Spanned<Expr>, arms: &[MatchArm], ctx: &CodegenContext) -> String {
    let subj = emit_expr(subject, ctx);

    // Find empty-list arm and cons arm
    let empty_arm = arms
        .iter()
        .find(|a| matches!(a.pattern, Pattern::EmptyList));
    let cons_arm = arms
        .iter()
        .find(|a| matches!(a.pattern, Pattern::Cons(_, _)));
    let wildcard_arm = arms
        .iter()
        .find(|a| matches!(a.pattern, Pattern::Wildcard | Pattern::Ident(_)));

    let empty_body = if let Some(arm) = empty_arm {
        emit_expr(&arm.body, ctx)
    } else if let Some(arm) = wildcard_arm {
        emit_expr(&arm.body, ctx)
    } else {
        "/* missing empty case */".to_string()
    };

    let cons_body = if let Some(arm) = cons_arm {
        if let Pattern::Cons(head, tail) = &arm.pattern {
            let head_name = aver_name_to_dafny(head);
            let tail_name = aver_name_to_dafny(tail);
            let body = emit_expr(&arm.body, ctx);
            format!(
                "var {} := {}[0]; var {} := {}[1..]; {}",
                head_name, subj, tail_name, subj, body
            )
        } else {
            unreachable!()
        }
    } else if let Some(arm) = wildcard_arm {
        emit_expr(&arm.body, ctx)
    } else {
        "/* missing cons case */".to_string()
    };

    format!(
        "(if |{}| == 0 then {} else {})",
        subj, empty_body, cons_body
    )
}

fn emit_pattern(pattern: &Pattern) -> String {
    match pattern {
        Pattern::Wildcard => "_".to_string(),
        Pattern::Literal(lit) => emit_literal(lit),
        Pattern::Ident(name) => aver_name_to_dafny(name),
        Pattern::EmptyList => "Nil".to_string(),
        Pattern::Cons(head, tail) => {
            format!(
                "Cons({}, {})",
                aver_name_to_dafny(head),
                aver_name_to_dafny(tail)
            )
        }
        Pattern::Tuple(pats) => {
            let subs: Vec<String> = pats.iter().map(emit_pattern).collect();
            format!("({})", subs.join(", "))
        }
        Pattern::Constructor(name, bindings) => {
            let variant = if let Some(dot_pos) = name.rfind('.') {
                &name[dot_pos + 1..]
            } else {
                name.as_str()
            };
            if bindings.is_empty() {
                variant.to_string()
            } else {
                let subs: Vec<String> = bindings.iter().map(|b| aver_name_to_dafny(b)).collect();
                format!("{}({})", variant, subs.join(", "))
            }
        }
    }
}

fn emit_constructor(name: &str, arg: Option<&Spanned<Expr>>, ctx: &CodegenContext) -> String {
    // In Dafny expression context, qualify constructors to avoid
    // ambiguity. User-defined types and the built-in `Result` /
    // `Option` are kept fully qualified — the latter because user code
    // can declare its own `enum ParseResult { Ok, Err }` with the same
    // variant names, and Dafny needs the discriminator to pick the
    // right datatype.
    let qualified = if let Some(dot_pos) = name.rfind('.') {
        let type_name = &name[..dot_pos];
        let variant = &name[dot_pos + 1..];
        if is_user_type(type_name, ctx) || type_name == "Result" || type_name == "Option" {
            format!("{}.{}", type_name, variant)
        } else {
            variant.to_string()
        }
    } else {
        name.to_string()
    };
    if let Some(a) = arg {
        let arg_str = emit_expr(a, ctx);
        format!("{}({})", qualified, arg_str)
    } else {
        qualified
    }
}

fn emit_interpolated_str(parts: &[StrPart], ctx: &CodegenContext) -> String {
    let mut pieces = Vec::new();
    for part in parts {
        match part {
            StrPart::Literal(s) => {
                pieces.push(format!(
                    "\"{}\"",
                    crate::codegen::common::escape_string_literal_unicode(s)
                ));
            }
            StrPart::Parsed(expr) => {
                pieces.push(format!("ToString({})", emit_expr(expr, ctx)));
            }
        }
    }
    if pieces.len() == 1 {
        pieces.into_iter().next().unwrap()
    } else {
        pieces.join(" + ")
    }
}