Skip to content

Commit

Permalink
better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris Brown authored and olynch committed Sep 13, 2023
1 parent 61c182e commit b912ea2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 44 deletions.
12 changes: 1 addition & 11 deletions src/syntax/GATs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ headof(t::TrmTyp) = t.head
argsof(t::TrmTyp) = t.args

rename(tag::ScopeTag, reps::Dict{Symbol,Symbol}, t::T) where T<:TrmTyp =
T(rename(tag, reps, headof(t)), rename.(Ref(tag), Ref(reps), argsof(t)))
T(rename(tag, reps, headof(t)), AlgTerm[rename.(Ref(tag), Ref(reps), argsof(t))...])

function Base.show(io::IO, type::T) where T<:TrmTyp
print(io, "$(nameof(T))(")
Expand Down Expand Up @@ -257,11 +257,6 @@ sortsignature(tc::Union{AlgTypeConstructor, AlgTermConstructor}) =
argcontext(t::Union{AlgTypeConstructor,AlgTermConstructor}) =
t.localcontext + t.args

function retag_args(t::AlgTermConstructor)
ac = gettag(argcontext(t))
Dict(gettag(t.localcontext)=>ac, gettag(t.args)=>ac)
end

"""
`AlgAxiom`
Expand Down Expand Up @@ -736,11 +731,6 @@ end
ExprInterop.toexpr(c::Context, ts::TypeScope) =
Expr(:vect,[Expr(:(::), nameof(b), toexpr(c, getvalue(b))) for b in ts]...)

ExprInterop.fromexpr(c::Context, e, ::Type{TypeScope}) = @match e begin
Expr(:vect, ps...) => parsetypescope(c, ps)
_ => error("Here $e $(dump(e))")
end

ExprInterop.toexpr(c::Context, at::Binding{AlgType, Nothing}) =
Expr(:(::), nameof(at), ExprInterop.toexpr(c, getvalue(at)))

Expand Down
14 changes: 0 additions & 14 deletions src/syntax/Scopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,6 @@ retag(replacements::Dict{ScopeTag,ScopeTag}, s::Scope{T,Sig}) where {T,Sig} =
retag.(Ref(replacements), s.bindings),
s.names)

function rename(tag::ScopeTag,
replacements::Dict{Symbol, Symbol},
scope::Scope{T, Sig}) where {T, Sig}
if tag == gettag(scope)
Scope{T,Sig}(tag, map(scope.bindings) do b
Binding{T,Sig}(get(replacements,b.primary,b.primary),
Set([get(replacements, a, a) for a in b.aliases]),
rename(tag, replacements, b.value), b.sig, b.line)
end, Dict(get(replacements, k, k)=>v for (k,v) in collect(scope.names)))
else
Scope{T,Sig}(tag, retag(Ref(replacements), ))
end
end

function Base.:(+)(x::Scope{T,Sig}, y::Scope{T,Sig}) where {T,Sig}
newtag = x.tag+y.tag
rep = Dict(gettag(x) => newtag, gettag(y)=>newtag)
Expand Down
32 changes: 13 additions & 19 deletions src/syntax/TheoryMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,22 @@ thought of in many ways.
3. X is a syntax for Y's semantics.
The main methods for an AbsTheoryMap to implement are:
dom, codom, typemap, termmap
- dom, codom,
- typemap: A dictionary of Ident (of AlgTypeConstructor in domain) to AlgSort
(This must be a AlgSort of the same arity.)
- termmap: A dictionary of Ident (of AlgTermConstructor in domain) to
TermInCtx. (The TypeScope of the TrmInCtx must be structurally
identical to the localcontext + args of the term constructor
associated with the key.)
"""
abstract type AbsTheoryMap end

"""
A dictionary of Ident (of AlgTypeConstructor in domain) to AlgSort
This must be a AlgSort of the same arity.
"""
typemap(::AbsTheoryMap)::Dict{Ident, AlgSort} = error("Not implemented")

"""
A dictionary of Ident (of AlgTermConstructor in domain) to TermInCtx.
The TypeScope of the TrmInCtx must be structurally identical to the
localcontext + args of the term constructor associated with the key.
"""
termmap(::AbsTheoryMap)::Dict{Ident, TermInCtx} = error("Not implemented")

# Category of GATs
#-----------------
dom(f::AbsTheoryMap)::GAT = f.dom
dom(f::AbsTheoryMap)::GAT = f.dom # assume this exists by default

codom(f::AbsTheoryMap)::GAT = f.codom
codom(f::AbsTheoryMap)::GAT = f.codom # assume this exists by default

function compose(f::AbsTheoryMap, g::AbsTheoryMap)
typmap = Dict(k => typemap(g)[only(v.ref)] for (k,v) in pairs(typemap(f)))
Expand Down Expand Up @@ -128,6 +121,8 @@ dom(f::IdTheoryMap)::GAT = f.gat

codom(f::IdTheoryMap)::GAT = f.gat

compose(f::IdTheoryMap, ::IdTheoryMap) = f

compose(::IdTheoryMap, g::AbsTheoryMap) = g

compose(f::AbsTheoryMap, ::IdTheoryMap) = f
Expand Down Expand Up @@ -240,7 +235,6 @@ end

macro theorymap(head, body)
(domname, codomname) = @match head begin
(name::Symbol) => (name, nothing)
Expr(:call,:(=>), name, parent) => (name, parent)
_ => error("could not parse head of @theory: $head")
end
Expand All @@ -251,6 +245,6 @@ macro theorymap(head, body)
end

"""Invert a theory iso"""
Base.inv(::TheoryMap) = error("Not implemented")
# Base.inv(::TheoryMap) = error("Not implemented")

end # module
8 changes: 8 additions & 0 deletions test/syntax/GATs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ O, H, i, cmp = idents(seg; lid=LID.(1:4))
# Extend seg with a context of (A: Ob)
sortscope = Scope([Binding{AlgType}(:A, Set([:A]), AlgType(O))])
A = ident(sortscope; name=:A)
ATerm = AlgTerm(A)


ss = AppendScope(ScopeList([seg]), sortscope)
@test sortcheck(ss, AlgTerm(A)) == AlgSort(O)
Expand All @@ -78,6 +80,12 @@ haia = AlgType(H,[AlgTerm(A),ida])
@test sortcheck(ss, haa)
@test_throws Exception sortcheck(ss, haia)

# Renaming
BTerm = rename(gettag(sortscope), Dict(:A=>:B), ATerm)
Bsortscope = Scope([Binding{AlgType}(:B, Set([:B]), AlgType(O))]; tag=gettag(sortscope))
BTerm_expected = AlgTerm(ident(Bsortscope;name=:B))
@test BTerm == BTerm_expected

# Subset
#-------
T = ThCategory.THEORY
Expand Down
7 changes: 7 additions & 0 deletions test/syntax/Scopes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ using Test, GATlab
# ScopeTags
###########

basicprinted(x; color=false) = sprint(show, x; context=(:color=>color))

tag1 = newscopetag()
tag2 = newscopetag()

Expand All @@ -16,6 +18,9 @@ err = ScopeTagError(nothing, nothing)

@test sprint(showerror, err) isa String

@test basicprinted(tag1) != basicprinted(tag2)
@test basicprinted(tag1; color=true) != basicprinted(tag2)

# Local IDs
###########

Expand Down Expand Up @@ -52,6 +57,8 @@ nameless = Ident(tag1, LID(1), nothing)

# References
############
@test_throws ArgumentError only(Reference())
@test basicprinted(Reference()) == "_"

y = Ident(tag2, LID(1), :y)
xdoty = Reference(x, Reference(y))
Expand Down
19 changes: 19 additions & 0 deletions test/syntax/TheoryMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ end
@test PreorderCat(Cmp) isa TermInCtx
@test PreorderCat(argcontext(getvalue(T[Cmp]))) isa TypeScope

@test_throws KeyError PreorderCat(first(typecons(T2)))

xterm = fromexpr(ThMonoid.THEORY, :(x [x]), TermInCtx)
res = NatPlusMonoid(xterm)
toexpr(ThNat.THEORY, res)
Expand All @@ -70,7 +72,24 @@ expected = fromexpr(ThNatPlus.THEORY, :(Z+(Z+x) ⊣ [x::ℕ]), TermInCtx)
#############
TLC = ThLawlessCat.THEORY
incl = TheoryIncl(TLC, T)
@test TheoryMaps.dom(incl) == TLC
@test TheoryMaps.codom(incl) == T
incl2 = TheoryIncl(ThGraph.THEORY, TLC)
incl3 = TheoryIncl(ThGraph.THEORY, T)

@test TheoryMaps.compose(incl2, incl) == incl3

toexpr(incl)
@test_throws ErrorException TheoryIncl(T, TLC)
@test_throws ErrorException inv(incl)

# Identity
##########
i = IdTheoryMap(T)
@test TheoryMaps.dom(i) == T
@test TheoryMaps.codom(i) == T
@test TheoryMaps.compose(i,i) == i
@test TheoryMaps.compose(incl,i) == incl


end # module

0 comments on commit b912ea2

Please sign in to comment.