Wiki

Changes between Initial Version and Version 1 of Contracts

Show
Ignore:
Timestamp:
03/20/10 12:35:52 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Contracts

    v1 v1  
     1= Contracts =  
     2 
     3Cobra supports contracts which are expressed through lists of assertion expressions  
     4(must-evaluate-true) attached to objects and methods (and properties).[[BR]]  
     5They act as documentation, specification and run-time assertions  
     6and should be viewed as part of the public interface of the items they  
     7are associated with. 
     8 
     9When a contract condition fails, an exception is raised.[[BR]] 
     10The exception message shows the value of each subexpression in the condition, just like the Cobra 
     11'assert' exception. 
     12  For example, 'x > y' failing will show the values of 'x' and 'y'. 
     13 
     14You can turn off contracts when building shipping software in order to boost 
     15run-time performance and/or reduce executable size via the -contracts compiler switch.  
     16 
     17== Keywords == 
     18Class instance invariants are specified as part of the class definition and use the 
     19keyword '''invariant'''. 
     20 
     21Code member preconditions are specified on the code member and use the keyword '''require'''. 
     22 
     23Code member postconditions are also specified on the code member and use the keyword '''ensure'''. 
     24 
     25== Implicit variables related to contracts == 
     26To make writing contracts easier some implicitly declared variables are available for use in expressions 
     27within contracts: 
     28 
     29    * '''result''' - available in '''require''' and '''ensure''', 
     30      provides a fixed name for the return value of any method or property 
     31 
     32    * '''old'''    -  avail in '''ensure''',  
     33      provides an object reference that captures the original state of the object before the method/property executed.  
     34      Use to validate a state change. 
     35 
     36In addition there is an additional expression construct 
     37    * '''implies''' - available in all contracts. 
     38      allows an assertion to be triggered only if a condition is true.  
     39      Grammar is  
     40        <conditional-expr> implies <assertion-expr> 
     41 
     42    e.g.  
     43{{{  
     44     result.count implies result[0].length # non 0-length list item 
     45     old.value implies old.value < .value  # value non zero and increased 
     46}}} 
     47 
     48== Preconditions == 
     49Preconditions (requirements) are assertion expressions (expressions must evaluate true) 
     50which can be placed on methods and properties.[[BR]] 
     51They check or constrain the state of the current object and/or the state and values of any arguments. 
     52 
     53Keyword is '''require''' 
     54 
     55e.g. 
     56    {{{ 
     57    def sqroot(i as int) as float 
     58        require  
     59           i > 0 
     60        body 
     61            ... 
     62 
     63    def fraction( numer as int, denom as int) as float 
     64        require 
     65            numer > 0 
     66            denom <> 0 
     67        body 
     68          ... 
     69    }}} 
     70 
     71== Postconditions == 
     72Post conditions (guarantees) are also assertion expressions (must evaluate true) 
     73and can also be placed on methods and properties.[[BR]]  
     74They check/assert/assure the state of the current object or intended state change 
     75and/or the state and value of any result.  
     76 
     77Keyword is '''ensure''' 
     78 
     79e.g. 
     80   {{{ 
     81    def sqroot(i as int) as float 
     82        ensure  
     83           result > 0 
     84        body 
     85            ... 
     86 
     87    def bumpState( incr as int) as int 
     88        require 
     89            incr > 0 
     90        ensure  
     91            result >= incr 
     92            .state = old.state + incr  
     93        body 
     94          .state += incr 
     95          return .state 
     96    }}} 
     97 
     98== Object State == 
     99 
     100The 'invariant' keyword leads a clause within a class definition and is  
     101followed by a list of assertion expressions (must evaluate true).[[BR]] 
     102These are executed at the end of each code member (initialiser, method, property, etc.).  
     103They speak to the state of the object. 
     104 
     105Keyword is '''invariant''' 
     106 
     107 
     108== Other == 
     109 
     110Assertions can be sprinkled through the bodies of code members to 
     111verify intermediate state or values of local variables. 
     112 
     113e.g 
     114{{{ 
     115    ... 
     116    .new = .old + incr 
     117    assert .new > .old 
     118}}} 
     119 
     120== Inheritance == 
     121When code member contracts are inherited (and overridden),  
     122the preconditions/requirements ('''require''') can be augmented through an 
     123    '''or require'''  
     124clause meaning that the derivedClass is more flexible about what conditions  
     125the work can be done in. 
     126 
     127Similarly postconditions ('''ensure''') can be strengthened with an 
     128    '''and ensure'''  
     129clause meaning that the derived class can guarantee additional conditions  
     130about the results of the work. 
     131 
     132 
     133== Compiler Command Line == 
     134The -contracts commandline switch controls contracts code  generation:  
     135values are 
     136    * none      - suppress contracts code generation entirely 
     137    * inline (default) - place contracts code inline to the item the contract is associated with 
     138    * method - place contracts code in a separate method and provide a method call from the associated item 
     139         
     140Turn off contracts with   
     141    cobra -contracts:none ... 
     142 
     143== Grammar == 
     144{{{ 
     145require  
     146    BODY 
     147 
     148ensure  
     149    BODY 
     150 
     151invariant  
     152    BODY 
     153}}} 
     154 
     155 
     156If a contract has only one condition, you can write it 
     157on one line and skip 'body'. 
     158{{{ 
     159require EXPR 
     160ensure  EXPR 
     161invariant EXPR 
     162}}} 
     163 
     164 
     165== Examples == 
     166 
     167see !HowTo file [http://cobra-language.com/how-to/DeclareContracts/ How To Declare Contracts] 
     168 
     169