We should introduce `map` and `filter` implementations for `Expr` that
traverse the expression tree. It would be quite convenient if `Expr` was a
full-scale collection.

Or maybe there should be a generic (parameterized) tree data structure, and
`Expr` is then an instance of it.

-erik

On Wed, Dec 9, 2015 at 5:24 PM, <[email protected]> wrote:

> Oh cool, did not know! Thank you! (I'm very new to Julia, sorry)
>
> For anyone else looking for this, here's a function that strips metadata
> (so far just LineNumberNodes) from Exprs so you can compare them.
>
> function stripmeta(expr)
>   println(typeof(expr))
>   if isa(expr, Expr)
>     return Expr(expr.head, stripmeta(expr.args)...)
>   elseif isa(expr, Array)
>     return map(stripmeta, filter(x -> !isa(x, LineNumberNode), expr))
>   else
>     return expr
>   end
> end
>
> All of the tests work as expected now:
>
> Expr(:if, true, Expr(:block, 0), Expr(:block,1)) == :(if true 0 1) --> TRUE
>
> :) Thanks!
>
>
> On Wednesday, December 9, 2015 at 2:15:25 PM UTC-8, Yichao Yu wrote:
>>
>> On Wed, Dec 9, 2015 at 5:12 PM,  <[email protected]> wrote:
>> > sorry ok, I see - it's
>> > Expr.head
>> > and
>> > Expr.args
>> >
>> > Man, I really wish Julia had something like python's help(Expr) to see
>> all
>> > the methods/fields of a class...
>> >
>>
>> ```
>> help?> Expr
>> search: Expr export nextprod expanduser expm exp2 exp expm1 exp10
>> expand exponent
>>
>>   No documentation found.
>>
>>   Summary:
>>
>>   type Expr <: Any
>>
>>   Fields:
>>
>>   head :: Symbol
>>   args :: Array{Any,1}
>>   typ  :: Any
>> ```
>>
>> >
>> > On Tuesday, December 8, 2015 at 8:26:35 PM UTC-8, [email protected]
>> wrote:
>> >>
>> >> Situations where the printed form of an expression (i.e, what you'd
>> type
>> >> into julia REPL, for example) are equivalent aren't "equal" per
>> julia's
>> >> standards:
>> >>
>> >> Expr(:call, :<, 1, 2) --> :(1 < 2)
>> >> Meta.show_sexpr(:(1 < 2)) --> (:comparison, 1, :<, 2)
>> >> Expr(:call, :<, 1, 2) == :(1 < 2) --> FALSE
>> >>
>> >> I figure that's expected because the s-expressions behind the scenes
>> >> aren't accurate.
>> >> So the workaround is:
>> >>
>> >> Expr(:call, :<, 1, 2) == :(<(1,2)) --> TRUE
>> >>
>> >> This isn't ideal, but at least there's a way to express the Expr
>> object I
>> >> want in terms of julia's syntax.
>> >> Is there another way to make these two semantically equivalent
>> >> representations actually be equal?
>> >>
>> >> Second - a much weirder problem:
>> >>
>> >> Meta.show_sexpr(:(symbol.x())) --> (:call, (:., :symbol, (:quote,
>> :x)))
>> >>
>> >>
>> >> Ok, so make an expression:
>> >>
>> >> Meta.show_sexpr(Expr(:call, Expr(:., :symbol, Expr(:quote, :x)))) -->
>> >> (:call, (:., :symbol, (:quote, :x)))
>> >>
>> >>
>> >> Cool. So this time the expression object and the actual julia
>> >> representation are the exact same.
>> >>
>> >> However:
>> >>
>> >> Expr(:call, Expr(:., :symbol, Expr(:quote, :x))) == :(symbol.x()) -->
>> >> FALSE
>> >>
>> >>
>> >> And stragely
>> >>
>> >>
>> >> Meta.show_sexpr(Expr(:call, Expr(:., :symbol, Expr(:quote, :x)))) ==
>> >> Meta.show_sexpr(:(symbol.x())) --> TRUE
>> >>
>> >>
>> >> What is going on!? And how do I get these expressions to agree?
>> >>
>> >>
>> >> Vishesh
>> >>
>> >>
>> >>
>> >>
>> >>
>> >
>>
>


-- 
Erik Schnetter <[email protected]>
http://www.perimeterinstitute.ca/personal/eschnetter/

Reply via email to