Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Preview new hook sys_portrait_eq/2

8 views
Skip to first unread message

burs...@gmail.com

unread,
Nov 26, 2016, 5:34:12 PM11/26/16
to
A solution to a long standing problem in CLP(*) came
by choosing a CAS middleground.

The problem is what should be the meaning of X #= E.
Am I allowed to supply different expressions E at
runtime, or should everything happen at compile time
for a CLP(*) system.

Ulrich Neumerkel first came up with monotonicy considerations
and then with new terms such as ?X in CLP(FD). This is
all nice and might solve some problems. A slight similar
Problem is also existent in pure prolog.

There some Prolog systems have a term eval(X) in arithmethic
expressions, to indicate that a part of an arithmetic expression
might change during runtime. I am doing the converse now,
and extending the semantic as follows.

Namely I have introduced a term expression/1
with the following requirements:

Lisp Quote Functionality:
-------------------------
The term doesn't indicate that something will be evaluated
at runtime, it indicates the contrary. It indicates that the
argument should be handled symbolically.

Quoting Must be Repeated:
-------------------------
Superficially we add the requirement, that inside a expression/1,
when ever a subexpression exists, this subexpression needs
also an expression/1 wrapper.

As a result we get for our first baby steps in Python sympy like
symbolic evaluation:

?- X is 2*Y+1.
X = expression(expression(2*Y)+1)

The rational for this requirement is that rule formulation gets
easier. Here are some rules for Auto Diff:

:- public d/3.
d(expression(-X), Y, R) :- !,
R is -d(X,Y).
d(expression(X+Y), Z, R) :- !,
R is d(X,Z)+d(Y,Z).
etc..

Without repeated quoting we would generate an overhead when using
is/2 in inside rules, since subexpressions would be recalculated.

Portray Hooks for the End-User:
-------------------------------
Where as CLP(*) uses hooks for the constraint store, to pretty print
it, the Python sympy middle ground, when we don't have assumptions
doesn't need this route.

A problem is more that in display of ordinary Prolog variables, that
can now carry around symbolic expressions, we might want to get rid
of the repeated quoting.

Our approach is simple. We use (=)/2 in the top-level to display
ordinary variable instantiations. And this is the new approach,
we use (is)/2 itself in the top-level and unwrap the repeated quoting.
This is done by a new hook sys_portrait_eq/2.

Here is the same example as above, executed with a top-level that
supports this new sys_portrait_eq/2 hook:

?- X is 2*Y+1.
X is 2*Y+1

(Now there is a is/2 shown instead of a =/2 in the top-level)

What can be gained by all these complications. We have already seen above that auto diff rules look pretty easy. In the formulation of any rules we can the is/2 operator itself, which is extended to symbolic expressions, and use is/2 to reassemble symbolic expressions.

Here are some show cases:

Symbolic Expressions no Expansion
---------------------------------

By default the arithmetic expressions are very conservative
concerning expansion. We get for example:

?- Y is (X-1)^2
Y is (X-1)^2
?- F is (1+1/X)^X.
F is (1+1/X)^X

Symbolic Expressions with Expansion
-----------------------------------

Experimentally we use the function e/1 for expansion. Distributivity
will be applied, the rules are similarly to auto diff implemented
with the help of the new symbolic is/2:

?- Y is (X-1)^2, Z is e(Y).
Y is (X-1)^2,
Z is X*X-X-(X-1)

The conservative simplification during symbolic expression creation
with is/2 is not yet done completely in our prototype so far. Thats
why we see Z is X*X-X-(X-1) and not yet Z is X^2-2*X+1.

Substitution of Symbolic Expressions
------------------------------------

Experimentally we use the function s/3 for substitution. This can be used
for many purposes. If a variable is replaced by a value this can be used to
evaluated a symbolic expression at some point.

?- F is (X-1)^2, G is s(F,X,3).
F is (X-1)^2,
G = 4
?- F is (1+1/X)^X, R is s(F,X,10).
F is (1+1/X)^X,
R is 25937424601/10000000000
?- F is (1+1/X)^X, R is s(F,X,Y).
F is (1+1/X)^X,
R is (1+1/Y)^Y,

Its also possible to use another symbolic expression for the replacement of a variable. This can be used for variable renaming or even making an expression much larger by pluggin in a bigger expression.

Again we use the new symbolic is/2 predicate to implement the function s/3. The is/2 will reassemble the resulting expression, but not reevaluate each time the replacement expression, since it is quoted.

Auto Diff of Symbolic Expressions
---------------------------------

Experimentally we use the function d/2 for differentiation. The result is a new symbolic expression if the original expression was a symbolic expression:

?- F is (X-1)^2, G is d(F,X).
F is (X-1)^2,
G is 2*(X-1)
?- F is (X-1)^2, G is d(d(F,X),X).
F is (X-1)^2,
G = 2

Expansion is not done automatically, and rules formulate with is/2 also don't need to do this. We will do some further experiments next. A lot can be done with what has already be presented

- using symbolic expressions in matrices,
- providing taylor expansions,
- doing some tensor flow learning via auto diff,
etc.. etc..

Open Source so far: Use of new sys_portrait_eq/2 hook:
https://github.com/jburse/jekejeke-devel/blob/master/jekrun/headless/jekpro/reference/runtime/session.p#L143

Open Source so far: Auto Diff, Substitute and Expand:
https://github.com/jburse/jekejeke-devel/blob/master/jekrun/headless/jekpro/reference/dispatch/expression.p#L119

burs...@gmail.com

unread,
Nov 26, 2016, 5:45:55 PM11/26/16
to
Constraint stores will come again into play, when we will introduce
assumptions. Idea is that we will as a first step simply freeze
(=:=)/2 and (<)/2 when one of its arguments is symbolic.

When (=:=)/2 is woken up, we have different options what to do then.
Mostlikely we will not re-evaluate the expression that caused the
variable binding, but the skeleton of (=:=)/2 will be re-evaluated.

Thus we will freeze (=:=) without the expression/1 quoting, but
will get expressions/1 inputs as a wake-up.

What we have already figured out is that is/2 will probably not
need some freeze, we can easily chain it:

?- X is 2/3, Y is X*3/2.
X is 2/3,
Y = 1
?- X is Y-1, Z is X^2.
X is Y-1,
Z is (Y-1)^2
0 new messages