-
Notifications
You must be signed in to change notification settings - Fork 8
/
eval.jl
51 lines (44 loc) · 1.81 KB
/
eval.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
export eval_apl
# eval
eval_apl(ex) = eval_apl(ex, nothing, nothing)
eval_apl(f, α, ω) = f
eval_apl(v::JlVal, α, ω) = v.val
eval_apl(::Α, α, ω) = α
eval_apl(::Ω, α, ω) = ω
eval_apl(x::Apply, α, ω) = eval_apl(x.f, α, ω)(eval_apl(x.r, α, ω))
eval_apl(x::ConcArr, α, ω) = vcat(eval_apl(x.l, α, ω), eval_apl(x.r, α, ω))
eval_apl(x::Apply2, α, ω) = eval_apl(x.f, α, ω)(eval_apl(x.l, α, ω), eval_apl(x.r, α, ω))
# call methods for primitive functions
mkbody1(x::Symbol) = :($x(ω))
mkbody1(x::Expr) = x
mkbody2(x::Symbol) = :($x(α, ω))
mkbody2(x::Expr) = x
for (sym, fns) in prim_fns
mon, dya = fns
mon != nothing && @eval (f::PrimFn{$sym})(ω) = $(mkbody1(mon))
dya != nothing && @eval (f::PrimFn{$sym})(α, ω) = $(mkbody2(dya))
end
function actuallyreducedim(f, xs::AbstractArray, ident)
dropdims(
reduce(f, xs, dims=ndims(xs), init=ident),
dims=ndims(xs)
)
end
function actuallyreducedim(f, xs::AbstractVector, ident)
reduce(f, xs, init=ident)
end
# call methods for primitive operators
(op::Op1{'/'})(ω) = actuallyreducedim(op.l, ω, identity(op.l, eltype(ω)))
(op::Op1{'⌿'})(ω) = dropdims(reduce(op.l, ω, init=identity(op.l, eltype(ω)), dims=1), dims=1)
(op::Op1{'\\'})(ω) = prefix_scan(op.l, ω, identity(op.l, ω))
(op::Op1{'⍀'})(ω) = prefix_scan(op.l, ω, identity(op.l, ω)) # Todo
(op::Op1{'¨'})(ω) = map(op.l, ω)
(op::Op1{'↔'})(α, ω) = op.l(ω, α)
(op::Op1{'⍨'})(α, ω) = op.l(ω, α)
(op::Op2{'.'})(α, ω) = reduce(op.l, op.r(convert(Array, α), convert(Array, ω)))
(op::Op2{'⋅'})(α) = op.l(op.r(α)) # compose
(op::Op1{'∘'})(α, ω) = [op.l(x, y) for x in α, y in ω]
# user defined functions
(fn::UDefFn{0})() = eval_apl(fn.ast)
(fn::UDefFn{1})(ω) = eval_apl(fn.ast, nothing, ω)
(fn::UDefFn{2})(α, ω) = eval_apl(fn.ast, α, ω)