polydat-core 0.12.0

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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Function registry: known function signatures for DSL validation.
//!
//! Each registered function declares its name, category, expected wire
//! inputs, constant parameters, output count, and variadic behavior.
//! The compiler uses this to validate calls at parse time and to
//! generically dispatch variadic functions.
//!
//! Categories are a type-safe enum — every function must declare one.
//! Categories group the registry for listings (`by_category`). The
//! stdlib files carry a `// @category: Name` line for readers;
//! `FuncCategory::parse` maps that text but no compile path consumes
//! it.
//!
//! Signatures are owned by their respective node modules. This file
//! defines the shared types and the collector function.

pub use crate::ast::CompileLevel;
use crate::compile::assembly::WireRef;
use crate::dsl::factories::NodeFactory;

/// Builder for a node module: `(build context, name, wires, resolved
/// wire port types, const args) -> Some(Ok(node)) / Some(Err(msg))`,
/// or `None` when the name isn't handled by this module. The build
/// context carries the bindings under construction and the program
/// tree's resource scope ([`crate::dsl::factory::BuildContext`]).
pub type NodeBuildFn = fn(
    &crate::dsl::factory::BuildContext,
    &str,
    &[WireRef],
    &[crate::ast::PortType],
    &[crate::dsl::factory::ConstArg],
) -> Option<Result<Box<dyn crate::ast::PolydatNode>, String>>;

/// A node module's registration: signatures + builder.
///
/// Each node module submits one of these at link time via `inventory::submit!`.
/// The runtime collects all submissions to build the function registry and
/// dispatch table without any explicit module list.
///
/// A registration is a [`NodeFactory`]: its `build` is the factory's
/// body, and the registry builds its nodes through that factory as it
/// builds every other node ([`crate::dsl::factory::build_node`]).
pub struct NodeRegistration {
    /// Returns the static slice of `FuncSig` entries for this module.
    pub signatures: fn() -> &'static [FuncSig],
    /// Builds a node for the given function name: the body of this
    /// registration's [`NodeFactory::build`].
    ///
    /// Returns `Some(Ok(node))` / `Some(Err(msg))` for a name it
    /// builds, and `None` for one it leaves to its signature: a
    /// variadic function then gets its signature's identity element or
    /// variadic constructor, and any other name is an unknown function.
    ///
    /// `wire_types[i]` is the resolved [`crate::ast::PortType`] of
    /// `wires[i]` — the output type of the upstream node feeding
    /// that wire input. Modules that build type-polymorphic nodes
    /// (e.g. `log_info`, whose output type equals its input type)
    /// read this to construct the node with the correct port
    /// types. Modules whose nodes have type-fixed signatures can
    /// ignore the slice. When the assembler can't resolve a
    /// wire's type (forward reference, dangling), the slot
    /// defaults to [`crate::ast::PortType::U64`].
    pub build: NodeBuildFn,
    /// Optional assembly-time validator for this module's constants.
    ///
    /// The factory calls this **before** `build` whenever the name
    /// matches one of this module's functions. Returning `Err` makes
    /// the compile fail with a structured `bad constant` error, so
    /// the node itself never sees a malformed literal and can keep
    /// its constructor and `eval()` branch-free.
    pub validate: Option<crate::dsl::const_constraints::NodeValidator>,
}

inventory::collect!(NodeRegistration);

impl NodeFactory for NodeRegistration {
    fn signatures(&self) -> &[FuncSig] {
        (self.signatures)()
    }

    fn validate(&self, name: &str, consts: &[crate::dsl::factory::ConstArg]) -> Result<(), String> {
        match self.validate {
            Some(validator) => validator(name, consts),
            None => Ok(()),
        }
    }

    /// The registered builder's node. A builder that declines a name its
    /// signatures list leaves a variadic function to its signature: the
    /// identity element for no wires, the variadic constructor otherwise.
    fn build(
        &self,
        ctx: &crate::dsl::factory::BuildContext,
        name: &str,
        wires: &[WireRef],
        wire_types: &[crate::ast::PortType],
        consts: &[crate::dsl::factory::ConstArg],
    ) -> Result<Box<dyn crate::ast::PolydatNode>, String> {
        if let Some(result) = (self.build)(ctx, name, wires, wire_types, consts) {
            return result;
        }
        let sig = (self.signatures)().iter().find(|s| s.name == name);
        match sig {
            Some(sig) => crate::dsl::factory::variadic_node(sig, wires.len()),
            None => Err(format!("unknown function: '{name}'")),
        }
    }
}

/// A host's own [`NodeFactory`], linked into the registry.
///
/// A host submits one at link time, as a node module submits its
/// [`NodeRegistration`]:
///
/// ```ignore
/// static FACTORY: MyFactory = MyFactory;
/// polydat::inventory::submit! {
///     polydat::dsl::registry::FactoryRegistration { factory: &FACTORY }
/// }
/// ```
///
/// The registry lists the factory's signatures and builds every call to
/// one of them through its `build`, on every engine.
pub struct FactoryRegistration {
    /// The factory.
    pub factory: &'static dyn NodeFactory,
}

inventory::collect!(FactoryRegistration);

/// Every factory linked into the registry: each node module's
/// [`NodeRegistration`], then each host [`FactoryRegistration`]. A
/// function name belongs to the first factory whose signatures list it.
pub fn factories() -> impl Iterator<Item = &'static dyn NodeFactory> {
    inventory::iter::<NodeRegistration>
        .into_iter()
        .map(|reg| reg as &'static dyn NodeFactory)
        .chain(
            inventory::iter::<FactoryRegistration>
                .into_iter()
                .map(|reg| reg.factory),
        )
}

/// A function name that stands for one node per argument type, as
/// `vec_dot` stands for `vec_dot_f32` and `vec_dot_f64`
/// (polydat_grammar.md §18.1, `T-Call-OverloadResolve`).
///
/// A call to the name builds, among the forms every operand reaches by
/// its own type or through a lossless adapter, the one agreeing with
/// the most of the call's wire types, position by position, and the
/// earliest such form when several agree with as many
/// ([`Overload::resolve`]). The assembler's adapters then reach the
/// form's other ports, as they reach any node's. So `vec_scale(v, 2)`
/// over a `vec_f64` builds the f64 form and widens the integer, and
/// `vec_dot(a_f64, b_f32)` builds the f64 form and widens `b`, rather
/// than narrowing an operand to reach a form. The first form is the one
/// the name meant before it had others, so operands that reach every
/// form equally build it, and a program that compiled against the name
/// still does. A node module submits one at
/// link time:
///
/// ```ignore
/// polydat::inventory::submit! {
///     Overload::new(
///         "vec_dot",
///         &[
///             OverloadForm::new(&[PortType::VecF32, PortType::VecF32], "vec_dot_f32"),
///             OverloadForm::new(&[PortType::VecF64, PortType::VecF64], "vec_dot_f64"),
///         ],
///     )
/// }
/// ```
///
/// Every form takes the same parameters in the same order, so the name's
/// parameters are those of any form; only the wires' types differ.
///
/// Built with [`Overload::new`], a `const fn`, so a field added later
/// does not break the hosts that submit one.
#[non_exhaustive]
pub struct Overload {
    /// The name programs call.
    pub name: &'static str,
    /// The forms the name resolves to, the default first.
    pub forms: &'static [OverloadForm],
}

/// One form of an [`Overload`]: the registered function a call builds
/// when its wires have these types. Built with [`OverloadForm::new`].
#[non_exhaustive]
pub struct OverloadForm {
    /// The port types of the form's wire inputs, in order.
    pub wires: &'static [crate::ast::PortType],
    /// The registered function this form builds.
    pub func: &'static str,
}

impl OverloadForm {
    /// The form that builds `func` for wires of the types `wires`.
    pub const fn new(wires: &'static [crate::ast::PortType], func: &'static str) -> Self {
        Self { wires, func }
    }
}

inventory::collect!(Overload);

impl Overload {
    /// The overloaded name `name` of `forms`, the default first. A
    /// node module submits it with `inventory::submit!`.
    pub const fn new(name: &'static str, forms: &'static [OverloadForm]) -> Self {
        Self { name, forms }
    }

    /// The function a call whose wires have `wire_types` builds.
    ///
    /// Among the forms every wire reaches, by its own type or through
    /// a lossless adapter ([`auto_adapter`]), the one agreeing with the
    /// most of the wire types, the earliest among equals. So
    /// `vec_dot(a_f64, b_f32)` builds the f64 form and widens `b`,
    /// rather than narrowing `a`. When no form is reachable so, the
    /// form agreeing with the most wire types, the earliest among
    /// equals, and the assembler names the operand it cannot connect.
    ///
    /// [`auto_adapter`]: crate::compile::assembly::auto_adapter
    pub fn resolve(&self, wire_types: &[crate::ast::PortType]) -> &'static str {
        let agreement = |form: &OverloadForm| {
            form.wires
                .iter()
                .zip(wire_types)
                .filter(|(declared, given)| declared == given)
                .count()
        };
        let reachable = |form: &OverloadForm| {
            form.wires.iter().zip(wire_types).all(|(declared, given)| {
                declared == given
                    || crate::compile::assembly::auto_adapter(*given, *declared).is_some()
            })
        };
        let best_of = |forms: &mut dyn Iterator<Item = &'static OverloadForm>| {
            let mut best: Option<&'static OverloadForm> = None;
            for form in forms {
                if best.is_none_or(|b| agreement(form) > agreement(b)) {
                    best = Some(form);
                }
            }
            best
        };
        best_of(&mut self.forms.iter().filter(|f| reachable(f)))
            .or_else(|| best_of(&mut self.forms.iter()))
            .map_or(self.forms[0].func, |form| form.func)
    }

    /// The signature of the first form, which a call to the name is
    /// checked against before its wire types are known: every form
    /// shares its parameters. Its `name` is the form's, such as
    /// `vec_dot_f32`, not the overload's. `None` only when the first
    /// form names no registered function.
    pub fn signature(&self) -> Option<&'static FuncSig> {
        lookup_registered(self.forms[0].func)
    }

    /// The category the name is listed under: its first form's, or
    /// [`FuncCategory::Uncategorised`] when the first form names no
    /// registered function.
    pub fn category(&self) -> FuncCategory {
        self.signature()
            .map_or(FuncCategory::Uncategorised, |sig| sig.category)
    }
}

/// Every overloaded name linked into the registry.
pub fn overloads() -> impl Iterator<Item = &'static Overload> {
    inventory::iter::<Overload>.into_iter()
}

/// The overloaded name `name`, if it is one.
pub fn overload(name: &str) -> Option<&'static Overload> {
    overloads().find(|o| o.name == name)
}

/// The function a call to `name` with wires of `wire_types` builds:
/// the resolved form when `name` is an [`Overload`], `name` itself
/// otherwise.
pub fn resolve_call<'a>(name: &'a str, wire_types: &[crate::ast::PortType]) -> &'a str {
    overload(name).map_or(name, |o| o.resolve(wire_types))
}

/// Every overloaded name, grouped by its [`Overload::category`] in
/// display order and sorted by name within a category: the names
/// [`by_category`] leaves out, since it lists registered functions.
/// A listing of what a program can call shows both.
///
/// An overload whose first form names no registered function is
/// listed under [`FuncCategory::Uncategorised`], last; nothing can call
/// it, and [`unlisted_overloads`] names those registration errors.
pub fn overloads_by_category() -> Vec<(FuncCategory, Vec<&'static Overload>)> {
    let mut groups: std::collections::HashMap<FuncCategory, Vec<&'static Overload>> =
        std::collections::HashMap::new();
    for o in overloads() {
        groups.entry(o.category()).or_default().push(o);
    }
    FuncCategory::display_order()
        .iter()
        .filter_map(|cat| {
            groups.remove(cat).map(|mut names| {
                names.sort_unstable_by_key(|o| o.name);
                (*cat, names)
            })
        })
        .collect()
}

/// Every overload whose first form names no registered function,
/// sorted by name: a registration error, such as a misspelled form or
/// a form whose module is not linked. [`lookup`] answers `None` for
/// these names and [`overloads_by_category`] lists them under
/// [`FuncCategory::Uncategorised`], so a host checks that this is
/// empty, in a test or at startup, to find its own broken
/// registrations.
pub fn unlisted_overloads() -> Vec<&'static Overload> {
    let mut unlisted: Vec<_> = overloads().filter(|o| o.signature().is_none()).collect();
    unlisted.sort_unstable_by_key(|o| o.name);
    unlisted
}

/// Register a node module's signatures and builder with the Polydat runtime.
///
/// Place this call at module scope in each node module. The inventory crate
/// arranges for the registration to run before `main` so that `registry()`
/// and `build_node()` see all entries.
///
/// Two forms:
///
/// - `register_nodes!(signatures, build_node)` — no assembly-time
///   validation. The builder is responsible for handling any bad
///   input itself (usually by trusting the caller or panicking).
/// - `register_nodes!(signatures, build_node, validate_node)` — the
///   factory calls `validate_node(name, consts)` before `build_node`.
///   Use this to declare [`ConstConstraint`]-style checks so
///   constructors can stay infallible.
///
/// [`ConstConstraint`]: crate::dsl::const_constraints::ConstConstraint
#[macro_export]
macro_rules! register_nodes {
    ($sigs:expr, $builder:expr) => {
        inventory::submit! {
            $crate::dsl::registry::NodeRegistration {
                signatures: $sigs,
                build: $builder,
                validate: None,
            }
        }
    };
    ($sigs:expr, $builder:expr, $validator:expr) => {
        inventory::submit! {
            $crate::dsl::registry::NodeRegistration {
                signatures: $sigs,
                build: $builder,
                validate: Some($validator),
            }
        }
    };
}

/// Functional category for a Polydat node function.
///
/// Every native node and stdlib module belongs to exactly one category.
/// Categories group the registry for listings (`by_category`) and provide
/// semantic organization for documentation and discovery.
///
/// The set grows as the library does, so a `match` over it outside
/// this crate needs a `_` arm.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FuncCategory {
    /// Core deterministic hashing.
    Hashing,
    /// Integer arithmetic with constant parameters.
    Arithmetic,
    /// Comparison and selection: ==, !=, <, >, <=, >=, if(...).
    /// Comparison nodes return u64 truth values (0 or 1); select
    /// nodes pick between two operand values based on a u64 cond.
    Comparison,
    /// Variadic N-ary operations (sum, product, min, max).
    Variadic,
    /// Type conversions between u64, f64, String, etc.
    Conversions,
    /// Statistical distribution LUT builders and samplers.
    Distributions,
    /// Date and time generation and decomposition.
    Datetime,
    /// HTML, URL, hex, base64 encoding/decoding.
    Encoding,
    /// Linear interpolation, range mapping, quantization.
    Interpolation,
    /// Trigonometric and mathematical functions (sin, cos, sqrt, etc.).
    Math,
    /// Probability modeling: coins, selection, conditionals.
    Probability,
    /// Weighted categorical selection.
    Weighted,
    /// Printf-style and structured string formatting.
    Formatting,
    /// String generation: combinations, number words.
    String,
    /// JSON construction, serialization, merging.
    Json,
    /// Byte buffer construction and manipulation.
    ByteBuffers,
    /// Cryptographic and non-cryptographic digests.
    Digest,
    /// Coherent noise: Perlin, simplex.
    Noise,
    /// Regular expression matching and substitution.
    Regex,
    /// Bijective permutations and shuffles.
    Permutation,
    /// Real-world data: names, places, codes.
    RealData,
    /// Non-deterministic context: wall clock, counters.
    Context,
    /// Debugging and introspection.
    Diagnostic,
    /// File-based data access: CSV, JSONL, text files.
    Data,
    /// No category: an [`Overload`] whose first form names no
    /// registered function, listed here by [`overloads_by_category`]
    /// so a listing shows the broken registration instead of leaving
    /// it out ([`unlisted_overloads`]). No registered function has it.
    Uncategorised,
}

impl FuncCategory {
    /// Display name for the category (used in describe output).
    pub fn display_name(&self) -> &'static str {
        match self {
            Self::Hashing => "Hashing",
            Self::Arithmetic => "Arithmetic",
            Self::Comparison => "Comparison",
            Self::Variadic => "Variadic",
            Self::Conversions => "Conversions",
            Self::Distributions => "Distributions",
            Self::Datetime => "Datetime",
            Self::Encoding => "Encoding",
            Self::Interpolation => "Interpolation",
            Self::Math => "Math",
            Self::Probability => "Probability",
            Self::Weighted => "Weighted",
            Self::Formatting => "Formatting",
            Self::String => "String",
            Self::Json => "JSON",
            Self::ByteBuffers => "Byte Buffers",
            Self::Digest => "Digest",
            Self::Noise => "Noise",
            Self::Regex => "Regex",
            Self::Permutation => "Permutation",
            Self::RealData => "Real Data",
            Self::Context => "Context",
            Self::Diagnostic => "Diagnostic",
            Self::Data => "Data",
            Self::Uncategorised => "Uncategorised",
        }
    }

    /// Parse a category name from a string (case-insensitive): the
    /// text of a stdlib file's `// @category: Name` line. No compile
    /// path consumes it.
    pub fn parse(s: &str) -> Option<Self> {
        match s.trim().to_lowercase().as_str() {
            "hashing" => Some(Self::Hashing),
            "arithmetic" => Some(Self::Arithmetic),
            "comparison" | "compare" => Some(Self::Comparison),
            "variadic" => Some(Self::Variadic),
            "conversions" | "conversion" => Some(Self::Conversions),
            "distributions" | "distribution" => Some(Self::Distributions),
            "datetime" | "date" | "time" => Some(Self::Datetime),
            "encoding" => Some(Self::Encoding),
            "interpolation" | "lerp" => Some(Self::Interpolation),
            "math" | "trig" | "trigonometry" => Some(Self::Math),
            "probability" => Some(Self::Probability),
            "weighted" => Some(Self::Weighted),
            "formatting" | "format" | "printf" => Some(Self::Formatting),
            "string" | "strings" => Some(Self::String),
            "json" => Some(Self::Json),
            "byte buffers" | "bytebuffers" | "bytes" => Some(Self::ByteBuffers),
            "digest" => Some(Self::Digest),
            "noise" => Some(Self::Noise),
            "regex" => Some(Self::Regex),
            "permutation" | "shuffle" => Some(Self::Permutation),
            "real data" | "realdata" | "realer" => Some(Self::RealData),
            "context" => Some(Self::Context),
            "diagnostic" | "diagnostics" | "debug" => Some(Self::Diagnostic),
            "data" | "datafile" | "csv" | "jsonl" => Some(Self::Data),
            "uncategorised" | "uncategorized" => Some(Self::Uncategorised),
            _ => None,
        }
    }

    /// Canonical ordering for display (same order as the enum definition).
    pub fn display_order() -> &'static [Self] {
        &[
            Self::Hashing,
            Self::Arithmetic,
            Self::Comparison,
            Self::Variadic,
            Self::Conversions,
            Self::Distributions,
            Self::Datetime,
            Self::Encoding,
            Self::Interpolation,
            Self::Math,
            Self::Probability,
            Self::Weighted,
            Self::Formatting,
            Self::String,
            Self::Json,
            Self::ByteBuffers,
            Self::Digest,
            Self::Noise,
            Self::Regex,
            Self::Permutation,
            Self::RealData,
            Self::Context,
            Self::Diagnostic,
            Self::Data,
            Self::Uncategorised,
        ]
    }
}

// ---------------------------------------------------------------------------
// Unified parameter specification (library_catalog.md, "Shapes")
// ---------------------------------------------------------------------------

use crate::ast::SlotType;

/// Describes one parameter in a function's call signature.
///
/// A "slot template" — the type-level version of a `Slot` without
/// a concrete value. Parameters are listed in positional order
/// matching the DSL syntax.
///
/// A parameter is built with [`ParamSpec::new`] or [`ParamSpec::wire`]
/// and the `with_` methods, all `const`, so a signature can live in a
/// `static`. The struct is `#[non_exhaustive]`: a field added later is
/// not a breaking change to a host that builds one.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ParamSpec {
    /// Parameter name (for error messages and describe output).
    pub name: &'static str,
    /// Wire or constant, and if constant, what type.
    pub slot_type: SlotType,
    /// Whether this parameter must be provided.
    pub required: bool,
    /// Example value for this parameter, as program text, used for
    /// probing compile level and for documentation. Wire params use
    /// `"cycle"`; a const param with a declared default uses that
    /// default, which passes validation; a const param without one is
    /// empty, since the signature offers no value to show.
    pub example: &'static str,
    /// Optional assembly-time validation rule. The factory enforces this before
    /// `build_node` so node constructors can stay infallible and
    /// branch-free at runtime. `None` = no constraint declared
    /// (default for wires and unconstrained constants).
    pub constraint: Option<crate::dsl::const_constraints::ConstConstraint>,
}

impl ParamSpec {
    /// A required parameter `name` of the slot type `slot_type`, with no
    /// constraint. Its example is `cycle` for a wire, which every
    /// program can pass, and empty for a constant, which has none to
    /// offer until [`Self::with_example`] gives one.
    pub const fn new(name: &'static str, slot_type: SlotType) -> Self {
        Self {
            name,
            slot_type,
            required: true,
            example: if matches!(slot_type, SlotType::Wire) {
                "cycle"
            } else {
                ""
            },
            constraint: None,
        }
    }

    /// A required wire parameter `name`.
    pub const fn wire(name: &'static str) -> Self {
        Self::new(name, SlotType::Wire)
    }

    /// The parameter, but one a call may leave out: a constant with a
    /// default, or a variadic tail that may be empty.
    pub const fn optional(mut self) -> Self {
        self.required = false;
        self
    }

    /// The parameter with `example` as its example value, written as
    /// program text.
    pub const fn with_example(mut self, example: &'static str) -> Self {
        self.example = example;
        self
    }

    /// The parameter with the constraint `c`, which the factory checks
    /// before it builds the node.
    pub const fn with_constraint(
        mut self,
        c: crate::dsl::const_constraints::ConstConstraint,
    ) -> Self {
        self.constraint = Some(c);
        self
    }
}

/// Arity specification for a function signature.
///
/// Describes which parts of the parameter list are fixed vs repeatable.
#[derive(Debug, Clone, Default)]
pub enum Arity {
    /// Exactly the parameters declared in `params`.
    #[default]
    Fixed,
    /// Trailing wire parameters repeat (sum, product, min, max).
    VariadicWires {
        /// The fewest trailing wires allowed.
        min_wires: usize,
    },
    /// Trailing constant parameters repeat (mixed_radix).
    VariadicConsts {
        /// The fewest trailing constants allowed.
        min_consts: usize,
    },
    /// A repeating group of slot types (weighted_sum).
    VariadicGroup {
        /// The slot types of one repetition, in order.
        group: &'static [SlotType],
        /// The fewest repetitions allowed.
        min_repeats: usize,
    },
}

/// Output-type contract for a registered function.
///
/// Most nodes have fixed port types declared by their constructor's
/// `NodeMeta`. Some — `log_info`, `identity`, anything documented
/// as "pass-through" — produce an output whose type matches one
/// of their inputs. Declaring this here makes the contract visible
/// to the assembler, the build-node dispatch path, registry
/// listings, and any future static analysis, instead of being
/// buried inside an `eval` that silently passes values through a
/// wire whose declared type lies.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputType {
    /// Output types are whatever the constructor's `NodeMeta`
    /// declares — independent of input wire types. The default;
    /// covers the vast majority of nodes (`hash`, `regex_match`,
    /// `mod`, …) whose I/O contract is type-fixed.
    Fixed,
    /// The function's single output port has the same type as the
    /// input wire at the given index. The build dispatch resolves
    /// the wire's type from the assembler and the module's
    /// `build_node` reads it from the supplied `wire_types` slice
    /// to construct a port-typed node. Used by pass-through
    /// nodes (`log_info`, `log_debug`, …).
    SameAsInput(usize),
}

/// Description of a registered function's signature.
///
/// A signature is built with [`FuncSig::new`] and the `with_` methods,
/// all `const`, so a module's signatures can live in a `static`:
///
/// ```
/// use polydat_core::ast::{PortType, SlotType};
/// use polydat_core::dsl::registry::{FuncCategory, FuncSig, ParamSpec};
///
/// static SIGS: &[FuncSig] = &[FuncSig::new(
///     "counted_mod",
///     FuncCategory::Arithmetic,
///     &[ParamSpec::wire("x"), ParamSpec::new("m", SlotType::ConstU64).with_example("7")],
/// )
/// .with_description("mod, built by a host factory")
/// .with_output_port(PortType::U64)];
/// # assert_eq!(SIGS[0].outputs, 1);
/// ```
///
/// The struct is `#[non_exhaustive]`: a field added later is not a
/// breaking change to a host that builds one, since the new field
/// arrives with its default.
#[non_exhaustive]
pub struct FuncSig {
    /// Function name as used in the DSL.
    pub name: &'static str,
    /// Functional category.
    pub category: FuncCategory,
    /// Number of output ports (0 = dynamic, determined at compile time).
    pub outputs: usize,
    /// Short description for help/error messages.
    pub description: &'static str,
    /// Detailed help text: theory, usage examples, parameter meanings.
    /// Long-form help text for listings and documentation.
    pub help: &'static str,
    /// For variadic functions: the identity element for zero inputs.
    pub identity: Option<u64>,
    /// Factory for variadic nodes: takes wire count, returns node.
    pub variadic_ctor: Option<fn(usize) -> Box<dyn crate::ast::PolydatNode>>,
    /// Positional parameter list: wires and constants in call order.
    pub params: &'static [ParamSpec],
    /// Arity specification.
    pub arity: Arity,
    /// Input commutativity for this function.
    pub commutativity: crate::ast::Commutativity,
    /// Optional resolver hint for `Handle`-typed input ports. When
    /// the binding compiler emits this function and a `Handle`
    /// input is wired to a `Str`-producing source, it splices in
    /// the named resolver to convert the string into a handle. This
    /// is the "string-conversion node insertion" mechanism of
    /// source-string call-site sugar. `None` means no
    /// auto-promotion — the caller must pass a `Handle` directly.
    pub default_resolver: Option<DefaultResolver>,
    /// Output-type contract — `Fixed` for the vast majority of
    /// nodes; `SameAsInput(idx)` for type-polymorphic pass-throughs
    /// (e.g. `log_info` whose output type tracks its sole input).
    pub output_type: OutputType,
    /// Concrete port type of the single output, when statically
    /// known (`#[polydat_node]` emits it from the return type's
    /// `Wire::PORT`). `None` for tuple/dynamic/polymorphic outputs
    /// and for hand registrations that don't declare one. The DSL
    /// type inference (`binding::infer_expr_type`) reads this
    /// FIRST — the name-prefix heuristic is only the fallback —
    /// so call-expression operand typing flows from the symbol
    /// registry, not from a hand-maintained list.
    pub output_port: Option<crate::ast::PortType>,
}

/// Auto-resolver kind attached to handle-taking functions. Tells the
/// binding compiler which resolver to splice in when a string source
/// is wired to a handle input port.
#[derive(Debug, Clone, Copy)]
pub enum DefaultResolver {
    /// Insert `dataset_open(<source_wire>, "<facet>")` between the
    /// string source and the handle input.
    Facet(&'static str),
    /// Insert `dataset_group_open(<source_wire>)` between the string
    /// source and the handle input.
    Group,
}

impl FuncSig {
    /// The signature of `name` in `category` taking `params`: one
    /// output of a fixed type not stated here, a fixed arity, positional
    /// inputs, no description or help, and none of the optional parts.
    /// The `with_` methods change each of these.
    pub const fn new(
        name: &'static str,
        category: FuncCategory,
        params: &'static [ParamSpec],
    ) -> Self {
        Self {
            name,
            category,
            outputs: 1,
            description: "",
            help: "",
            identity: None,
            variadic_ctor: None,
            params,
            arity: Arity::Fixed,
            commutativity: crate::ast::Commutativity::Positional,
            default_resolver: None,
            output_type: OutputType::Fixed,
            output_port: None,
        }
    }

    /// The signature with `outputs` output ports (0 for a count fixed at
    /// compile time).
    pub const fn with_outputs(mut self, outputs: usize) -> Self {
        self.outputs = outputs;
        self
    }

    /// The signature with the short description `description`.
    pub const fn with_description(mut self, description: &'static str) -> Self {
        self.description = description;
        self
    }

    /// The signature with the long-form help text `help`.
    pub const fn with_help(mut self, help: &'static str) -> Self {
        self.help = help;
        self
    }

    /// The signature of a variadic function whose identity element, for
    /// zero inputs, is `identity`.
    pub const fn with_identity(mut self, identity: u64) -> Self {
        self.identity = Some(identity);
        self
    }

    /// The signature of a variadic function whose node for a given
    /// wire count `ctor` builds.
    pub const fn with_variadic_ctor(
        mut self,
        ctor: fn(usize) -> Box<dyn crate::ast::PolydatNode>,
    ) -> Self {
        self.variadic_ctor = Some(ctor);
        self
    }

    /// The signature with the arity `arity`.
    pub const fn with_arity(mut self, arity: Arity) -> Self {
        self.arity = arity;
        self
    }

    /// The signature with the input commutativity `commutativity`.
    pub const fn with_commutativity(mut self, commutativity: crate::ast::Commutativity) -> Self {
        // `Commutativity::Groups` owns a `Vec`, so a `const fn` cannot
        // drop the value it replaces and forgets it instead. A static
        // signature cannot hold a `Groups` (no `Vec` is built in a
        // `const` context), so what is forgotten there owns nothing;
        // a runtime caller replacing a `Groups` leaks its lists.
        let replaced = std::mem::replace(&mut self.commutativity, commutativity);
        std::mem::forget(replaced);
        self
    }

    /// The signature whose `Handle` inputs, wired from a `Str`, are
    /// resolved by `resolver`.
    pub const fn with_default_resolver(mut self, resolver: DefaultResolver) -> Self {
        self.default_resolver = Some(resolver);
        self
    }

    /// The signature with the output-type contract `output_type`.
    pub const fn with_output_type(mut self, output_type: OutputType) -> Self {
        self.output_type = output_type;
        self
    }

    /// The signature whose single output has the port type `port`.
    pub const fn with_output_port(mut self, port: crate::ast::PortType) -> Self {
        self.output_port = Some(port);
        self
    }

    /// Number of wire inputs in the fixed parameter list.
    pub fn wire_input_count(&self) -> usize {
        self.params.iter().filter(|p| p.slot_type.is_wire()).count()
    }

    /// Whether this function accepts variadic arguments.
    pub fn is_variadic(&self) -> bool {
        !matches!(self.arity, Arity::Fixed)
    }

    /// Constant parameter names and whether they're required.
    pub fn const_param_info(&self) -> Vec<(&'static str, bool)> {
        self.params
            .iter()
            .filter(|p| p.slot_type.is_const())
            .map(|p| (p.name, p.required))
            .collect()
    }
}

impl std::fmt::Debug for FuncSig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FuncSig")
            .field("name", &self.name)
            .field("category", &self.category)
            .field("params", &self.params)
            .field("arity", &self.arity)
            .finish()
    }
}

impl Clone for FuncSig {
    fn clone(&self) -> Self {
        Self {
            name: self.name,
            category: self.category,
            outputs: self.outputs,
            description: self.description,
            help: self.help,
            identity: self.identity,
            variadic_ctor: self.variadic_ctor,
            params: self.params,
            arity: self.arity.clone(),
            commutativity: self.commutativity.clone(),
            default_resolver: self.default_resolver,
            output_type: self.output_type,
            output_port: self.output_port,
        }
    }
}

/// Return the full registry of known functions: the signatures of every
/// linked factory ([`factories`]).
pub fn registry() -> Vec<FuncSig> {
    let mut funcs = Vec::new();
    for factory in factories() {
        funcs.extend_from_slice(factory.signatures());
    }
    funcs
}

/// Return functions grouped by category in display order.
pub fn by_category() -> Vec<(FuncCategory, Vec<FuncSig>)> {
    let reg = registry();
    let mut groups: std::collections::HashMap<FuncCategory, Vec<FuncSig>> =
        std::collections::HashMap::new();
    for sig in reg {
        groups.entry(sig.category).or_default().push(sig);
    }
    FuncCategory::display_order()
        .iter()
        .filter_map(|cat| groups.remove(cat).map(|funcs| (*cat, funcs)))
        .collect()
}

/// Find the closest function name to a misspelling, among the
/// registered functions and the [`Overload`] names.
pub fn suggest_function(name: &str) -> Option<&'static str> {
    let reg = registry();
    let names = reg
        .iter()
        .map(|sig| sig.name)
        .chain(overloads().map(|o| o.name));
    let mut best: Option<(&str, usize)> = None;
    for candidate in names {
        let dist = edit_distance(name, candidate);
        if dist <= 3 && (best.is_none() || dist < best.unwrap().1) {
            best = Some((candidate, dist));
        }
    }
    best.map(|(name, _)| name)
}

/// The signature of what a program calls by `name`: the registered
/// function `name`, or, for an [`Overload`] such as `vec_dot`, its
/// first form's ([`Overload::signature`]), whose parameters every form
/// shares. `None` means no call to `name` can build. For an overload
/// the signature's `name` is the form's, such as `vec_dot_f32`;
/// [`resolve_call`] names the form a call with given wire types builds,
/// and [`lookup_registered`] answers only for registered functions.
/// Every factory is `'static`, so its signatures are too, and no
/// allocation is needed.
pub fn lookup(name: &str) -> Option<&'static FuncSig> {
    lookup_registered(name).or_else(|| overload(name).and_then(Overload::signature))
}

/// Find a registered function by its exact name: the signature of the
/// factory that builds it. Unlike [`lookup`], it answers `None` for an
/// overloaded name, which no factory builds itself.
pub fn lookup_registered(name: &str) -> Option<&'static FuncSig> {
    factories().find_map(|factory| factory.signatures().iter().find(|sig| sig.name == name))
}

fn edit_distance(a: &str, b: &str) -> usize {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
    let mut matrix = vec![vec![0usize; b.len() + 1]; a.len() + 1];
    for (i, row) in matrix.iter_mut().enumerate() {
        row[0] = i;
    }
    for (j, cell) in matrix[0].iter_mut().enumerate() {
        *cell = j;
    }
    for i in 1..=a.len() {
        for j in 1..=b.len() {
            let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 };
            matrix[i][j] = (matrix[i - 1][j] + 1)
                .min(matrix[i][j - 1] + 1)
                .min(matrix[i - 1][j - 1] + cost);
        }
    }
    matrix[a.len()][b.len()]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn suggest_no_match() {
        assert_eq!(suggest_function("zzzzzzzzz"), None);
    }

    #[test]
    fn lookup_missing() {
        assert!(lookup("nonexistent").is_none());
    }

    #[test]
    fn every_function_has_category() {
        let reg = registry();
        for sig in &reg {
            // Just verify the category display name is non-empty
            assert!(
                !sig.category.display_name().is_empty(),
                "function '{}' has no category display name",
                sig.name
            );
        }
    }

    #[test]
    fn lookup_answers_for_an_overloaded_name_and_lookup_registered_does_not() {
        for o in overloads().filter(|o| lookup_registered(o.forms[0].func).is_some()) {
            let sig = lookup(o.name).unwrap_or_else(|| panic!("{} is not callable", o.name));
            assert_eq!(sig.name, o.forms[0].func, "{}", o.name);
            assert!(lookup_registered(o.name).is_none(), "{}", o.name);
        }
    }

    #[test]
    fn overloads_by_category_lists_every_overload_once() {
        let listed: usize = overloads_by_category().iter().map(|(_, o)| o.len()).sum();
        assert_eq!(listed, overloads().count());
    }

    // An overload whose first form is not registered, as a host's
    // misspelled registration would be.
    inventory::submit! {
        Overload::new(
            "registry_test_misspelled_overload",
            &[OverloadForm::new(
                &[crate::ast::PortType::U64],
                "registry_test_no_such_function",
            )],
        )
    }

    #[test]
    fn an_overload_of_no_registered_function_is_uncategorised_and_uncallable() {
        let name = "registry_test_misspelled_overload";
        let unlisted: Vec<_> = unlisted_overloads().iter().map(|o| o.name).collect();
        assert_eq!(unlisted, [name]);
        assert!(lookup(name).is_none());
        let (category, names) = overloads_by_category()
            .pop()
            .expect("the misspelled overload is listed");
        assert_eq!(category, FuncCategory::Uncategorised);
        assert_eq!(names.iter().map(|o| o.name).collect::<Vec<_>>(), [name]);
    }

    #[test]
    fn no_registered_function_is_uncategorised() {
        assert!(
            registry()
                .iter()
                .all(|sig| sig.category != FuncCategory::Uncategorised)
        );
    }

    #[test]
    fn by_category_covers_all() {
        let grouped = by_category();
        let total: usize = grouped.iter().map(|(_, funcs)| funcs.len()).sum();
        let reg = registry();
        assert_eq!(
            total,
            reg.len(),
            "by_category must cover all registered functions"
        );
    }

    #[test]
    fn category_parse_roundtrip() {
        for cat in FuncCategory::display_order() {
            let name = cat.display_name();
            let parsed = FuncCategory::parse(name);
            assert_eq!(parsed, Some(*cat), "failed to parse category '{name}'");
        }
    }

    #[test]
    fn registry_has_entries() {
        let reg = registry();
        assert!(reg.len() > 50, "registry should have 50+ functions");
    }

    // --- Unified param model tests ---

    #[test]
    fn printf_has_const_str_param() {
        // printf is a `#[polydat_node]` with a `Const<&str> format`
        // arg + `&[Value] parts` variadic. The macro lists both in
        // `params` (the variadic arg appears as a SlotType::Wire
        // entry whose count is governed by the node's
        // `Arity::VariadicWires`). The load-bearing assertion is that
        // the FIRST param is the format ConstStr and the arity is
        // variadic.
        let sig = lookup("printf").unwrap();
        assert!(
            !sig.params.is_empty(),
            "printf must have at least the format param"
        );
        assert!(matches!(sig.params[0].slot_type, SlotType::ConstStr));
        assert!(matches!(sig.arity, Arity::VariadicWires { .. }));
    }
}