0

Let say I have a function that return the lesser of two input values of type int. I want to set the precondition to only allow a and b of type int.

class Example

functions
    min: int * int -> int
    min(a, b) ==
        if a < b
        then a
        else b
        -- The code below doesn't work
        -- pre varName1 = int

end Example

When I omit the precondition and enter print Example.min(12.345, 0.123) in the interpreter, I got 0.123 in return.

Interpreter Window

How do I ensure that the function will only accept inputs of type int?

a..
  • 109
  • 13

1 Answers1

0

You have declared the function to take arguments of type int. It is a type-checking error to attempt to call the function with a non-integer argument. So when I attempt to launch this in Overture or VDMJ, you get that error:

Error 4075: Value 12.345 is not an integer in 'Example'

You seem to be using VDMTools? I get the following:

>> print new Example().min(12.345, 0.123)
command line, l. 1, c. 13:
Run-Time Error 298: Incompatible types found in call of function/operation with dynamic type check
actual value: 12.345
expected type: int

To get the behaviour you see, I have to turn OFF all the dynamic type checking in Project Options. Perhaps you have done this?

Nick Battle
  • 638
  • 4
  • 6
  • Thank you for hinting that I might have turn off all dynamic type checking. Because that's precisely what I did (haha). I'm using VDM++ Toolbox Lite v8.0 – a.. Nov 24 '20 at 12:24
  • VDMTools is perfectly OK, though you may find that Overture has a more modern interface and is more familiar if you're used to Eclipse. https://github.com/overturetool/overture/releases/tag/Release%2F3.0.2 – Nick Battle Nov 24 '20 at 17:02