User loginNavigation |
Language outline of Modern Eiffel (SW Verification)Link to the original blog entry. In the course of this blog I am going to use a variant of Eiffel called Modern Eiffel is an object oriented language. All types are defined by
class
MY_CLASS -- class name
create
creator1(a:A) ...
creator2 ...
feature
some_function(b:B): RT ...
some_command(c:C) ...
all(i:INT) some_property(i)
proof
...
end
CHAR = CHARACTER -- type alias
invariant
... -- consistency conditions
end
Comments begin with "--" and span to the end of the line. There are some basic types. These are types with built-in functions and
immutable class
BOOLEAN
feature
implication alias "=>" (o:BOOLEAN): BOOLEAN
external "built_in" end
negated alias "not": BOOLEAN
external "built_in" end
conjuncted alias "and" (o:BOOLEAN): BOOLEAN
external "built_in" end
disjuncted alias "or" (o:BOOLEAN): BOOLEAN
external "built_in" end
...
end
Functions can have operator aliases for unary and binary operators. I.e. not a -- highest precedence a and b a or b a => b -- lowest precedence
have the expected meaning. The most important boolean operator is the A class can inherit features from other classes. For this blog entry
class
T
inherit
-> ANY
COMPARABLE
...
end
The symbol "->" stands for conforming inheritance. For the following
class
COMPARABLE
feature
is_equal(o:like Current): BOOLEAN
external "built_in"
ensure
all(x:like Current) x=x
all(x,y: like Current) x=y => y=x
all(x,y,z: like Current) x=y => y=z => x=z
-- "=" has higher precedence than "=>"
end
end
The feature "is_equal" is used in Modern Eiffel to define equality. It x=y => y=z => x=z Usually one would expect the transitivity law written as x=y and y=z => x=z
Both forms are equivalent. This will be proved later. If possible we Each class inheriting from COMPARABLE can redefine (i.e. overwrite) the People with experience in imperative languages like C++, C#, Java, etc. Inductive data types are best explained with some examples. The simplest
case class
COLOR
create
red
green
blue
end
The keyword "case" introduces an inductive class. The class COLOR has
r: COLOR := red
g: COLOR := green
...
The assigment operator ":=" can be used to initialize variables. In a similar fashion we can define a class WEEKDAY
case class
WEEKDAY
create
monday
thuesday
wednesday
...
sunday
feature
next: WEEKDAY
...
end
The class WEEKDAY has a feature "next" in order to calculate the next
next: WEEKDAY =
inspect Current
case monday then thuesday
case thuesday then wednesday
...
case sunday then monday
end
Inductive types allow case expression to make distinctions on how the The above form is the short form of a function definition. The function
next: WEEKDAY
require
... -- precondition(s)
ensure
Result =
inspect Current
case monday then thuesday
...
end
end
The keyword "Result" indicates the return value of the function. The Objects of the very simple inductive types like COLOR and WEEKDAY just Let us consider the following class
case class
OPTIONAL[G]
create
none
value(value: G)
end
This class is generic. A generic class is a type constructor. It can The class has just two creators: "none" and "value". The object n: OPTIONAL[INT] := none has no value and the object i7: OPTIONAL[INT] := value(7)
has the value 7. The name "value" is used for the creator and for an The access of the attribute "value" is illegal, if the corresponding In order to introduce pattern matching we use a contrived example of a
value_plus_100(o:OPTIONAL[INT]) =
inspect o
case none then -1
case value(i) then i+100
end
The function "value_plus_100 is contrived, because one would not write The first case clause is for objects created with the "none" creator. The second case clause is for objects which have been created with the The function "value_plus_100" can be written in a form which accesses
value_plus_100(o:OPTIONAL[INT]) =
inspect o
case none then -1
case value(_) then o.value + 100
end
The wild card "_" is used to supress the creations of a fresh local The full expressive power of inductive types unfolds with recursively
case class
UNARY_NATURAL
create
zero
succ(pred: UNARY_NATURAL)
feature
plus alias "+" (o:UNARY_NATURAL): UNARY_NATURAL = ...
times alias "*" (o:UNARY_NATURAL): UNARY_NATURAL = ...
power alias "^" (o:UNARY_NATURAL): UNARY_NATURAL = ...
...
end
The class UNARY_NATURAL is highly inefficient to do arithmetics. But it The class UNARY_NATURAL has tow creators. One to create the number 0 and The recursive structure of the type can be used to define the functions
plus alias "+" (o:UNARY_NATURAL): UNARY_NATURAL =
inspect Current
case zero then o
case succ(p) then succ(p+o)
end
times alias "*" (o:UNARY_NATURAL): UNARY_NATURAL =
inspect Current
case zero then zero
case succ(p) then o + p*o
end
power alias "^" (o:UNARY_NATURAL): UNARY_NATURAL =
inspect o
case zero then succ(zero)
case succ(p) then Current * Current^p
end
Recursive functions for inductive types defined in this fashion have the case succ(p) then succ(p+o)
This branch is entered only if the current object has been created with succ(p+o)
uses the predecessor to make the recursive call "p+o", i.e. it calls the Another classic inductive type is defined by the class LIST.
case class
LIST[G]
create
nil
cons alias "::" (first:G; tail:LIST[G])
feature
...
end
A list is either empty or is an element (the first element) followed by If a creator has two arguments, a binary operator can be used as an lst: LIST[INT] := 1::2::3::nil -- parsed as 1::(2::(3::nil)) -- equivalent to cons(1,cons(2,(cons(3,nil)))
Since lists are very expressive on its own and can serve as models an This blog has introduced some basic language elements of Modern Eiffel. Keep in mind that all sorts of mutable structures and imperative By hbrandl at 2012-01-25 13:57 | LtU Forum | previous forum topic | next forum topic | other blogs | 4612 reads
|
Browse archives
Active forum topics |
Recent comments
3 days 4 hours ago
3 days 5 hours ago
3 days 5 hours ago
3 weeks 3 days ago
4 weeks 2 days ago
4 weeks 2 days ago
4 weeks 3 days ago
4 weeks 3 days ago
4 weeks 4 days ago
4 weeks 6 days ago