0

I have an arithmetic expression containing Var, and I need to match it to the following format:

Step * Var + Offset

If it can be represented in this form, I need to get Step and Offset values. If not, I need to know that too.

So far I tried matching expression trees, i. e.

Expression = ["+", ["*", Var, Step], Offset] % and other variations

and using CLP(FD):

78 ?- 2*Ind+3 #= Step*Ind+Offset, Ind in inf..sup.            
Step*Ind#=_A,
2*Ind#=_B,
_B+3#=_C,
_A+Offset#=_C.

However, the first approach would require simplifying expressions (for example, Var+5-3) and handling multiple different cases, and the second does not work as I expected it to.

Am I missing something? Is there a simpler way, or is this problem too complex?

4
  • 1
    Where is the expression created? Sounds like it would be easier to control its format at the point of creation. Commented Nov 5, 2023 at 12:59
  • ?- Step*Var + Offset = 5*10+20. gives Step = 5, Var = 10, Offset = 20. Prolog terms are not nested lists like your expression tree, they are nested compound terms; ?- +(*(Step,Var),Offset) = 5*10+20. Commented Nov 5, 2023 at 13:26
  • @brebs these expressions are initially created by users, and I cannot add any format restrictions, unfortunately. Commented Nov 7, 2023 at 3:51
  • @TessellatingHeckler I don't match Prolog terms, I convert expressions to the same tree format. (in my expample, Expression would be something like ["*", Var, 2]). Comparing Prolog terms would not work for expressions that don't follow the Step*Var+Offset structure for the same reason (for example ?- (2*Var+1)-1 = Step*Var+Offset. is false). Commented Nov 7, 2023 at 4:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.