Skip to content

Commit

Permalink
refined subtype macro and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Apr 9, 2019
1 parent acbea1b commit 1d5971b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ after_success:
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("Reduce")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
# update docs
- julia -e 'Pkg.add("Documenter")'
- julia -e 'using Pkg; ps=PackageSpec(name="Documenter", version="0.19"); Pkg.add(ps); Pkg.pin(ps)'
- julia -e 'cd(Pkg.dir("Reduce")); include(joinpath("docs", "make.jl"))'

9 changes: 4 additions & 5 deletions src/Reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,15 @@ include("switch.jl") # load switch operators

module Algebra
using Reduce, LinearAlgebra
import ..extract

include("unary.jl") # load unary operators
include("args.jl") # load calculus operators

function init_subtype(name)
Expr(:block,[:(Base.$i(r::$name...)=$i(extract.(r)...)|>$name) for i [alg;iops]]...) |> eval
Expr(:block,[:($i(r::$name...)=$i(extract.(r)...)|>$name) for i [calculus;cnan;cmat]]...) |> eval
Expr(:block,[:(Base.$i(r::$name)=$i(extract(r))|>$name) for i [sbas;[:length]]]...) |> eval
Expr(:block,[:($i(r::$name)=$i(extract(r))|>$name) for i [sfun;snan;snum;scom;sint;sran;smat]]...) |> eval
Expr(:block,[:(Base.$i(r::$name...)=$i(RExpr.(r)...)|>$name) for i [alg;iops]]...) |> eval
Expr(:block,[:($i(r::$name...)=$i(RExpr.(r)...)|>$name) for i [calculus;cnan;cmat]]...) |> eval
Expr(:block,[:(Base.$i(r::$name)=$i(RExpr(r))|>$name) for i [sbas;[:length]]]...) |> eval
Expr(:block,[:($i(r::$name)=$i(RExpr(r))|>$name) for i [sfun;snan;snum;scom;sint;sran;smat]]...) |> eval
end

const variables = [
Expand Down
20 changes: 19 additions & 1 deletion src/rexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,32 @@ end

function extract end

"""
Reduce.@subtype
Can be used to create an `RExpr` wrapper type with a subtype relation
```Julia
julia> Reduce.@subtype FakeReal <: Real
julia> FakeReal(:(x+1)) + FakeReal(:y)
y + 1 + x
```
"""
macro subtype(x)
x.head :<: && throw(error("$x is not a subtype expression"))
name = x.args[1]
Expr(:struct,false,x,Expr(:block,Expr(:(::), :r, :RExpr))) |> eval
Expr(:block,[:($fun(r::$name) = $fun(r.r)) for fun [:String,:string,:join,:list,:parse,:mat]]...) |> eval
@eval begin
export $name
$name(x) = $name(RExpr(x))
RExpr(r::$name) = r.r
show(io::IO, r::$name) = show(io,r.r)
extract(r::$name) = r.r
==(a::$name,b::$name) = a.r == b.r
rcall(r::$name,s...) = rcall(r.r,s...)
convert(::Type{$name}, r::RExpr) = r.r
convert(::Type{Array{String,1}}, r::$name) = r.r.str
convert(::Type{String}, r::$name) = convert(String,r.r)
Algebra.init_subtype($name)
end
nothing
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ operator(:cbrt)
@test [:x,:y]' + 1 == 1 + [:x,:y]'
@test solve(:(x-1),:x) == solve((:(x-1),),:x)
@test (order(nothing); korder(nothing); true)
@test (Reduce.@subtype FakeReal <: Real; FakeReal(:(x+1)) + FakeReal(:y) == FakeReal(:(x+1+y)))

0 comments on commit 1d5971b

Please sign in to comment.