Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_constraints_test.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 <gtest/gtest.h>
38
39# include <tpl_constraints.H>
40
41using namespace Aleph;
42
43
45{
47 const auto alpha = ctx.make_type_variable("A");
48
49 Compiler_Type_Unifier unifier(ctx);
50 const auto result = unifier.unify(alpha, ctx.integer_type());
51
52 ASSERT_TRUE(result.ok());
53 EXPECT_EQ(unifier.apply(alpha), ctx.integer_type());
54 EXPECT_NE(unifier.dump_substitution().find("A := Int"), std::string::npos);
55}
56
57
59{
61 const auto alpha = ctx.make_type_variable("A");
62 const auto beta = ctx.make_type_variable("B");
63
64 const auto lhs = ctx.make_function_type({alpha, ctx.bool_type()}, alpha);
65 const auto rhs = ctx.make_function_type({ctx.integer_type(), beta}, ctx.integer_type());
66
67 Compiler_Type_Unifier unifier(ctx);
68 const auto result = unifier.unify(lhs, rhs);
69
70 ASSERT_TRUE(result.ok());
71 EXPECT_EQ(unifier.apply(alpha), ctx.integer_type());
72 EXPECT_EQ(unifier.apply(beta), ctx.bool_type());
73 EXPECT_EQ(ctx.to_string(unifier.apply(lhs)), "fn(Int, Bool) -> Int");
74}
75
76
78{
80 const auto alpha = ctx.make_type_variable("A");
81 const auto recursive = ctx.make_function_type({ctx.integer_type()}, alpha);
82
83 Compiler_Type_Unifier unifier(ctx);
84 const auto result = unifier.unify(alpha, recursive);
85
86 EXPECT_EQ(result.kind, Compiler_Unify_Result_Kind::Occurs_Check_Failed);
87}
88
89
91{
93 const auto rigid = ctx.make_type_variable("R", true);
94
95 Compiler_Type_Unifier unifier(ctx);
96 const auto result = unifier.unify(rigid, ctx.integer_type());
97
98 EXPECT_EQ(result.kind, Compiler_Unify_Result_Kind::Rigid_Variable);
99}
100
101
103{
105 const auto alpha = ctx.make_type_variable("A");
106 const auto beta = ctx.make_type_variable("B");
107
109 constraints.add(alpha, ctx.integer_type(), {}, "parameter");
110 constraints.add(beta, ctx.string_type(), {}, "result");
111 constraints.add(ctx.make_tuple_type({alpha, beta}),
112 ctx.make_tuple_type({ctx.integer_type(), ctx.string_type()}),
113 {},
114 "tuple check");
115
116 Compiler_Type_Unifier unifier(ctx);
117 const auto result = unifier.solve(constraints);
118
119 ASSERT_TRUE(result.ok());
120 EXPECT_EQ(unifier.apply(alpha), ctx.integer_type());
121 EXPECT_EQ(unifier.apply(beta), ctx.string_type());
122}
123
124
126{
128 const auto beta = ctx.make_type_variable("B");
129
131 // First constraint is impossible: integer cannot unify with string
132 constraints.add(ctx.integer_type(), ctx.string_type(), {}, "failing");
133 // Second constraint would succeed on its own but should not be processed
134 constraints.add(beta, ctx.string_type(), {}, "unreachable");
135
136 Compiler_Type_Unifier unifier(ctx);
137 const auto result = unifier.solve(constraints);
138
139 EXPECT_FALSE(result.ok());
140 // 'beta' was never bound, so applying it yields itself unchanged
141 EXPECT_EQ(unifier.apply(beta), beta);
142}
Context owning all compiler type nodes.
Compiler_Type_Id string_type() const noexcept
Returns the preloaded String type id.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
Compiler_Type_Id make_type_variable(std::string label="", const bool rigid=false)
Creates a fresh type variable.
Compiler_Type_Id make_tuple_type(const DynArray< Compiler_Type_Id > &members)
Creates a tuple type.
Compiler_Type_Id make_function_type(const DynArray< Compiler_Type_Id > &parameters, const Compiler_Type_Id result)
Creates a function type.
Compiler_Type_Id bool_type() const noexcept
Returns the preloaded Bool type id.
Compiler_Type_Id integer_type() const noexcept
Returns the preloaded Int type id.
Structural unifier for compiler types.
std::string dump_substitution()
Dumps the current substitution in a deterministic format.
Compiler_Unify_Result unify(const Compiler_Type_Id lhs, const Compiler_Type_Id rhs)
Attempts to unify lhs and rhs.
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.
#define TEST(name)
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
Equality constraints and type unification helpers for compiler work.