Skip to content

Commit

Permalink
Fix typecheck error on atom negation with equivalent type (#2399)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gueckmooh committed Feb 22, 2023
1 parent 5fe57c0 commit 8aae521
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ast/transform/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,12 @@ void TypeCheckerImpl::visit_(type_identity<Atom>, const Atom& atom) {
// Declared attribute and deduced type agree if:
// They are the same type, or
// They are derived from the same constant type.
// They are equivalent types.
bool validAttribute = all_of(argTypes, [&](const analysis::Type& type) {
return type == attributeType || any_of(typeEnv.getConstantTypes(), [&](auto& constantType) {
return isSubtypeOf(attributeType, constantType) && isSubtypeOf(type, constantType);
});
return type == attributeType || areEquivalentTypes(type, attributeType) ||
any_of(typeEnv.getConstantTypes(), [&](auto& constantType) {
return isSubtypeOf(attributeType, constantType) && isSubtypeOf(type, constantType);
});
});

if (!validAttribute) {
Expand Down
1 change: 1 addition & 0 deletions tests/syntactic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ positive_test(dot_identifiers)
positive_test(input_adt_names1)
positive_test(input_adt_names2)
positive_test(input_directive_rfc4180)
positive_test(equivalent_types_in_negation)
if (NOT MSVC)
# does not pass with Visual Studio pre-processor because it preserves all whitespaces
positive_test(whitespaces)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.type List = [name: symbol, rest: List]
.decl list_decl(l:List)
list_decl(["A", ["B", ["C", nil]]]).
list_decl(["D", ["E", ["F", nil]]]).

.type Alias = List

.decl flatten_list(l:List)
.output flatten_list
flatten_list(l) :- list_decl(l).
flatten_list(l) :- flatten_list([_, l]).

.decl alias_map(a: Alias, l:List)
alias_map(l, l) :-
flatten_list(l).

.decl alias(a: Alias)
alias(a) :- alias_map(a, _).

.decl parent(a: Alias, p: Alias)
.output parent
parent(a, p) :-
alias_map(a, [_, parent]),
alias_map(p, parent).

.decl has_no_parent(a: Alias)
has_no_parent(a) :-
alias(a), !parent(a, _). // No more error here
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions tests/syntactic/equivalent_types_in_negation/flatten_list.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
nil
[C, nil]
[B, [C, nil]]
[A, [B, [C, nil]]]
[F, nil]
[E, [F, nil]]
[D, [E, [F, nil]]]
6 changes: 6 additions & 0 deletions tests/syntactic/equivalent_types_in_negation/parent.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[C, nil] nil
[B, [C, nil]] [C, nil]
[A, [B, [C, nil]]] [B, [C, nil]]
[F, nil] nil
[E, [F, nil]] [F, nil]
[D, [E, [F, nil]]] [E, [F, nil]]

0 comments on commit 8aae521

Please sign in to comment.