1

I am studying software engineering course, and there i saw the usage of JML. Here is an example piece of code:

//@ requires f >= 0.0 
public float sqrt(float f) { 
   return f/2;
}

It says that formal JML specifications are executable!

My question is, when we call this sqrt function with f=-4,does this code give an error or throw an exception or give any warnings? I tried it on my computer it just worked well and printed out -2. Then what does it mean that JML are executable? Why don't we use just comments to do this? Can anyone explain?

Thanks

yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • Well, here is the code and i am asking something about the code. I do not understand the downvote! – yrazlik May 28 '13 at 21:29

3 Answers3

2

Well, this code itself does not create any warning for you. There exist a variety of tools which make use of JML specs, for example:

  • jmlrac: test for violations of assertions during execution
  • ESC/Java2: static verification; compile-time proving that contracts are never violated
  • jmldoc: javadoc-style documentation
  • jmlc: assertion-checking compiler
  • jmlunit: unit testing tool

In addition to that, JML specs, if properly used, reveal programmer intentions to foreign readers/maintainers of the code.

McMannus
  • 259
  • 3
  • 10
1

You are not doing the square root (Math.sqrt(f), see http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#sqrt(double)) but dividing by 2 (f/2). So there is nothing wrong with that. What you need is using Math.sqrt(...) if this is what I assume you need looking at the name of your method.

Lucia Pasarin
  • 2,268
  • 1
  • 21
  • 37
  • 2
    well actually it is not important here. I just wonder this: I gave a precondition to the function saying that the parameteter must be >=0 but i call it with parameter<0 and it works. If JML is executable, why is this the case? Why is there no errors or warnings? – yrazlik May 28 '13 at 21:40
  • Oh, then in this case I think you are missing the last `;` at the end of the line. – Lucia Pasarin May 28 '13 at 21:43
1

JML is not part of Java. It is an extension designed by a coalition of researchers but without official endorsement. As such, JML annotations are not taken into account by the Java compiler, but you can compile a JML-annotated Java program with a special compiler so that the executable specifications will be taken into account. For instance, JML4C.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281