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

Issues with mk_function Typeable trait and blocks #18

Open
ChrisRackauckas opened this issue Sep 13, 2019 · 6 comments
Open

Issues with mk_function Typeable trait and blocks #18

ChrisRackauckas opened this issue Sep 13, 2019 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@ChrisRackauckas
Copy link
Contributor

These were working with the older mk_function before the registration occurred:

oop_ex = quote
    function func(u, p, t)
        X = let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
                ((*)(σ, (-)(y, x)), (-)((*)(x, (-)(ρ, z)), y), (-)((*)(x, y), (*)(β, z)))
            end
        T = promote_type(map(typeof, X)...)
        convert.(T, X)
        construct = if u isa (ModelingToolkit.StaticArrays).StaticArray
                (ModelingToolkit.StaticArrays).similar_type(typeof(u), eltype(X))
            else
                x->begin
                        du = similar(u, T, 3)
                        vec(du) .= x
                        du
                    end
            end
            construct(X)
    end
end
iip_ex = quote
    function (A, u, p, t)
        let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
            A[1] = (*)(σ, (-)(y, x))
            A[2] = (-)((*)(x, (-)(ρ, z)), y)
            A[3] = (-)((*)(x, y), (*)(β, z))
        end
        nothing
    end
end
using GeneralizedGenerated
GeneralizedGenerated.mk_function(oop_ex)
GeneralizedGenerated.mk_function(iip_ex)
@ChrisRackauckas
Copy link
Contributor Author

The second one looks like it's an issue with using quote, since

oop_ex = :(function func(u, p, t)
        X = let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
                ((*)(σ, (-)(y, x)), (-)((*)(x, (-)(ρ, z)), y), (-)((*)(x, y), (*)(β, z)))
            end
        T = promote_type(map(typeof, X)...)
        convert.(T, X)
        construct = if u isa (ModelingToolkit.StaticArrays).StaticArray
                (ModelingToolkit.StaticArrays).similar_type(typeof(u), eltype(X))
            else
                x->begin
                        du = similar(u, T, 3)
                        vec(du) .= x
                        du
                    end
            end
            construct(X)
    end
)
iip_ex = :(function (A, u, p, t)
        let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
            A[1] = (*)(σ, (-)(y, x))
            A[2] = (-)((*)(x, (-)(ρ, z)), y)
            A[3] = (-)((*)(x, y), (*)(β, z))
        end
        nothing
    end
)

is fine. Maybe it needs to check if ex.head == :block then use ex.args[2]?

@ChrisRackauckas ChrisRackauckas changed the title Issues with mk_function Typeable trait Issues with mk_function Typeable trait and blocks Sep 13, 2019
@thautwarm
Copy link
Member

Okay, that's simply due to a purposeful design. Currently mk_function just accepts function-like AST, but I think you prefer fewer restrictions.
I'm to resolve it once I get my laptop.

@ChrisRackauckas
Copy link
Contributor Author

For reference I'm getting:

Not implemented trait GeneralizedGenerated.Typeable for (typeof(typeof)).
error(::String) at error.jl:33
GeneralizedGenerated.Typeable(::Type) at Typeclasses.jl:144
to_type(::typeof(typeof)) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Function, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:105
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
to_typelist(::Array{Any,1}) at typeable.jl:42
##GeneralizedGenerated 19#389 at typeable.jl:64 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:102
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
to_typelist(::Array{Any,1}) at typeable.jl:42
##GeneralizedGenerated 19#389 at typeable.jl:64 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:105
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
to_typelist(::Array{Any,1}) at typeable.jl:42
##GeneralizedGenerated 19#389 at typeable.jl:64 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:102
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
to_typelist(::Array{Any,1}) at typeable.jl:42
##GeneralizedGenerated 19#389 at typeable.jl:64 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:102
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
to_typelist(::Array{Any,1}) at typeable.jl:42
##GeneralizedGenerated 19#389 at typeable.jl:64 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:102
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
to_typelist(::Array{Any,1}) at typeable.jl:42
##GeneralizedGenerated 19#389 at typeable.jl:64 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:102
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined]
(::getfield(GeneralizedGenerated, Symbol("###to_type#383#12")))(::Expr) at typeable.jl:63
to_type(::Expr) at Typeclasses.jl:45
(::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType})(::Expr, ::Type) at typeable.jl:43
mapfoldr_impl(::typeof(identity), ::getfield(GeneralizedGenerated, Symbol("##7#8")){DataType}, ::NamedTuple{(:init,),Tuple{DataType}}, ::Array{Any,1}, ::Int64) at reduce.jl:105
#mapfoldr#189 at reduce.jl:125 [inlined]
#mapfoldr at none:0 [inlined]
#foldr#190 at reduce.jl:144 [inlined]
#foldr at none:0 [inlined...

I think it's due to Function not having a Typeable, so I tried

GeneralizedGenerated.@implement GeneralizedGenerated.Typeable{T} where T <: Function begin
    function to_type(x::Function)
        wrapped = Symbol(x)
        GeneralizedGenerated.TVal{Symbol, wrapped}
    end
end

and crashed some things 🤷‍♂

@thautwarm
Copy link
Member

Hi, in fact, the default implementation is okay:

GeneralizedGenerated.@implement GeneralizedGenerated.Typeable{T} where T <: Function

There're special protocols for type encoding so it's okay that your tried one didn't work.

However after correcting this, still some problems are remained. Working on this.

thautwarm added a commit that referenced this issue Sep 13, 2019
@thautwarm
Copy link
Member

Now it complains correctly @ChrisRackauckas

julia> mk_function(oop_ex)
ERROR: Expect a function expression

Besides, instead of expecting mk_function accept any Julia ASTs, I just added an API runtime_eval:

julia> runtime_eval(1)
1

julia> runtime_eval(Main, :(
           a + 1))
ERROR: UndefVarError: a not defined

julia> runtime_eval(Main, :(
           a + 1))
3

julia> runtime_eval(oop_ex)
RuntimeFn{list(GeneralizedGenerated.Argument(:u, nothing, GeneralizedGenerated.Unset()), GeneralizedGenerated.Argument(:p, nothing, GeneralizedGenerated.Unset()), GeneralizedGenerated.Argument(:t, nothing, GeneralizedGenerated.Unset())),nil(GeneralizedGenerated.Argument),let
    #= REPL[4]:3 =#
    X = let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])

....

thautwarm added a commit that referenced this issue Sep 13, 2019
@thautwarm thautwarm added the bug Something isn't working label Sep 13, 2019
@thautwarm thautwarm self-assigned this Sep 13, 2019
@ChrisRackauckas
Copy link
Contributor Author

Cool. I meant for it to be a long-form anonymous function:

using GeneralizedGenerated
oop_ex = :(function (u, p, t)
        X = let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
                ((*)(σ, (-)(y, x)), (-)((*)(x, (-)(ρ, z)), y), (-)((*)(x, y), (*)(β, z)))
            end
        T = promote_type(map(typeof, X)...)
        convert.(T, X)
        construct = if u isa (ModelingToolkit.StaticArrays).StaticArray
                (ModelingToolkit.StaticArrays).similar_type(typeof(u), eltype(X))
            else
                x->begin
                        du = similar(u, T, 3)
                        vec(du) .= x
                        du
                    end
            end
            construct(X)
    end
)
mk_function(oop_ex)

This works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants