Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_types_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <iostream>
38
39# include <Compiler_Types.H>
40# include <tpl_constraints.H>
41
42using namespace Aleph;
43
44int main()
45{
49 point_fields.append("y");
50
53 point_field_types.append(ctx.integer_type());
54
55 const auto point = ctx.make_struct_type("Point");
57
60 color_variants.append("Green");
61 color_variants.append("Blue");
62
63 const auto color = ctx.make_enum_type("Color");
65
66 const auto alpha = ctx.make_type_variable("A");
67 const auto beta = ctx.make_type_variable("B");
68
69 const auto expected =
70 ctx.make_function_type({point, color}, color);
71 const auto inferred =
72 ctx.make_function_type({alpha, beta}, beta);
73
75 constraints.add(inferred, expected, {}, "function signature match");
76
77 Compiler_Type_Unifier unifier(ctx);
78 const auto result = unifier.solve(constraints);
79
80 std::cout << ctx.dump_types();
81 std::cout << '\n';
82 if (result.ok())
83 {
84 std::cout << unifier.dump_substitution();
85 std::cout << '\n';
86 std::cout << "Normalized inferred type: "
87 << ctx.to_string(unifier.apply(inferred))
88 << '\n';
89 return 0;
90 }
91
92 std::cout << "Unification failed: "
94 << " - " << result.message
95 << '\n';
96 return 1;
97}
Stable type graph for the compiler-support MVP.
Context owning all compiler type nodes.
void set_struct_fields(const Compiler_Type_Id id, const DynArray< std::string > &field_names, const DynArray< Compiler_Type_Id > &field_types) const
Assigns field metadata to a previously created struct type.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
Compiler_Type_Id make_enum_type(std::string name)
Creates a nominal enum type placeholder.
std::string dump_types() const
Dumps all known types in allocation order.
void set_enum_variants(const Compiler_Type_Id id, const DynArray< std::string > &variant_names) const
Assigns variant metadata to a previously created enum type.
Compiler_Type_Id make_type_variable(std::string label="", const bool rigid=false)
Creates a fresh type variable.
Compiler_Type_Id make_function_type(const DynArray< Compiler_Type_Id > &parameters, const Compiler_Type_Id result)
Creates a function type.
Compiler_Type_Id integer_type() const noexcept
Returns the preloaded Int type id.
Compiler_Type_Id make_struct_type(std::string name)
Creates a nominal struct type placeholder.
Structural unifier for compiler types.
std::string dump_substitution()
Dumps the current substitution in a deterministic format.
Compiler_Unify_Result solve(const Compiler_Type_Constraint_Set &constraints)
Solves a whole ordered set of type constraints.
Compiler_Type_Id apply(const Compiler_Type_Id id)
Applies the current substitution to id.
Ordered collection of equality constraints.
size_t add(const T &lhs, const T &rhs, const Source_Span &span={}, const std::string &message="")
Appends an equality constraint.
T & append()
Allocate a new entry to the end of array.
Minimal std::expected-style result type for C++20.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
const char * compiler_unify_result_kind_name(const Compiler_Unify_Result_Kind kind) noexcept
Returns a stable debug name for one unification result kind.
static long & color(typename GT::Node *p)
Equality constraints and type unification helpers for compiler work.