Skip to content

Commit

Permalink
Clarify exports
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrisonGrodin committed Jul 8, 2019
1 parent a3ea3d4 commit ee16ffa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
50 changes: 32 additions & 18 deletions src/core.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export Variable
import Base: match

export Variable, match


const Σ = Symbol
Expand All @@ -11,13 +13,39 @@ abstract type AbstractMatcher end
abstract type AbstractSubproblem end


mutable struct Variable end


const Substitution = Dict{Variable,AbstractTerm}

struct Matches{S<:AbstractSubproblem}
p::Substitution
s::S
end
Base.IteratorSize(::Type{<:Matches}) = Base.SizeUnknown()

struct Fail end
Base.iterate(::Fail) = nothing
Base.length(::Fail) = 0
const fail = Fail()

"""
match(pattern::AbstractMatcher, term::AbstractTerm)
Match `term` against `pattern`, producing an iterator containing all matches.
"""
function match(p::AbstractMatcher, t::AbstractTerm)
σ = Substitution()
s = match!(σ, p, t)
s === nothing && return fail
Matches(σ, s)
end


struct EmptySubproblem <: AbstractSubproblem end
Base.iterate(m::Matches{EmptySubproblem}) = (m.p, nothing)
Base.iterate(::Matches{EmptySubproblem}, ::Any) = nothing


mutable struct Variable end

vars(x::Variable) = Set([x])
compile(x::Variable, V) = (x, push!(copy(V), x))
@inline function match!(σ, x::Variable, t::AbstractTerm)
Expand All @@ -32,17 +60,3 @@ end
>(::Variable, ::AbstractTerm) = false
>(::AbstractTerm, ::Variable) = true
>(x::Variable, y::Variable) = objectid(x) > objectid(y)


const Substitution = Dict{Variable,AbstractTerm}

struct Matches{S<:AbstractSubproblem}
p::Substitution
s::S
end
Base.IteratorSize(::Type{<:Matches}) = Base.SizeUnknown()

struct Fail end
Base.iterate(::Fail) = nothing
Base.length(::Fail) = 0
const fail = Fail()
25 changes: 8 additions & 17 deletions src/interface.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export compile


"""
theory(T::Type{<:AbstractTerm}) -> Theory
Expand Down Expand Up @@ -50,27 +53,15 @@ Compile `t` to a matcher.
"""
compile(t) = compile(t, Set{Variable}())[1]

"""
match(pattern::AbstractMatcher, term::AbstractTerm)
Match `term` against `pattern`, producing an iterator containing all matches.
!!! note
The iterator should only be nonempty when `pattern` and `term` are derived from the
same theory.
"""
function match(p::AbstractMatcher, t::AbstractTerm)
σ = Substitution()
s = match!(σ, p, t)
s === nothing && return fail
Matches(σ, s)
end

"""
match!(σ, pattern::AbstractMatcher, term::AbstractTerm) -> Union{AbstractSubproblem,Nothing}
Match `term` against `pattern` given the partial substitution `σ`, mutating `σ` and
producing a subproblem to solve or producing `nothing` if a match is impossible.
!!! note
Unless `pattern` and `term` are derived from the same theory, `match!` should
necessarily produce `nothing`.
"""
match!(::Any, ::AbstractMatcher, ::AbstractTerm) = nothing
4 changes: 2 additions & 2 deletions test/match.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ TESTS = [

@testset "$set" for (set, tests) TESTS
@testset "$pattern" for (pattern, cases) tests
m = Terms.compile(pattern)
m = compile(pattern)
@testset "$term" for (term, ms) cases
matches = Terms.match(m, term)
matches = match(m, term)
@test Set(matches) == Set(matches) # immutability of iterator
@test Set(matches) == Set(ms)
end
Expand Down

0 comments on commit ee16ffa

Please sign in to comment.