Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add import statements to @theory declaration #107

Merged
merged 1 commit into from
Sep 21, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/models/ModelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function parse_model_param(e)
model_type = @match paramdecl begin
Expr(:vect, Expr(:(::), :model, model_type)) => model_type
nothing => nothing
_ => error("invalid syntax for declaring model type: $model")
_ => error("invalid syntax for declaring model type: $paramdecl")
end

(model_type, whereparams)
Expand Down
16 changes: 16 additions & 0 deletions src/stdlib/models/Arithmetic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Arithmetic

export IntNatPlus

using ....Models
using ...StdTheories

struct IntNatPlus <: Model{Tuple{Int}} end

@instance ThNatPlus{Int} [model::IntNatPlus] begin
Z() = 0
S(n::Int) = n + 1
+(x::Int, y::Int) = x + y
end

end # module
2 changes: 2 additions & 0 deletions src/stdlib/models/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module StdModels
using Reexport

include("FinSets.jl")
include("Arithmetic.jl")
include("FinMatrices.jl")
include("SliceCategories.jl")
include("Op.jl")
include("Nothings.jl")
include("GATs.jl")

@reexport using .FinSets
@reexport using .Arithmetic
@reexport using .FinMatrices
@reexport using .SliceCategories
@reexport using .Op
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/theories/Naturals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using ....Syntax
end

@theory ThNatPlus <: ThNat begin
import Base: +
((x::ℕ) + (y::ℕ))::ℕ
(n + S(m) == S(n+m) :: ℕ) ⊣ [n::ℕ,m::ℕ]
end
Expand Down
26 changes: 15 additions & 11 deletions src/syntax/GATs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@
(name, arglist) = @match head begin
Expr(:call, name, args...) => (name, args)
name::Symbol => (name, [])
_ => error("failed to parse head of term constructor $call")
_ => error("failed to parse head of term constructor $head")
end
args = parsetypescope(c′, arglist; bound=Set(nameof.(localcontext)))
@match type_expr begin
Expand Down Expand Up @@ -795,25 +795,29 @@
c′ = AppendScope(c, seg)
linenumber = nothing
for line in e.args
@match line begin
l::LineNumberNode => (linenumber = l)
Expr(:macrocall, var"@op", _, aliasexpr) => begin
@switch line begin
@case l::LineNumberNode
(linenumber = l)
@case Expr(:macrocall, var"@op", _, aliasexpr)
lines = @match aliasexpr begin
Expr(:block, lines...) => lines
_ => [aliasexpr]
end
for line in lines
@match line begin
_::LineNumberNode => nothing
:($alias := $name) => Scopes.unsafe_addalias!(seg, name, alias)
_ => error("could not match @op line $line")
@switch line begin
@case (_::LineNumberNode)

Check warning on line 808 in src/syntax/GATs.jl

View check run for this annotation

Codecov / codecov/patch

src/syntax/GATs.jl#L808

Added line #L808 was not covered by tests
nothing
@case :($alias := $name)
Scopes.unsafe_addalias!(seg, name, alias)
@case _

Check warning on line 812 in src/syntax/GATs.jl

View check run for this annotation

Codecov / codecov/patch

src/syntax/GATs.jl#L812

Added line #L812 was not covered by tests
error("could not match @op line $line")
end
end
end
_ => begin
@case Expr(:import, Expr(:(:), Expr(:(.), mod), imports))
nothing
@case _

Check warning on line 818 in src/syntax/GATs.jl

View check run for this annotation

Codecov / codecov/patch

src/syntax/GATs.jl#L818

Added line #L818 was not covered by tests
binding = setline(fromexpr(c′, line, JudgmentBinding), linenumber)
Scopes.unsafe_pushbinding!(seg, binding)
end
end
end
seg
Expand Down
12 changes: 11 additions & 1 deletion src/syntax/TheoryInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,23 @@
end

newsegment = fromexpr(parent, body, GATSegment)
importlines = Expr[]
for line in body.args
@switch line begin
@case Expr(:import, Expr(:(:), Expr(:., mod), imports))
push!(importlines, line)
@case _

Check warning on line 61 in src/syntax/TheoryInterface.jl

View check run for this annotation

Codecov / codecov/patch

src/syntax/TheoryInterface.jl#L61

Added line #L61 was not covered by tests
nothing
end
end


theory = GAT(name, parent, newsegment)

modulelines = Any[]

push!(modulelines, :(export $(allnames(theory; aliases=true)...)))

append!(modulelines, importlines)
if !isnothing(parentname)
push!(modulelines, Expr(:using, Expr(:(.), :(.), :(.), parentname)))
end
Expand Down
13 changes: 13 additions & 0 deletions test/stdlib/models/Arithmetic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module TestArithmetic

using GATlab
using Test

using .ThNatPlus

@withmodel IntNatPlus() (ℕ, Z, S, +) begin
@test S(S(Z())) + Z() == 2
end


end # module
1 change: 1 addition & 0 deletions test/stdlib/models/tests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TestStdModels

include("FinSets.jl")
include("Arithmetic.jl")
include("FinMatrices.jl")
include("SliceCategories.jl")
include("Op.jl")
Expand Down