Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/csg_annotated/csg_annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ g₁ = @csgrammar_annotated begin
end
```
"""
macro csgrammar_annotated(expression)
macro csgrammar_annotated(expression::Expr)
# collect and remove labels
labels = _get_labels!(expression)

# parse rules, get constraints from annotations
rules = Any[]
types = Symbol[]
bytype = Dict{Symbol,Vector{Int}}()
bytype = Dict{Symbol,Vector{Integer}}()
constraints = Vector{AbstractConstraint}()

rule_index = 1
Expand Down Expand Up @@ -84,7 +84,7 @@ macro csgrammar_annotated(expression)
for new_rule ∈ new_rules
push!(rules, new_rule)
push!(types, lhs)
bytype[lhs] = push!(get(bytype, lhs, Int[]), rule_index)
bytype[lhs] = push!(get(bytype, lhs, Integer[]), rule_index)

rule_index += 1
end
Expand Down Expand Up @@ -145,10 +145,10 @@ end
Converts an annotation to a constraint.
commutative: creates an Ordered constraint
transitive: creates an (incorrect) Forbidden constraint
forbidden_path(path::Vector{Union{Symbol, Int}}): creates a ForbiddenPath constraint with the original rule included
forbidden_path(path::Vector{Union{Symbol, Integer}}): creates a ForbiddenPath constraint with the original rule included
... || ...: creates a OneOf constraint (also works with ... || ... || ... et cetera, though not very performant)
"""
function annotation2constraint(annotation::Any, rule_index::Int, labels::Vector{String})::AbstractConstraint
function annotation2constraint(annotation::Any, rule_index::Integer, labels::Vector{String})::AbstractConstraint
if annotation isa Expr
# function-like annotations
if annotation.head == :call
Expand Down Expand Up @@ -193,4 +193,4 @@ end


# helper function for label lookup
_get_rule_index(labels::Vector{String}, label::String)::Int = findfirst(isequal(label), labels)
_get_rule_index(labels::Vector{String}, label::String)::Integer = findfirst(isequal(label), labels)
4 changes: 2 additions & 2 deletions src/domainrulenode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ struct DomainRuleNode <: AbstractRuleNode
children::Vector{AbstractRuleNode}
end

function DomainRuleNode(grammar::AbstractGrammar, rules::Vector{Int}, children::Vector{<:AbstractRuleNode})
function DomainRuleNode(grammar::AbstractGrammar, rules::Vector{<:Integer}, children::Vector{<:AbstractRuleNode})
domain = falses(length(grammar.rules))
for r ∈ rules
domain[r] = true
end
return DomainRuleNode(domain, children)
end

DomainRuleNode(grammar::AbstractGrammar, rules::Vector{Int}) = DomainRuleNode(grammar, rules, Vector{AbstractRuleNode}())
DomainRuleNode(grammar::AbstractGrammar, rules::Vector{<:Integer}) = DomainRuleNode(grammar, rules, Vector{AbstractRuleNode}())

#DomainRuleNode(get_domain(grammar, sym), [])
DomainRuleNode(domain::BitVector) = DomainRuleNode(domain, [])
4 changes: 2 additions & 2 deletions src/grammarconstraints/contains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Contains <: AbstractGrammarConstraint
This [`AbstractGrammarConstraint`] enforces that a given `rule` appears in the program tree at least once.
"""
struct Contains <: AbstractGrammarConstraint
rule::Int
rule::Integer
end

function on_new_node(solver::Solver, c::Contains, path::Vector{Int})
function on_new_node(solver::Solver, c::Contains, path::Vector{<:Integer})
if length(path) == 0
#only post a local constraint at the root
post!(solver, LocalContains(path, c.rule))
Expand Down
4 changes: 2 additions & 2 deletions src/grammarconstraints/contains_subtree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ struct ContainsSubtree <: AbstractGrammarConstraint
tree::AbstractRuleNode
end

function on_new_node(solver::UniformSolver, c::ContainsSubtree, path::Vector{Int})
function on_new_node(solver::UniformSolver, c::ContainsSubtree, path::Vector{<:Integer})
if length(path) == 0
post!(solver, LocalContainsSubtree(path, c.tree, nothing, nothing))
end
end

function on_new_node(::GenericSolver, ::ContainsSubtree, ::Vector{Int}) end
function on_new_node(::GenericSolver, ::ContainsSubtree, ::Vector{<:Integer}) end

"""
check_tree(c::ContainsSubtree, tree::AbstractRuleNode)::Bool
Expand Down
2 changes: 1 addition & 1 deletion src/grammarconstraints/forbidden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Forbidden <: AbstractGrammarConstraint
tree::AbstractRuleNode
end

function on_new_node(solver::Solver, c::Forbidden, path::Vector{Int})
function on_new_node(solver::Solver, c::Forbidden, path::Vector{<:Integer})
#minor optimization: prevent the first hardfail (https://github.com/orgs/Herb-AI/projects/6/views/1?pane=issue&itemId=55570518)
if c.tree isa RuleNode
@match get_node_at_location(solver, path) begin
Expand Down
8 changes: 4 additions & 4 deletions src/grammarconstraints/forbidden_sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ Consider the following paths from the root:
- `[1, 99, 1, 2, 3]` is forbidden, as there is a subsequence that does not contain `99`
"""
struct ForbiddenSequence <: AbstractGrammarConstraint
sequence::Vector{Int}
ignore_if::Vector{Int}
sequence::Vector{Integer}
ignore_if::Vector{Integer}
end

ForbiddenSequence(sequence::Vector{Int}; ignore_if=Vector{Int}()) = ForbiddenSequence(sequence, ignore_if)
ForbiddenSequence(sequence::Vector{<:Integer}; ignore_if=Vector{Integer}()) = ForbiddenSequence(sequence, ignore_if)

function on_new_node(solver::Solver, c::ForbiddenSequence, path::Vector{Int})
function on_new_node(solver::Solver, c::ForbiddenSequence, path::Vector{<:Integer})
#minor optimization: prevent the first hardfail (https://github.com/orgs/Herb-AI/projects/6/views/1?pane=issue&itemId=55570518)
@match get_node_at_location(solver, path) begin
hole::AbstractHole => if !hole.domain[c.sequence[end]] return end
Expand Down
2 changes: 1 addition & 1 deletion src/grammarconstraints/ordered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Ordered <: AbstractGrammarConstraint
order::Vector{Symbol}
end

function on_new_node(solver::Solver, c::Ordered, path::Vector{Int})
function on_new_node(solver::Solver, c::Ordered, path::Vector{<:Integer})
#minor optimization: prevent the first hardfail (https://github.com/orgs/Herb-AI/projects/6/views/1?pane=issue&itemId=55570518)
if c.tree isa RuleNode
@match get_node_at_location(solver, path) begin
Expand Down
8 changes: 4 additions & 4 deletions src/grammarconstraints/unique.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
This [`AbstractGrammarConstraint`] enforces that a given `rule` appears in the program tree at most once.
"""
struct Unique <: AbstractGrammarConstraint
rule::Int
rule::Integer
end


function on_new_node(solver::Solver, c::Unique, path::Vector{Int})
function on_new_node(solver::Solver, c::Unique, path::Vector{<:Integer})
if length(path) == 0
#only post a local constraint at the root
post!(solver, LocalUnique(path, c.rule))
Expand All @@ -17,11 +17,11 @@ end


"""
function _count_occurrences(rule::Int, node::AbstractRuleNode)::Int
function _count_occurrences(rule::Integer, node::AbstractRuleNode)::Integer

Recursively counts the number of occurrences of the `rule` in the `node`.
"""
function _count_occurrences(node::AbstractRuleNode, rule::Int)::Int
function _count_occurrences(node::AbstractRuleNode, rule::Integer)::Integer
@assert isfilled(node)
count = (get_rule(node) == rule) ? 1 : 0
for child ∈ get_children(node)
Expand Down
10 changes: 5 additions & 5 deletions src/lessthanorequal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ function make_less_than_or_equal!(
hole1::Union{RuleNode, AbstractHole},
hole2::Union{RuleNode, AbstractHole}
)::LessThanOrEqualResult
make_less_than_or_equal!(solver, hole1, hole2, Vector{Tuple{AbstractHole, Int}}())
make_less_than_or_equal!(solver, hole1, hole2, Vector{Tuple{AbstractHole, Integer}}())
end

"""
function make_less_than_or_equal!(h1::Union{RuleNode, AbstractHole}, h2::Union{RuleNode, AbstractHole}, guards::Vector{Tuple{AbstractHole, Int}})::LessThanOrEqualResult
function make_less_than_or_equal!(h1::Union{RuleNode, AbstractHole}, h2::Union{RuleNode, AbstractHole}, guards::Vector{Tuple{AbstractHole, Integer}})::LessThanOrEqualResult

Helper function that keeps track of the guards
"""
function make_less_than_or_equal!(
solver::Solver,
hole1::Union{RuleNode, AbstractHole},
hole2::Union{RuleNode, AbstractHole},
guards::Vector{Tuple{AbstractHole, Int}}
guards::Vector{Tuple{AbstractHole, Integer}}
)::LessThanOrEqualResult
@assert isfeasible(solver)
@match (isfilled(hole1), isfilled(hole2)) begin
Expand Down Expand Up @@ -241,15 +241,15 @@ function make_less_than_or_equal!(
end

"""
function make_less_than_or_equal!(solver::Solver, nodes1::Vector{AbstractRuleNode}, nodes2::Vector{AbstractRuleNode}, guards::Vector{Tuple{AbstractHole, Int}})::LessThanOrEqualResult
function make_less_than_or_equal!(solver::Solver, nodes1::Vector{AbstractRuleNode}, nodes2::Vector{AbstractRuleNode}, guards::Vector{Tuple{AbstractHole, Integer}})::LessThanOrEqualResult

Helper function that tiebreaks on children.
"""
function make_less_than_or_equal!(
solver::Solver,
nodes1::Vector{AbstractRuleNode},
nodes2::Vector{AbstractRuleNode},
guards::Vector{Tuple{AbstractHole, Int}}
guards::Vector{Tuple{AbstractHole, Integer}}
)::LessThanOrEqualResult
for (node1, node2) ∈ zip(nodes1, nodes2)
result = make_less_than_or_equal!(solver, node1, node2, guards)
Expand Down
12 changes: 6 additions & 6 deletions src/localconstraints/local_contains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ LocalContains
Enforces that a given `rule` appears at or below the given `path` at least once.
"""
struct LocalContains <: AbstractLocalConstraint
path::Vector{Int}
rule::Int
path::Vector{Integer}
rule::Integer
end

"""
Expand Down Expand Up @@ -51,19 +51,19 @@ function propagate!(solver::Solver, c::LocalContains)
end

"""
_contains(node::AbstractRuleNode, rule::Int)::Bool
_contains(node::AbstractRuleNode, rule::Integer)::Bool

Recursive helper function for the LocalContains constraint
Returns one of the following:
- `true`, if the `node` does contains the `rule`
- `false`, if the `node` does not contain the `rule`
- `Vector{AbstractHole}`, if the `node` contains the `rule` if one the `holes` gets filled with the target rule
"""
function _contains(node::AbstractRuleNode, rule::Int)::Union{Vector{AbstractHole}, Bool}
function _contains(node::AbstractRuleNode, rule::Integer)::Union{Vector{AbstractHole}, Bool}
return _contains(node, rule, Vector{AbstractHole}())
end

function _contains(node::AbstractRuleNode, rule::Int, holes::Vector{AbstractHole})::Union{Vector{AbstractHole}, Bool}
function _contains(node::AbstractRuleNode, rule::Integer, holes::Vector{AbstractHole})::Union{Vector{AbstractHole}, Bool}
if !isuniform(node)
# the rule might appear underneath this non-uniform hole
push!(holes, node)
Expand All @@ -81,7 +81,7 @@ function _contains(node::AbstractRuleNode, rule::Int, holes::Vector{AbstractHole
return _contains(get_children(node), rule, holes)
end

function _contains(children::Vector{AbstractRuleNode}, rule::Int, holes::Vector{AbstractHole})::Union{Vector{AbstractHole}, Bool}
function _contains(children::Vector{AbstractRuleNode}, rule::Integer, holes::Vector{AbstractHole})::Union{Vector{AbstractHole}, Bool}
for child ∈ children
if _contains(child, rule, holes) == true
return true
Expand Down
6 changes: 3 additions & 3 deletions src/localconstraints/local_contains_subtree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ Enforces that a given `tree` appears at or below the given `path` at least once.
The `indices` and `candidates` fields should not be set by the user.
"""
mutable struct LocalContainsSubtree <: AbstractLocalConstraint
path::Vector{Int}
path::Vector{Integer}
tree::AbstractRuleNode
candidates::Union{Vector{AbstractRuleNode}, Nothing}
indices::Union{StateSparseSet, Nothing}
end

"""
LocalContainsSubtree(path::Vector{Int}, tree::AbstractRuleNode)
LocalContainsSubtree(path::Vector{<:Integer}, tree::AbstractRuleNode)

Enforces that a given `tree` appears at or below the given `path` at least once.
"""
function LocalContainsSubtree(path::Vector{Int}, tree::AbstractRuleNode)
function LocalContainsSubtree(path::Vector{<:Integer}, tree::AbstractRuleNode)
LocalContainsSubtree(path, tree, Vector{AbstractRuleNode}(), nothing)
end

Expand Down
2 changes: 1 addition & 1 deletion src/localconstraints/local_forbidden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ provided by the path.
Use a `Forbidden` constraint for enforcing this throughout the entire search space.
"""
struct LocalForbidden <: AbstractLocalConstraint
path::Vector{Int}
path::Vector{Integer}
tree::AbstractRuleNode
end

Expand Down
18 changes: 9 additions & 9 deletions src/localconstraints/local_forbidden_sequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ Forbids the given `sequence` of rule nodes ending at the node at the `path`.
If any of the rules in `ignore_if` appears in the sequence, the constraint is ignored.
"""
struct LocalForbiddenSequence <: AbstractLocalConstraint
path::Vector{Int}
sequence::Vector{Int}
ignore_if::Vector{Int}
path::Vector{Integer}
sequence::Vector{Integer}
ignore_if::Vector{Integer}
end

"""
shouldschedule(::Solver, constraint::LocalForbiddenSequence, path::Vector{Int})::Bool
shouldschedule(::Solver, constraint::LocalForbiddenSequence, path::Vector{<:Integer})::Bool

Return true iff the manipulation happened at or above the constraint path.
"""
function shouldschedule(::Solver, constraint::LocalForbiddenSequence, path::Vector{Int})::Bool
function shouldschedule(::Solver, constraint::LocalForbiddenSequence, path::Vector{<:Integer})::Bool
return (length(constraint.path) >= length(path)) && (path== constraint.path[1:length(path)] )
end

Expand All @@ -28,7 +28,7 @@ function propagate!(solver::Solver, c::LocalForbiddenSequence)
track!(solver, "LocalForbiddenSequence propagation")

# Smallest match
forbidden_assignments = Vector{Tuple{Int, Any}}()
forbidden_assignments = Vector{Tuple{Integer, Any}}()
i = length(c.sequence)
for (path_idx, node) ∈ Iterators.reverse(enumerate(nodes))
forbidden_rule = c.sequence[i]
Expand Down Expand Up @@ -75,7 +75,7 @@ function propagate!(solver::Solver, c::LocalForbiddenSequence)
return
elseif length(forbidden_assignments) == 1
path_idx, rule = forbidden_assignments[1]
if rule isa Int
if rule isa Integer
track!(solver, "LocalForbiddenSequence deduction")
else
track!(solver, "LocalForbiddenSequence deduction by ignore_if")
Expand Down Expand Up @@ -134,11 +134,11 @@ end


"""
function get_nodes_on_path(root::AbstractRuleNode, path::Vector{Int})::Vector{AbstractRuleNode}
function get_nodes_on_path(root::AbstractRuleNode, path::Vector{<:Integer})::Vector{AbstractRuleNode}

Gets a list of nodes on the `path`, starting (and including) the `root`.
"""
function get_nodes_on_path(node::AbstractRuleNode, path::Vector{Int})::Vector{AbstractRuleNode}
function get_nodes_on_path(node::AbstractRuleNode, path::Vector{<:Integer})::Vector{AbstractRuleNode}
nodes = Vector{AbstractRuleNode}()
push!(nodes, node)
for i ∈ path
Expand Down
2 changes: 1 addition & 1 deletion src/localconstraints/local_ordered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ specified in `order` when the pattern is applied at the location given by `path`
Use an `Ordered` constraint for enforcing this throughout the entire search space.
"""
mutable struct LocalOrdered <: AbstractLocalConstraint
path::Vector{Int}
path::Vector{Integer}
tree::AbstractRuleNode
order::Vector{Symbol}
end
Expand Down
14 changes: 7 additions & 7 deletions src/localconstraints/local_unique.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Enforces that a given `rule` appears at or below the given `path` at most once.
In case of the UniformSolver, cache the list of `holes`, since no new holes can appear.
"""
struct LocalUnique <: AbstractLocalConstraint
path::Vector{Int}
rule::Int
path::Vector{Integer}
rule::Integer
holes::Vector{AbstractHole}
end

LocalUnique(path::Vector{Int}, rule::Int) = LocalUnique(path, rule, Vector{AbstractHole}())
LocalUnique(path::Vector{<:Integer}, rule::Integer) = LocalUnique(path, rule, Vector{AbstractHole}())

"""
function propagate!(solver::Solver, c::LocalUnique)
Expand Down Expand Up @@ -51,7 +51,7 @@ function propagate!(solver::Solver, c::LocalUnique)
end

"""
function _count_occurrences!(node::AbstractRuleNode, rule::Int, holes::Vector{AbstractHole})::Int
function _count_occurrences!(node::AbstractRuleNode, rule::Integer, holes::Vector{AbstractHole})::Integer

Recursive helper function for the LocalUnique constraint.
Returns the number of certain occurrences of the rule in the tree.
Expand All @@ -61,7 +61,7 @@ All holes that potentially can hold the target rule are stored in the `holes` ve
Stops counting if the rule occurs more than once.
Counting beyond 2 is not needed for LocalUnique.
"""
function _count_occurrences!(node::AbstractRuleNode, rule::Int, holes::Vector{AbstractHole})::Int
function _count_occurrences!(node::AbstractRuleNode, rule::Integer, holes::Vector{AbstractHole})::Integer
count = 0
if isfilled(node)
# if the rulenode is the second occurence of the rule, hardfail
Expand All @@ -87,15 +87,15 @@ function _count_occurrences!(node::AbstractRuleNode, rule::Int, holes::Vector{Ab
end

"""
function _count_occurrences(holes::Vector{AbstractHole}, rule::Int)
function _count_occurrences(holes::Vector{AbstractHole}, rule::Integer)

Counts the occurences of the `rule` in the cached list of `holes`.

!!! warning:
Stops counting if the rule occurs more than once.
Counting beyond 2 is not needed for LocalUnique.
"""
function _count_occurrences(holes::Vector{AbstractHole}, rule::Int)
function _count_occurrences(holes::Vector{AbstractHole}, rule::Integer)
count = 0
for hole ∈ holes
if isfilled(hole) && get_rule(hole) == rule
Expand Down
2 changes: 1 addition & 1 deletion src/patternmatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The pattern can be matched when the `hole` is filled with any of the given `ind`
"""
struct PatternMatchSuccessWhenHoleAssignedTo <: PatternMatchResult
hole::AbstractHole
ind::Union{Int, Vector{Int}}
ind::Union{Integer, Vector{<:Integer}}
end

"""
Expand Down
Loading
Loading