24 auto& world = type->world();
32 return world.call(
quant::star, optional_app->arg());
37 if (
auto quant_app =
Axm::isa<quant>(arg))
return world.app(callee, quant_app->arg());
49template<
class ConjOrDisj>
51 assert(!args.empty());
52 auto& world = args.front()->world();
53 return std::accumulate(args.begin() + 1, args.end(), args.front(), [&world](
const Def* lhs,
const Def* rhs) {
54 return world.call<ConjOrDisj, false>(Defs{lhs, rhs});
59 auto& world = type->world();
60 world.log().d(
"conj {}: {} ({})", callee, type, arg);
64 case 0:
return world.lit_tt();
67 if (
auto args = detail::flatten_in_arg<conj>(arg); !args.empty())
70 return world.annex<
empty>();
81 if (lhs_range && rhs_range)
return Lit::as(lhs_range->arg()->proj(0)) <
Lit::as(rhs_range->arg()->proj(0));
83 if (lhs_range)
return false;
84 if (rhs_range)
return true;
86 return lhs->
gid() < rhs->
gid();
90 std::stable_sort(args.begin(), args.end(), &
compare_re);
92 auto new_end = std::unique(args.begin(), args.end());
93 args.erase(new_end, args.end());
110 auto ranges_begin = args.begin();
113 if (ranges_begin == args.end())
return;
116 auto& world = (*ranges_begin)->world();
118 std::transform(ranges_begin, args.end(), std::back_inserter(old_ranges),
get_range);
120 auto new_ranges =
automaton::merge_ranges(old_ranges, [&world](std::string_view msg) { world.log().d(
"{}", msg); });
123 args.erase(ranges_begin, args.end());
124 std::transform(new_ranges.begin(), new_ranges.end(), std::back_inserter(args),
app_range{world});
129template<cls A, cls B>
135 auto check_arg_equiv = [](
const Def* lhs,
const Def* rhs) {
138 if (
auto rng_rhs =
Axm::isa<range>(not_rhs->arg()))
return rng_lhs == rng_rhs;
143 return check_arg_equiv(lhs, rhs) || check_arg_equiv(rhs, lhs);
148 auto to_range = std::views::filter(is_range) | std::views::transform(
get_range);
149 auto rhs_view = negated_rhs | to_range;
151 if (std::ranges::distance(rhs_view) != std::ranges::distance(negated_rhs))
return false;
153 return std::ranges::includes(lhs | to_range, rhs_view);
157 auto& world = type->world();
160 case 0:
return world.lit_ff();
163 auto new_args = detail::flatten_in_arg<disj>(arg);
165 const bool contains_any
166 = std::ranges::find_if(new_args, [](
const Def* ax) ->
bool {
return Axm::isa<any>(ax); })
168 const bool contains_empty
169 = std::ranges::find_if(new_args, [](
const Def* ax) ->
bool {
return Axm::isa<empty>(ax); })
172 auto make_any = [&world, contains_empty]() {
176 return world.call<
disj,
false>(
Defs{world.annex<
any>(), world.annex<
empty>()});
178 return world.annex<
any>();
181 if (contains_any)
return make_any();
185 const Def* to_remove =
nullptr;
186 for (
const auto* cls0 : new_args) {
187 for (
const auto* cls1 : new_args)
188 if (
equals_any(cls0, cls1))
return make_any();
192 auto rngs = detail::flatten_in_arg<disj>(disj_rhs->arg());
194 if (
equals_any(new_args, rngs))
return make_any();
199 erase(new_args, to_remove);
200 world.log().d(
"final ranges: {}", fe::Join(new_args));
203 if (new_args.size() > 1)
return world.call<
disj,
false>(new_args);
204 return new_args.back();
211 auto& world = type->world();
212 auto [lhs, rhs] = arg->
projs<2>();
214 if (!lhs->isa<
Var>() && !rhs->isa<
Var>())
215 if (lhs->as<
Lit>()->
get() > rhs->as<
Lit>()->
get())
return world.raw_app(type, callee, {rhs, lhs});
225 for (
const auto* disj_arg :
disj->args())
234 arg->
blame(
"regex.not_ must only be used with regex.disj, regex.range, regex.any and regex.not_: {} {}", callee,
236 .n(
"found unwanted: {}", unwanted)
static auto isa(const Def *def)
auto projs(F f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
fe::Error & blame(fe::cite_string< Args... > s, Args &&... args) const
Reports an error that blames this; chain Error::n for Notes and Error::bail to throw.
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
const Def * arity() const
Number of elements available to Extract / Insert (may be dynamic).
static std::optional< T > isa(const Def *def)
static T as(const Def *def)
A variable introduced by a binder (mutable).
The World represents the whole program and manages creation of MimIR nodes (Defs).
fe::Vector< Range > Ranges
std::pair< std::uint64_t, std::uint64_t > Range
std::optional< Range > merge_ranges(Range a, Range b) noexcept
void merge_ranges(DefVec &args)
void make_vector_unique(DefVec &args)
auto get_range(const Def *rng) -> Range
bool is_in_range(Range range, nat_t needle)
const Def * normalize_conj(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_range(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_disj(const Def *type, const Def *, const Def *arg)
const Def * normalize_quant(const Def *type, const Def *callee, const Def *arg)
const Def * normalize_not(const Def *, const Def *callee, const Def *arg)
bool compare_re(const Def *lhs, const Def *rhs)
const Def * make_binary_tree(Defs args)
const Def * any_unwanted_for_not(const Def *arg)
bool equals_any(const Def *cls0, const Def *cls1)
fe::View< const Def * > Defs
fe::Vector< const Def * > DefVec
#define MIM_regex_NORMALIZER_IMPL
const Def * operator()(Range rng)