├── ad hoc polymorphism (coercion).cxx ├── ad hoc polymorphism (overloading).cxx ├── dependent type (behavioral subtyping).cxx ├── dependent type (inductive construction).cxx ├── dependent type (pi type).cxx ├── dependent type (refinement type).cxx ├── dependent type (sigma type).cxx ├── existential type (multiple dispatch).cxx ├── existential type.cxx ├── flow-sensitive type.cxx ├── higher kinded polymorphism.cxx ├── higher kinded type.cxx ├── higher ranked type.cxx ├── impredicative polymorphism.cxx ├── indexed type family.cxx ├── polymorphic recursion.cxx ├── predicate covariance.cxx ├── row polymorphism.cxx ├── substructural type (affine type).cxx ├── subtype polymorphism.cxx ├── uninhabited type (data kind - type level nat).cxx └── universal type (parametric polymorphism).cxx /ad hoc polymorphism (coercion).cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std::literals; 4 | 5 | struct A { 6 | // coercion: A >-> string 7 | operator auto() { 8 | return "A"s; 9 | } 10 | }; 11 | 12 | struct B { 13 | // coercion: B >-> string 14 | operator auto() { 15 | return "B"s; 16 | } 17 | }; 18 | 19 | auto Print(std::string msg) { 20 | std::cout << msg << std::endl; 21 | } 22 | 23 | auto main()->int { 24 | Print("oops"); 25 | Print(A{}); 26 | Print(B{}); 27 | } -------------------------------------------------------------------------------- /ad hoc polymorphism (overloading).cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct A { 5 | // overloading +: A -> A -> A 6 | auto operator+(A) const { 7 | return A{}; 8 | } 9 | }; 10 | 11 | struct B {}; 12 | 13 | // overloading f: A -> A 14 | auto f(A) { 15 | std::cout << "f: A -> A" << std::endl; 16 | return A{}; 17 | } 18 | 19 | // overloading f: B -> B 20 | auto f(B) { 21 | std::cout << "f: B -> B" << std::endl; 22 | return B{}; 23 | } 24 | 25 | auto main()->int { 26 | auto x = A{} + A{}; 27 | auto y = f(A{}); 28 | auto z = f(B{}); 29 | 30 | static_assert(std::same_as); 31 | static_assert(std::same_as); 32 | static_assert(std::same_as); 33 | } -------------------------------------------------------------------------------- /dependent type (behavioral subtyping).cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/dependent type (behavioral subtyping).cxx -------------------------------------------------------------------------------- /dependent type (inductive construction).cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using Zero = struct {}; 4 | 5 | template 6 | struct Succ {}; 7 | 8 | template 9 | auto NatConstructor() { 10 | if constexpr (x == 0) 11 | return Zero{}; 12 | else 13 | return Succ())>{}; 14 | } 15 | 16 | // Nat: nat -> * 17 | // Nat 0 = Zero 18 | // Nat x = Succ (Nat x - 1) 19 | template 20 | using Nat = decltype(NatConstructor()); 21 | 22 | auto main()->int { 23 | using One = Succ; 24 | using Two = Succ>; 25 | using Three = Succ>>; 26 | 27 | using x = Nat<1>; 28 | using y = Nat<2>; 29 | using z = Nat<3>; 30 | using w = Nat<0>; 31 | 32 | static_assert(std::same_as); 33 | static_assert(std::same_as); 34 | static_assert(std::same_as); 35 | static_assert(std::same_as); 36 | } -------------------------------------------------------------------------------- /dependent type (pi type).cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std::literals; 5 | 6 | // B: real -> * 7 | // B x = int if x >= 42 else string 8 | // 9 | // f: real[x] -> B x 10 | template 11 | auto f() { 12 | if constexpr (x >= 42) 13 | return 42; 14 | else 15 | return "oops"s; 16 | } 17 | 18 | auto main()->int { 19 | auto x = f<10>(); 20 | auto y = f<42>(); 21 | 22 | static_assert(std::same_as); 23 | static_assert(std::same_as); 24 | 25 | std::cout << "x: " << x << std::endl; 26 | std::cout << "y: " << y << std::endl; 27 | } -------------------------------------------------------------------------------- /dependent type (refinement type).cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/dependent type (refinement type).cxx -------------------------------------------------------------------------------- /dependent type (sigma type).cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/dependent type (sigma type).cxx -------------------------------------------------------------------------------- /existential type (multiple dispatch).cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/existential type (multiple dispatch).cxx -------------------------------------------------------------------------------- /existential type.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/existential type.cxx -------------------------------------------------------------------------------- /flow-sensitive type.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/flow-sensitive type.cxx -------------------------------------------------------------------------------- /higher kinded polymorphism.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/higher kinded polymorphism.cxx -------------------------------------------------------------------------------- /higher kinded type.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Apply: (* -> *) -> * -> * 4 | template typename TypeConstructor, typename TypeArgument> 5 | using Apply = TypeConstructor; 6 | 7 | template 8 | struct A{}; 9 | 10 | template 11 | struct B{}; 12 | 13 | auto main()->int { 14 | using X = Apply; 15 | using Y = Apply; 16 | 17 | static_assert(std::same_as>); 18 | static_assert(std::same_as>); 19 | } -------------------------------------------------------------------------------- /higher ranked type.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/higher ranked type.cxx -------------------------------------------------------------------------------- /impredicative polymorphism.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/impredicative polymorphism.cxx -------------------------------------------------------------------------------- /indexed type family.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/indexed type family.cxx -------------------------------------------------------------------------------- /polymorphic recursion.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/polymorphic recursion.cxx -------------------------------------------------------------------------------- /predicate covariance.cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/predicate covariance.cxx -------------------------------------------------------------------------------- /row polymorphism.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | template 5 | concept Real = std::integral> || std::floating_point>; 6 | 7 | template 8 | concept SameAs = std::same_as, std::decay_t>; 9 | 10 | using Vec2D = struct { double x; double y; }; 11 | using iVec3D = struct { int x; int y; int z; }; 12 | 13 | auto TransposeXY(auto Vec) requires requires { 14 | { Vec.x }->Real; 15 | { Vec.y }->SameAs; 16 | } { 17 | std::swap(Vec.x, Vec.y); 18 | return Vec; 19 | } 20 | 21 | auto main()->int { 22 | auto v1 = TransposeXY(Vec2D{ .x = 2.71, .y = 3.14 }); 23 | auto v2 = TransposeXY(iVec3D{ .x = 42, .y = 13, .z = 7 }); 24 | 25 | std::cout << "v1: x = " << v1.x << ", y = " << v1.y << std::endl; 26 | std::cout << "v2: x = " << v2.x << ", y = " << v2.y << ", z = " << v2.z << std::endl; 27 | } -------------------------------------------------------------------------------- /substructural type (affine type).cxx: -------------------------------------------------------------------------------- 1 | template 2 | struct ADLType { 3 | friend consteval auto ADLFunc(ADLType, auto...); 4 | }; 5 | 6 | template 7 | struct InjectDefinitionForADLFunc { 8 | friend consteval auto ADLFunc(ADLType, auto...) {} 9 | }; 10 | 11 | template 12 | consteval auto ExtractThenUpdateCurrentState()->decltype(x) { 13 | if constexpr (requires { ADLFunc(ADLType{}); }) 14 | return ExtractThenUpdateCurrentState(); 15 | else 16 | InjectDefinitionForADLFunc{}; 17 | return x; 18 | } 19 | 20 | template 21 | struct AffineType {}; 22 | 23 | template()> 24 | auto Use(AffineType) requires (x == 0) {} 25 | 26 | auto main()->int { 27 | auto x = AffineType{}; 28 | auto y = AffineType{}; 29 | 30 | Use(x); 31 | Use(y); 32 | 33 | // Use(x); <- type level failure 34 | } 35 | -------------------------------------------------------------------------------- /subtype polymorphism.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct Messenger { 6 | virtual auto Print(std::string_view msg)->void = 0; 7 | }; 8 | 9 | // A <: Messenger 10 | struct A : Messenger { 11 | auto Print(std::string_view msg)->void override { 12 | std::cout << "A says " << msg << std::endl; 13 | } 14 | }; 15 | 16 | // B <: Messenger 17 | struct B : Messenger { 18 | auto Print(std::string_view msg)->void override { 19 | std::cout << "B says " << msg << std::endl; 20 | } 21 | }; 22 | 23 | auto main()->int { 24 | auto x = std::vector>{}; 25 | x.emplace_back(new A{}); 26 | x.emplace_back(new B{}); 27 | for (auto& y : x) 28 | y->Print("hi"); 29 | } -------------------------------------------------------------------------------- /uninhabited type (data kind - type level nat).cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using Zero = struct {}; 4 | 5 | template 6 | struct Succ {}; 7 | 8 | template 9 | constexpr auto IsNat = false; 10 | 11 | template<> 12 | constexpr auto IsNat = true; 13 | 14 | template 15 | constexpr auto IsNat> = IsNat; 16 | 17 | // kind Nat = Zero | Succ Nat 18 | template 19 | concept Nat = IsNat; 20 | 21 | template 22 | struct AddFunc {}; 23 | 24 | // Add: Nat -> Nat -> Nat 25 | // Add Zero y = y 26 | // Add (Succ x) y = Succ (Add x y) 27 | template 28 | using Add = AddFunc::Result; 29 | 30 | template 31 | struct AddFunc { 32 | using Result = y; 33 | }; 34 | 35 | template 36 | struct AddFunc, y> { 37 | using Result = Succ>; 38 | }; 39 | 40 | auto main()->int { 41 | using One = Succ; 42 | using Two = Succ>; 43 | using Three = Succ>>; 44 | 45 | using x = Add; 46 | using y = Succ; 47 | 48 | static_assert(Nat); 49 | static_assert(Nat); 50 | static_assert(Nat == false); 51 | static_assert(Nat> == false); 52 | 53 | static_assert(std::same_as); 54 | static_assert(std::same_as); 55 | static_assert(std::same_as, Three>); 56 | } -------------------------------------------------------------------------------- /universal type (parametric polymorphism).cxx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IFeelBloated/Type-System-Zoo/17da196821bbf375cf2cba9cd19a61a36f497780/universal type (parametric polymorphism).cxx --------------------------------------------------------------------------------