VDM++ type error: The state component "totalPrice" must not be used here

126 Views Asked by At

I wrote a code which is for the total price of book when multiplied by number of book and the book price in VDM++.

class Book

types
    public Title = seq of char;
    
instance variables
    private bookTitle: Title;
    private numberOfPages: nat1;
    private totalPrice : real;
    private price : real;
    private noofBook : nat1;
    
    inv numberOfPages >= 1 and numberOfPages <= 100;

values
    public Price: real = 20.50;

operations
    expCalculateTotalPrice: nat1 * real ==> real
    expCalculateTotalPrice (noofBook,price) ==
    ( 
        totalPrice := noofBook * price;
        return totalPrice;
    )

operations
    impCalculateTotalPrice(Book:real) price:real
      pre price > noofBook and price > 0
      post totalPrice = noofBook * price

end Book

the error is "The state component "totalPrice" must not be used here" which is

post totalPrice = noofBook * price

what is wrong with these?

1

There are 1 best solutions below

2
On

You need an external access declaration in an implicit operation definition, something like:

      ext
        rd noofBook:nat1
        wr totalPrice:real

if you want to write to the instance variable totalPrice like the explicit version.

Another thing that might confuse the tool is the name crash. Your formal return value price is shadowing the instance variable of the same name, and the tool might consider the price in the precondition refers to the return value.