@@ -10,7 +10,9 @@ function walk(opmap, argmap, ex)
1010 if ! iscall (ex)
1111 return argmap (ex)
1212 else
13- return mapfoldl ((args... ) -> walk (opmap, argmap, args... ), opmap (operation (ex)), arguments (ex))
13+ return mapfoldl (opmap (operation (ex)), arguments (ex)) do (args... )
14+ return walk (opmap, argmap, args... )
15+ end
1416 end
1517end
1618# Walk the expression `ex`, modifying the
@@ -21,13 +23,22 @@ opwalk(opmap, a) = walk(opmap, identity, a)
2123argwalk (argmap, a) = walk (identity, argmap, a)
2224
2325# Generic lazy functionality.
26+ using DerivableInterfaces: AbstractArrayInterface, InterfaceFunction
27+ struct LazyInterface{N} <: AbstractArrayInterface{N} end
28+ LazyInterface () = LazyInterface {Any} ()
29+ LazyInterface (:: Val{N} ) where {N} = LazyInterface {N} ()
30+ LazyInterface {M} (:: Val{N} ) where {M, N} = LazyInterface {N} ()
31+ const lazy_interface = LazyInterface ()
32+
33+ const maketerm_lazy = lazy_interface (maketerm)
2434function maketerm_lazy (type:: Type , head, args, metadata)
2535 if head ≡ *
2636 return type (maketerm (Mul, head, args, metadata))
2737 else
2838 return error (" Only mul supported right now." )
2939 end
3040end
41+ const getindex_lazy = lazy_interface (getindex)
3142function getindex_lazy (a:: AbstractArray , I... )
3243 u = unwrap (a)
3344 if ! iscall (u)
@@ -36,6 +47,7 @@ function getindex_lazy(a::AbstractArray, I...)
3647 return error (" Indexing into expression not supported." )
3748 end
3849end
50+ const arguments_lazy = lazy_interface (arguments)
3951function arguments_lazy (a)
4052 u = unwrap (a)
4153 if ! iscall (u)
@@ -46,18 +58,18 @@ function arguments_lazy(a)
4658 return error (" Variant not supported." )
4759 end
4860end
49- function children_lazy (a)
50- return arguments (a )
51- end
52- function head_lazy (a)
53- return operation (a )
54- end
55- function iscall_lazy (a )
56- return iscall (unwrap (a))
57- end
58- function isexpr_lazy (a )
59- return iscall (a)
60- end
61+ using TermInterface : children
62+ const children_lazy = lazy_interface (children )
63+ children_lazy (a) = arguments (a)
64+ using TermInterface : head
65+ const head_lazy = lazy_interface (head )
66+ head_lazy (a) = operation (a)
67+ const iscall_lazy = lazy_interface (iscall )
68+ iscall_lazy (a) = iscall (unwrap (a))
69+ using TermInterface : isexpr
70+ const isexpr_lazy = lazy_interface (isexpr )
71+ isexpr_lazy (a) = iscall (a)
72+ const operation_lazy = lazy_interface (operation)
6173function operation_lazy (a)
6274 u = unwrap (a)
6375 if ! iscall (u)
@@ -68,6 +80,7 @@ function operation_lazy(a)
6880 return error (" Variant not supported." )
6981 end
7082end
83+ const sorted_arguments_lazy = lazy_interface (sorted_arguments)
7184function sorted_arguments_lazy (a)
7285 u = unwrap (a)
7386 if ! iscall (u)
@@ -78,27 +91,35 @@ function sorted_arguments_lazy(a)
7891 return error (" Variant not supported." )
7992 end
8093end
81- function sorted_children_lazy (a)
82- return sorted_arguments (a)
83- end
94+ using TermInterface: sorted_children
95+ const sorted_children_lazy = lazy_interface (sorted_children)
96+ sorted_children_lazy (a) = sorted_arguments (a)
97+ const ismul_lazy = lazy_interface (ismul)
8498ismul_lazy (a) = ismul (unwrap (a))
99+ using AbstractTrees: AbstractTrees
100+ const abstracttrees_children_lazy = lazy_interface (AbstractTrees. children)
85101function abstracttrees_children_lazy (a)
86102 if ! iscall (a)
87103 return ()
88104 else
89105 return arguments (a)
90106 end
91107end
108+ using AbstractTrees: nodevalue
109+ const nodevalue_lazy = lazy_interface (nodevalue)
92110function nodevalue_lazy (a)
93111 if ! iscall (a)
94112 return unwrap (a)
95113 else
96114 return operation (a)
97115 end
98116end
99- materialize_lazy (a) = argwalk (unwrap, a)
100117using Base. Broadcast: materialize
118+ const materialize_lazy = lazy_interface (materialize)
119+ materialize_lazy (a) = argwalk (unwrap, a)
120+ const copy_lazy = lazy_interface (copy)
101121copy_lazy (a) = materialize (a)
122+ const equals_lazy = lazy_interface (== )
102123function equals_lazy (a1, a2)
103124 u1, u2 = unwrap .((a1, a2))
104125 if ! iscall (u1) && ! iscall (u2)
@@ -109,6 +130,7 @@ function equals_lazy(a1, a2)
109130 return false
110131 end
111132end
133+ const isequal_lazy = lazy_interface (isequal)
112134function isequal_lazy (a1, a2)
113135 u1, u2 = unwrap .((a1, a2))
114136 if ! iscall (u1) && ! iscall (u2)
@@ -119,11 +141,13 @@ function isequal_lazy(a1, a2)
119141 return false
120142 end
121143end
144+ const hash_lazy = lazy_interface (hash)
122145function hash_lazy (a, h:: UInt64 )
123146 h = hash (Symbol (unspecify_type_parameters (typeof (a))), h)
124147 # Use `_hash`, which defines a custom hash for NamedDimsArray.
125148 return _hash (unwrap (a), h)
126149end
150+ const map_arguments_lazy = lazy_interface (map_arguments)
127151function map_arguments_lazy (f, a)
128152 u = unwrap (a)
129153 if ! iscall (u)
@@ -134,19 +158,22 @@ function map_arguments_lazy(f, a)
134158 return error (" Variant not supported." )
135159 end
136160end
161+ function substitute end
162+ const substitute_lazy = lazy_interface (substitute)
137163function substitute_lazy (a, substitutions:: AbstractDict )
138164 haskey (substitutions, a) && return substitutions[a]
139165 ! iscall (a) && return a
140166 return map_arguments (arg -> substitute (arg, substitutions), a)
141167end
142- function substitute_lazy (a, substitutions)
143- return substitute (a, Dict (substitutions))
144- end
168+ substitute_lazy (a, substitutions) = substitute (a, Dict (substitutions) )
169+ using AbstractTrees : printnode
170+ const printnode_lazy = lazy_interface (printnode)
145171function printnode_lazy (io, a)
146172 # Use `printnode_nameddims` to avoid type piracy,
147173 # since it overloads on `AbstractNamedDimsArray`.
148174 return printnode_nameddims (io, unwrap (a))
149175end
176+ const show_lazy = lazy_interface (show)
150177function show_lazy (io:: IO , a)
151178 if ! iscall (a)
152179 return show (io, unwrap (a))
@@ -160,9 +187,12 @@ function show_lazy(io::IO, mime::MIME"text/plain", a)
160187 ! iscall (a) ? show (io, mime, unwrap (a)) : show (io, a)
161188 return nothing
162189end
190+ const add_lazy = lazy_interface (+ )
163191add_lazy (a1, a2) = error (" Not implemented." )
192+ const sub_lazy = lazy_interface (- )
164193sub_lazy (a) = error (" Not implemented." )
165194sub_lazy (a1, a2) = error (" Not implemented." )
195+ const mul_lazy = lazy_interface (* )
166196function mul_lazy (a)
167197 u = unwrap (a)
168198 if ! iscall (u)
@@ -186,6 +216,7 @@ mul_lazy(a1::Number, a2::Number) = a1 * a2
186216div_lazy (a1, a2:: Number ) = error (" Not implemented." )
187217
188218# NamedDimsArrays.jl interface.
219+ const inds_lazy = lazy_interface (inds)
189220function inds_lazy (a)
190221 u = unwrap (a)
191222 if ! iscall (u)
@@ -196,6 +227,7 @@ function inds_lazy(a)
196227 return error (" Variant not supported." )
197228 end
198229end
230+ const dename_lazy = lazy_interface (dename)
199231function dename_lazy (a)
200232 u = unwrap (a)
201233 if ! iscall (u)
0 commit comments