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

New syntax to replace {} #6

Closed
tshort opened this issue Dec 27, 2014 · 20 comments
Closed

New syntax to replace {} #6

tshort opened this issue Dec 27, 2014 · 20 comments

Comments

@tshort
Copy link
Owner

tshort commented Dec 27, 2014

Because curly brackets are going away (JuliaLang/julia#8578), we need something else. Two options I can think of are:

Option 1

Equations[
    der(x, -1.0) - ((1 - y^2) * x - y)
    der(y) - x
]

or

Model[
    der(x, -1.0) - ((1 - y^2) * x - y)
    der(y) - x
]

This is the easiest change. We'd just need to set Equations = Any.

Option 2

@model begin
    der(x, -1.0) = (1 - y^2) * x - y
    der(y) = x
end

This has the advantage that we write the macro to allow = in model equations. We could support either or both of these.

Here are options I don't think will work:
- @model { ... } -- Curly brackets are depreciated in the parser, so macros don't help us avoid that.
- [ ... ] -- uses vcat

@iraikov
Copy link
Collaborator

iraikov commented Dec 27, 2014

I favor Option 2 slightly, as it would permit equality syntax. I don't
really mind either option.

On Sat, Dec 27, 2014 at 12:37 PM, Tom Short notifications@github.com
wrote:

Because curly brackets are going away (JuliaLang/julia#8578
JuliaLang/julia#8578), we need something else.
Two options I can think of are:
Option 1

Equations[
der(x, -1.0) - ((1 - y^2) * x - y)
der(y) - x
]

or

Model[
der(x, -1.0) - ((1 - y^2) * x - y)
der(y) - x
]

This is the easiest change. We'd just need to set Equations = Any.
Option 2

@model begin
der(x, -1.0) = (1 - y^2) * x - y
der(y) = xend

This has the advantage that we write the macro to allow = in model
equations. We could support either or both of these.

Here are options I don't think will work:

  • @model { ... } -- Curly brackets are depreciated in the parser, so
    macros don't help us avoid that.
    -[ ... ]-- usesvcat`


Reply to this email directly or view it on GitHub
#6.

@tshort
Copy link
Owner Author

tshort commented Dec 28, 2014

Here's a first cut at code to implement both options:

using Sims

macro equations(args)
    esc(equations_helper(args))
end

function parse_args(a::Expr)
    if a.head == :line
        nothing
    elseif a.head == :(=)
        Expr(:call, :-, parse_args(a.args[1]), parse_args(a.args[2]))
    else
        Expr(a.head, [parse_args(x) for x in a.args]...)
    end
end

parse_args(a::Array) = [parse_args(x) for x in a]
parse_args(x) = x


function equations_helper(arg)
    Expr(:ref, :Equation, parse_args(arg.args)...)
end

Equation = Any



function Vanderpol1()
    y = Unknown(1.0, "y")
    x = Unknown("x")    
    Equation[
        der(x, -1.0) - ((1 - y^2) * x - y) 
        der(y) - x
    ]
end

v1 = sim(Vanderpol)

function Vanderpol2()
    y = Unknown(1.0, "y")
    x = Unknown("x")    
    @equations begin
        der(x, -1.0) = (1 - y^2) * x - y 
        der(y) = x
    end
end

v1 = sim(Vanderpol1())
v2 = sim(Vanderpol2())

It needs a bit more error checking.

Edit: fix issue spotted by @iraikov.

@iraikov
Copy link
Collaborator

iraikov commented Dec 28, 2014

I had to remove a spurious '=' after 'function parse_args (a::Expr)' but
other than that I can confirm that this code works in Julia 0.3.4.
What kind of error checking does it need?

On Sun, Dec 28, 2014 at 5:37 AM, Tom Short notifications@github.com wrote:

Here's a first cut at code to implement both options:

using Sims
macro equations(args)
esc(equations_helper(args))end
function parse_args(a::Expr) =
if a.head == :line
nothing
elseif a.head == :(=)
Expr(:call, :-, parse_args(a.args[1]), parse_args(a.args[2]))
else
Expr(a.head, [parse_args(x) for x in a.args]...)
endend
parse_args(a::Array) = [parse_args(x) for x in a]parse_args(x) = x

function equations_helper(arg)
Expr(:ref, :Equation, parse_args(arg.args)...)end

Equation = Any

function Vanderpol1()
y = Unknown(1.0, "y")
x = Unknown("x")
Equation[
der(x, -1.0) - ((1 - y^2) * x - y)
der(y) - x
]end

v1 = sim(Vanderpol)
function Vanderpol2()
y = Unknown(1.0, "y")
x = Unknown("x")
@equations begin
der(x, -1.0) = (1 - y^2) * x - y
der(y) = x
endend

v1 = sim(Vanderpol1())
v2 = sim(Vanderpol2())

It needs a bit more error checking.


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Dec 29, 2014

For error checking, we probably need to check that there is a begin construct.

Also, we might also want to support one liners. I don't think that works now.

@iraikov
Copy link
Collaborator

iraikov commented Dec 29, 2014

Hi Tom,

I have extended the definition of equations_helper to check that its
argument is a block:

function equations_helper(arg)
if arg.head == :block
    Expr(:ref, :Equation, parse_args(arg.args)...)
else
    error("Argument to @equations is not a begin..end block")
end

end

On Sun, Dec 28, 2014 at 4:38 PM, Tom Short notifications@github.com wrote:

For error checking, we probably need to check that there is a begin
construct.

Also, we might also want to support one liners. I don't think that works
now.


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort tshort closed this as completed Jan 2, 2015
@iraikov
Copy link
Collaborator

iraikov commented Jan 2, 2015

Actually, I just realized I forgot to convert the examples to the new
syntax. Maybe we should wait to close this ticket until this is done.

On Thu, Jan 1, 2015 at 5:32 PM, Tom Short notifications@github.com wrote:

Closed #6 #6.


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Jan 2, 2015

Okay, reopening...

@tshort tshort reopened this Jan 2, 2015
@tshort
Copy link
Owner Author

tshort commented Jan 2, 2015

I'll go through the standard library and convert to one of these. Any preference?

@iraikov
Copy link
Collaborator

iraikov commented Jan 2, 2015

I was just looking at the vanderpol and vanderpol_with_events examples, and
discovered that neither @equations not Equation works with examples that
have events in them. This is apart from the issue that Equations does not
work with one-liners.

When I replace {} with @equations begin .. end in vanderpol_with_events,
leaving the Event definitions as they are,
I get the error, "ERROR: getindex has no method matching
getindex(::Function, ::Nothing, ::MExpr, ::Nothing, ::MExpr, ::Nothing,
::MExpr, ::Nothing, ::Event)"

Anyway, to answer your question, I prefer @equations for systems with
multiple equations, and Equation[] for single equations and things like
Event(..,{},{}).

On Thu, Jan 1, 2015 at 6:25 PM, Tom Short notifications@github.com wrote:

I'll go through the standard library and convert to one of these. Any
preference?


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Jan 2, 2015

I'm not surprised on the macro, but I am on the Equation[]. Hmm...

@iraikov
Copy link
Collaborator

iraikov commented Jan 2, 2015

I stand corrected, Equations[] works, I had just forgotten the commas... I
must admit I do not understand Julia macros enough to know how to debug
@equations.

On Thu, Jan 1, 2015 at 7:02 PM, Tom Short notifications@github.com wrote:

I'm not surprised on the macro, but I am on the Equation[]. Hmm...


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Jan 2, 2015

I'll take a look tomorrow. Also, you shouldn't need commas...

@iraikov
Copy link
Collaborator

iraikov commented Jan 2, 2015

If you can also look at Event, it seems to rely on the {} syntax so it
would need to be rewritten to use Equations[](I think).

On Thu, Jan 1, 2015 at 7:05 PM, Tom Short notifications@github.com wrote:

I'll take a look tomorrow. Also, you shouldn't need commas...


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Jan 2, 2015

I've gotten the examples converted. I'll work on stdlib now.

@tshort
Copy link
Owner Author

tshort commented Jan 3, 2015

@iraikov, this should be done, but please check your examples. There's a chance that I goofed parens accidentally.

@tshort tshort closed this as completed Jan 3, 2015
@iraikov
Copy link
Collaborator

iraikov commented Jan 3, 2015

Hi Tom,

All of the neural examples work, except for the only one that has
structural events:

https://github.com/tshort/Sims.jl/blob/master/examples/neural/iafrefr.jl

It seems that the trefr constant, computed on line 57 is somehow not
visible in the call to the Refractory function.
Any idea what is going on? Thanks,

-Ivan

On Sat, Jan 3, 2015 at 5:26 AM, Tom Short notifications@github.com wrote:

@iraikov https://github.com/iraikov, this should be done, but please
check your examples. There's a chance that I goofed parens accidentally.


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Jan 4, 2015

I don't think that should have worked. trefr shouldn't be visible. I pushed a version that may get around it. This again didn't work with @equations in one spot. I'll try to dig deeper.

@iraikov
Copy link
Collaborator

iraikov commented Jan 4, 2015

Thanks for looking at this. Of course, I would be happy with different
syntax, but the important thing is to be able to pass arguments to the
StructuralEvent transition function.

On Sat, Jan 3, 2015 at 4:19 PM, Tom Short notifications@github.com wrote:

I don't think that should have worked. trefr shouldn't be visible. I
pushed a version that may get around it. This again didn't work with
@equations in one spot. I'll try to dig deeper.


Reply to this email directly or view it on GitHub
#6 (comment).

@tshort
Copy link
Owner Author

tshort commented Jan 4, 2015

I think I fixed the trefr issue. It should have been obvious to me what was going on...

@iraikov
Copy link
Collaborator

iraikov commented Jan 4, 2015

Works like a charm, thanks a lot!

On Sat, Jan 3, 2015 at 7:23 PM, Tom Short notifications@github.com wrote:

I think I fixed the trefr issue. It should have been obvious to me what
was going on...


Reply to this email directly or view it on GitHub
#6 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants