A lot of examples with inconsistent results. But bottom line, the behaviour breaks my assumption that macros define a hard local scope (they are hygienic).
What is even weirder is that the macro behaves a mix of soft and hard scope. I am utterly confused.
using Tullio #0.3.3 - Julia 1.6.5
A = rand(4, 4, 5, 5)
B = rand(4, 4)
# OK
@tullio C1[k, l] := A[i, j, k, l] * B[i, j]
C1
# Return a sum instead of 4 x 4 matrix. Intended behaviour?
@tullio C7 := A[i, j, k, l] * B[i, j]
# OK
@tullio C2[k, l] := begin
A[i, j, k, l] * B[i, j]
end
C1 ≈ C2
# Return a sum instead of 4 x 4 matrix. Intended behaviour?
@tullio C6 := begin
A[i, j, k, l] * B[i, j]
end
# Breaks because base Julia would expect the matrix to be already existing
@tullio C3[k, l] := begin
TMP[k, l] = A[i, j, k, l] * B[i, j]
end
# Therefore this works
@tullio C3[k, l] := begin
TMP = A[i, j, k, l] * B[i, j]
end
C1 ≈ C3
# TMP was just used, but defined in a local scope. Not exported.
# This doesn't work
@tullio C4[k, l] := begin
TMP[k, l] = A[i, j, k, l] * B[i, j]
TMP
end
C1 ≈ C4
TMP = zeros(5, 5)
# Breaks with error:
# ERROR: LoadError: MethodError: no method matching zero(::Type{Matrix{Float64}})
@tullio C5[k, l] := begin
TMP[k, l] = A[i, j, k, l] * B[i, j]
TMP
end
C1 ≈ C5
TMP = zeros(5, 5)
# But this works
@tullio C5[k, l] := begin
TMP[k, l] = A[i, j, k, l] * B[i, j]
TMP[k, l]
end
C1 ≈ C5
##########################################
A = rand(4, 4, 5, 5)
B = rand(5, 5, 6, 6)
C = rand(6, 6)
@tullio D1[i, j, m, n] := A[i, j, k, l] * B[k, l, m, n]
@tullio E1[i, j] := D1[i, j, m, n] * C[m, n]
# Breaks D3 undefined. Exoected with soft local scope. Not within macro that should define the array.
@tullio F2 := begin
D2[i, j, m, n] = A[i, j, k, l] * B[k, l, m, n]
E2[i, j] = D[i, j, m, n] * C[m, n]
end
# Breaks D3 undefined. No idea why
@tullio F3 := begin
D3 = A[i, j, k, l] * B[k, l, m, n]
D3[i, j, m, n] * C[m, n]
end
# Breaks D3 undefined
@tullio F3 := begin
D3 = A[i, j, k, l] * B[k, l, m, n]
E3 = D3[i, j, m, n] * C[m, n]
end
# Bounds error
D4 = zeros(4, 4, 6, 6)
@tullio F4 := begin
D4 = A[i, j, k, l] * B[k, l, m, n]
E4 = D4[i, j, m, n] * C[m, n]
end
# Bounds error
D8 = zeros(4, 4, 6, 6)
@tullio F8[i, j] := begin
D8 = A[i, j, k, l] * B[k, l, m, n]
E8 = D8[i, j, m, n] * C[m, n]
end
# Bounds error
D9 = zeros(4, 4, 6, 6)
E9 = zeros(4, 4)
@tullio F9[i, j] := begin
D9 = A[i, j, k, l] * B[k, l, m, n]
E9 = D9[i, j, m, n] * C[m, n]
end
# Returns a sum instead of 2 x 2 matrix. Intended?
D5 = zeros(4, 4, 6, 6)
@tullio F5 := begin
D5[i, j, m, n] = A[i, j, k, l] * B[k, l, m, n]
E5 = D5[i, j, m, n] * C[m, n]
end
# Breaks: E6 not defined
D6 = zeros(4, 4, 6, 6)
@tullio F6 := begin
D6[i, j, m, n] = A[i, j, k, l] * B[k, l, m, n]
E6[i, j] = D6[i, j, m, n] * C[m, n]
end
# Works!
# D7 needs to be defined outside of scope and captured
# E7 cannot be already defined. See example 9
D7 = zeros(4, 4, 6, 6)
@tullio F7[i, j] := begin
D7[i, j, m, n] = A[i, j, k, l] * B[k, l, m, n]
E7 = D7[i, j, m, n] * C[m, n]
end
D7
# Breaks D10 undefined
@tullio F10[i, j] := begin
D10 = zeros(4, 4, 6, 6)
D10[i, j, m, n] = A[i, j, k, l] * B[k, l, m, n]
E10 = D10[i, j, m, n] * C[m, n]
end