@@ -196,9 +196,9 @@ Base.show(io::IO, v::Sym) = Base.show_unquoted(io, v.name)
196196# Maybe don't even need a new type, can just use Sym{FnType}
197197struct FnType{X<: Tuple ,Y} end
198198
199- (f:: Sym {<:FnType} )(args... ) = Term {promote_symtype(f, symtype.(args)...)} (f, [args... ])
199+ (f:: Symbolic {<:FnType} )(args... ) = Term {promote_symtype(f, symtype.(args)...)} (f, [args... ])
200200
201- function (f:: Sym )(args... )
201+ function (f:: Symbolic )(args... )
202202 error (" Sym $f is not callable. " *
203203 " Use @syms $f (var1, var2,...) to create it as a callable. " *
204204 " See ?@fun for more options" )
210210The output symtype of applying variable `f` to arugments of symtype `arg_symtypes...`.
211211if the arguments are of the wrong type then this function will error.
212212"""
213- function promote_symtype (f:: Sym {FnType{X,Y}} , args... ) where {X, Y}
213+ function promote_symtype (f:: Symbolic {FnType{X,Y}} , args... ) where {X, Y}
214214 if X === Tuple
215215 return Y
216216 end
@@ -250,8 +250,10 @@ macro syms(xs...)
250250 defs = map (xs) do x
251251 n, t = _name_type (x)
252252 :($ (esc (n)) = Sym {$(esc(t))} ($ (Expr (:quote , n))))
253+ nt = _name_type (x)
254+ n, t = nt. name, nt. type
255+ :($ (esc (n)) = Sym {$(esc(t))} ($ (Expr (:quote , n))))
253256 end
254-
255257 Expr (:block , defs... ,
256258 :(tuple ($ (map (x-> esc (_name_type (x). name), xs)... ))))
257259end
@@ -275,14 +277,20 @@ function _name_type(x)
275277 else
276278 return (name= lhs, type= rhs)
277279 end
280+ elseif x isa Expr && x. head === :ref
281+ ntype = _name_type (x. args[1 ]) # a::Number
282+ N = length (x. args)- 1
283+ return (name= ntype. name,
284+ type= :(Array{$ (ntype. type), $ N}),
285+ array_metadata= :(Base. Slice .(($ (x. args[2 : end ]. .. ),))))
278286 elseif x isa Expr && x. head === :call
279287 return _name_type (:($ x:: Number ))
280288 else
281289 syms_syntax_error ()
282290 end
283291end
284292
285- function Base. show (io:: IO , f:: Sym {<:FnType{X,Y}} ) where {X,Y}
293+ function Base. show (io:: IO , f:: Symbolic {<:FnType{X,Y}} ) where {X,Y}
286294 print (io, f. name)
287295 # Use `Base.unwrap_unionall` to handle `Tuple{T} where T`. This is not the
288296 # best printing, but it's better than erroring.
@@ -433,7 +441,7 @@ setargs(t, args) = Term{symtype(t)}(operation(t), args)
433441cdrargs (args) = setargs (t, cdr (args))
434442
435443print_arg (io, x:: Union{Complex, Rational} ; paren= true ) = print (io, " (" , x, " )" )
436- isbinop (f) = istree (f) && Base. isbinaryoperator (nameof (operation (f)))
444+ isbinop (f) = istree (f) && ! istree ( operation (f)) && Base. isbinaryoperator (nameof (operation (f)))
437445function print_arg (io, x; paren= false )
438446 if paren && isbinop (x)
439447 print (io, " (" , x, " )" )
@@ -506,8 +514,23 @@ function show_mul(io, args)
506514 end
507515end
508516
517+ function show_ref (io, f, args)
518+ x = args[1 ]
519+ idx = args[2 : end ]
520+
521+ istree (x) && print (io, " (" )
522+ print (io, x)
523+ istree (x) && print (io, " )" )
524+ print (io, " [" )
525+ for i= 1 : length (idx)
526+ print_arg (io, idx[i])
527+ i != length (idx) && print (io, " , " )
528+ end
529+ print (io, " ]" )
530+ end
531+
509532function show_call (io, f, args)
510- fname = nameof (f)
533+ fname = istree (f) ? Symbol ( repr (f)) : nameof (f)
511534 binary = Base. isbinaryoperator (fname)
512535 if binary
513536 for (i, t) in enumerate (args)
@@ -543,6 +566,8 @@ function show_term(io::IO, t)
543566 show_mul (io, args)
544567 elseif f === (^ )
545568 show_pow (io, args)
569+ elseif f === (getindex)
570+ show_ref (io, f, args)
546571 else
547572 show_call (io, f, args)
548573 end
@@ -573,7 +598,7 @@ where `coeff` and the vals are `<:Number` and keys are symbolic.
573598- `arguments(::Add)` -- returns a totally ordered vector of arguments. i.e.
574599 `[coeff, keyM*valM, keyN*valN...]`
575600"""
576- struct Add{X, T<: Number , D, M} <: Symbolic{X}
601+ struct Add{X<: Number , T<: Number , D, M} <: Symbolic{X}
577602 coeff:: T
578603 dict:: D
579604 sorted_args_cache:: Ref{Any}
@@ -699,7 +724,7 @@ where `coeff` and the vals are `<:Number` and keys are symbolic.
699724- `arguments(::Mul)` -- returns a totally ordered vector of arguments. i.e.
700725 `[coeff, keyM^valM, keyN^valN...]`
701726"""
702- struct Mul{X, T<: Number , D, M} <: Symbolic{X}
727+ struct Mul{X<: Number , T<: Number , D, M} <: Symbolic{X}
703728 coeff:: T
704729 dict:: D
705730 sorted_args_cache:: Ref{Any}
0 commit comments