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

x[i,j] and x[i;j] are parsed the same #8541

Closed
joehuchette opened this issue Oct 1, 2014 · 14 comments
Closed

x[i,j] and x[i;j] are parsed the same #8541

joehuchette opened this issue Oct 1, 2014 · 14 comments
Labels
help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@joehuchette
Copy link
Member

Consider

julia> dump(:(x[i,j]))
Expr
  head: Symbol ref
  args: Array(Any,(3,))
    1: Symbol x
    2: Symbol i
    3: Symbol j
  typ: Any

julia> dump(:(x[i;j]))
Expr
  head: Symbol ref
  args: Array(Any,(3,))
    1: Symbol x
    2: Symbol i
    3: Symbol j
  typ: Any

What's the rationale for this behavior? This is now seems ambiguous, since #8250 gives

julia> dump(:(x[i,j;k]))
Expr
  head: Symbol ref
  args: Array(Any,(4,))
    1: Symbol x
    2: Expr
      head: Symbol parameters
      args: Array(Any,(1,))
        1: Symbol k
      typ: Any
    3: Symbol i
    4: Symbol j
  typ: Any
@joehuchette
Copy link
Member Author

cc @mlubin

@mlubin
Copy link
Member

mlubin commented Oct 1, 2014

cc @jakebolewski

@jakebolewski
Copy link
Member

#8250 didn't change this behavior. In 0.3 [x,y] and [x; y] both are parsed as 1-d vectors. Isn't this just another consequence of automatic concatenation? The second form can be thought of as
[[1],[2]].

The 2D case works as you would expect:

julia> [1 1; 2 2]
2x2 Array{Int64,2}:
 1  1
 2  2

julia> [[1 1]; [2 2]]
2x2 Array{Int64,2}:
 1  1
 2  2

julia> [[1 1], [2 2]]
2x2 Array{Int64,2}:
 1  1
 2  2

@JeffBezanson
Copy link
Member

The problem seems to be that parsing x[i] calls parse-cat.

@jakebolewski
Copy link
Member

Which is necessary to support array syntax for typed arrays [1 1; 2 2] -> Int[1 1; 2 2].

@JeffBezanson
Copy link
Member

That makes sense. If we change [x,y] to not concatenate (:+1:), then [x,y] and [x;y] should parse differently, as should a[x,y] and a[x;y]. In fact it seems like it would be better to parse these differently already, even if they mean the same thing for now.

@mlubin
Copy link
Member

mlubin commented Oct 1, 2014

Right, it seems like they should be parsed differently regardless of the current behavior of [a,b].

@joehuchette
Copy link
Member Author

+1 to parsing them differently (and for dropping automatic concatenation...)

@mlubin
Copy link
Member

mlubin commented Dec 21, 2014

Any way we could resolve this before a decision is made on #8599?

@StefanKarpinski StefanKarpinski added the help wanted Indicates that a maintainer wants help on an issue or pull request label Dec 22, 2014
@StefanKarpinski
Copy link
Member

I've marked this as "up for grabs". It's a slightly tedious though relatively straightforward parser change. I think that if you want to change the way this parses, as long as it doesn't break stuff, it's fine.

@mlubin
Copy link
Member

mlubin commented Feb 11, 2015

Is this fixed now?

julia> dump(:(x[i;j]))
Expr 
  head: Symbol typed_vcat
  args: Array(Any,(3,))
    1: Symbol x
    2: Symbol i
    3: Symbol j
  typ: Any

@jakebolewski
Copy link
Member

Yup.

julia> Meta.show_sexpr(:(x[i,j]))
(:ref, :x, :i, :j)
julia> Meta.show_sexpr(:(x[i;j]))
(:typed_vcat, :x, :i, :j)
julia> Meta.show_sexpr(:(x[i,j;k]))
(:typed_vcat, :x, (:parameters, :k), :i, :j)

@mlubin
Copy link
Member

mlubin commented Feb 11, 2015

Shouldn't x[i,j;k] be a ref not a typed_vcat? The presence of parameters shouldn't change the type of the expression.

@jakebolewski
Copy link
Member

Yes, but I feel that is a separate issue. We currently give a really bad error message as well:

julia> Int[1,2;3,4]
ERROR: unsupported or misplaced expression parameters

whereas in v0.3 we gave something more helpful

julia> Int[1,2;3,4]
ERROR: syntax: unexpected semicolon in array expression

related to #9601

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

5 participants