MikeInnes / DataFlow.jl

Other
122 stars 12 forks source link

How to extract the CSE form? #14

Closed dpsanders closed 6 years ago

dpsanders commented 6 years ago

I would like to extract the CSE form of an expression, but I can't work out how. I don't see any difference before and after CSE (e.g. in the resulting types of the objects).

Somehow the package knows that they are different (since it displays them differently), but in all other respects they seem to be identical.

julia> using DataFlow
@flow (x^2, x^2 + 2)^A
^R
julia> t = @flow (x^2, x^2 + 2)
t2 = DataFlow.cse(IVertex(t)(x ^ 2, x ^ 2 + 2))

julia> t2 = DataFlow.cse(t)
IVertex(
opossum = x ^ 2
(opossum, opossum + 2))

julia> t2.inputs
3-element Array{IVertex,1}:
 IVertex(tuple)
 IVertex(x ^ 2)
 IVertex(x ^ 2 + 2)

julia> t2.value
DataFlow.Call()

julia> t.value
DataFlow.Call()

julia> t == t2
true
MikeInnes commented 6 years ago

Basically this is the difference:

julia> t[2], t[3,2]
(IVertex(x ^ 2), IVertex(x ^ 2))

julia> t[2] === t[3,2]
false

julia> t2[2] === t2[3,2]
true

The x^2 nodes are always embedded directly, so we'll never see a variable binding, just the expression. Two expressions are considered the same when they are === identical, so the second case gets an explicit variable binding.

dpsanders commented 6 years ago

Ah, I think I see, thanks !

dpsanders commented 6 years ago

I think DataFlow.syntax was what I was looking for:

julia> DataFlow.syntax(t)
quote
    (x ^ 2, x ^ 2 + 2)
end

julia> DataFlow.syntax(t2)
quote
    ##edge#662 = x ^ 2
    (##edge#662, ##edge#662 + 2)
end