simfony 0.1.0

Rust-like language that compiles to Simplicity bytecode.
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
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

use miniscript::iter::{Tree, TreeLike};
use simplicity::types::{CompleteBound, Final};

use crate::array::{BTreeSlice, Partition};
use crate::num::{NonZeroPow2Usize, Pow2Usize};
use crate::str::AliasName;

/// Primitives of the Simfony type system, excluding type aliases.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
#[non_exhaustive]
pub enum TypeInner<A> {
    /// Sum of the left and right types
    Either(A, A),
    /// Option of the inner type
    Option(A),
    /// Boolean type
    Boolean,
    /// Unsigned integer type
    UInt(UIntType),
    /// Tuple of potentially different types
    Tuple(Arc<[A]>),
    /// Array of the same type
    Array(A, usize),
    /// List of the same type
    List(A, NonZeroPow2Usize),
}

impl<A> TypeInner<A> {
    /// Helper method for displaying type primitives based on the number of yielded children.
    ///
    /// We cannot implement [`fmt::Display`] because `n_children_yielded` is an extra argument.
    fn display(&self, f: &mut fmt::Formatter<'_>, n_children_yielded: usize) -> fmt::Result {
        match self {
            TypeInner::Either(_, _) => match n_children_yielded {
                0 => f.write_str("Either<"),
                1 => f.write_str(","),
                n => {
                    debug_assert_eq!(n, 2);
                    f.write_str(">")
                }
            },
            TypeInner::Option(_) => match n_children_yielded {
                0 => f.write_str("Option<"),
                n => {
                    debug_assert_eq!(n, 1);
                    f.write_str(">")
                }
            },
            TypeInner::Boolean => f.write_str("bool"),
            TypeInner::UInt(ty) => write!(f, "{ty}"),
            TypeInner::Tuple(elements) => match n_children_yielded {
                0 => {
                    f.write_str("(")?;
                    if 0 == elements.len() {
                        f.write_str(")")?;
                    }
                    Ok(())
                }
                n if n == elements.len() => {
                    if n == 1 {
                        f.write_str(",")?;
                    }
                    f.write_str(")")
                }
                n => {
                    debug_assert!(n < elements.len());
                    f.write_str(", ")
                }
            },
            TypeInner::Array(_, size) => match n_children_yielded {
                0 => f.write_str("["),
                n => {
                    debug_assert_eq!(n, 1);
                    write!(f, "; {size}]")
                }
            },
            TypeInner::List(_, bound) => match n_children_yielded {
                0 => f.write_str("List<"),
                n => {
                    debug_assert_eq!(n, 1);
                    write!(f, ", {bound}>")
                }
            },
        }
    }
}

/// Unsigned integer type.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum UIntType {
    /// 1-bit unsigned integer
    U1,
    /// 2-bit unsigned integer
    U2,
    /// 4-bit unsigned integer
    U4,
    /// 8-bit unsigned integer
    U8,
    /// 16-bit unsigned integer
    U16,
    /// 32-bit unsigned integer
    U32,
    /// 64-bit unsigned integer
    U64,
    /// 128-bit unsigned integer
    U128,
    /// 256-bit unsigned integer
    U256,
}

impl UIntType {
    /// Take `n` and return the `2^n`-bit unsigned integer type.
    pub const fn two_n(n: u32) -> Option<Self> {
        match n {
            0 => Some(UIntType::U1),
            1 => Some(UIntType::U2),
            2 => Some(UIntType::U4),
            3 => Some(UIntType::U8),
            4 => Some(UIntType::U16),
            5 => Some(UIntType::U32),
            6 => Some(UIntType::U64),
            7 => Some(UIntType::U128),
            8 => Some(UIntType::U256),
            _ => None,
        }
    }

    /// Return the bit width of values of this type.
    pub const fn bit_width(self) -> Pow2Usize {
        let bit_width: usize = match self {
            UIntType::U1 => 1,
            UIntType::U2 => 2,
            UIntType::U4 => 4,
            UIntType::U8 => 8,
            UIntType::U16 => 16,
            UIntType::U32 => 32,
            UIntType::U64 => 64,
            UIntType::U128 => 128,
            UIntType::U256 => 256,
        };
        debug_assert!(bit_width.is_power_of_two());
        Pow2Usize::new_unchecked(bit_width)
    }

    /// Create the unsigned integer type for the given `bit_width`.
    pub const fn from_bit_width(bit_width: Pow2Usize) -> Option<Self> {
        match bit_width.get() {
            1 => Some(UIntType::U1),
            2 => Some(UIntType::U2),
            4 => Some(UIntType::U4),
            8 => Some(UIntType::U8),
            16 => Some(UIntType::U16),
            32 => Some(UIntType::U32),
            64 => Some(UIntType::U64),
            128 => Some(UIntType::U128),
            256 => Some(UIntType::U256),
            _ => None,
        }
    }

    /// Return the byte width of values of this type.
    ///
    /// Return 0 for types that take less than an entire byte: `u1`, `u2`, `u4`.
    pub const fn byte_width(self) -> usize {
        self.bit_width().get() / 8
    }
}

impl fmt::Debug for UIntType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl fmt::Display for UIntType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            UIntType::U1 => f.write_str("u1"),
            UIntType::U2 => f.write_str("u2"),
            UIntType::U4 => f.write_str("u4"),
            UIntType::U8 => f.write_str("u8"),
            UIntType::U16 => f.write_str("u16"),
            UIntType::U32 => f.write_str("u32"),
            UIntType::U64 => f.write_str("u64"),
            UIntType::U128 => f.write_str("u128"),
            UIntType::U256 => f.write_str("u256"),
        }
    }
}

impl TryFrom<&StructuralType> for UIntType {
    type Error = ();

    fn try_from(value: &StructuralType) -> Result<Self, Self::Error> {
        let mut current = value.as_ref();
        let mut n = 0;
        while let Some((left, right)) = current.as_product() {
            if left.tmr() != right.tmr() {
                return Err(());
            }
            current = left;
            n += 1;
        }
        if let Some((left, right)) = current.as_sum() {
            if left.is_unit() && right.is_unit() {
                return UIntType::two_n(n).ok_or(());
            }
        }
        Err(())
    }
}

impl TryFrom<&ResolvedType> for UIntType {
    type Error = ();

    fn try_from(value: &ResolvedType) -> Result<Self, Self::Error> {
        UIntType::try_from(&StructuralType::from(value))
    }
}

macro_rules! construct_int {
    ($name: ident, $ty: ident, $text: expr) => {
        #[doc = "Create the type of"]
        #[doc = $text]
        #[doc = "integers."]
        fn $name() -> Self {
            Self::from(UIntType::$ty)
        }
    };
}

/// Various type constructors.
pub trait TypeConstructible: Sized + From<UIntType> {
    /// Create a sum of the given `left` and `right` types.
    fn either(left: Self, right: Self) -> Self;

    /// Create an option of the given `inner` type.
    fn option(inner: Self) -> Self;

    /// Create the Boolean type.
    fn boolean() -> Self;

    /// Create a tuple from the given `elements`.
    ///
    /// The empty tuple is the unit type.
    /// A tuple of two types is a product.
    fn tuple<I: IntoIterator<Item = Self>>(elements: I) -> Self;

    /// Create the unit type.
    fn unit() -> Self {
        Self::tuple([])
    }

    /// Create a product of the given `left` and `right` types.
    fn product(left: Self, right: Self) -> Self {
        Self::tuple([left, right])
    }

    /// Create an array with `size` many values of the `element` type.
    fn array(element: Self, size: usize) -> Self;

    /// Create an array of `size` many bytes.
    fn byte_array(size: usize) -> Self {
        Self::array(Self::u8(), size)
    }

    /// Create a list with less than `bound` many values of the `element` type.
    fn list(element: Self, bound: NonZeroPow2Usize) -> Self;

    construct_int!(u1, U1, "1-bit");
    construct_int!(u2, U2, "2-bit");
    construct_int!(u4, U4, "4-bit");
    construct_int!(u8, U8, "8-bit");
    construct_int!(u16, U16, "16-bit");
    construct_int!(u32, U32, "32-bit");
    construct_int!(u64, U64, "64-bit");
    construct_int!(u128, U128, "128-bit");
    construct_int!(u256, U256, "256-bit");
}

/// Various type destructors for types that maintain the structure in which they were created.
///
/// [`StructuralType`] collapses its structure into Simplicity's units, sums and products,
/// which is why it does not implement this trait.
pub trait TypeDeconstructible: Sized {
    /// Access the left and right types of a sum.
    fn as_either(&self) -> Option<(&Self, &Self)>;

    /// Access the inner type of an option.
    fn as_option(&self) -> Option<&Self>;

    /// Check if the type is Boolean.
    fn is_boolean(&self) -> bool;

    /// Access the internals of an integer type.
    fn as_integer(&self) -> Option<UIntType>;

    /// Access the element types of a tuple.
    fn as_tuple(&self) -> Option<&[Arc<Self>]>;

    /// Check if the type is the unit (empty tuple).
    fn is_unit(&self) -> bool {
        matches!(self.as_tuple(), Some(components) if components.is_empty())
    }

    /// Access the element type and size of an array.
    fn as_array(&self) -> Option<(&Self, usize)>;

    /// Access the element type and bound of a list.
    fn as_list(&self) -> Option<(&Self, NonZeroPow2Usize)>;
}

/// Simfony type without type aliases.
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct ResolvedType(TypeInner<Arc<Self>>);

impl ResolvedType {
    /// Access the inner type primitive.
    pub fn as_inner(&self) -> &TypeInner<Arc<Self>> {
        &self.0
    }
}

impl TypeConstructible for ResolvedType {
    fn either(left: Self, right: Self) -> Self {
        Self(TypeInner::Either(Arc::new(left), Arc::new(right)))
    }

    fn option(inner: Self) -> Self {
        Self(TypeInner::Option(Arc::new(inner)))
    }

    fn boolean() -> Self {
        Self(TypeInner::Boolean)
    }

    fn tuple<I: IntoIterator<Item = Self>>(elements: I) -> Self {
        Self(TypeInner::Tuple(
            elements.into_iter().map(Arc::new).collect(),
        ))
    }

    fn array(element: Self, size: usize) -> Self {
        Self(TypeInner::Array(Arc::new(element), size))
    }

    fn list(element: Self, bound: NonZeroPow2Usize) -> Self {
        Self(TypeInner::List(Arc::new(element), bound))
    }
}

impl TypeDeconstructible for ResolvedType {
    fn as_either(&self) -> Option<(&Self, &Self)> {
        match self.as_inner() {
            TypeInner::Either(ty_l, ty_r) => Some((ty_l, ty_r)),
            _ => None,
        }
    }

    fn as_option(&self) -> Option<&Self> {
        match self.as_inner() {
            TypeInner::Option(ty) => Some(ty),
            _ => None,
        }
    }

    fn is_boolean(&self) -> bool {
        matches!(self.as_inner(), TypeInner::Boolean)
    }

    fn as_integer(&self) -> Option<UIntType> {
        match self.as_inner() {
            TypeInner::UInt(ty) => Some(*ty),
            _ => None,
        }
    }

    fn as_tuple(&self) -> Option<&[Arc<Self>]> {
        match self.as_inner() {
            TypeInner::Tuple(components) => Some(components),
            _ => None,
        }
    }

    fn as_array(&self) -> Option<(&Self, usize)> {
        match self.as_inner() {
            TypeInner::Array(ty, size) => Some((ty, *size)),
            _ => None,
        }
    }

    fn as_list(&self) -> Option<(&Self, NonZeroPow2Usize)> {
        match self.as_inner() {
            TypeInner::List(ty, bound) => Some((ty, *bound)),
            _ => None,
        }
    }
}

impl TreeLike for &ResolvedType {
    fn as_node(&self) -> Tree<Self> {
        match &self.0 {
            TypeInner::Boolean | TypeInner::UInt(..) => Tree::Nullary,
            TypeInner::Option(l) | TypeInner::Array(l, _) | TypeInner::List(l, _) => Tree::Unary(l),
            TypeInner::Either(l, r) => Tree::Binary(l, r),
            TypeInner::Tuple(elements) => Tree::Nary(elements.iter().map(Arc::as_ref).collect()),
        }
    }
}

impl fmt::Debug for ResolvedType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl fmt::Display for ResolvedType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for data in self.verbose_pre_order_iter() {
            data.node.0.display(f, data.n_children_yielded)?;
        }
        Ok(())
    }
}

impl From<UIntType> for ResolvedType {
    fn from(value: UIntType) -> Self {
        Self(TypeInner::UInt(value))
    }
}

#[cfg(feature = "arbitrary")]
impl crate::ArbitraryRec for ResolvedType {
    fn arbitrary_rec(u: &mut arbitrary::Unstructured, budget: usize) -> arbitrary::Result<Self> {
        use arbitrary::Arbitrary;

        match budget.checked_sub(1) {
            None => match u.int_in_range(0..=1)? {
                0 => Ok(Self::boolean()),
                1 => UIntType::arbitrary(u).map(Self::from),
                _ => unreachable!(),
            },
            Some(new_budget) => match u.int_in_range(0..=6)? {
                0 => Ok(Self::boolean()),
                1 => UIntType::arbitrary(u).map(Self::from),
                2 => Self::arbitrary_rec(u, new_budget).map(Self::option),
                3 => {
                    let left = Self::arbitrary_rec(u, new_budget)?;
                    let right = Self::arbitrary_rec(u, new_budget)?;
                    Ok(Self::either(left, right))
                }
                4 => {
                    let len = u.int_in_range(0..=3)?;
                    (0..len)
                        .map(|_| Self::arbitrary_rec(u, new_budget))
                        .collect::<arbitrary::Result<Vec<Self>>>()
                        .map(Self::tuple)
                }
                5 => {
                    let element = Self::arbitrary_rec(u, new_budget)?;
                    let size = u.int_in_range(0..=3)?;
                    Ok(Self::array(element, size))
                }
                6 => {
                    let element = Self::arbitrary_rec(u, new_budget)?;
                    let bound = NonZeroPow2Usize::arbitrary(u)?;
                    Ok(Self::list(element, bound))
                }
                _ => unreachable!(),
            },
        }
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for ResolvedType {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        <Self as crate::ArbitraryRec>::arbitrary_rec(u, 3)
    }
}

/// Simfony type with type aliases.
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct AliasedType(AliasedInner);

/// Type alias or primitive.
///
/// Private struct to allow future changes.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
enum AliasedInner {
    /// Type alias.
    Alias(AliasName),
    /// Builtin type alias.
    Builtin(BuiltinAlias),
    /// Type primitive.
    Inner(TypeInner<Arc<AliasedType>>),
}

/// Type alias with predefined definition.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum BuiltinAlias {
    Ctx8,
    Pubkey,
    Message,
    Message64,
    Signature,
    Scalar,
    Fe,
    Ge,
    Gej,
    Point,
    Height,
    Time,
    Distance,
    Duration,
    Lock,
    Outpoint,
    Confidential1,
    ExplicitAsset,
    Asset1,
    ExplicitAmount,
    Amount1,
    ExplicitNonce,
    Nonce,
    TokenAmount1,
}

impl AliasedType {
    /// Access a user-defined alias.
    pub const fn as_alias(&self) -> Option<&AliasName> {
        match &self.0 {
            AliasedInner::Alias(name) => Some(name),
            _ => None,
        }
    }

    /// Access a buitlin alias.
    pub const fn as_builtin(&self) -> Option<&BuiltinAlias> {
        match &self.0 {
            AliasedInner::Builtin(builtin) => Some(builtin),
            _ => None,
        }
    }

    /// Create a type alias from the given `identifier`.
    pub const fn alias(name: AliasName) -> Self {
        Self(AliasedInner::Alias(name))
    }

    /// Create a builtin type alias.
    pub const fn builtin(builtin: BuiltinAlias) -> Self {
        Self(AliasedInner::Builtin(builtin))
    }

    /// Resolve all aliases in the type based on the given map of `aliases` to types.
    pub fn resolve<F>(&self, mut get_alias: F) -> Result<ResolvedType, AliasName>
    where
        F: FnMut(&AliasName) -> Option<ResolvedType>,
    {
        let mut output = vec![];
        for data in self.post_order_iter() {
            match &data.node.0 {
                AliasedInner::Alias(name) => {
                    let resolved = get_alias(name).ok_or(name.clone())?;
                    output.push(resolved);
                }
                AliasedInner::Builtin(builtin) => {
                    let resolved = builtin.resolve();
                    output.push(resolved);
                }
                AliasedInner::Inner(inner) => match inner {
                    TypeInner::Either(_, _) => {
                        let right = output.pop().unwrap();
                        let left = output.pop().unwrap();
                        output.push(ResolvedType::either(left, right));
                    }
                    TypeInner::Option(_) => {
                        let inner = output.pop().unwrap();
                        output.push(ResolvedType::option(inner));
                    }
                    TypeInner::Boolean => output.push(ResolvedType::boolean()),
                    TypeInner::UInt(integer) => output.push(ResolvedType::from(*integer)),
                    TypeInner::Tuple(_) => {
                        let size = data.node.n_children();
                        let elements = output.split_off(output.len() - size);
                        debug_assert_eq!(elements.len(), size);
                        output.push(ResolvedType::tuple(elements));
                    }
                    TypeInner::Array(_, size) => {
                        let element = output.pop().unwrap();
                        output.push(ResolvedType::array(element, *size));
                    }
                    TypeInner::List(_, bound) => {
                        let element = output.pop().unwrap();
                        output.push(ResolvedType::list(element, *bound));
                    }
                },
            }
        }
        debug_assert_eq!(output.len(), 1);
        Ok(output.pop().unwrap())
    }

    /// Resolve all aliases in the type based on the builtin type aliases only.
    pub fn resolve_builtin(&self) -> Result<ResolvedType, AliasName> {
        self.resolve(|_| None)
    }
}

impl TypeConstructible for AliasedType {
    fn either(left: Self, right: Self) -> Self {
        Self(AliasedInner::Inner(TypeInner::Either(
            Arc::new(left),
            Arc::new(right),
        )))
    }

    fn option(inner: Self) -> Self {
        Self(AliasedInner::Inner(TypeInner::Option(Arc::new(inner))))
    }

    fn boolean() -> Self {
        Self(AliasedInner::Inner(TypeInner::Boolean))
    }

    fn tuple<I: IntoIterator<Item = Self>>(elements: I) -> Self {
        Self(AliasedInner::Inner(TypeInner::Tuple(
            elements.into_iter().map(Arc::new).collect(),
        )))
    }

    fn array(element: Self, size: usize) -> Self {
        Self(AliasedInner::Inner(TypeInner::Array(
            Arc::new(element),
            size,
        )))
    }

    fn list(element: Self, bound: NonZeroPow2Usize) -> Self {
        Self(AliasedInner::Inner(TypeInner::List(
            Arc::new(element),
            bound,
        )))
    }
}

impl TypeDeconstructible for AliasedType {
    fn as_either(&self) -> Option<(&Self, &Self)> {
        match &self.0 {
            AliasedInner::Inner(TypeInner::Either(ty_l, ty_r)) => Some((ty_l, ty_r)),
            _ => None,
        }
    }

    fn as_option(&self) -> Option<&Self> {
        match &self.0 {
            AliasedInner::Inner(TypeInner::Option(ty)) => Some(ty),
            _ => None,
        }
    }

    fn is_boolean(&self) -> bool {
        matches!(&self.0, AliasedInner::Inner(TypeInner::Boolean))
    }

    fn as_integer(&self) -> Option<UIntType> {
        match &self.0 {
            AliasedInner::Inner(TypeInner::UInt(ty)) => Some(*ty),
            _ => None,
        }
    }

    fn as_tuple(&self) -> Option<&[Arc<Self>]> {
        match &self.0 {
            AliasedInner::Inner(TypeInner::Tuple(components)) => Some(components),
            _ => None,
        }
    }

    fn as_array(&self) -> Option<(&Self, usize)> {
        match &self.0 {
            AliasedInner::Inner(TypeInner::Array(ty, size)) => Some((ty, *size)),
            _ => None,
        }
    }

    fn as_list(&self) -> Option<(&Self, NonZeroPow2Usize)> {
        match &self.0 {
            AliasedInner::Inner(TypeInner::List(ty, bound)) => Some((ty, *bound)),
            _ => None,
        }
    }
}

impl TreeLike for &AliasedType {
    fn as_node(&self) -> Tree<Self> {
        match &self.0 {
            AliasedInner::Alias(_) | AliasedInner::Builtin(_) => Tree::Nullary,
            AliasedInner::Inner(inner) => match inner {
                TypeInner::Boolean | TypeInner::UInt(..) => Tree::Nullary,
                TypeInner::Option(l) | TypeInner::Array(l, _) | TypeInner::List(l, _) => {
                    Tree::Unary(l)
                }
                TypeInner::Either(l, r) => Tree::Binary(l, r),
                TypeInner::Tuple(elements) => {
                    Tree::Nary(elements.iter().map(Arc::as_ref).collect())
                }
            },
        }
    }
}

impl fmt::Debug for AliasedType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl fmt::Display for AliasedType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for data in self.verbose_pre_order_iter() {
            match &data.node.0 {
                AliasedInner::Alias(alias) => write!(f, "{alias}")?,
                AliasedInner::Builtin(builtin) => write!(f, "{builtin}")?,
                AliasedInner::Inner(inner) => inner.display(f, data.n_children_yielded)?,
            }
        }
        Ok(())
    }
}

impl From<UIntType> for AliasedType {
    fn from(value: UIntType) -> Self {
        Self(AliasedInner::Inner(TypeInner::UInt(value)))
    }
}

impl From<AliasName> for AliasedType {
    fn from(value: AliasName) -> Self {
        Self::alias(value)
    }
}

impl From<BuiltinAlias> for AliasedType {
    fn from(value: BuiltinAlias) -> Self {
        Self::builtin(value)
    }
}

#[cfg(feature = "arbitrary")]
impl crate::ArbitraryRec for AliasedType {
    fn arbitrary_rec(u: &mut arbitrary::Unstructured, budget: usize) -> arbitrary::Result<Self> {
        use arbitrary::Arbitrary;

        match budget.checked_sub(1) {
            None => match u.int_in_range(0..=3)? {
                0 => AliasName::arbitrary(u).map(Self::alias),
                1 => BuiltinAlias::arbitrary(u).map(Self::builtin),
                2 => Ok(Self::boolean()),
                3 => UIntType::arbitrary(u).map(Self::from),
                _ => unreachable!(),
            },
            Some(new_budget) => match u.int_in_range(0..=8)? {
                0 => AliasName::arbitrary(u).map(Self::alias),
                1 => BuiltinAlias::arbitrary(u).map(Self::builtin),
                2 => Ok(Self::boolean()),
                3 => UIntType::arbitrary(u).map(Self::from),
                4 => Self::arbitrary_rec(u, new_budget).map(Self::option),
                5 => {
                    let left = Self::arbitrary_rec(u, new_budget)?;
                    let right = Self::arbitrary_rec(u, new_budget)?;
                    Ok(Self::either(left, right))
                }
                6 => {
                    let len = u.int_in_range(0..=3)?;
                    (0..len)
                        .map(|_| Self::arbitrary_rec(u, new_budget))
                        .collect::<arbitrary::Result<Vec<Self>>>()
                        .map(Self::tuple)
                }
                7 => {
                    let element = Self::arbitrary_rec(u, new_budget)?;
                    let size = u.int_in_range(0..=3)?;
                    Ok(Self::array(element, size))
                }
                8 => {
                    let element = Self::arbitrary_rec(u, new_budget)?;
                    let bound = NonZeroPow2Usize::arbitrary(u)?;
                    Ok(Self::list(element, bound))
                }
                _ => unreachable!(),
            },
        }
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for AliasedType {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        <Self as crate::ArbitraryRec>::arbitrary_rec(u, 3)
    }
}

impl BuiltinAlias {
    pub fn resolve(self) -> ResolvedType {
        use BuiltinAlias as B;
        use UIntType::*;

        match self {
            B::Ctx8 => ResolvedType::tuple([
                ResolvedType::list(U8.into(), NonZeroPow2Usize::new(64).unwrap()),
                ResolvedType::tuple([U64.into(), U256.into()]),
            ]),
            B::Pubkey | B::Message | B::Scalar | B::Fe | B::ExplicitAsset | B::ExplicitNonce => {
                U256.into()
            }
            B::Message64 | B::Signature => ResolvedType::array(U8.into(), 64),
            B::Ge => ResolvedType::tuple([U256.into(), U256.into()]),
            B::Gej => {
                ResolvedType::tuple([ResolvedType::tuple([U256.into(), U256.into()]), U256.into()])
            }
            B::Point | B::Confidential1 => ResolvedType::tuple([U1.into(), U256.into()]),
            B::Height | B::Time | B::Lock => U32.into(),
            B::Distance | B::Duration => U16.into(),
            B::Outpoint => ResolvedType::tuple([U256.into(), U32.into()]),
            B::Asset1 | B::Nonce => {
                ResolvedType::either(ResolvedType::tuple([U1.into(), U256.into()]), U256.into())
            }
            B::ExplicitAmount => U64.into(),
            B::Amount1 | B::TokenAmount1 => {
                ResolvedType::either(ResolvedType::tuple([U1.into(), U256.into()]), U64.into())
            }
        }
    }
}

impl fmt::Debug for BuiltinAlias {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl fmt::Display for BuiltinAlias {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BuiltinAlias::Ctx8 => f.write_str("Ctx8"),
            BuiltinAlias::Pubkey => f.write_str("Pubkey"),
            BuiltinAlias::Message => f.write_str("Message"),
            BuiltinAlias::Message64 => f.write_str("Message64"),
            BuiltinAlias::Signature => f.write_str("Signature"),
            BuiltinAlias::Scalar => f.write_str("Scalar"),
            BuiltinAlias::Fe => f.write_str("Fe"),
            BuiltinAlias::Ge => f.write_str("Ge"),
            BuiltinAlias::Gej => f.write_str("Gej"),
            BuiltinAlias::Point => f.write_str("Point"),
            BuiltinAlias::Height => f.write_str("Height"),
            BuiltinAlias::Time => f.write_str("Time"),
            BuiltinAlias::Distance => f.write_str("Distance"),
            BuiltinAlias::Duration => f.write_str("Duration"),
            BuiltinAlias::Lock => f.write_str("Lock"),
            BuiltinAlias::Outpoint => f.write_str("Outpoint"),
            BuiltinAlias::Confidential1 => f.write_str("Confidential1"),
            BuiltinAlias::ExplicitAsset => f.write_str("ExplicitAsset"),
            BuiltinAlias::Asset1 => f.write_str("Asset1"),
            BuiltinAlias::ExplicitAmount => f.write_str("ExplicitAmount"),
            BuiltinAlias::Amount1 => f.write_str("Amount1"),
            BuiltinAlias::ExplicitNonce => f.write_str("ExplicitNonce"),
            BuiltinAlias::Nonce => f.write_str("Nonce"),
            BuiltinAlias::TokenAmount1 => f.write_str("TokenAmount1"),
        }
    }
}

impl FromStr for BuiltinAlias {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "Ctx8" => Ok(BuiltinAlias::Ctx8),
            "Pubkey" => Ok(BuiltinAlias::Pubkey),
            "Message" => Ok(BuiltinAlias::Message),
            "Message64" => Ok(BuiltinAlias::Message64),
            "Signature" => Ok(BuiltinAlias::Signature),
            "Scalar" => Ok(BuiltinAlias::Scalar),
            "Fe" => Ok(BuiltinAlias::Fe),
            "Ge" => Ok(BuiltinAlias::Ge),
            "Gej" => Ok(BuiltinAlias::Gej),
            "Point" => Ok(BuiltinAlias::Point),
            "Height" => Ok(BuiltinAlias::Height),
            "Time" => Ok(BuiltinAlias::Time),
            "Distance" => Ok(BuiltinAlias::Distance),
            "Duration" => Ok(BuiltinAlias::Duration),
            "Lock" => Ok(BuiltinAlias::Lock),
            "Outpoint" => Ok(BuiltinAlias::Outpoint),
            "Confidential1" => Ok(BuiltinAlias::Confidential1),
            "ExplicitAsset" => Ok(BuiltinAlias::ExplicitAsset),
            "Asset1" => Ok(BuiltinAlias::Asset1),
            "ExplicitAmount" => Ok(BuiltinAlias::ExplicitAmount),
            "Amount1" => Ok(BuiltinAlias::Amount1),
            "ExplicitNonce" => Ok(BuiltinAlias::ExplicitNonce),
            "Nonce" => Ok(BuiltinAlias::Nonce),
            "TokenAmount1" => Ok(BuiltinAlias::TokenAmount1),
            _ => Err("Unknown alias".to_string()),
        }
    }
}

/// Internal structure of a Simfony type.
/// 1:1 isomorphism to Simplicity.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct StructuralType(Arc<Final>);

impl AsRef<Final> for StructuralType {
    fn as_ref(&self) -> &Final {
        &self.0
    }
}

impl From<StructuralType> for Arc<Final> {
    fn from(value: StructuralType) -> Self {
        value.0
    }
}

impl From<Arc<Final>> for StructuralType {
    fn from(value: Arc<Final>) -> Self {
        Self(value)
    }
}

impl TreeLike for StructuralType {
    fn as_node(&self) -> Tree<Self> {
        match self.0.bound() {
            CompleteBound::Unit => Tree::Nullary,
            CompleteBound::Sum(l, r) | CompleteBound::Product(l, r) => {
                Tree::Binary(Self(l.clone()), Self(r.clone()))
            }
        }
    }
}

impl fmt::Debug for StructuralType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", &self.0)
    }
}

impl fmt::Display for StructuralType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", &self.0)
    }
}

impl From<UIntType> for StructuralType {
    fn from(value: UIntType) -> Self {
        let inner = match value {
            UIntType::U1 => Final::two_two_n(0),
            UIntType::U2 => Final::two_two_n(1),
            UIntType::U4 => Final::two_two_n(2),
            UIntType::U8 => Final::two_two_n(3),
            UIntType::U16 => Final::two_two_n(4),
            UIntType::U32 => Final::two_two_n(5),
            UIntType::U64 => Final::two_two_n(6),
            UIntType::U128 => Final::two_two_n(7),
            UIntType::U256 => Final::two_two_n(8),
        };
        Self(inner)
    }
}

impl From<&ResolvedType> for StructuralType {
    fn from(value: &ResolvedType) -> Self {
        let mut output = vec![];
        for data in value.post_order_iter() {
            match &data.node.0 {
                TypeInner::Either(_, _) => {
                    let right = output.pop().unwrap();
                    let left = output.pop().unwrap();
                    output.push(StructuralType::either(left, right));
                }
                TypeInner::Option(_) => {
                    let inner = output.pop().unwrap();
                    output.push(StructuralType::option(inner));
                }
                TypeInner::Boolean => output.push(StructuralType::boolean()),
                TypeInner::UInt(integer) => output.push(StructuralType::from(*integer)),
                TypeInner::Tuple(_) => {
                    let size = data.node.n_children();
                    let elements = output.split_off(output.len() - size);
                    debug_assert_eq!(elements.len(), size);
                    output.push(StructuralType::tuple(elements));
                }
                TypeInner::Array(_, size) => {
                    let element = output.pop().unwrap();
                    output.push(StructuralType::array(element, *size));
                }
                TypeInner::List(_, bound) => {
                    let element = output.pop().unwrap();
                    output.push(StructuralType::list(element, *bound));
                }
            }
        }
        debug_assert_eq!(output.len(), 1);
        output.pop().unwrap()
    }
}

impl TypeConstructible for StructuralType {
    fn either(left: Self, right: Self) -> Self {
        Self(Final::sum(left.0, right.0))
    }

    fn option(inner: Self) -> Self {
        Self::either(Self::unit(), inner)
    }

    fn boolean() -> Self {
        Self::either(Self::unit(), Self::unit())
    }

    fn tuple<I: IntoIterator<Item = Self>>(elements: I) -> Self {
        let elements: Vec<_> = elements.into_iter().collect();
        let tree = BTreeSlice::from_slice(&elements);
        tree.fold(Self::product).unwrap_or_else(Self::unit)
    }

    // Keep this implementation to prevent an infinite loop in <Self as TypeConstructible>::tuple
    fn unit() -> Self {
        Self(Final::unit())
    }

    // Keep this implementation to prevent an infinite loop in <Self as TypeConstructible>::tuple
    fn product(left: Self, right: Self) -> Self {
        Self(Final::product(left.0, right.0))
    }

    fn array(element: Self, size: usize) -> Self {
        // Cheap clone because Arc<Final> consists of Arcs
        let elements = vec![element; size];
        let tree = BTreeSlice::from_slice(&elements);
        tree.fold(Self::product).unwrap_or_else(Self::unit)
    }

    fn list(element: Self, bound: NonZeroPow2Usize) -> Self {
        // Cheap clone because Arc<Final> consists of Arcs
        let el_vector = vec![element.0; bound.get() - 1];
        let partition = Partition::from_slice(&el_vector, bound);
        debug_assert!(partition.is_complete());
        let process = |block: &[Arc<Final>], size: usize| -> Arc<Final> {
            debug_assert_eq!(block.len(), size);
            let tree = BTreeSlice::from_slice(block);
            let array = tree.fold(Final::product).unwrap();
            Final::sum(Final::unit(), array)
        };
        let inner = partition.fold(process, Final::product);
        Self(inner)
    }
}

impl StructuralType {
    /// Convert into an unfinalized type that can be used in Simplicity's unification algorithm.
    pub fn to_unfinalized(
        &self,
        inference_context: &simplicity::types::Context,
    ) -> simplicity::types::Type {
        simplicity::types::Type::complete(inference_context, self.0.clone())
    }
}

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

    #[test]
    fn display_type() {
        let unit = ResolvedType::unit();
        assert_eq!("()", &unit.to_string());
        let singleton = ResolvedType::tuple([ResolvedType::u1()]);
        assert_eq!("(u1,)", &singleton.to_string());
        let pair = ResolvedType::tuple([ResolvedType::u1(), ResolvedType::u8()]);
        assert_eq!("(u1, u8)", &pair.to_string());
        let triple =
            ResolvedType::tuple([ResolvedType::u1(), ResolvedType::u8(), ResolvedType::u16()]);
        assert_eq!("(u1, u8, u16)", &triple.to_string());
        let empty_array = ResolvedType::array(ResolvedType::unit(), 0);
        assert_eq!("[(); 0]", &empty_array.to_string());
        let array = ResolvedType::array(ResolvedType::unit(), 3);
        assert_eq!("[(); 3]", &array.to_string());
        let list = ResolvedType::list(ResolvedType::unit(), NonZeroPow2Usize::TWO);
        assert_eq!("List<(), 2>", &list.to_string());
    }
}