Alloy Community

User login

conditional expressions

 
Posted on: Tue, 06/09/2009 - 07:35
rseater's picture
Joined: 2008-01-24
Points: 59
User is offline
conditional expressions
Does A4 still support conditional expressions?  Section 3.5.1 of the alloy book (Software Abstractions) mentions conditional expressions, where you can write "if C then E1 else E2" (where E1 and E2 are expressions not formula).  That book is for Alloy 3, which is very similar (but not identical) to the syntax of Alloy 4.  Can you still do that in Alloy 4?  When I type it in, I do not get syntax highlighting on the words "if" and "then" and I get compilation errors on the word "if".  The compiler also doesn't seem to like it if I write "C => E1".  Is there another syntax for it?

Neither of these versions compiles:

fun Nclose [N: Int, r: univ -> univ] : univ -> univ {
  if (N=1) then r //complains that "if" is not a valid token
}

fun Nclose [N: Int, r: univ -> univ] : univ -> univ {
  (N=1) => r //complains that r is not a formula
}
 
Posted on: Tue, 06/09/2009 - 07:37    #1
rseater's picture
Joined: 2008-01-24
Points: 59
User is offline
I just figured it out -- it can be done.  Conditional expressions have to have an "else" clause.  A working version of my function would be this:

fun Nclose [N: Int, r: univ -> univ] : univ -> univ {
  (N=1) => r else ~r
}

The compilation error is misleading -- the problem is that there is no else clause, not that there is any problem with having an expression as the result of a conditional.

Syndicate content  

The development of this site is supported by the National Science Foundation under Computing Research Infrastructure Grant No. 0707612.

Theme originally designed by Chris Herberte