Skip to content

Having trouble building a rule #32

@Vaguery

Description

@Vaguery

The use case is relatively simple:

The arithmetic expressions I'm working with have a k symbol in them (an "ephemeral random constant"), which is interpreted as "some number, which is almost certainly different in each case".

So for example, in a simple-sounding scheme where k just appears like a variable name in an expression, I would want to override expresso's simplification dynamics so that (/ k k) does not get simplified to 1.0, but instead is reduced to k. The same applies to almost any arithmetic function; (+ k k) :=> k, for example, and so on.

(ns hashed-symbolic-regression.core-test
  (:require 
            [numeric.expresso.core :as ex]
            [numeric.expresso.rules :as er]
            ))

(declare k) ;; ERC function

(defn k? [item] (= k item))

(prn (k? k)) ;; true

(def erc-mult (er/rule (ex/ex (* ?x ?y)) :=> k
  :if (er/guard (and (k? ?x) (k? ?y)))
  ))

(def two (ex/ex (* k k)))

(prn (er/apply-rule erc-mult two))

I've tried an awful lot of different variations on the rule definition of erc-mult, and none of them seem to recognize a pattern like (+ k k), regardless of number of matchers, serial matchers, quotes, all kinds of stuff. The same kind of pattern will definitely recognize (+ 88 88) if I define it to handle an explicit number, it just won't manage k as far as I can tell.

Is there something I've missed in defining a rule on an abstract symbol like this? I feel as though I've been over all the docs in detail, but I just can't seem to get through this blockage.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions