Difference between revisions of "Agent"

From Liberty Eiffel Wiki
Jump to navigation Jump to search
m
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
In Eiffel, the notion of agent makes it possible to describe and manipulate computation parts (i.e. operations) like ordinary data. Operations may be partially described, may be passed as ordinary data and may have their execution delayed. Agents are very convenient for many purposes, such as going through data structures and implementing call-backs in graphical libraries.
 
In Eiffel, the notion of agent makes it possible to describe and manipulate computation parts (i.e. operations) like ordinary data. Operations may be partially described, may be passed as ordinary data and may have their execution delayed. Agents are very convenient for many purposes, such as going through data structures and implementing call-backs in graphical libraries.
 
See http://www.jot.fm/issues/issue_2004_04/article7/ — some parts are reused here.
 
   
 
== Overview ==
 
== Overview ==
Line 7: Line 5:
 
The agent mechanism gives the Eiffel object-oriented language the ability to handle operations, or commands, as such, like in functional programming languages.
 
The agent mechanism gives the Eiffel object-oriented language the ability to handle operations, or commands, as such, like in functional programming languages.
   
Agents are a new type of objects that allow to store code to be executed and data in an object, named the agent. In Eiffel, four types of agents exist: an abstract (deferred) type ROUTINE, and three concrete types PROCEDURE, FUNCTION and PREDICATE. The following figure shows their inheritance relationship:
+
Agents are a new type of objects that allow to store the code to be executed together with its data into an object, named the agent. In Eiffel, four types of agents exist: a deferred type [[library_class:ROUTINE|<tt>ROUTINE</tt>]], and three concrete types [[library_class:PROCEDURE|<tt>PROCEDURE</tt>]], [[library_class:FUNCTION|<tt>FUNCTION</tt>]] and [[library_class:PREDICATE|<tt>PREDICATE</tt>]].
  +
  +
The following figure shows their inheritance relationship:
   
 
[[File:Agents-fig1.png]]
 
[[File:Agents-fig1.png]]
   
Agents are a way of storing operations for later execution. They are objects. As such, they can be stored, compared to Void, or passed around to other software components. The operation stored in the agent may then be executed whenever the component decides. The most common uses of agents comprise delayed calls, multiple calls (on different values), lazy evaluation, and so on.
+
Agents are a way of storing operations for later execution. They are objects. As such, they can be stored, compared to <tt>Void</tt>, or passed around to other software components. The operation stored in the agent may then be executed whenever the component decides to do so. The most common uses of agents are delayed calls, multiple calls (on different values), lazy evaluation, and so on.
   
  +
== Conformance rules ==
  +
  +
The [[library_class:ROUTINE|<tt>ROUTINE</tt>]] types are generic types with more semantic. As [[library_class:ROUTINE|<tt>ROUTINE</tt>]] types do not have their own conformance rules one may think that the generic types rules apply. We will hold true this assumption in this chapter, and show that we can make dogs eat tomatoes.
  +
  +
The [[library_class:ROUTINE|<tt>ROUTINE</tt>]] type is a generic type with one formal type parameter: [[library_class:ROUTINE|<tt>ROUTINE</tt>]]<tt>[OPEN -> </tt>[[library_class:TUPLE|<tt>TUPLE</tt>]]<tt>]</tt>. A type [[library_class:ROUTINE|<tt>ROUTINE</tt>]]<tt>[O1]</tt> conforms to a type [[library_class:ROUTINE|<tt>ROUTINE</tt>]]<tt>[O2]</tt> only if <tt>O2</tt> conforms to <tt>O1</tt>.
  +
  +
The [[library_class:PROCEDURE|<tt>PROCEDURE</tt>]] type inherits from [[library_class:ROUTINE|<tt>ROUTINE</tt>]], and has the same formal type parameter: [[library_class:PROCEDURE|<tt>PROCEDURE</tt>]]<tt>[OPEN –> </tt>[[library_class:TUPLE|<tt>TUPLE</tt>]]<tt>]</tt>. Its conformance rule is thus the same as for [[library_class:ROUTINE|<tt>ROUTINE</tt>]].
  +
  +
The [[library_class:FUNCTION|<tt>FUNCTION</tt>]] type inherits from [[library_class:ROUTINE|<tt>ROUTINE</tt>]], and has two formal type parameters: [[library_class:FUNCTION|<tt>FUNCTION</tt>]]<tt>[OPEN –> </tt>[[library_class:TUPLE|<tt>TUPLE</tt>]]<tt>, RESULT_TYPE]</tt>. According to the conformance rule for generic types, a type [[library_class:FUNCTION|<tt>FUNCTION</tt>]]<tt>[O1, R1]</tt> conforms to a type [[library_class:FUNCTION|<tt>FUNCTION</tt>]]<tt>[O2, R2]</tt> only if <tt>O2</tt> conforms to <tt>O1</tt> and <tt>R1</tt> conforms to <tt>R2</tt>.
  +
  +
[[library_class:PREDICATE|<tt>PREDICATE</tt>]]<tt>[O]</tt> type being just a shortcut for [[library_class:FUNCTION|<tt>FUNCTION</tt>]]<tt>[O, </tt>[[library_class:BOOLEAN|<tt>BOOLEAN</tt>]]<tt>]</tt>, and it has the same conformance rules as [[library_class:FUNCTION|<tt>FUNCTION</tt>]].
  +
  +
'''Note:''' You must be aware of the conformance inversion for the open arguments [[library_class:TUPLE|tuple]]. See http://www.jot.fm/issues/issue_2004_04/article7/ for the rationale.
   
 
== Inline agents ==
 
== Inline agents ==
  +
  +
''> '''NB:''' The following will apply from the Bell release onwards. In Adler, inline agents are just anonymous features.''
   
 
Inline agents are closures: they may access the arguments and local variables defined in their outside feature (called ''outside variables'').
 
Inline agents are closures: they may access the arguments and local variables defined in their outside feature (called ''outside variables'').
Line 26: Line 41:
 
* beware of automatically allocated pointers for outside local variables, especially if the GC is not used. There is no such problem for arguments since their value cannot change throughout the execution of the outside feature
 
* beware of automatically allocated pointers for outside local variables, especially if the GC is not used. There is no such problem for arguments since their value cannot change throughout the execution of the outside feature
 
* expanded arguments are twinned in the inline agent context (of course)
 
* expanded arguments are twinned in the inline agent context (of course)
  +
  +
== Example: agent iterating over a range ==
  +
  +
class EULERO
  +
--
  +
-- Project Euler Question 1
  +
-- https://projecteuler.net/problem=1
  +
--
  +
  +
create {ANY}
  +
make
  +
  +
feature {ANY}
  +
low: INTEGER 1
  +
  +
high: INTEGER 999
  +
  +
make
  +
do
  +
io.put_string("Sum of all naturals divisible by 3 or 5 lower than 1000 is: ")
  +
io.put_line((low |..| high).aggregate(
  +
agent (progressive, i: INTEGER): INTEGER
  +
do
  +
if i.divisible(3) or i.divisible(5) then
  +
Result := progressive + i
  +
else
  +
Result := progressive
  +
end
  +
end (?, ?), 0).out)
  +
end
  +
  +
end -- class EULERO

Latest revision as of 17:38, 17 June 2016

In Eiffel, the notion of agent makes it possible to describe and manipulate computation parts (i.e. operations) like ordinary data. Operations may be partially described, may be passed as ordinary data and may have their execution delayed. Agents are very convenient for many purposes, such as going through data structures and implementing call-backs in graphical libraries.

Overview

The agent mechanism gives the Eiffel object-oriented language the ability to handle operations, or commands, as such, like in functional programming languages.

Agents are a new type of objects that allow to store the code to be executed together with its data into an object, named the agent. In Eiffel, four types of agents exist: a deferred type ROUTINE, and three concrete types PROCEDURE, FUNCTION and PREDICATE.

The following figure shows their inheritance relationship:

Agents-fig1.png

Agents are a way of storing operations for later execution. They are objects. As such, they can be stored, compared to Void, or passed around to other software components. The operation stored in the agent may then be executed whenever the component decides to do so. The most common uses of agents are delayed calls, multiple calls (on different values), lazy evaluation, and so on.

Conformance rules

The ROUTINE types are generic types with more semantic. As ROUTINE types do not have their own conformance rules one may think that the generic types rules apply. We will hold true this assumption in this chapter, and show that we can make dogs eat tomatoes.

The ROUTINE type is a generic type with one formal type parameter: ROUTINE[OPEN -> TUPLE]. A type ROUTINE[O1] conforms to a type ROUTINE[O2] only if O2 conforms to O1.

The PROCEDURE type inherits from ROUTINE, and has the same formal type parameter: PROCEDURE[OPEN –> TUPLE]. Its conformance rule is thus the same as for ROUTINE.

The FUNCTION type inherits from ROUTINE, and has two formal type parameters: FUNCTION[OPEN –> TUPLE, RESULT_TYPE]. According to the conformance rule for generic types, a type FUNCTION[O1, R1] conforms to a type FUNCTION[O2, R2] only if O2 conforms to O1 and R1 conforms to R2.

PREDICATE[O] type being just a shortcut for FUNCTION[O, BOOLEAN], and it has the same conformance rules as FUNCTION.

Note: You must be aware of the conformance inversion for the open arguments tuple. See http://www.jot.fm/issues/issue_2004_04/article7/ for the rationale.

Inline agents

> NB: The following will apply from the Bell release onwards. In Adler, inline agents are just anonymous features.

Inline agents are closures: they may access the arguments and local variables defined in their outside feature (called outside variables).

The usage limits are:

  • names must be unique (i.e. shadowing an outside variable is not allowed)
  • Result is not accessible to inline agents; one must use another local variable (rationale: if the agent is a function, what would Result refer to?)
  • access is read-only: you cannot assign to an outside local variable (and, of course, to an argument, but that's standard Eiffel anyway)

The technical limits are:

  • beware of automatically allocated pointers for outside local variables, especially if the GC is not used. There is no such problem for arguments since their value cannot change throughout the execution of the outside feature
  • expanded arguments are twinned in the inline agent context (of course)

Example: agent iterating over a range

 class EULERO
    --
    -- Project Euler Question 1
    -- https://projecteuler.net/problem=1
    --
 
 create {ANY}
    make
 
 feature {ANY}
    low: INTEGER 1
 
    high: INTEGER 999
 
    make
       do
          io.put_string("Sum of all naturals divisible by 3 or 5 lower than 1000 is: ")
          io.put_line((low |..| high).aggregate(
             agent (progressive, i: INTEGER): INTEGER
                do
                   if i.divisible(3) or i.divisible(5) then
                      Result := progressive + i
                   else
                      Result := progressive
                   end
                end (?, ?), 0).out)
       end
 
 end -- class EULERO