Giter VIP home page Giter VIP logo

juliavariables.jl-b14d175d-62b4-44ba-8fb7-3064adc8c3ec's Introduction

JuliaVariables

Stable Dev Build Status Codecov

About

The solve function will solve the scopes of a simplified Julia expression.

  • The variables(Symbol) are transformed to Var:
    struct Var
        name       :: Symbol
        is_mutable :: Bool
        is_shared  :: Bool
        is_global  :: Bool
    end
  • Some expressions will be wrapped within Expr(:scoped, (bounds=..., freevars=..., bound_inits=...), inner_expression).

Example

solve & solve_from_local

julia> using MLStyle

julia> unwrap_scoped(ex) =
           @match ex begin
               Expr(:scoped, _, a) => unwrap_scoped(a)
               Expr(head, args...) => Expr(head, map(unwrap_scoped, args)...)
               a => a
           end
unwrap_scoped (generic function with 1 method)

julia> quote
           x = 1
           function (a)
               x = 1
           end
       end |>  solve_from_local |> rmlines |> unwrap_scoped
quote
    mut @shared x = 1
    function (a,)
        mut @shared x = 1
    end
end


julia> quote
           x = 1
           function ()
               x = 1
           end
       end |>  solve |> rmlines
:($(Expr(:scoped, (bounds = Var[], freevars = Var[], bound_inits = Symbol[]), quote
    @global x = 1
    function ()
        $(Expr(:scoped, (bounds = Var[@local x], freevars = Var[], bound_inits = Symbol[]), quote
    @local x = 1
end))
    end
end)))


julia> quote
           x = 1
           function ()
               x = 1
           end
       end |>  solve_from_local |> rmlines
:($(Expr(:scoped, (bounds = Var[mut @shared x], freevars = Var[], bound_inits = Symbol[]), quote
    mut @shared x = 1
    function ()
        $(Expr(:scoped, (bounds = Var[], freevars = Var[mut @shared x], bound_inits = Symbol[]), quote
    mut @shared x = 1
end))
    end
end)))

simplify_ex

Not all expressions can be accepted as the input of solve or solve_from_local, thus we provide such a handy API to apply conversions from almost arbitrary expressions to the simplified expressions.

julia> quote
          function f(x)
               for i in I, j in J
                   let x = 1, y
                       () -> 2
                   end
               end
               f(x) = 2
          end
       end |> rmlines |> simplify_ex
quote
    function f(x)
        for i = I
            for j = J
                let x = 1
                    let y
                        function ()
                            2
                        end
                    end
                end
            end
        end
        function f(x)
            2
        end
    end
end

The reason why we don't couple this API with solve is, we need to let user aware that there exists destructive operations for expressing the scope information, for instance, it's impossible to inject scope information to for i in I, j in J; body end, because the AST shape of it is

Expr(:for,
    Expr(:block,
        :(i = I),
        :(j = J),
    ),
    Expr(:block, body)
)

Expr(:block, body) is actually in the sub-scope of that of :(j = J), and :(j=J)'s scope in inherited from that of :(i=I), which ruins the handy use(especially the top-down tree visiting) of scoped expressions.

Not only due to the uselessness of scoping the messy ASTs like for i in I, j in J; body end, the analyses for them are also much more ugly to implement than those of the simplified expressions. Finally, I give up doing this.

If you have understand the above concerns and made sure it's safe to return a restructured expression after injecting scope information, you can compose simplify_ex and solve to gain a more handy API:

mysolve = solve  simplify_ex
mysolve_from_local = solve_from_local  simplify_ex

juliavariables.jl-b14d175d-62b4-44ba-8fb7-3064adc8c3ec's People

Contributors

cscherrer avatar lfkdsk avatar thautwarm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.