项目作者: tomooda

项目描述 :
VDM-SL execution library using public/private VDMPad servers.
高级语言: Python
项目地址: git://github.com/tomooda/pyVDMC.git
创建时间: 2015-04-28T02:02:19Z
项目社区:https://github.com/tomooda/pyVDMC

开源协议:MIT License

下载


pyVDMC

pyVDMC is a VDMPad client library to animate VDM-SL specifications.

VDMC object

  1. >>> from pyVDMC import VDMC
  2. >>> vdm = VDMC()
  3. >>> vdm("1+2")
  4. u'3'

or with a specification,

  1. >>> from pyVDMC import VDMC
  2. >>> fib = VDMC("""
  3. /* fibonacci generator */
  4. state State of
  5. n1 : nat
  6. n2 : nat
  7. init s == s = mk_State(0, 1)
  8. end
  9. operations
  10. next : () ==> nat
  11. next() == (dcl n : nat := n1 + n2; n1 := n2; n2 := n; return n);
  12. """)
  13. >>> fib("next()")
  14. u'1'
  15. >>> fib("next()")
  16. u'2'
  17. >>> fib("next()")
  18. u'3'
  19. >>> fib("next()")
  20. u'5'
  21. >>> fib("next()")
  22. u'8'
  23. >>> fib("next()")
  24. u'13'
  25. >>> fib("next()")
  26. u'21'

DocString and Decorators

Decorators are also available.
(Because we haven’t developed value mappers, only values whose expressions can be interpreted in both VDM-SL and python is supported. Such values include numbers and lists.)

Here is another fibonacci example.

  1. from pyVDMC import *
  2. @vdm_module('n1', 'n2')
  3. class fibonacci:
  4. """
  5. state State of
  6. n1 : nat
  7. n2 : nat
  8. init s == s = mk_State(0, 1)
  9. end
  10. operations
  11. next : () ==> nat
  12. next() == (dcl n : nat := n1 + n2; n1 := n2; n2 := n; return n)
  13. post RESULT = n1~ + n2~ and n1 = n2~ and n2 = RESULT;
  14. prev : () ==> nat
  15. prev() == (dcl n : nat := n2 - n1; n2 := n1; n1 := n; return n2)
  16. post n1 + n2 = n2~ and n2 = n1~ and n2 = RESULT;
  17. """
  18. def __init__(self):
  19. self.n1 = 0
  20. self.n2 = 1
  21. @vdm_method
  22. def next(self):
  23. pass
  24. @vdm_test
  25. def prev(self):
  26. n = self.n2 - self.n1
  27. self.n2 = self.n1
  28. self.n1 = n
  29. return self.n2

Here, a VDM-SL spec of fibonacci numbers is embedded as a docstring of the python fibonacci class.
The arguments of the @vdm_module decorator are state variables to be associated with instance variables of the python object.

The next method has no python implementation.
The @vdm_method decorator specifies that this method is animated by the VDM-SL spec.
In this particular case, next() in VDM-SL is evaluated and the resulting nat number is converted into python’s int value.

The prev method has a python implemenation.
The @vdm_test decorator specifies that invoking this method will automatically evaluate the VDM-SL spec along with the python method, and all state variables and resulting value is compared with python’s counterparts.

  1. >>> from fibonacci import fibonacci
  2. >>> f = fibonacci()
  3. >>> f.next()
  4. 1
  5. >>> f.next()
  6. 2
  7. >>> f.next()
  8. 3
  9. >>> f.next()
  10. 5
  11. >>> f.prev()
  12. 3
  13. >>> f.prev()
  14. 2

Enjoy!


This project is partly supported by Grant-in-Aid Scientific Research (C) 26330099