polydat-core 0.3.1

Polydat runtime: value model, graph compiler, execution engines, kernels
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Lowering a `tile` statement to a program (SRD 114 ยง6).
//!
//! Each hole becomes a `tile_encode` binding in the enclosing scope
//! whose constant spec carries the tile's encoding, the hole's position,
//! its declared type, its format, and its raw flag. Static runs fold
//! into single instructions. Projections compile their body into a
//! child program text whose holes are `tile_encode` bindings too; the
//! render node compiles that body once at setup and re-runs it per
//! tuple. The tile itself becomes a `tile_render` call over the encoded
//! hole wires and any wires a body imports.

use std::collections::BTreeSet;

use crate::ast::PortType;
use crate::compile::assembly::PolydatAssembler;
use crate::iteration::comprehension::StreamerValue;
use crate::library::tile_render::{
    ChildSpec, HoleEncoding, HolePosition, HoleSource, TileOp, TileSpec,
};

use super::ast::{Arg, CallExpr, Expr, ForSource, ForSourceKind, TileDef, TileOptions, TilePiece};
use super::compile::Compiler;
use super::refs::collect_expr_refs;
use super::tile::render_template;

impl Compiler {
    /// Lower a tile into encode bindings and a `tile_render` binding
    /// named after the tile. Records the tile so later tiles can splice
    /// it.
    pub(super) fn compile_tile(
        &mut self,
        asm: &mut PolydatAssembler,
        tile: &TileDef,
    ) -> Result<(), String> {
        let encoding = tile.encoding.clone().unwrap_or_else(|| "text".to_string());
        let mut lowering = TileLowering {
            tile_name: tile.name.clone(),
            encoding: encoding.clone(),
            options: tile.options.clone(),
            inputs: Vec::new(),
            children: Vec::new(),
            hole_counter: 0,
            in_string: tile.options.in_string,
            strict: self.strict || tile.options.strict,
            span: tile.span,
        };
        let pieces = self.splice_tiles(&tile.pieces, &tile.name, &tile.encoding, 0)?;
        if encoding == "json" && !tile.options.in_string && !tile.name.starts_with("__") {
            Self::validate_json_skeleton_impl(&tile.name, &pieces)?;
        }
        let ops = lowering.lower_pieces(self, asm, &pieces, None)?;
        let spec = TileSpec {
            name: tile.name.clone(),
            encoding,
            ops,
            children: lowering.children,
        };
        let mut shape = SkeletonShape::default();
        shape.count(&spec.ops);
        self.tile_events
            .push(super::events::CompileEvent::TileCompiled {
                tile: tile.name.clone(),
                encoding: spec.encoding.clone(),
                statics: shape.statics,
                static_bytes: shape.static_bytes,
                holes: shape.holes,
                branches: shape.branches,
                projections: shape.projections,
                bodies: spec.children.iter().map(|c| c.source.clone()).collect(),
            });
        let mut args = vec![Arg::Positional(Expr::StringLit(spec.to_json(), tile.span))];
        for name in &lowering.inputs {
            args.push(Arg::Positional(Expr::Ident(name.clone(), tile.span)));
        }
        let call = Expr::Call(CallExpr {
            func: "tile_render".into(),
            args,
            span: tile.span,
        });
        self.compile_binding(asm, std::slice::from_ref(&tile.name), &call)?;
        if lowering.inputs.is_empty() {
            // No holes: the tile is a constant, and says so.
            asm.mark_const_output(&tile.name);
            asm.set_output_modifier(&tile.name, super::ast::BindingModifier::CONST);
        }
        self.tiles.push(tile.clone());
        Ok(())
    }

    /// SRD 114 ยง5.2: a `json` skeleton must be valid JSON once every
    /// hole is a placeholder. Every hole stands in as `0`, which is a
    /// value in value position and text inside a string or a key; a
    /// projection body appears once; a branch shows its first arm. A
    /// document that fails to parse is rejected here, with serde's
    /// position, rather than rendering malformed output per cycle.
    /// Author tiles only: the compiler's own body tiles are fragments.
    fn validate_json_skeleton_impl(name: &str, pieces: &[TilePiece]) -> Result<(), String> {
        fn placeholder(pieces: &[TilePiece], out: &mut String) {
            for piece in pieces {
                match piece {
                    TilePiece::Static(s) => out.push_str(s),
                    TilePiece::Hole(_) => out.push('0'),
                    TilePiece::Projection { body, .. } => placeholder(body, out),
                    TilePiece::Branch { then, .. } => placeholder(then, out),
                }
            }
        }
        let mut doc = String::new();
        placeholder(pieces, &mut doc);
        match serde_json::from_str::<serde_json::Value>(&doc) {
            Ok(_) => Ok(()),
            Err(e) => Err(format!(
                "tile '{name}': the json body is not valid JSON once every hole is a placeholder: {e}; \
                 with holes as `0`, one repetition per projection, and each branch's first arm, the skeleton reads: {}",
                doc.trim()
            )),
        }
    }

    /// Replace splice holes, `${name}` where `name` is an earlier tile,
    /// with that tile's pieces, recursively.
    fn splice_tiles(
        &self,
        pieces: &[TilePiece],
        current: &str,
        encoding: &Option<String>,
        depth: usize,
    ) -> Result<Vec<TilePiece>, String> {
        if depth > 16 {
            return Err(format!(
                "tile '{current}': splice nesting too deep; is there a cycle?"
            ));
        }
        let mut out = Vec::with_capacity(pieces.len());
        for piece in pieces {
            match piece {
                TilePiece::Hole(h) => {
                    if let Expr::Ident(name, _) = &h.expr
                        && h.decl_type.is_none()
                        && h.format.is_none()
                        && let Some(other) = self.tiles.iter().rev().find(|t| &t.name == name)
                    {
                        if name == current {
                            return Err(format!("tile '{current}' splices itself"));
                        }
                        // Pieces are spliced only into a tile of the same
                        // encoding, where they mean the same thing. Across
                        // encodings the inner tile is an ordinary wire:
                        // its rendered text enters through the hole and
                        // is encoded (or, with `!`, inlined) as any string.
                        if other.encoding.as_deref().unwrap_or("text")
                            == encoding.as_deref().unwrap_or("text")
                        {
                            out.extend(self.splice_tiles(
                                &other.pieces,
                                name,
                                encoding,
                                depth + 1,
                            )?);
                            continue;
                        }
                    }
                    out.push(piece.clone());
                }
                TilePiece::Projection {
                    source,
                    sep,
                    body,
                    span,
                } => out.push(TilePiece::Projection {
                    source: source.clone(),
                    sep: sep.clone(),
                    body: self.splice_tiles(body, current, encoding, depth + 1)?,
                    span: *span,
                }),
                TilePiece::Branch {
                    cond,
                    then,
                    otherwise,
                    span,
                } => out.push(TilePiece::Branch {
                    cond: cond.clone(),
                    then: self.splice_tiles(then, current, encoding, depth + 1)?,
                    otherwise: match otherwise {
                        Some(o) => Some(self.splice_tiles(o, current, encoding, depth + 1)?),
                        None => None,
                    },
                    span: *span,
                }),
                other => out.push(other.clone()),
            }
        }
        Ok(out)
    }
}

/// State for one tile's lowering.
struct TileLowering {
    tile_name: String,
    encoding: String,
    options: TileOptions,
    /// Wire names passed to `tile_render`, in input order.
    inputs: Vec<String>,
    children: Vec<ChildSpec>,
    hole_counter: usize,
    /// Whether the static text so far leaves a `json` skeleton inside a
    /// string literal.
    in_string: bool,
    /// Reject implicit adapters at holes (SRD 114 ยง4.4).
    strict: bool,
    span: super::lexer::Span,
}

/// How one hole was typed (SRD 114 ยง4): the wire's compile-time type,
/// the declared type if any, the type the encoder sees, and the
/// adapter between them when the declaration asks for one.
struct HoleTyping {
    wire: PortType,
    declared: Option<PortType>,
    effective: PortType,
    adapter: Option<(PortType, PortType)>,
    expectation: &'static str,
}

/// A projection body under construction.
struct BodyContext {
    elements: Vec<(String, PortType)>,
    bindings: Vec<String>,
    /// Outer names the body references: `(name, node input index, type keyword)`.
    cascade: Vec<(String, usize, String)>,
    /// Producers re-bound inside the body program for nested projections.
    producers: Vec<String>,
    counter: usize,
}

impl TileLowering {
    fn lower_pieces(
        &mut self,
        compiler: &mut Compiler,
        asm: &mut PolydatAssembler,
        pieces: &[TilePiece],
        mut body: Option<&mut BodyContext>,
    ) -> Result<Vec<TileOp>, String> {
        let mut ops: Vec<TileOp> = Vec::new();
        let mut static_buf = String::new();
        let flush = |buf: &mut String, ops: &mut Vec<TileOp>| {
            if !buf.is_empty() {
                ops.push(TileOp::Static(std::mem::take(buf)));
            }
        };
        for piece in pieces {
            match piece {
                TilePiece::Static(s) => {
                    self.track_string_state(s);
                    static_buf.push_str(s);
                }
                TilePiece::Hole(h) => {
                    flush(&mut static_buf, &mut ops);
                    let enc = HoleEncoding {
                        encoding: self.encoding.clone(),
                        position: self.position(),
                        ty: h.decl_type.clone(),
                        format: h.format.clone(),
                        raw: h.raw,
                        cond: false,
                    };
                    let source = match body.as_deref_mut() {
                        Some(ctx) => self.body_hole(compiler, asm, &h.text, &h.expr, enc, ctx)?,
                        None => self.wire_hole(compiler, asm, &h.text, &h.expr, enc)?,
                    };
                    ops.push(TileOp::Hole(source));
                }
                TilePiece::Branch {
                    cond,
                    then,
                    otherwise,
                    ..
                } => {
                    flush(&mut static_buf, &mut ops);
                    let enc = HoleEncoding {
                        encoding: self.encoding.clone(),
                        position: HolePosition::Text,
                        ty: None,
                        format: None,
                        raw: true,
                        cond: true,
                    };
                    let text = format!("if {}", super::pprint::pp_expr(cond));
                    let source = match body.as_deref_mut() {
                        Some(ctx) => self.body_hole(compiler, asm, &text, cond, enc, ctx)?,
                        None => self.wire_hole(compiler, asm, &text, cond, enc)?,
                    };
                    let saved = self.in_string;
                    let then_ops = self.lower_pieces(compiler, asm, then, body.as_deref_mut())?;
                    self.in_string = saved;
                    let else_ops = match otherwise {
                        Some(o) => self.lower_pieces(compiler, asm, o, body.as_deref_mut())?,
                        None => Vec::new(),
                    };
                    self.in_string = saved;
                    ops.push(TileOp::Branch {
                        cond: source,
                        then: then_ops,
                        otherwise: else_ops,
                    });
                }
                TilePiece::Projection {
                    source,
                    sep,
                    body: proj_body,
                    ..
                } => {
                    flush(&mut static_buf, &mut ops);
                    if let Some(ctx) = body.as_deref_mut() {
                        let op =
                            self.nested_projection(compiler, asm, source, sep, proj_body, ctx)?;
                        ops.push(op);
                        continue;
                    }
                    // The same resolution the `for` construct uses, so
                    // inline text, bound producers, and derivations
                    // (`base where ... order ...`) all project.
                    let comprehension =
                        super::traversal::resolve_source(source, &compiler.producers_seen)
                            .map_err(|e| format!("tile '{}': projection: {e}", self.tile_name))?;
                    self.check_bounded(&comprehension, &source.text)?;
                    self.check_predicates(&comprehension, &source.text)?;
                    let stream =
                        StreamerValue::new(source.text.clone(), comprehension.clone()).to_json();
                    let mut probe = |expr: &str| self.generator_type(compiler, asm, expr, None);
                    let elements = super::traversal::element_types(&comprehension, &mut probe)
                        .map_err(|e| {
                            format!(
                                "tile '{}': projection `for {}`: {e}",
                                self.tile_name, source.text
                            )
                        })?;
                    // Generator-call sources are expressions over the
                    // enclosing scope: each compiles to a wire here and
                    // its value reaches the render node as an input, so
                    // the projection needs no kernel of its own.
                    let mut generators = Vec::new();
                    for (name, expr_text) in generator_clauses(&comprehension) {
                        if expr_text.contains('{') {
                            return Err(format!(
                                "tile '{}': projection `for {}`: generator `{expr_text}` uses a `{{name}}` placeholder; \
                                 in a tile the generator is an expression over the scope's wires, so name the wire directly",
                                self.tile_name, source.text
                            ));
                        }
                        let expr = super::tile::parse_hole_expr(&expr_text).map_err(|e| {
                            format!(
                                "tile '{}': projection `for {}`: generator `{expr_text}`: {e}",
                                self.tile_name, source.text
                            )
                        })?;
                        let wire = self.next_name("g");
                        compiler
                            .compile_binding(asm, std::slice::from_ref(&wire), &expr)
                            .map_err(|e| {
                                format!(
                                    "tile '{}': projection `for {}`: generator `{expr_text}`: {e}",
                                    self.tile_name, source.text
                                )
                            })?;
                        let idx = self.push_input(wire);
                        let ty = elements
                            .iter()
                            .find(|(n, _)| *n == name)
                            .map(|(_, t)| t.to_keyword())
                            .unwrap_or("str");
                        generators.push((name, idx, ty.to_string()));
                    }
                    let mut ctx = BodyContext {
                        elements,
                        bindings: Vec::new(),
                        cascade: Vec::new(),
                        producers: Vec::new(),
                        counter: 0,
                    };
                    let saved = self.in_string;
                    let body_ops = self.lower_pieces(compiler, asm, proj_body, Some(&mut ctx))?;
                    self.in_string = saved;
                    // The body's own input is the tuple index; the
                    // program's `cycle` reaches it as a cascaded extern
                    // like any other outer wire.
                    let mut src = String::from("input __tuple: u64\n");
                    for (name, ty) in &ctx.elements {
                        src.push_str(&format!("extern {name}: {}\n", ty.to_keyword()));
                    }
                    for (name, _, ty) in &ctx.cascade {
                        src.push_str(&format!("extern {name}: {ty}\n"));
                    }
                    for b in &ctx.bindings {
                        src.push_str(b);
                        src.push('\n');
                    }
                    // Compile the body once here so a bad reference is a
                    // compile error of the enclosing program, not a
                    // failure inside `tile_render` construction.
                    super::compile_polydat(&src)
                        .map_err(|e| format!("tile '{}': projection body: {e}", self.tile_name))?;
                    self.children.push(ChildSpec {
                        source: src,
                        cascade: ctx.cascade,
                    });
                    let child = self.children.len() - 1;
                    let default_sep = match self.encoding.as_str() {
                        "json" | "csv" => ",",
                        _ => "",
                    };
                    ops.push(TileOp::Repeat {
                        stream,
                        child,
                        sep: sep.clone().unwrap_or_else(|| default_sep.to_string()),
                        body: body_ops,
                        generators,
                    });
                }
            }
        }
        flush(&mut static_buf, &mut ops);
        Ok(ops)
    }

    /// The compile-time type of a hole expression (SRD 114 ยง4.2): the
    /// same inference every binding gets, with projection elements and
    /// cascaded outer wires resolved from the body context.
    fn infer_type(
        &self,
        compiler: &Compiler,
        asm: &PolydatAssembler,
        expr: &Expr,
        ctx: Option<&BodyContext>,
    ) -> PortType {
        if let Expr::Ident(name, _) = expr
            && let Some(ctx) = ctx
        {
            if let Some((_, t)) = ctx.elements.iter().find(|(n, _)| n == name) {
                return *t;
            }
            if let Some((_, _, t)) = ctx.cascade.iter().find(|(n, _, _)| n == name) {
                return PortType::from_keyword(t).unwrap_or(PortType::U64);
            }
        }
        super::binding::infer_expr_type(expr, asm, &compiler.input_names)
    }

    /// The element type of a generator-call source. In a tile the
    /// generator is an expression over the enclosing scope, so it is
    /// typed by the same inference as a hole; the isolated probe the
    /// `for` construct uses is the fallback when it does not parse.
    fn generator_type(
        &self,
        compiler: &Compiler,
        asm: &PolydatAssembler,
        expr: &str,
        ctx: Option<&BodyContext>,
    ) -> Result<PortType, String> {
        match super::tile::parse_hole_expr(expr) {
            Ok(e) => Ok(self.infer_type(compiler, asm, &e, ctx)),
            Err(_) => compiler.probe_element_type(expr),
        }
    }

    /// Type one hole (SRD 114 ยง4): the declared type wins and must be
    /// reachable from the wire type through the assembler's own adapter
    /// catalog; otherwise the wire type stands. Strict mode rejects the
    /// adapter a declaration would need.
    fn type_hole(
        &self,
        text: &str,
        wire: PortType,
        declared: Option<&str>,
        position: HolePosition,
        cond: bool,
    ) -> Result<HoleTyping, String> {
        let tile = &self.tile_name;
        let expectation = if cond {
            "a truth value"
        } else {
            match (self.encoding.as_str(), position) {
                ("json", HolePosition::Value) => "any JSON value",
                ("json", HolePosition::InString) => "text inside a JSON string",
                ("csv", _) => "a CSV field",
                _ => "text",
            }
        };
        if matches!(
            wire,
            PortType::Ext
                | PortType::Handle
                | PortType::Reg128
                | PortType::RegI8x16
                | PortType::RegI16x8
                | PortType::RegI32x4
                | PortType::RegI64x2
                | PortType::RegF16x8
                | PortType::RegF32x4
                | PortType::RegF64x2
        ) {
            return Err(format!(
                "tile '{tile}': hole `{text}`: a {} wire has no text form and cannot fill a hole",
                wire.to_keyword()
            ));
        }
        let declared_ty = match declared {
            Some(kw) => Some(
                PortType::from_keyword(kw)
                    .ok_or_else(|| format!("tile '{tile}': hole `{text}`: unknown type '{kw}'"))?,
            ),
            None => None,
        };
        let mut adapter = None;
        if let Some(to) = declared_ty
            && to != wire
        {
            if crate::compile::assembly::auto_adapter(wire, to).is_none() {
                return Err(format!(
                    "tile '{tile}': hole `{text}`: no conversion from the wire type {} to the declared type {}; \
                     write the conversion explicitly in the expression",
                    wire.to_keyword(),
                    to.to_keyword()
                ));
            }
            if self.strict {
                return Err(format!(
                    "tile '{tile}': hole `{text}`: strict mode rejects the implicit {} -> {} adapter the declaration needs; \
                     write the conversion explicitly in the expression",
                    wire.to_keyword(),
                    to.to_keyword()
                ));
            }
            adapter = Some((wire, to));
        }
        Ok(HoleTyping {
            wire,
            declared: declared_ty,
            effective: declared_ty.unwrap_or(wire),
            adapter,
            expectation,
        })
    }

    /// Record how a hole was typed for `explain tiles` (SRD 114 ยง4.4).
    fn record(&self, compiler: &mut Compiler, text: &str, typing: &HoleTyping, enc: &HoleEncoding) {
        let kw = typing.effective.to_keyword();
        let mut encoder = if enc.cond {
            "truth value as 1 or 0".to_string()
        } else if enc.raw {
            "raw text, no escaping".to_string()
        } else {
            match (enc.encoding.as_str(), enc.position) {
                ("json", HolePosition::Value) => match typing.effective {
                    PortType::Str | PortType::Bytes => {
                        "json string, quoted and escaped".to_string()
                    }
                    PortType::Bool => "json boolean".to_string(),
                    PortType::Json => "json value, serialized".to_string(),
                    t if is_numeric(t) => "json number".to_string(),
                    _ => "json string, quoted and escaped".to_string(),
                },
                ("json", HolePosition::InString) => "json escaped text".to_string(),
                ("csv", _) => "csv field, quoted when needed".to_string(),
                _ => "display text".to_string(),
            }
        };
        if let Some(f) = &enc.format {
            encoder.push_str(&format!(", format {f}"));
        }
        compiler
            .tile_events
            .push(super::events::CompileEvent::TileHoleTyped {
                tile: self.tile_name.clone(),
                hole: text.to_string(),
                wire_type: typing.wire.to_keyword().to_string(),
                declared: typing.declared.map(|t| t.to_keyword().to_string()),
                expectation: format!("{} ({kw})", typing.expectation),
                encoder,
                adapter: typing
                    .adapter
                    .map(|(f, t)| format!("{} -> {}", f.to_keyword(), t.to_keyword())),
            });
    }

    /// A hole in the tile's own scope: the expression as a binding of
    /// the enclosing program, `__tile_<name>_hN := expr`, with the
    /// declaration's adapter node between when one is needed; a hole
    /// that names a wire uses the wire itself. The render node takes
    /// the value and encodes it at the hole (SRD 117 step 1).
    fn wire_hole(
        &mut self,
        compiler: &mut Compiler,
        asm: &mut PolydatAssembler,
        text: &str,
        expr: &Expr,
        mut enc: HoleEncoding,
    ) -> Result<HoleSource, String> {
        let wire = self.infer_type(compiler, asm, expr, None);
        let typing = self.type_hole(text, wire, enc.ty.as_deref(), enc.position, enc.cond)?;
        enc.ty = Some(typing.effective.to_keyword().to_string());
        let name = self.next_name("h");
        let err = |e: String| format!("tile '{}': hole `{text}`: {e}", self.tile_name);
        let value = match typing.adapter {
            Some((from, to)) => {
                let vname = format!("{name}_v");
                compiler
                    .compile_binding(asm, std::slice::from_ref(&vname), expr)
                    .map_err(err)?;
                let aname = format!("{name}_a");
                let node = crate::compile::assembly::auto_adapter(from, to)
                    .expect("adapter checked by type_hole");
                asm.add_node(
                    &aname,
                    node,
                    vec![crate::compile::assembly::WireRef::node(&vname)],
                );
                compiler.all_names.push(aname.clone());
                Expr::Ident(aname, self.span)
            }
            None => expr.clone(),
        };
        let input = match &value {
            Expr::Ident(wire, _) if asm.output_type(wire.as_str()).is_some() => wire.clone(),
            _ => {
                compiler
                    .compile_binding(asm, std::slice::from_ref(&name), &value)
                    .map_err(err)?;
                name
            }
        };
        self.record(compiler, text, &typing, &enc);
        let index = self.push_input(input);
        Ok(HoleSource::Wire {
            index,
            spec: enc.to_spec(),
        })
    }

    /// A hole inside a projection body: a binding of the child program
    /// whose value the render node encodes at the hole. Outer names it
    /// references cascade in as render node inputs, typed by the
    /// parent's wire.
    fn body_hole(
        &mut self,
        compiler: &mut Compiler,
        asm: &mut PolydatAssembler,
        text: &str,
        expr: &Expr,
        mut enc: HoleEncoding,
        ctx: &mut BodyContext,
    ) -> Result<HoleSource, String> {
        let mut refs = BTreeSet::new();
        collect_expr_refs(expr, &mut refs);
        for r in refs {
            self.cascade_ref(asm, r, ctx);
        }
        let wire = self.infer_type(compiler, asm, expr, Some(ctx));
        let typing = self.type_hole(text, wire, enc.ty.as_deref(), enc.position, enc.cond)?;
        enc.ty = Some(typing.effective.to_keyword().to_string());
        let value = match typing.adapter {
            // The child is source text, so the adapter is the `as`
            // fusion, which inserts the same catalog adapter the parent
            // scope would.
            Some((_, to)) => Expr::Cast(Box::new(expr.clone()), to, self.span),
            None => expr.clone(),
        };
        let name = format!("__b{}", ctx.counter);
        ctx.counter += 1;
        ctx.bindings
            .push(format!("{name} := {}", super::pprint::pp_expr(&value)));
        self.record(compiler, text, &typing, &enc);
        Ok(HoleSource::Child {
            name,
            spec: enc.to_spec(),
        })
    }

    /// Make an outer name the body references available inside the body
    /// program as a cascaded extern typed by the parent's wire. Names
    /// the body defines itself, and names the parent does not define,
    /// are left alone; the latter surface as unknown wires when the
    /// body compiles.
    fn cascade_ref(&mut self, asm: &PolydatAssembler, r: String, ctx: &mut BodyContext) {
        if ctx.elements.iter().any(|(n, _)| *n == r)
            || ctx.cascade.iter().any(|(n, _, _)| *n == r)
            || ctx.producers.contains(&r)
        {
            return;
        }
        // An extern carries its declared type; a coordinate input is u64.
        let ty = asm.node_output_type(&r).or_else(|| {
            if asm.input_names().iter().any(|n| *n == r) {
                Some(asm.input_type(&r).unwrap_or(PortType::U64))
            } else {
                None
            }
        });
        if let Some(ty) = ty {
            let idx = self.push_input(r.clone());
            ctx.cascade.push((r, idx, ty.to_keyword().to_string()));
        }
    }

    /// SRD 114 ยง5.4: a projection's comprehension must have bounded
    /// cardinality.
    fn check_bounded(
        &self,
        c: &crate::iteration::comprehension::Comprehension,
        text: &str,
    ) -> Result<(), String> {
        use crate::iteration::comprehension::Comprehension as K;
        use crate::iteration::comprehension::cardinality::CardinalityClass as C;
        use crate::iteration::comprehension::strategy::StrategyName as S;
        // Sampling a continuous source needs a space-filling strategy;
        // say so here rather than when the first render fails.
        if let K::Order {
            child, strategy, ..
        } = c
            && crate::iteration::comprehension::runtime::continuous_axes(child).is_some()
            && !matches!(strategy, S::Halton | S::Sobol | S::Lhs | S::Shuffle)
        {
            return Err(format!(
                "tile '{}': projection `for {text}` orders a continuous source with `{strategy:?}`; \
                 a continuous source needs a sampling strategy: halton, sobol, lhs, or shuffle",
                self.tile_name
            ));
        }
        match c.metadata().cardinality {
            C::Bounded(_) | C::BoundedAtMost(_) => Ok(()),
            // A generator or parameter list with no cardinality hint
            // reports unbounded because its count is unknown, not
            // infinite. Its finiteness is the source's own contract, as
            // it is for the `for` construct, so it projects.
            C::Unbounded if has_unhinted_source(c) => Ok(()),
            C::Unbounded => Err(format!(
                "tile '{}': projection `for {text}` has unbounded cardinality; a projection renders once per tuple and needs a finite source",
                self.tile_name
            )),
            // A continuous interval has no finite tuple set of its own;
            // an order strategy with a count samples one.
            C::Continuous { .. } | C::ContinuousAtMost { .. } | C::Hybrid(_) => Err(format!(
                "tile '{}': projection `for {text}` ranges over a continuous source, which has no finite tuple set; \
                 add `order <strategy>/<count>` (halton, sobol, lhs, or shuffle) to sample that many points",
                self.tile_name
            )),
        }
    }

    /// A filter predicate may name the comprehension's own elements;
    /// a `{name}` for a wire outside it has no value at render time.
    fn check_predicates(
        &self,
        c: &crate::iteration::comprehension::Comprehension,
        text: &str,
    ) -> Result<(), String> {
        let elements = c.coordinate_names();
        for pred in filter_predicates(c) {
            for name in placeholder_names(&pred) {
                if !elements.contains(&name) {
                    return Err(format!(
                        "tile '{}': projection `for {text}`: predicate placeholder `{{{name}}}` names a wire outside the comprehension; \
                         a projection's predicate sees only its elements",
                        self.tile_name
                    ));
                }
            }
        }
        Ok(())
    }

    /// A projection inside a projection body (SRD 114 ยง5.4). The body
    /// program is Polydat source, so the nested projection becomes a
    /// `tile` statement inside it, compiled by this same lowering one
    /// level down; the outer body reads the nested tile's text through a
    /// raw hole. Producers the nested source names are re-bound inside
    /// the body program from their original text.
    fn nested_projection(
        &mut self,
        compiler: &mut Compiler,
        asm: &mut PolydatAssembler,
        source: &ForSource,
        sep: &Option<String>,
        body: &[TilePiece],
        ctx: &mut BodyContext,
    ) -> Result<TileOp, String> {
        // Validate the nested source here, in the enclosing scope, so a
        // bad source is this tile's error and its element names are known.
        let comprehension = super::traversal::resolve_source(source, &compiler.producers_seen)
            .map_err(|e| format!("tile '{}': nested projection: {e}", self.tile_name))?;
        self.check_bounded(&comprehension, &source.text)?;
        self.check_predicates(&comprehension, &source.text)?;
        let mut probe = |expr: &str| self.generator_type(compiler, asm, expr, Some(&*ctx));
        let nested_elements =
            super::traversal::element_types(&comprehension, &mut probe).map_err(|e| {
                format!(
                    "tile '{}': nested projection `for {}`: {e}",
                    self.tile_name, source.text
                )
            })?;
        // Generator expressions of the nested source read the scope's
        // wires; they must reach the body program too.
        for (_, expr_text) in generator_clauses(&comprehension) {
            if let Ok(expr) = super::tile::parse_hole_expr(&expr_text) {
                let mut refs = BTreeSet::new();
                collect_expr_refs(&expr, &mut refs);
                for r in refs {
                    self.cascade_ref(asm, r, ctx);
                }
            }
        }
        // Producers the source names, directly or as a derivation base,
        // are re-bound in the body program from their original text.
        let producer = match &source.kind {
            ForSourceKind::Producer(n) | ForSourceKind::Derived { base: n, .. } => Some(n.clone()),
            ForSourceKind::Comprehension(_) => None,
        };
        if let Some(name) = producer
            && !ctx.producers.contains(&name)
        {
            let p = compiler
                .producers_seen
                .iter()
                .rev()
                .find(|p| p.name == name)
                .cloned()
                .ok_or_else(|| {
                    format!(
                        "tile '{}': producer '{name}' is not bound before this tile",
                        self.tile_name
                    )
                })?;
            for r in placeholder_names(&p.source_text) {
                self.cascade_ref(asm, r, ctx);
            }
            ctx.producers.push(name.clone());
            ctx.bindings
                .push(format!("{name} := for {}", p.source_text));
        }
        for r in placeholder_names(&source.text) {
            self.cascade_ref(asm, r, ctx);
        }
        // Every outer wire the nested body reads must reach the body
        // program; the nested tile then cascades it one level further.
        let mut refs = BTreeSet::new();
        collect_piece_refs(body, &compiler.producers_seen, &mut refs);
        for r in refs {
            if nested_elements.iter().any(|(n, _)| *n == r) {
                continue;
            }
            self.cascade_ref(asm, r, ctx);
        }
        let piece = TilePiece::Projection {
            source: source.clone(),
            sep: sep.clone(),
            body: body.to_vec(),
            span: self.span,
        };
        let text = render_template(std::slice::from_ref(&piece), &self.options);
        let name = format!("__b{}", ctx.counter);
        ctx.counter += 1;
        let defaults = TileOptions::default();
        let mut opts = Vec::new();
        if self.options.open != defaults.open || self.options.close != defaults.close {
            opts.push(format!(
                "delims \"{}\" \"{}\"",
                escape_polydat_string(&self.options.open),
                escape_polydat_string(&self.options.close)
            ));
        }
        if self.options.sigil != defaults.sigil {
            opts.push(format!(
                "sigil \"{}\"",
                escape_polydat_string(&self.options.sigil)
            ));
        }
        if self.strict {
            opts.push("strict".to_string());
        }
        // Inside a JSON string the nested tile's holes must escape as
        // text from its first byte; the option carries the position.
        if self.in_string {
            opts.push("instring".to_string());
        }
        let opts = if opts.is_empty() {
            String::new()
        } else {
            format!(" ({})", opts.join(", "))
        };
        ctx.bindings.push(format!(
            "tile {name} : {}{opts} := \"{}\"",
            self.encoding,
            escape_polydat_string(&text)
        ));
        // The nested tile's text is complete: it is copied as it is.
        let raw = HoleEncoding {
            encoding: self.encoding.clone(),
            position: HolePosition::Text,
            ty: None,
            format: None,
            raw: true,
            cond: false,
        };
        Ok(TileOp::Hole(HoleSource::Child {
            name,
            spec: raw.to_spec(),
        }))
    }

    fn push_input(&mut self, name: String) -> usize {
        if let Some(i) = self.inputs.iter().position(|n| *n == name) {
            return i;
        }
        self.inputs.push(name);
        self.inputs.len() - 1
    }

    fn next_name(&mut self, kind: &str) -> String {
        let n = self.hole_counter;
        self.hole_counter += 1;
        format!("__tile_{}_{kind}{n}", self.tile_name)
    }

    fn position(&self) -> HolePosition {
        if self.encoding != "json" {
            HolePosition::Text
        } else if self.in_string {
            HolePosition::InString
        } else {
            HolePosition::Value
        }
    }

    /// Track whether static JSON text ends inside a string literal.
    fn track_string_state(&mut self, s: &str) {
        if self.encoding != "json" {
            return;
        }
        let mut escaped = false;
        for c in s.chars() {
            if self.in_string {
                if escaped {
                    escaped = false;
                } else if c == '\\' {
                    escaped = true;
                } else if c == '"' {
                    self.in_string = false;
                }
            } else if c == '"' {
                self.in_string = true;
            }
        }
    }
}

/// Counts over a skeleton for `explain tiles`.
#[derive(Default)]
struct SkeletonShape {
    statics: usize,
    static_bytes: usize,
    holes: usize,
    branches: usize,
    projections: usize,
}

impl SkeletonShape {
    fn count(&mut self, ops: &[TileOp]) {
        for op in ops {
            match op {
                TileOp::Static(s) => {
                    self.statics += 1;
                    self.static_bytes += s.len();
                }
                TileOp::Hole(_) => self.holes += 1,
                TileOp::Branch {
                    then, otherwise, ..
                } => {
                    self.branches += 1;
                    self.count(then);
                    self.count(otherwise);
                }
                TileOp::Repeat { body, .. } => {
                    self.projections += 1;
                    self.count(body);
                }
            }
        }
    }
}

/// Wire names a run of pieces references: hole expressions, branch
/// conditions, `{name}` placeholders in nested projection sources, and
/// the wires their generator expressions read.
fn collect_piece_refs(
    pieces: &[TilePiece],
    producers: &[super::traversal::Producer],
    out: &mut BTreeSet<String>,
) {
    for p in pieces {
        match p {
            TilePiece::Static(_) => {}
            TilePiece::Hole(h) => collect_expr_refs(&h.expr, out),
            TilePiece::Branch {
                cond,
                then,
                otherwise,
                ..
            } => {
                collect_expr_refs(cond, out);
                collect_piece_refs(then, producers, out);
                if let Some(o) = otherwise {
                    collect_piece_refs(o, producers, out);
                }
            }
            TilePiece::Projection { source, body, .. } => {
                out.extend(placeholder_names(&source.text));
                if let ForSourceKind::Producer(n) | ForSourceKind::Derived { base: n, .. } =
                    &source.kind
                {
                    out.insert(n.clone());
                }
                if let Ok(c) = super::traversal::resolve_source(source, producers) {
                    for (_, expr_text) in generator_clauses(&c) {
                        if let Ok(expr) = super::tile::parse_hole_expr(&expr_text) {
                            collect_expr_refs(&expr, out);
                        }
                    }
                }
                collect_piece_refs(body, producers, out);
            }
        }
    }
}

/// `(element, expression)` for each generator-call clause.
fn generator_clauses(c: &crate::iteration::comprehension::Comprehension) -> Vec<(String, String)> {
    use crate::iteration::comprehension::Comprehension as K;
    use crate::iteration::comprehension::source::Source;
    let mut out = Vec::new();
    match c {
        K::Clause {
            name,
            source: Source::Generator { expr, .. },
        } => out.push((name.clone(), expr.clone())),
        K::Clause { .. } => {}
        K::Cartesian { children } | K::Zip { children, .. } | K::Union { children } => {
            for ch in children {
                out.extend(generator_clauses(ch));
            }
        }
        K::Filter { child, .. } | K::Order { child, .. } => out.extend(generator_clauses(child)),
    }
    out
}

/// Every filter predicate in a comprehension.
fn filter_predicates(c: &crate::iteration::comprehension::Comprehension) -> Vec<String> {
    use crate::iteration::comprehension::Comprehension as K;
    let mut out = Vec::new();
    match c {
        K::Clause { .. } => {}
        K::Cartesian { children } | K::Zip { children, .. } | K::Union { children } => {
            for ch in children {
                out.extend(filter_predicates(ch));
            }
        }
        K::Filter { child, predicate } => {
            out.push(predicate.clone());
            out.extend(filter_predicates(child));
        }
        K::Order { child, .. } => out.extend(filter_predicates(child)),
    }
    out
}

/// Whether a comprehension draws on a generator call or a workload
/// parameter list that carries no cardinality hint.
fn has_unhinted_source(c: &crate::iteration::comprehension::Comprehension) -> bool {
    use crate::iteration::comprehension::Comprehension as K;
    use crate::iteration::comprehension::source::Source;
    match c {
        K::Clause { source, .. } => matches!(
            source,
            Source::Generator {
                cardinality_hint: None,
                ..
            } | Source::WorkloadParamList { len_hint: None, .. }
        ),
        K::Cartesian { children } | K::Zip { children, .. } | K::Union { children } => {
            children.iter().any(has_unhinted_source)
        }
        K::Filter { child, .. } | K::Order { child, .. } => has_unhinted_source(child),
    }
}

/// `{name}` placeholders in comprehension text.
fn placeholder_names(text: &str) -> Vec<String> {
    let mut out = Vec::new();
    let chars: Vec<char> = text.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        if chars[i] == '{' {
            let start = i + 1;
            let mut j = start;
            while j < chars.len() && (chars[j].is_ascii_alphanumeric() || chars[j] == '_') {
                j += 1;
            }
            if j > start && chars.get(j) == Some(&'}') {
                out.push(chars[start..j].iter().collect());
                i = j;
            }
        }
        i += 1;
    }
    out
}

/// Escape text for a Polydat string literal.
fn escape_polydat_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\t', "\\t")
}

fn is_numeric(t: PortType) -> bool {
    matches!(
        t,
        PortType::U64
            | PortType::I64
            | PortType::F64
            | PortType::U32
            | PortType::I32
            | PortType::F32
            | PortType::U16
            | PortType::I16
            | PortType::U8
            | PortType::I8
            | PortType::F16
            | PortType::U128
            | PortType::I128
    )
}