Re: [julia-users] string version of expressions not parseable

2016-04-02 Thread vishesh
Ah, thank you for that example.


On Saturday, April 2, 2016 at 11:02:07 AM UTC-7, Yichao Yu wrote:
>
> On Sat, Apr 2, 2016 at 1:02 PM,  > 
> wrote: 
> > I seem to be unable to get this to work 
> > 
> >>serialize(STDOUT, :(x + 1)) 
> > =b+1 
> > 
> >>deserialize(IOBuffer("=b+1")) 
> > :call 
> > 
> > which is only form 1 out of 4. 
>
> deserializing and serializing do no use a plain text format. 
>
> julia> io = IOBuffer() 
> IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, 
> append=false, size=0, maxsize=Inf, ptr=1, mark=-1) 
>
> julia> serialize(io, :(x + 1)) 
>
> julia> seekstart(io) 
> IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, 
> append=false, size=9, maxsize=Inf, ptr=1, mark=-1) 
>
> julia> deserialize(io) 
> :(x + 1) 
>
>
> > 
> > Also, could you give me an example of a situation where the aliasing is 
> an 
> > issue? I'm unclear when that pops up. 
>


Re: [julia-users] string version of expressions not parseable

2016-04-02 Thread vishesh
I seem to be unable to get this to work

>serialize(STDOUT, :(x + 1))
=b+1

>deserialize(IOBuffer("=b+1"))
:call

which is only form 1 out of 4.

Also, could you give me an example of a situation where the aliasing is an 
issue? I'm unclear when that pops up.


[julia-users] Re: programatically unquote something

2016-04-01 Thread vishesh
Hi Eric,

Thanks for being so helpful.
My larger goal (and I understand there are others working on this too) is a 
lisp-looking syntax for julia. 
I'd like to be able to use Julia's inbuilt macro mechanisms to basically 
macroexpand a clojure-looking file of code

I can write

(defmacro m [x] '(if ~x 1 0)) 
(defn f [x] (@m x)) 

and get

macro m(x)
  :($x ? 1 : 0)
end

function f(x) @m x end

then also macro expand the module, so

macro m(x)
  :(if $x 1 0)
end 

function f(x) x ? 1 : 0 end

and then convert it back, so i get

(defn f [x] (if x 1 0)) 

In that case, I'd be able to use julia as the macroexpander in a process 
that then can use wisp.js and the like to port everything to js, or hy to 
port to python, etc. 
There's really no "need" to do this (can just use cljs or just write js 
directly for any actual purpose), but I wanted to do some learning about 
transpilers/macro systems and quoting issues and figured this would be a 
fun learning project. I also wanted to learn julia and about julia, so it's 
been very good for that too.

You can see what's going on here:
https://github.com/vshesh/Sexpr.jl 

The idea about stringifying expressions comes from the fact that I'm trying 
to write all the modules to file first, so that they all exist if they 
import symbols from each other (and so I can macro expand them 
individually). 

this particular issue is showing up because there are reserved words in 
julia that you can't just print out/stringify and expect to parse back.

Eg for the macro example above, you end up with (if, true, 1, 0) as the 
form you're returning. Of course, the parser chokes on an empty if followed 
by a comma, so the solution is to quote it, and get  (:if, true, 1, 0) 
(quote of a constant = constant). Now I'd need to unquote stuff on the way 
out after the macro returns something 
I'm not even sure I'm asking the right questions here; there's a chance 
I've set up the whole thing the wrong way, but I'm pretty sure it's 
necessary to translate the forms returned by the macro back into julia 
(since they come out as s-expressions) and that's where the current two 
questions are coming from.

Thank you!
Vishesh


On Friday, April 1, 2016 at 6:37:34 PM UTC-7, Eric Forgy wrote:
>
> Hi Visesh,
>
> I'm interested in your questions and they are similar to things I've 
> struggled with, but I can't help much so I hope someone else can.
>
> BUT... I wonder if you can explain a bit more about what is the big 
> picture you are trying to achieve? It feels to me that you are trying to do 
> something slightly un-Julian. If you explain the big picture of what you're 
> trying to achieve, I suspect someone here can help you find a clean Julian 
> way to achieve it.
>
> So far, your questions are clear, focused and concise, but without a 
> bigger context, I suspect some experts here are unsure how to help although 
> they could probably suggest a cleaner approach.
>
> It sounds like you want to read an expression for a file? Is that right? 
> If so, that doesn't "feel" right to me.
>
> In any case, good luck and welcome to Julia :)
>
> Best regards,
> Eric
>
> On Saturday, April 2, 2016 at 8:56:48 AM UTC+8, vis...@stanford.edu wrote:
>>
>> oh wait, is it eval?
>>
>> eval(:(:x)) -> :x?
>>
>> On Friday, April 1, 2016 at 5:52:42 PM UTC-7, vis...@stanford.edu wrote:
>>>
>>> > x = :(:x)
>>> > $x
>>>
>>> *ERROR: unsupported or misplaced expression $*
>>>
>>>
>>> Is there a quote/unquote function that mimics the behavior or what 
>>> happens when you wrap something with :() or do $x? I want to retrieve 
>>> what's inside the variable x's expression.
>>>
>>> It's not sufficient to just wrap in Expr(:quote, ...) since that's not 
>>> the same as :() and similarly I can't find any documentation on how to 
>>> unquote something.
>>>
>>> There must be some routine being called in order to do the 
>>> quoting/unquoting - is there a way to access it?
>>>
>>>
>>> Vishesh
>>>
>>

[julia-users] Re: programatically unquote something

2016-04-01 Thread vishesh
oh wait, is it eval?

eval(:(:x)) -> :x?

On Friday, April 1, 2016 at 5:52:42 PM UTC-7, vis...@stanford.edu wrote:
>
> > x = :(:x)
> > $x
>
> *ERROR: unsupported or misplaced expression $*
>
>
> Is there a quote/unquote function that mimics the behavior or what happens 
> when you wrap something with :() or do $x? I want to retrieve what's inside 
> the variable x's expression.
>
> It's not sufficient to just wrap in Expr(:quote, ...) since that's not the 
> same as :() and similarly I can't find any documentation on how to unquote 
> something.
>
> There must be some routine being called in order to do the 
> quoting/unquoting - is there a way to access it?
>
>
> Vishesh
>


[julia-users] programatically unquote something

2016-04-01 Thread vishesh
> x = :(:x)
> $x

*ERROR: unsupported or misplaced expression $*


Is there a quote/unquote function that mimics the behavior or what happens 
when you wrap something with :() or do $x? I want to retrieve what's inside 
the variable x's expression.

It's not sufficient to just wrap in Expr(:quote, ...) since that's not the 
same as :() and similarly I can't find any documentation on how to unquote 
something.

There must be some routine being called in order to do the 
quoting/unquoting - is there a way to access it?


Vishesh


[julia-users] string version of expressions not parseable

2016-04-01 Thread vishesh
basically, if I do:

> e = Expr(:quote, :(:x)) # (quoting a symbol)
> show(e)

*:($(Expr(:quote, :(:x*

> print(e)

*$(Expr(:quote, :(:x)))*

> string(e)

*"\$(Expr(:quote, :(:x)))"*

> parse(string(e))

*:($(Expr(:$, :(Expr(:quote,$(Expr(:quote, :(:x*


that's not the same expression as represented by the string. Then when I go 
to eval it (which should still work, semantically speaking)

*ERROR: unsupported or misplaced expression $*


Is there a way around this? I'd like to make an expression a string, print 
it to a file, then slurp it up later and eval it.

At the very least, there should be a consistent way to stringify and 
reparse/eval expression objects, even if they look ugly like 
:($(Expr(:quote, :(:x. I'd hate to have to reimplement the whole 
printing of every expression ever in order to make this work (there doesn't 
seem to be a way to extend a method, like print or show, to only work on 
expressions with a particular head?)



Vishesh


Re: [julia-users] macroexpanding with unicode variables throws error

2016-03-29 Thread vishesh
oh woops, so it should be macroexpand(:(@m 1)). 
and yes, i had set x to something, which explains why it works there too.
that was driving me crazy, thank you!

Vishesh


[julia-users] macroexpanding with unicode variables throws error

2016-03-29 Thread vishesh
 

here's a simple macro


*>macro m(x) :((█if, true, 1, 0, $x)) end*

*>print(macroexpand(@m 1))*

*ERROR: UndefVarError: █if not defined*

*>**macro m(x) :((x, true, 1, 0, $x)) end*

*>**print(macroexpand(@m 1))*
(x, true, 1, 0, 1)

 I would expect (*█if, true, 1, 0, 1) *in the first case, but that 
throws an error. 

Anyone have any idea why this is happening?

Thanks,
Vishesh



[julia-users] :nothing != nothing?

2016-03-23 Thread vishesh
Apparently

:true == true
:false == false
:1 == 1
but 
:nothing != nothing

Is this intentional? If so, what's the rationale?

Vishesh


[julia-users] Re: string function for expressions: surgically add a specific method to a recursion chain

2016-03-23 Thread vishesh
But the idea is that if there is an expression like:

:(function(x) :(2 $x) end)

this looks like

(:function, (:call, :*, 2, (:$, :x)))

Now, when I do string() of this expression, i'd like it to use my 
definition of string when it matches the head of some expressions. 
Basically, if I call Base.string, it will never call my function back on 
nested calls.



On Wednesday, March 23, 2016 at 12:41:05 AM UTC-7, Eric Forgy wrote:
>
> Hi Vishesh,
>
> I'm not an expert, but one thought that comes to mind is to* not* import 
> Base.string. Then you can just define a "string" function local to your 
> module and then call Base.string, when you want to use the Base Julia 
> "string". By importing Base.string and then defining string, you are 
> changing the nature of Base Julia "string", which doesn't sound like a good 
> idea to me.
>
> Hope this helps.
>
> Eric
>
> On Wednesday, March 23, 2016 at 11:09:04 AM UTC+8, vis...@stanford.edu 
> wrote:
>>
>> Hi,
>>
>> I'd like to understand how one can surgically add a piece to a long type 
>> chain of something like string in julia.
>>
>> Say I want to update the printing of expression objects. 
>>
>> So:
>>
>> module M
>> import Base.string
>> function string(ex::Expr)
>>   if ex.head == :$
>> string("\$(", map(string, ex.args)..., ")")
>>   else 
>> Base.string(ex)
>>   end
>> end 
>> end
>>
>> However, if it executes the last command in the list, this somehow calls 
>> my string function rather than the Base string function, leading to an 
>> infinite recursion.
>>
>> If I name the function differently and don't override Base.string, eg:
>>
>> module M
>> function tostring(ex::Expr)
>>   if ex.head == :$
>> string("\$(", map(tostring, ex.args)..., ")")
>>   else 
>> string(ex)
>>   end
>> end 
>> end
>>
>> then the string(ex) call never calls tostring again (for obvious reasons).
>>
>> Which means that if I want to implement my own custom string function on 
>> expressions, I have to actually replicate the logic for EVERY type 
>> possible, which is a LOT of work. I'd rather not do this, and I was hoping 
>> to understand why I can't just exist as a new method definition that will 
>> work only on a specific type (expr) in this case, and do what would 
>> normally be right in other cases. 
>>
>> Vishesh
>>
>>

[julia-users] string function for expressions: surgically add a specific method to a recursion chain

2016-03-22 Thread vishesh
Hi,

I'd like to understand how one can surgically add a piece to a long type 
chain of something like string in julia.

Say I want to update the printing of expression objects. 

So:

module M
import Base.string
function string(ex::Expr)
  if ex.head == :$
string("\$(", map(string, ex.args)..., ")")
  else 
Base.string(ex)
  end
end 
end

However, if it executes the last command in the list, this somehow calls my 
string function rather than the Base string function, leading to an 
infinite recursion.

If I name the function differently and don't override Base.string, eg:

module M
function tostring(ex::Expr)
  if ex.head == :$
string("\$(", map(tostring, ex.args)..., ")")
  else 
string(ex)
  end
end 
end

then the string(ex) call never calls tostring again (for obvious reasons).

Which means that if I want to implement my own custom string function on 
expressions, I have to actually replicate the logic for EVERY type 
possible, which is a LOT of work. I'd rather not do this, and I was hoping 
to understand why I can't just exist as a new method definition that will 
work only on a specific type (expr) in this case, and do what would 
normally be right in other cases. 

Vishesh



Re: [julia-users] macroexpand entire module

2016-03-22 Thread vishesh
Ah, thank you! I didn't know you could do things like instantiate a module 
that way.

On Monday, March 21, 2016 at 3:21:45 PM UTC-7, Yichao Yu wrote:
>
> On Mon, Mar 21, 2016 at 5:54 PM,  > 
> wrote: 
> > Oops, maybe name it differently 
> > 
> > function expand(ex::Expr) 
> >   if ex.head == :module 
> > Expr(:module, ex.args[1], ex.args[2], macroexpand(ex.args[3])) 
> >   else 
> > macroexpand(ex) 
> >   end 
> > end 
> > 
>
> FYI, expand is another base function. 
>
> > 
> > So if someone were to give me: 
> > 
> > 
> > module M 
> > 
> > include("X.jl") 
> > import X: @y, @z 
> > 
> > f(x) = X.@y(3) 
> > 
> > end 
> > 
> > I would then... 
> > 
> > eval(:(module M 
> > include("X.jl") 
> > import X: @y, @z 
> > 
> > f(x) = X.@y(3) 
> > 
> > end) 
> > 
> > expand(:(module M ... end)) 
> > 
> > ? 
> > Sorry, not sure how this contextual evaluating/expanding would look 
> like. 
> > It's not clear to me how evaluating the module M will make relevant 
> > definitions accessible to the expand function, since that ignores the 
> fact 
> > that I'm inside module M when expanding each form. 
> > 
>
> As I said, you need to manually go through each of the statement in 
> the module, macro expand and evaluate them. 
> Try this: 
>
> ``` 
> julia> function expand_module(ex::Expr) 
>@assert ex.head === :module 
>std_imports = ex.args[1]::Bool 
>name = ex.args[2]::Symbol 
>body = ex.args[3]::Expr 
>mod = Module(name, std_imports) 
>newbody = quote end 
>modex = Expr(:module, std_imports, name, newbody) 
>for subex in body.args 
>expandf = ()->macroexpand(subex) 
>subex = eval(mod, :($expandf())) 
>push!(newbody.args, subex) 
>eval(mod, subex) 
>end 
>modex, mod 
>end 
> expand_module (generic function with 1 method) 
>
> julia> expand_module(:(module A 
>macro X() 
>1 
>end 
>b = 1 + @X 
>@show b 
>end)) 
> b = 2 
> (:(module A 
> eval(x) = begin  # none, line 1: 
> top(Core).eval(A,x) 
> end 
> eval(m,x) = begin  # none, line 1: 
> top(Core).eval(m,x) 
> end # none, line 2: 
> $(Expr(:macro, :(X()), quote  # none, line 3: 
> 1 
> end)) # none, line 5: 
> b = 1 + 1 # none, line 6: 
> begin 
> Base.println("b = ",Base.repr(begin  # show.jl, line 166: 
> #2#value = b 
> end)) 
> #2#value 
> end 
> end),A) 
> ``` 
>
> > 
> > 
>


Re: [julia-users] macroexpand entire module

2016-03-21 Thread vishesh
Oops, maybe name it differently

function expand(ex::Expr)
  if ex.head == :module
Expr(:module, ex.args[1], ex.args[2], macroexpand(ex.args[3]))
  else
macroexpand(ex)
  end
end


So if someone were to give me:


module M 

include("X.jl")
import X: @y, @z

f(x) = X.@y(3)

end

I would then...

eval(:(module M
include("X.jl")
import X: @y, @z

f(x) = X.@y(3)

end)

expand(:(module M ... end))

? 
Sorry, not sure how this contextual evaluating/expanding would look like. 
It's not clear to me how evaluating the module M will make relevant 
definitions accessible to the expand function, since that ignores the fact 
that I'm inside module M when expanding each form. 





Re: [julia-users] macroexpand entire module

2016-03-21 Thread vishesh
>> > > *and* 
> >> > > 
> >> > > > then used within the module body. If that does occur, the only 
> way 
> >> > > > to do 
> >> > > > macro expansion correctly is to evaluate the module since the 
> module 
> >> > > > definition can depend on arbitrary previously evaluated code. 
> >> > > 
> >> > > Probably true. I haven't played with it in a long time, but it's 
> >> > > possible 
> >> > > you 
> >> > > could load the module (so the macros are defined) and then parse 
> the 
> >> > > file...but 
> >> > > I can't remember if that works. 
> >> > > 
> >> > > Best, 
> >> > > --Tim 
> >> > > 
> >> > > > On Sun, Mar 20, 2016 at 9:00 PM, Tim Holy  >> > > 
> >> > > > wrote: 
> >> > > > > It probably needs updating, but 
> >> > > > > https://github.com/timholy/MacroExpandJL.jl 
> >> > > > > might help. It lets you macroexpand a whole source file. 
> >> > > > > 
> >> > > > > Best, 
> >> > > > > --Tim 
> >> > > > > 
> >> > > > > On Sunday, March 20, 2016 08:53:49 PM Yichao Yu wrote: 
> >> > > > > > On Sun, Mar 20, 2016 at 8:26 PM,   >> > > > > > > 
> >> > > 
> >> > > wrote: 
> >> > > > > > > Hi all, 
> >> > > > > > > 
> >> > > > > > > I'd like to be able to load in a module, then macroexpand 
> the 
> >> > > 
> >> > > whole 
> >> > > 
> >> > > > > thing, 
> >> > > > > 
> >> > > > > > > then print out the macroexpanded version. 
> >> > > > > > > 
> >> > > > > > > This should be a full, recursive macroexpand. 
> >> > > > > > > 
> >> > > > > > > I've noticed there is a function called macroexpand that 
> >> > > > > > > normally 
> >> > > 
> >> > > does 
> >> > > 
> >> > > > > > > what 
> >> > > > > > > 
> >> > > > > > > i want: 
> >> > > > > > >> macro m(x) 1 end 
> >> > > > > > > 
> >> > > > > > > .. 
> >> > > > > > > 
> >> > > > > > >> @m(2) 
> >> > > > > > > 
> >> > > > > > > 1 
> >> > > > > > > 
> >> > > > > > >> macroexpand(:(1 + @m(2))) 
> >> > > > > > >> 
> >> > > > > > > :(1 + 1) 
> >> > > > > > > 
> >> > > > > > > so that is fine and dandy, but inside a module this doesn't 
> >> > > > > > > seem 
> >> > > 
> >> > > to 
> >> > > 
> >> > > > > work: 
> >> > > > > > >> macroexpand(:( 
> >> > > > > > >> 
> >> > > > > > >module M 
> >> > > > > > >macro m(x) 1 end 
> >> > > > > > >x = 1 + @m(2) 
> >> > > > > > >end 
> >> > > > > > >)) 
> >> > > > > > > : 
> >> > > > > > > :(module M 
> >> > > > > > > : 
> >> > > > > > > eval(x) = begin  # none, line 2: 
> >> > > > > > > top(Core).eval(M,x) 
> >> > > > > > > 
> >> > > > > > > end 
> >> > > > > > > 
> >> > > > > > > eval(m,x) = begin  # none, line 2: 
> >> > > > > > > top(Core).eval(m,x) 
> >> > > > > > > 
> >> > > > > > > end # none, line 3: 
> >> > > > > > > $(Expr(:macro, :(m(x)), quote  # none, line 3: 
> >> > > > > > > 1 
> >> > > > > > > 
> >> > > > > > > end)) # none, line 4: 
> >> > > > > > > x = 1 + @m(2) 
> >> > > > > > > end) 
> >> > > > > > > 
> >> > > > > > > As you can see in the second to last line, @m(2) is not 
> >> > > > > > > expanded, 
> >> > > 
> >> > > and 
> >> > > 
> >> > > > > I'm 
> >> > > > > 
> >> > > > > > > confused as to why that is. 
> >> > > > > > > 
> >> > > > > > > Ideally, this macroexpanding of a module would allow me to 
> >> > > > > > > also 
> >> > > > > > > resolve 
> >> > > > > > > imports and includes properly, so I could just slurp up a 
> file 
> >> > > > > > > and 
> >> > > > > > > dump 
> >> > > > > > > out 
> >> > > > > > > the macroexpanded version. 
> >> > > > > > 
> >> > > > > > TL;DR this is generally not possible without evaluating the 
> >> > > > > > whole 
> >> > > > > > module. 
> >> > > > > > 
> >> > > > > > Macros are executed at parse time and therefore resolved in 
> >> > > > > > global 
> >> > > > > > scope (since local scope doesn't even exist yet) or in 
> another 
> >> > > > > > word 
> >> > > > > > module scope. 
> >> > > > > > Therefore when doing macro expansion in a new module, the 
> macros 
> >> > > 
> >> > > needs 
> >> > > 
> >> > > > > > to be resolved in the new module and since there's no way to 
> >> > > > > > statically know what macros are available in a module you 
> can't 
> >> > > > > > do 
> >> > > > > > that without evaluating the module. 
> >> > > > > > 
> >> > > > > > > Thank you! 
> >> > > > > > > 
> >> > > > > > > Vishesh 
> >> 
> > 
>


Re: [julia-users] macroexpand entire module

2016-03-21 Thread vishesh
gt; I've noticed there is a function called macroexpand that 
> normally 
> > > 
> > > does 
> > > 
> > > > > > > what 
> > > > > > > 
> > > > > > > i want: 
> > > > > > >> macro m(x) 1 end 
> > > > > > > 
> > > > > > > .. 
> > > > > > > 
> > > > > > >> @m(2) 
> > > > > > > 
> > > > > > > 1 
> > > > > > > 
> > > > > > >> macroexpand(:(1 + @m(2))) 
> > > > > > >> 
> > > > > > > :(1 + 1) 
> > > > > > > 
> > > > > > > so that is fine and dandy, but inside a module this doesn't 
> seem 
> > > 
> > > to 
> > > 
> > > > > work: 
> > > > > > >> macroexpand(:( 
> > > > > > >> 
> > > > > > >module M 
> > > > > > >macro m(x) 1 end 
> > > > > > >x = 1 + @m(2) 
> > > > > > >end 
> > > > > > >)) 
> > > > > > > : 
> > > > > > > :(module M 
> > > > > > > : 
> > > > > > > eval(x) = begin  # none, line 2: 
> > > > > > > top(Core).eval(M,x) 
> > > > > > > 
> > > > > > > end 
> > > > > > > 
> > > > > > > eval(m,x) = begin  # none, line 2: 
> > > > > > > top(Core).eval(m,x) 
> > > > > > > 
> > > > > > > end # none, line 3: 
> > > > > > > $(Expr(:macro, :(m(x)), quote  # none, line 3: 
> > > > > > > 1 
> > > > > > > 
> > > > > > > end)) # none, line 4: 
> > > > > > > x = 1 + @m(2) 
> > > > > > > end) 
> > > > > > > 
> > > > > > > As you can see in the second to last line, @m(2) is not 
> expanded, 
> > > 
> > > and 
> > > 
> > > > > I'm 
> > > > > 
> > > > > > > confused as to why that is. 
> > > > > > > 
> > > > > > > Ideally, this macroexpanding of a module would allow me to 
> also 
> > > > > > > resolve 
> > > > > > > imports and includes properly, so I could just slurp up a file 
> and 
> > > > > > > dump 
> > > > > > > out 
> > > > > > > the macroexpanded version. 
> > > > > > 
> > > > > > TL;DR this is generally not possible without evaluating the 
> whole 
> > > > > > module. 
> > > > > > 
> > > > > > Macros are executed at parse time and therefore resolved in 
> global 
> > > > > > scope (since local scope doesn't even exist yet) or in another 
> word 
> > > > > > module scope. 
> > > > > > Therefore when doing macro expansion in a new module, the macros 
> > > 
> > > needs 
> > > 
> > > > > > to be resolved in the new module and since there's no way to 
> > > > > > statically know what macros are available in a module you can't 
> do 
> > > > > > that without evaluating the module. 
> > > > > > 
> > > > > > > Thank you! 
> > > > > > > 
> > > > > > > Vishesh 
>
>

Re: [julia-users] macroexpand entire module

2016-03-21 Thread vishesh
The MacroExpandJL package seems promising, but maybe I'm not able to get it 
to work. After updating syntax to match julia 0.4,
MacroExpandJL.macroexpand_jl(STDOUT, :(module M function f(x) 1+@m(2) end 
end))
module M
begin  # line 1:
function f(x) # line 1:
1 + @m 2
end
endend

Notice how the @m 2 is still there. Also, why is everything wrapped in an 
extra do block inside the module? Is this a printing issue, because that 
expression doesn't have one.

How would I go about evaluating a module and it's macros, macro expanding 
the whole thing, and then dumping it out? @eval seems like, name wise, it 
should do this but it doesn't.
Do you first eval() the module, then @eval the module? That didn't work for 
me either.
Predefining a macro and then trying to evaluate:

> macro m(x) 1 end
> @eval(:(module M function f(x) @m 2 end end))
:(module M
eval(x) = begin  # none, line 1:
top(Core).eval(M,x)
end
eval(m,x) = begin  # none, line 1:
top(Core).eval(m,x)
end # none, line 1:
function f(x) # none, line 1:
@m 2
end
end)

Also doesn't work.

On Monday, March 21, 2016 at 7:54:59 AM UTC-7, Tim Holy wrote:
>
> On Monday, March 21, 2016 09:34:19 AM Stefan Karpinski wrote: 
> > Tim, I'm assuming that module must assume that no macros are defined 
> *and* 
> > then used within the module body. If that does occur, the only way to do 
> > macro expansion correctly is to evaluate the module since the module 
> > definition can depend on arbitrary previously evaluated code. 
>
> Probably true. I haven't played with it in a long time, but it's possible 
> you 
> could load the module (so the macros are defined) and then parse the 
> file...but 
> I can't remember if that works. 
>
> Best, 
> --Tim 
>
> > 
> > On Sun, Mar 20, 2016 at 9:00 PM, Tim Holy  > wrote: 
> > > It probably needs updating, but 
> > > https://github.com/timholy/MacroExpandJL.jl 
> > > might help. It lets you macroexpand a whole source file. 
> > > 
> > > Best, 
> > > --Tim 
> > > 
> > > On Sunday, March 20, 2016 08:53:49 PM Yichao Yu wrote: 
> > > > On Sun, Mar 20, 2016 at 8:26 PM,  > 
> wrote: 
> > > > > Hi all, 
> > > > > 
> > > > > I'd like to be able to load in a module, then macroexpand the 
> whole 
> > > 
> > > thing, 
> > > 
> > > > > then print out the macroexpanded version. 
> > > > > 
> > > > > This should be a full, recursive macroexpand. 
> > > > > 
> > > > > I've noticed there is a function called macroexpand that normally 
> does 
> > > > > what 
> > > > > 
> > > > > i want: 
> > > > >> macro m(x) 1 end 
> > > > > 
> > > > > .. 
> > > > > 
> > > > >> @m(2) 
> > > > > 
> > > > > 1 
> > > > > 
> > > > >> macroexpand(:(1 + @m(2))) 
> > > > >> 
> > > > > :(1 + 1) 
> > > > > 
> > > > > so that is fine and dandy, but inside a module this doesn't seem 
> to 
> > > 
> > > work: 
> > > > >> macroexpand(:( 
> > > > >> 
> > > > >module M 
> > > > >macro m(x) 1 end 
> > > > >x = 1 + @m(2) 
> > > > >end 
> > > > >)) 
> > > > > : 
> > > > > :(module M 
> > > > > : 
> > > > > eval(x) = begin  # none, line 2: 
> > > > > top(Core).eval(M,x) 
> > > > > 
> > > > > end 
> > > > > 
> > > > > eval(m,x) = begin  # none, line 2: 
> > > > > top(Core).eval(m,x) 
> > > > > 
> > > > > end # none, line 3: 
> > > > > $(Expr(:macro, :(m(x)), quote  # none, line 3: 
> > > > > 1 
> > > > > 
> > > > > end)) # none, line 4: 
> > > > > x = 1 + @m(2) 
> > > > > end) 
> > > > > 
> > > > > As you can see in the second to last line, @m(2) is not expanded, 
> and 
> > > 
> > > I'm 
> > > 
> > > > > confused as to why that is. 
> > > > > 
> > > > > Ideally, this macroexpanding of a module would allow me to also 
> > > > > resolve 
> > > > > imports and includes properly, so I could just slurp up a file and 
> > > > > dump 
> > > > > out 
> > > > > the macroexpanded version. 
> > > > 
> > > > TL;DR this is generally not possible without evaluating the whole 
> > > > module. 
> > > > 
> > > > Macros are executed at parse time and therefore resolved in global 
> > > > scope (since local scope doesn't even exist yet) or in another word 
> > > > module scope. 
> > > > Therefore when doing macro expansion in a new module, the macros 
> needs 
> > > > to be resolved in the new module and since there's no way to 
> > > > statically know what macros are available in a module you can't do 
> > > > that without evaluating the module. 
> > > > 
> > > > > Thank you! 
> > > > > 
> > > > > Vishesh 
>
>

[julia-users] structuring src/test directories

2016-03-20 Thread vishesh
So far, I've been using include("file") to import local files. This seems 
like a really bad idea in the long run, but I've been unable to find 
documentation of how to do it differently. 
So for all of my own modules, I have to do:

module M 

include("N.jl")
importall .N

# do some coding here using N, eg:
N.x(::NewType) = ... 

end

and when testing, it gets even weirder

module TestM

include("../src/M.jl")
importall .M

using FactCheck

# awesome tests here

end


It's kind of cumbersome to have to always do an include statement followed 
by an import. Is there a better way to structure my src/test directories so 
that they can find each other and coexist peacefully? Especially if things 
are nested a lot, it can get very weird to have to keep including the file 
from the filesystem.
I also don't understand why I end up with an M.N module if I include this 
module on the julia terminal, and if I update the N module 
(include("src/N.jl")) on the console, it does *not* update the M.N module, 
which seems completely wonky to me. Shouldn't each module just have its one 
definition in the system?

A second related question is how to best setup a program structure that's 
intended to be used as a command line tool, rather than a package that's 
installed by julia. I understand I can use Pkg.create, but that seems very 
strongly geared towards something that's installed through Pkg.add(). If 
there's no better system, I'll go with it, but if anyone has any best 
practices that would be awesome to understand.








[julia-users] macroexpand entire module

2016-03-20 Thread vishesh
Hi all,

I'd like to be able to load in a module, then macroexpand the whole thing, 
then print out the macroexpanded version.

This should be a full, recursive macroexpand. 

I've noticed there is a function called macroexpand that normally does what 
i want:

> macro m(x) 1 end
..
> @m(2)
1 
> macroexpand(:(1 + @m(2)))
:(1 + 1)

so that is fine and dandy, but inside a module this doesn't seem to work:
> macroexpand(:(
   module M 
   macro m(x) 1 end
   x = 1 + @m(2)
   end
   ))

:(module M
eval(x) = begin  # none, line 2:
top(Core).eval(M,x)
end
eval(m,x) = begin  # none, line 2:
top(Core).eval(m,x)
end # none, line 3:
$(Expr(:macro, :(m(x)), quote  # none, line 3:
1
end)) # none, line 4:
x = 1 + @m(2)
end)


As you can see in the second to last line, @m(2) is not expanded, and I'm 
confused as to why that is. 

Ideally, this macroexpanding of a module would allow me to also resolve 
imports and includes properly, so I could just slurp up a file and dump out 
the macroexpanded version.

Thank you!

Vishesh


[julia-users] let unpack

2016-02-10 Thread vishesh
You can normally do

z = (1,2,3)
(a,b,c)  = z

and it will unpack things from z into a,b,c.
However, if you do:

let (a,b,c) = z; a end

it's a syntax error. Is there a way to get the spirit of this unpacking 
inside a let statement? I don't want permanent assignment in the function 
(I already get how to do that).
Eg: let a=nothing,b=nothing,c=nothing; (a,b,c) = z; a end seems like a bad 
choice. There must be some other way

Thanks!
Vishesh


[julia-users] Re: Expression object for "unquote"

2016-01-23 Thread vishesh
Sorry, just to be clear:

*julia> **show(:(macro m(x) :($x) end))*

macro m(x)

$(Expr(:quote, :($(Expr(:$, :x)

end

On Saturday, January 23, 2016 at 4:28:49 PM UTC-8, vis...@stanford.edu 
wrote:
>
> Ok, I tried to do that:
>
> import Base.show
> function show(e::Expr) 
>   if e.head == :macro
> e.head = :function
> print("macro")
> print(@sprintf("%s", stripmeta(e))[9:end])
> e.head = :macro
> nothing
>   elseif e.head == :$
> print("\$(")
> for a in e.args
>   show(a)
> end
> print(")") 
>   else
> show(e)
>   end
> end
>
> I thought that show would be defined recursively on functions, but it 
> seems that's not the case, since trying to print :(macro m(x) :(x) end) 
> gives me $(Expr(:quote, :($(Expr(:$, :x).
> How would I get around this?
>
> On Monday, January 18, 2016 at 6:14:42 AM UTC-8, Ismael Venegas Castelló 
> wrote:
>
>> That's because in the case of expressions, we are interested in the AST, 
>> show representation is just an abstraction of that, there is also dump and 
>> xdump:
>>
>> julia> ex = :(:($x))
>> :($(Expr(:quote, :($(Expr(:$, :x))
>>
>> julia> Meta.show_sexpr(ex); println()
>> (:quote, (:$, :x))
>>
>> julia> dump(ex)
>>
>> Expr  
>>   head: Symbol quote
>>   args: Array(Any,(1,))
>> 1: Expr  
>>   head: Symbol $
>>   args: Array(Any,(1,))
>> 1: Symbol x
>>   typ: Any
>>   typ: Any
>>
>> julia> ex.args[1].args[1] = :y
>> :y
>>
>> julia> ex
>> :($(Expr(:quote, :($(Expr(:$, :y))
>>
>> julia> Meta.show_sexpr(ex); println()
>> (:quote, (:$, :y))
>>
>> julia> dump(ex)
>> Expr  
>>   head: Symbol quote
>>   args: Array(Any,(1,))
>> 1: Expr  
>>   head: Symbol $
>>   args: Array(Any,(1,))
>> 1: Symbol y
>>   typ: Any
>>   typ: Any
>>
>> You could overwrite those methods or write your own that prints them the 
>> way you want to.
>>
>> El domingo, 17 de enero de 2016, 1:50:05 (UTC-6), vis...@stanford.edu 
>> escribió:
>>>
>>> Hi!
>>>
>>> Was messing around with exceptions, and trying to see under the hood and 
>>> construct macro expressions. 
>>>
>>> Eg, Meta.show_sexpr(:(:(+ 1 2))) -> (:quote, (:call, :+, 1, 2))
>>>
>>> but how do you build an unquote expression?
>>>
>>> Meta.show_sexpr(:(:($(x+5) + 1))) -> (:quote, (:call, :+, (:$, (:call, 
>>> :+, :x, 5)), 1))
>>>
>>>
>>> But Expr(:quote, Expr(:call, :+, Expr(:$, Expr(:call, :+, :x, 5)), 1)) 
>>> -> :($(Expr(:quote, :($(Expr(:$, :(x + 5))) + 1
>>>
>>> In other words, it's not processing into the appropriate expression, and 
>>> there's some weird intermediate syntax going on. 
>>>
>>>
>>> How does one build an unquote expression? I.e, an expression that would 
>>> eval to unquoting a variable?
>>>
>>

[julia-users] Re: Expression object for "unquote"

2016-01-23 Thread vishesh
Ok, I tried to do that:

import Base.show
function show(e::Expr) 
  if e.head == :macro
e.head = :function
print("macro")
print(@sprintf("%s", stripmeta(e))[9:end])
e.head = :macro
nothing
  elseif e.head == :$
print("\$(")
for a in e.args
  show(a)
end
print(")") 
  else
show(e)
  end
end

I thought that show would be defined recursively on functions, but it seems 
that's not the case, since trying to print :(macro m(x) :(x) end) gives me 
$(Expr(:quote, :($(Expr(:$, :x).
How would I get around this?

On Monday, January 18, 2016 at 6:14:42 AM UTC-8, Ismael Venegas Castelló 
wrote:

> That's because in the case of expressions, we are interested in the AST, 
> show representation is just an abstraction of that, there is also dump and 
> xdump:
>
> julia> ex = :(:($x))
> :($(Expr(:quote, :($(Expr(:$, :x))
>
> julia> Meta.show_sexpr(ex); println()
> (:quote, (:$, :x))
>
> julia> dump(ex)
>
> Expr  
>   head: Symbol quote
>   args: Array(Any,(1,))
> 1: Expr  
>   head: Symbol $
>   args: Array(Any,(1,))
> 1: Symbol x
>   typ: Any
>   typ: Any
>
> julia> ex.args[1].args[1] = :y
> :y
>
> julia> ex
> :($(Expr(:quote, :($(Expr(:$, :y))
>
> julia> Meta.show_sexpr(ex); println()
> (:quote, (:$, :y))
>
> julia> dump(ex)
> Expr  
>   head: Symbol quote
>   args: Array(Any,(1,))
> 1: Expr  
>   head: Symbol $
>   args: Array(Any,(1,))
> 1: Symbol y
>   typ: Any
>   typ: Any
>
> You could overwrite those methods or write your own that prints them the 
> way you want to.
>
> El domingo, 17 de enero de 2016, 1:50:05 (UTC-6), vis...@stanford.edu 
> escribió:
>>
>> Hi!
>>
>> Was messing around with exceptions, and trying to see under the hood and 
>> construct macro expressions. 
>>
>> Eg, Meta.show_sexpr(:(:(+ 1 2))) -> (:quote, (:call, :+, 1, 2))
>>
>> but how do you build an unquote expression?
>>
>> Meta.show_sexpr(:(:($(x+5) + 1))) -> (:quote, (:call, :+, (:$, (:call, 
>> :+, :x, 5)), 1))
>>
>>
>> But Expr(:quote, Expr(:call, :+, Expr(:$, Expr(:call, :+, :x, 5)), 1)) 
>> -> :($(Expr(:quote, :($(Expr(:$, :(x + 5))) + 1
>>
>> In other words, it's not processing into the appropriate expression, and 
>> there's some weird intermediate syntax going on. 
>>
>>
>> How does one build an unquote expression? I.e, an expression that would 
>> eval to unquoting a variable?
>>
>

[julia-users] Re: Expression object for "unquote"

2016-01-17 Thread vishesh
Update, it seems like the unquote expressions are actually correct, in that 
they evaluate to the right thing.

However, that's some ugly printing. If I write :(:($x)) why does it look so 
messy? Why can't the printer outptut :(:($x)) as the representation of 
itself? This seems contrary to many other julia expression objects, which 
get printed out in a human readable fashion.

On Saturday, January 16, 2016 at 11:50:05 PM UTC-8, vis...@stanford.edu 
wrote:
>
> Hi!
>
> Was messing around with exceptions, and trying to see under the hood and 
> construct macro expressions. 
>
> Eg, Meta.show_sexpr(:(:(+ 1 2))) -> (:quote, (:call, :+, 1, 2))
>
> but how do you build an unquote expression?
>
> Meta.show_sexpr(:(:($(x+5) + 1))) -> (:quote, (:call, :+, (:$, (:call, :+, 
> :x, 5)), 1))
>
>
> But Expr(:quote, Expr(:call, :+, Expr(:$, Expr(:call, :+, :x, 5)), 1)) -> 
> :($(Expr(:quote, :($(Expr(:$, :(x + 5))) + 1
>
> In other words, it's not processing into the appropriate expression, and 
> there's some weird intermediate syntax going on. 
>
>
> How does one build an unquote expression? I.e, an expression that would 
> eval to unquoting a variable?
>


[julia-users] Importing Package and not running external code.

2016-01-17 Thread vishesh
In python, you can do 

if name == '__main__'

to preface code that should only be run when this module is DIRECTLY called 
as the entry point.

In julia, I tried replicating this (a la some other post in this group) as 

module M
export f
function f(x) 2x end
end

if length(ARGS) > 0 && ARGS[1] == "-r"
  eval(:(using M))
  # do something
end

Which is fine, so long as I do this for only one module.
Now let's say that I make a new file, with a new module, and want to import 
M as well

module N
export f2
function f2(x) 4x end
end

if length(ARGS) > 0 && ARGS[1] == "-r"
  include("m.jl")
  eval(:(using M))
  eval(:(using N))
  # do something
end

But when including m.jl, all the code in m is run, including the args part. 
So the program actually runs from there rather than from the module I 
actually called on the commandline. 
Is there a way around this that ISN'T hacky? EG, maybe read the name of the 
module on the commandline and see if the current file matches that? Seems 
clunky though, since you have to actually make I/O calls to the filesystem 
if the current filename isn't an environment variable already in julia 
(__FILE__ types?). 

I suppose the "real" solution is to make a main.jl file that appropriately 
exposes all the different running options of the collection of modules 
you're dealing with, but creating an entire command line util for a small 
set of modules seems like way overkill. Also, there's something nice about 
running `julia m.jl ...options` rather than `julia bootstrap.jl m.jl 
...options`, and it allows me to keep the runtime logic for each module 
contained within that module. An overall main file seems like it should 
only contain the use cases for the final program, not the run options for 
each intermediate module.


Thanks!
Vishesh















[julia-users] Expression object for "unquote"

2016-01-16 Thread vishesh
Hi!

Was messing around with exceptions, and trying to see under the hood and 
construct macro expressions. 

Eg, Meta.show_sexpr(:(:(+ 1 2))) -> (:quote, (:call, :+, 1, 2))

but how do you build an unquote expression?

Meta.show_sexpr(:(:($(x+5) + 1))) -> (:quote, (:call, :+, (:$, (:call, :+, 
:x, 5)), 1))


But Expr(:quote, Expr(:call, :+, Expr(:$, Expr(:call, :+, :x, 5)), 1)) -> 
:($(Expr(:quote, :($(Expr(:$, :(x + 5))) + 1

In other words, it's not processing into the appropriate expression, and 
there's some weird intermediate syntax going on. 


How does one build an unquote expression? I.e, an expression that would 
eval to unquoting a variable?


Re: [julia-users] Re: Expression Equality

2015-12-09 Thread vishesh
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,  > 
> 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, vis...@stanford.edu 
> 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 
> >> 
> >> 
> >> 
> >> 
> >> 
> > 
>


[julia-users] Re: Expression Equality

2015-12-09 Thread vishesh
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...

On Tuesday, December 8, 2015 at 8:26:35 PM UTC-8, vis...@stanford.edu 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
>
>
>
>
>
>

Re: [julia-users] Re: Expression Equality

2015-12-09 Thread vishesh
Indeed, my use case is also writing proper tests for macros using FactCheck.
I have a more bizarre use case which involves writing a lisp using julia 
(learning purposes only), but it's more out there.

In this case, it'd be nice to verify that the expression I get matches the 
one I expect.
Is there a way process an Expression and ignore nodes (i.e, linenumbernode, 
which has absolutely nothing to do with expression equality, and represents 
internal julia metadata)? It'd be a tree traversal function, maybe, but I'm 
unable to find any documentation on how to manipulate Expression objects.
Even something like cons/first/rest would be sufficient for my case.

On Wednesday, December 9, 2015 at 1:26:14 PM UTC-8, Erik Schnetter wrote:
>
> I want to test the expression
>
> ```
> macroexpand(:@fastmath :+)
> ```
>
> where `:+` is a symbol, not a function.
>
> I wasn't able to determine the difference. I used both `info` and `dump`, 
> and my LHS (the result of the expression above) and what I constructed 
> manually looked identical, and in the REPL, the epression
>
> ```
> macroexpand(:@fastmath :+) == :(:+)
> ```
> yields `true`. In `test/fastmath.jl`, it didn't. (It works fine for five 
> other expressions that are currently being tested.)
>
> Anyway -- my point being that it can be quite useful to compare 
> expressions, even if the exact value might change over time. Since Julia's 
> macros are all about manipulating expressions, having ways to output, 
> serialize, load, compare, and treat them like any other data makes totally 
> sense.
>
> -erik
>
>
>
> On Wed, Dec 9, 2015 at 4:05 PM, Yichao Yu > 
> wrote:
>
>> On Wed, Dec 9, 2015 at 3:57 PM, Erik Schnetter > > wrote:
>> > Comparing expressions is useful if you want to write a test case for a
>> > function that transforms expressions, such as e.g. `@fastmath`. I 
>> recently
>> > added some test cases, but was unable to test the result of
>> >
>> > macroexpand(:@fastmath :+)
>> >
>> > since I don't know how to explicitly construct the expected result. I 
>> know
>> > how to do it in the REPL, but this doesn't work in test cases.
>>
>> What's the difference between REPL and test script?
>>
>> julia -f -e 'println(macroexpand(:(@fastmath +)) == 
>> :(Base.FastMath.add_fast))'
>> true
>>
>>
>> >
>> > -erik
>> >
>> >
>> > On Wed, Dec 9, 2015 at 3:46 PM, Yichao Yu > > wrote:
>> >>
>> >> On Wed, Dec 9, 2015 at 3:05 PM,  > 
>> wrote:
>> >> > Interesting. I didn't think to use dump to check differences.
>> >> >
>> >> > Another followup question. After using dump on some simple if
>> >> > statements,
>> >> > I've noticed that all blocks induce this LineNumberNode which is 
>> messing
>> >> > up
>> >> > the equality.
>> >> > Is there a way to ignore these nodes in the equality check? They 
>> show up
>> >> > in
>> >> > any kind of block statement (function, if, etc)
>> >> >
>> >>
>> >> May I ask what do you need the equality of expressions for? It doesn't
>> >> sound like a too useful concept. There can be expressions that are
>> >> equal but have different side effects due to the variables they
>> >> capture (try constructing `Expr(:call, :push!, [1, 2], 1)` twice and
>> >> eval/compare them). There can also be expressions that are equivalant
>> >> but appears differently.
>> >>
>> >> In general, I never find `==` of expressions too useful. When
>> >> processing it, you can just ignore the line number nodes if you don't
>> >> care too much about debug info (also see base/docs/Docs.jl for some
>> >> example of stripping unwanted part from a expression). There's also
>> >> MacroTools.jl which provides a nice way to process expressions.
>> >>
>> >> > eg:
>> >> >
>> >> > dump(:(if true 1 else 0 end))
>> >> >
>> >> > Expr
>> >> >
>> >> >   head: Symbol if
>> >> >
>> >> >   args: Array(Any,(3,))
>> >> >
>> >> > 1: Bool true
>> >> >
>> >> > 2: Expr
>> >> >
>> >> >   head: Symbol block
>> >> >
>> >> >   args: Array(Any,(2,))
>> >> >
>> >> > 1: LineNumberNode
>> >> >
>> >> >   file: Symbol none
>> >> >
>> >> >   line: Int64 1
>> >> >
>> >> > 2: Int64 1
>> >> >
>> >> >   typ: Any
>> >> >
>> >> > 3: Expr
>> >> >
>> >> >   head: Symbol block
>> >> >
>> >> >   args: Array(Any,(2,))
>> >> >
>> >> > 1: LineNumberNode
>> >> >
>> >> >   file: Symbol none
>> >> >
>> >> >   line: Int64 1
>> >> >
>> >> > 2: Int64 0
>> >> >
>> >> >   typ: Any
>> >> >
>> >> >   typ: Any
>> >> >
>> >> >
>> >> > dump(Expr(:if, true, Expr(:block, 0), Expr(:block,1)))
>> >> >
>> >> > Expr
>> >> >
>> >> >   head: Symbol if
>> >> >
>> >> >   args: Array(Any,(3,))
>> >> >
>> >> > 1: Bool true
>> >> >
>> >> > 2: Expr
>> >> >
>> >> >   head: Symbol block
>> >> >
>> >> >   args: Array(Any,(1,))
>> >> >
>> >> > 1: Int64 0
>> >> >
>> >> >   typ: Any
>> >> >
>> >> > 3: Expr
>> >> >
>> >> >   head: Symbol block
>> >> >
>> >> >   args: Array(Any,(1,))
>> >> >
>> >> > 1:

[julia-users] Re: Expression Equality

2015-12-09 Thread vishesh
Interesting. I didn't think to use dump to check differences.

Another followup question. After using dump on some simple if statements, 
I've noticed that all blocks induce this LineNumberNode which is messing up 
the equality.
Is there a way to ignore these nodes in the equality check? They show up in 
any kind of block statement (function, if, etc)

eg:

dump(:(if true 1 else 0 end))

Expr 

  head: Symbol if

  args: Array(Any,(3,))

1: Bool true

2: Expr 

  head: Symbol block

  args: Array(Any,(2,))

1: LineNumberNode 

  file: Symbol none

  line: Int64 1

2: Int64 1

  typ: Any

3: Expr 

  head: Symbol block

  args: Array(Any,(2,))

1: LineNumberNode 

  file: Symbol none

  line: Int64 1

2: Int64 0

  typ: Any

  typ: Any


dump(Expr(:if, true, Expr(:block, 0), Expr(:block,1)))

Expr 

  head: Symbol if

  args: Array(Any,(3,))

1: Bool true

2: Expr 

  head: Symbol block

  args: Array(Any,(1,))

1: Int64 0

  typ: Any

3: Expr 

  head: Symbol block

  args: Array(Any,(1,))

1: Int64 1

  typ: Any

  typ: Any


They're the same minus LineNumberNodes, because block type expressions 
always create those things.

On Wednesday, December 9, 2015 at 7:21:55 AM UTC-8, STAR0SS wrote:
>
> You can also use dump() on your expressions to see how they differ 
> exactly. The normal printing doesn't really show you much.
>


[julia-users] Expression Equality

2015-12-08 Thread vishesh
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







[julia-users] Re: using statement with for loop inside if statement

2015-12-08 Thread vishesh
ok that also works.
is there some reason that I have to do an explicit eval call? This seems 
like an error at the grammar/parser level of Julia so the eval call just 
exists to make it compile... or so I think?

On Tuesday, December 8, 2015 at 3:53:16 PM UTC-8, Kristoffer Carlsson wrote:
>
> Maybe just @eval the "using" to execute it in global scope



[julia-users] Re: using statement with for loop inside if statement

2015-12-08 Thread vishesh
ugh it posted accidentally
Let's try this again:

Trying to get the command line style syntax working. 

Somewhere on StackOverflow someone suggested this way of doing things:

module M 
end

if length(ARGS) > 0 && ARGS[1] == "--run"
  using M
  # do whatever
end

however, if I write a simple example (in it's own file) like:

if length(ARGS) > 0 && ARGS[1] == "--run"
  using FactCheck
  for i in [1,2,3]
println(i)
  end
end

I get the following error:

ERROR: LoadError: error compiling anonymous: unsupported or misplaced 
expression "using" in function anonymous

 in include at /usr/local/Cellar/julia/0.4.0/lib/julia/sys.dylib

 in include_from_node1 at /usr/local/Cellar/julia/0.4.0/lib/julia/sys.dylib

 in process_options at /usr/local/Cellar/julia/0.4.0/lib/julia/sys.dylib

 in _start at /usr/local/Cellar/julia/0.4.0/lib/julia/sys.dylib

while loading /Users/Vishesh/repos/learning/julia/test.jl, in expression 
starting on line 3

The only way to make this work is like:

if length(ARGS) > 0 && ARGS[1] == "--run"
  using FactCheck
  eval(:(
  for i in [1,2,3]
println(i)
  end
  ))
end


which seems excessive. 
Basically, I can do a regular function call after a using statement, so 
using M; println("hello world") is fine, but using M; for ... end doesn't 
work.

ideas?
Vishesh

On Tuesday, December 8, 2015 at 3:43:55 PM UTC-8, vis...@stanford.edu wrote:
>
> Hi all,
>
> Trying to get the command line style syntax working. 
>
> Somewhere on StackOverflow someone suggested this way of doing things:
>
> module M 
> end
>
> if length(ARGS) > 0 && ARGS[1] == "--run"
>   using M
>   # do whatever
> end
>
> however, if I write a for loop like
>
> if ...
>   using M
>   for l in readlines(STDIN)
>
> end
>
>
>

[julia-users] using statement with for loop inside if statement

2015-12-08 Thread vishesh
Hi all,

Trying to get the command line style syntax working. 

Somewhere on StackOverflow someone suggested this way of doing things:

module M 
end

if length(ARGS) > 0 && ARGS[1] == "--run"
  using M
  # do whatever
end

however, if I write a for loop like

if ...
  using M
  for l in readlines(STDIN)

end




[julia-users] Re: Arbitrary Vector literal nesting

2015-11-18 Thread vishesh
Awesome, I see that 0.5 is scheduled for early December. Looking forward to 
having this fixed.

On Wednesday, November 18, 2015 at 9:37:37 AM UTC-8, Josh Langsfeld wrote:
>
> Should also mention that even when ["1", ["1","2","3"]] returns a two 
> element Vector{Any}, the inner array will still get inferred to be 
> Vector{ASCIIString} unless you add the Any prefix.
>
> On Wednesday, November 18, 2015 at 12:30:28 PM UTC-5, Josh Langsfeld wrote:
>>
>> Your conclusion is correct. The final switch should happen pretty soon on 
>> 0.5 master. Until then, the work-around is to prefix all your brackets with 
>> 'Any', including the inner arrays.
>>
>> On Wednesday, November 18, 2015 at 12:20:15 PM UTC-5, vis...@stanford.edu 
>> wrote:
>>>
>>> Hi everyone,
>>>
>>> Julia seems cool! Vector literals have me completely stumped though. A 
>>> little help? 
>>> What is going on here?
>>>
>>> ["1","1"] => ASCIIString["1","1"] 
>>> ["1",["1"]] => ASCIIString["1","1"] + "WARNING: [a,b] concatenation is 
>>> deprecated; use [a;b] instead"
>>> ["1",Any["1"]] => ASCIIString["1","1"] + "WARNING: [a,b] concatenation 
>>> is deprecated; use [a;b] instead"
>>> Any["1",["1"]] => Any["1", ASCIIString["1"]] # not what I want, the 
>>> second array should be general purpose a la python
>>>
>>> Basically I just want general heterogeneous lists like python.
>>>
>>> I read about the  {} syntax somewhere which does exactly what I want, 
>>> but I keep getting deprecation warnings, so I'd rather not poke that dragon.
>>> It seems based on the fact that ["1",["1"]] generates a deprecated 
>>> warning about behaving wrong, and {"1",{"1"}} is completely deprecated 
>>> syntax that at some point, ["1",["1"]] should do what I want it to, but 
>>> this feature has somehow been only half migrated over?
>>>
>>> Help!
>>>
>>> Vishesh
>>>
>>

[julia-users] Arbitrary Vector literal nesting

2015-11-18 Thread vishesh
Hi everyone,

Julia seems cool! Vector literals have me completely stumped though. A 
little help? 
What is going on here?

["1","1"] => ASCIIString["1","1"] 
["1",["1"]] => ASCIIString["1","1"] + "WARNING: [a,b] concatenation is 
deprecated; use [a;b] instead"
["1",Any["1"]] => ASCIIString["1","1"] + "WARNING: [a,b] concatenation is 
deprecated; use [a;b] instead"
Any["1",["1"]] => Any["1", ASCIIString["1"]] # not what I want, the second 
array should be general purpose a la python

Basically I just want general heterogeneous lists like python.

I read about the  {} syntax somewhere which does exactly what I want, but I 
keep getting deprecation warnings, so I'd rather not poke that dragon.
It seems based on the fact that ["1",["1"]] generates a deprecated warning 
about behaving wrong, and {"1",{"1"}} is completely deprecated syntax that 
at some point, ["1",["1"]] should do what I want it to, but this feature 
has somehow been only half migrated over?

Help!

Vishesh