Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 1d1d27b32e147f5cc417c8fbdb4cf013ab5127d2
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Thu Dec 8 02:45:30 2016 +0100

    clean up and test FullForm fix

commit 0034da61e56fd5a8347beaa50ec5f72c75a17cc6
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Thu Dec 8 02:32:33 2016 +0100

    fixes #56

commit f5ab0875644067f5cbdbc3eedc6322c1a38f247b
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Thu Dec 8 02:31:38 2016 +0100

    chained inequalities with a single operator can be
    parsed now. But, this is disabled, because we don't yet
    evaluate the result correctly

commit bbbf4f5094a4e4774c5840778de07cc74bd211eb
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Wed Dec 7 23:30:50 2016 +0100

    BuiltIns() now returns registered system symbols
    rather than all protected symbols.

commit 1a8e07e2f8419e796e03634f0fbd3db9c3b9dbb2
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Wed Dec 7 22:02:18 2016 +0100

    found and correct bug in clearing all upvalues.
    We were looking in the downvalues dict. There was no,
    and still is no, test that catches this.

commit 08478dae04d7ec1d450ba65a0fd242fb630f2a5f
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Wed Dec 7 21:42:53 2016 +0100

    enforce more abstraction

commit f4dfe528b1467e8767f7d6155cb109294d6b4ab1
Author: John Lapeyre <lapeyre.math122a@gmail.com>
Date:   Wed Dec 7 21:31:51 2016 +0100

    use isAttribute(x) to query attributes
  • Loading branch information
jlapeyre committed Dec 8, 2016
1 parent 94cf2fa commit 5330dcd
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 152 deletions.
3 changes: 3 additions & 0 deletions sjtest/output_test.sj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ T If(BigIntInput(), True, Isa(Format(1/2, Conversion => "e"), String))
T Isa(Format(1.0, Conversion => "e"), String)
T If(BigIntInput(), True, Isa(Format(1, Conversion => "e"), String))

## Issue #56
ToString(FullForm(f(x)(y))) == "f(x)(y)"

CompactOutput(savestate)

ClearAll(a,b,x,savestate,f,x,ex,n)
18 changes: 18 additions & 0 deletions src/AST_translation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ function rewrite_to_comparison(ex::Expr)
return Expr(:comparison, ex.args[2], ex.args[1], ex.args[3])
end

rewrite_comparison(ex::Expr) = ex
## The following does what we want. e.g. we get Less(x,y,z)
## But, we have not yet implemented the logic to reduce the expression.
## So, we disable this function and fall back to the older Comparison, for whic
## we do have logic
# function rewrite_comparison(ex::Expr)
# args = ex.args
# ops = @view args[4:2:end]
# op1 = args[2]
# if all( x -> x == op1, ops)
# Expr(:call,get(comparison_translation,op1,op1),args[1:2:end]...)
# else
# ex
# end
# end

# There is no binary minus, no division, and no sqrt in Mxpr's.
# Concrete example: a - b --> a + -b.
# We definitely need to dispatch on a hash query, or types somehow
Expand All @@ -390,6 +406,8 @@ function rewrite_expr(ex::Expr)
ex = rewrite_to_comparison(ex)
elseif is_single_comparison(ex, :(.>)) # julia 0.4 parser does this. In 0.5, this is already a call
return Expr(:call, :(.>), ex.args[1], ex.args[3])
elseif ex.head == :comparison
ex = rewrite_comparison(ex)
elseif is_unary_minus(ex) # - b --> -1 * b
ex = rewrite_unary_minus(ex)
elseif is_binary_minus(ex) # a - b --> a + -b.
Expand Down
3 changes: 2 additions & 1 deletion src/Symata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export name, typename
export isymata, insymata

include("version.jl")
include("util.jl")
include("sjcompat.jl")
#include("early_kernelstate.jl")
include("mxpr.jl")
include("attributes.jl")
include("mxpr_util.jl")
include("exceptions.jl")
include("level_specification.jl")
Expand Down
102 changes: 76 additions & 26 deletions src/attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,53 @@ Stub

const possible_attributes = map(Symbol,split(_attributes_string))

# For Heads that are not symbols
get_attribute(args...) = false

# Return true if sj has attribute attr
function get_attribute(sj::SJSymbol, attr::Symbol)
get(getssym(sj).attr,attr,false)
end

_set_attribute(s,attr) = getssym(Symbol(s)).attr[Symbol(attr)] = true


symattr(s::SJSymbol) = getssym(s).attr
get_attributes(sj::SymString) = ( ks = sort!(collect(Any, keys(symattr(Symbol(sj))))) )

## We will get rid of Qsym
set_attribute(sj::Qsym, attr::Symbol) = (getssym(sj).attr[attr] = true)

"""
unset_attribute(sj::SJSymbol, attr::Symbol)
unset a single attribute `attr` for `sj`.
"""
unset_attribute(sj::SJSymbol, attr::Symbol) = delete!(getssym(sj).attr, attr)

"""
clear_attributes(sja::SymString...)
clear all attributes set for the symbols `sja`.
"""
clear_attributes(sja::SymString...) = foreach( x -> empty!(getssym(x).attr), sja)

## to be removed
clear_attributes(sj::Qsym) = empty!(getssym(sj).attr)

### All access to data structures is above this line, or in mxpr.jl
### i.e. the interface is above this line

# Return true if head of mx has attribute attr
function get_attribute{T}(mx::Mxpr{T}, attr::Symbol)
get_attribute(T,attr)
end

## need a version that warns for users and asserts for developing ?
function _set_attribute(sj::SymString, attr::SymString)
function set_attribute1(sj::SymString, attr::SymString)
(ssj, sattr) = (Symbol(sj),Symbol(attr))
if sattr in possible_attributes
getssym(Symbol(sj)).attr[Symbol(attr)] = true
_set_attribute(sj,attr)
return nothing
end
symwarn("'$attr' is not an attribute")
Expand All @@ -39,9 +81,9 @@ end
set the `attr` attribute for `s`. Arguments may be `Symbol` or `String`.
"""
set_attribute(s::SymString,attr::SymString)= _set_attribute(Symbol(s),Symbol(attr))
set_attribute(sa::AbstractArray, attr::SymString) = foreach( x -> _set_attribute(x,attr), sa)
set_attribute(s::SymString, attra::AbstractArray) = foreach( x -> _set_attribute(s,x), attra)
set_attribute(s::SymString,attr::SymString)= set_attribute1(Symbol(s),Symbol(attr))
set_attribute(sa::AbstractArray, attr::SymString) = foreach( x -> set_attribute1(x,attr), sa)
set_attribute(s::SymString, attra::AbstractArray) = foreach( x -> set_attribute1(s,x), attra)
set_attribute(sa::AbstractArray, attra::AbstractArray) = foreach( x -> set_attribute(sa,x), attra)

"""
Expand Down Expand Up @@ -84,27 +126,6 @@ function set_pattribute(sa::AbstractArray, attra::AbstractArray)
set_attribute(sa,attra)
end


# Better to delete the symbol
#unset_attribute(sj::SJSym, attr::Symbol) = (getssym(sj).attr[attr] = false)

"""
unset_attribute(sj::SJSymbol, attr::Symbol)
unset a single attribute `attr` for `sj`.
"""
unset_attribute(sj::SJSymbol, attr::Symbol) = delete!(getssym(sj).attr, attr)

"""
clear_attributes(sja::SymString...)
clear all attributes set for the symbols `sja`.
"""
clear_attributes(sja::SymString...) = foreach( x -> empty!(getssym(x).attr), sja)

## to be removed
clear_attributes(sj::Qsym) = empty!(getssym(sj).attr)

"""
set_sysattributes(sym, attr)
Expand Down Expand Up @@ -132,3 +153,32 @@ function set_sysattributes(syms::AbstractArray)
foreach(register_system_symbol, sym)
foreach(protect,sym)
end

function protectedsymbols_strings()
symstrings = Array(Compat.String,0)
for s in keys(CurrentContext.symtab)
if get_attribute(s,:Protected) && s != :ans
push!(symstrings,string(getsym(s))) end
end
sort!(symstrings)
end

function protectedsymbols()
args = newargs()
for s in keys(CurrentContext.symtab)
if get_attribute(s,:Protected) && s != :ans
push!(args,getsym(s)) end
end
mx = mxpra(:List, sort!(args))
end

# Related code in predicates.jl and attributes.jl
unprotect(sj::SJSym) = unset_attribute(sj,:Protected)

protect(sj::SymString) = set_attribute(sj,:Protected)

## hmmm, we could do this for all attributes
for s in (:HoldFirst,:HoldAll,:HoldRest,:HoldAllComplete, :SequenceHold, :Flat, :Listable, :Protected, :Constant)
sf = Symbol("is",s)
@eval ($sf)(x) = get_attribute(x,$(QuoteNode(s)))
end
20 changes: 17 additions & 3 deletions src/comparison_logic.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
### Sameq

@mkapprule SameQ nargs => 2
@doap SameQ(x,y) = sameq(x,y)

@mkapprule SameQ nodefault => true
@doap SameQ(args...) = sameq(args...)
## Mma does the following
@doap SameQ(x) = true
@doap SameQ() = true

### UnSameq

Expand All @@ -13,6 +17,15 @@ sameq(x,y) = x === y
sameq(x::BigInt,y::BigInt) = (x == y)
sameq(x::BigFloat,y::BigFloat) = (x == y)
sameq(x::String,y::String) = (x == y)
sameq(x) = true
sameq() = true

function sameq(args...)
for i in 1:length(args)-1
sameq(args[i],args[i+1]) || return false
end
true
end

immutable Compare
result
Expand All @@ -23,7 +36,7 @@ end

## TODO: Implement Equal(a,b,c), etc.

@mkapprule Equal nargs => 2
@mkapprule Equal
@doap function Equal(x,y)
res = sjequal(x,y)
res.known == false && return mx
Expand Down Expand Up @@ -83,6 +96,7 @@ end
## TODO: use a macro to generate these. or otherwise organize this.
@doap Less() = true
@doap Less(x) = true
#@doap Less(args...) = mx

sjless(x::Real, y::Real) = Compare(x < y, true)
function sjless(x,y)
Expand Down
16 changes: 8 additions & 8 deletions src/evaluation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,8 @@ end
# ?? why the first test ! is_canon(nmx). This must apparently always be satisfied.
function meval_apply_all_rules(nmx::Mxpr)
if ! is_canon(nmx)
if hasFlat(nmx) nmx = flatten!(nmx) end
if hasListable(nmx) nmx = threadlistable(nmx) end
if isFlat(nmx) nmx = flatten!(nmx) end
if isListable(nmx) nmx = threadlistable(nmx) end
res = canonexpr!(nmx)
end
# We disable the following. Unlike Flat and Listable and Orderless, OneIdentity is only used for pattern matching.
Expand Down Expand Up @@ -518,22 +518,22 @@ function meval_arguments(mx::Mxpr)
len::Int = length(mxargs)
if len == 0
return mxpr(nhead)
elseif hasHoldFirst(nhead)
elseif isHoldFirst(nhead)
nargs = newargs(len)
nargs[1] = mxargs[1]
@inbounds for i in 2:length(mxargs)
nargs[i] = argeval(mxargs[i])
end
elseif hasHoldAll(nhead)
elseif isHoldAll(nhead)
nargs = copy(mxargs)
for i=1:length(nargs)
if isa(nargs[i],Mxpr{:Evaluate}) && length(nargs[i]) > 0
nargs[i] = doeval(nargs[i][1])
end
end
elseif hasHoldAllComplete(nhead)
elseif isHoldAllComplete(nhead)
nargs = copy(mxargs)
elseif hasHoldRest(nhead)
elseif isHoldRest(nhead)
nargs = copy(mxargs)
nargs[1] = argeval(nargs[1])
else # Evaluate all arguments
Expand All @@ -542,12 +542,12 @@ function meval_arguments(mx::Mxpr)
nargs[i] = argeval(mxargs[i])
end
end
if (! hasSequenceHold(nhead)) && (! hasHoldAllComplete(nhead))
if (! isSequenceHold(nhead)) && (! isHoldAllComplete(nhead))
splice_sequences!(nargs)
end
for i=1:len
if (isa(nargs[i],Mxpr{:Unevaluated}) && length(nargs[i]) > 0) &&
! (hasHoldAll(nhead) || hasHoldAllComplete(nhead)) && !( (hasHoldFirst(nhead) && i == 1) || (hasHoldRest(nhead) && i > 1))
! (isHoldAll(nhead) || isHoldAllComplete(nhead)) && !( (isHoldFirst(nhead) && i == 1) || (isHoldRest(nhead) && i > 1))
nargs[i] = nargs[i][1]
end
end
Expand Down
Loading

0 comments on commit 5330dcd

Please sign in to comment.