Reputation: 3
I want to have a function for arithmetic operations in alloy, is it a good idea to define them in a fun as bellow?
sig expre{
add: expre -> expre,
sub: expre -> expre,
mult: expre -> expre,
div: expre -> expre,
mod: expre -> expre,
a: AttributeNames,
val: Int
}
fun Exp(e: expre): Int{
plus[e.val, e.val] +
minus[e.val, e.val] +
mul[e.val, e.val] +
div[e.val, e.val] +
rem[e.val, e.val] +
Exp[e]
}
Upvotes: 0
Views: 390
Reputation: 2768
Not sure what you're trying to do, but it looks as if you might be looking to model a grammar of arithmetic expressions along with an evaluation function. If so, you're on the right track, but rather than defining the expression types as fields of a single signature, you probably want to define them as subsignatures:
abstract sig Expr { val: Int }
abstract sig UnaryExpr extends Expr { target: Expr }
abstract sig BinaryExpr extends Expr { left, right: Expr }
sig PlusExpr extends BinaryExpr { } {val = plus[left.@val, right.@val] }
sig Literal extends Expr { }
Upvotes: 1