From: Pierre-Alexandre Voye <ontologiae@gmail.com>
To: caml-list <caml-list@inria.fr>
Subject: [Caml-list] Trouble with subtyping
Date: Thu, 7 Apr 2011 18:48:45 +0200 [thread overview]
Message-ID: <BANLkTi=7a9+hjgYEDHD3Kbj2krmR9B7dGQ@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 5526 bytes --]
Hi all, I'm working on a lib to modelize a Multi-Agent System where each
agent are driven by a Hierarchical Finite State Machine (HFSM). Hierarchical
means that each state could contain a HFSM which is runned when the machine
come into the state.
Transitions are made by evaluating condition and events which arrives in a
mail inbox.
The goal is to define dynamically all HFSM we want, just by defining states
and transitions
This little grammar represents all possibilities of mix of condition and
event handling
type ('myAgentType, 'event) transition =
Condition of ('myAgentType -> bool)
| Event of ('event -> bool)
| EventOr of ('event -> bool) * ('myAgentType,'event) transition
| EventAnd of ('event -> bool) * ('myAgentType,'event) transition
| EventXor of ('event -> bool) * ('myAgentType,'event) transition
| EventNot of ('event -> bool)
| ConditionOr of ('myAgentType -> bool) * ('myAgentType,'event)
transition
| ConditionAnd of ('myAgentType -> bool) * ('myAgentType,'event)
transition
| ConditionXor of ('myAgentType -> bool) * ('myAgentType,'event)
transition
| ConditionNot of ('myAgentType -> bool);;
I have two classes : agent and state
"state" manage current state and is able to search, according to the agent
given in argument, what is the following state.
So state have a transition list which is (state * ('myAgentType, 'event)
transition ) list (see before).
in this list, state is the (candidate) next state, and ('myAgentType,
'event) transition the expression which allows to validate or not the
transition.
'event will always be the same object, but I want 'myAgentType to be every
object which inherit from agent.
The agent have a current state, and a 'cycle' method which searchs what is
the next state of the agent according to its (the current state) transition
list
You use it like it :
let mydwarf = new agent;;
let (ctdwarf:(dwarf,masEvent) transition) = Condition (fun (e:agent) ->
true);;
let aa = new state mydwarf;;
let aaa = new state mydwarf;;
aaa#setParent aa;;
let aab = new state mydwarf;;
aab#isIn aa;;
aaa#addTransition aab (Condition (fun (e:agent) -> true));;
mydwarf#setStartState aa;;
So we have defined an agent mydwarf which have a start state "aa"
here a simplified prototype of agent and state class :
class ['myAgentType] state :
'myAgentType ->
object
constraint 'myAgentType =
< getCurrentState : < getNom : string; .. > ... >;
val mutable action : 'myAgentType -> unit
val mutable begin_action : 'myAgentType -> unit
val mutable end_action : 'myAgentType -> unit
val mutable transitions :
('myAgentType state option * ('myAgentType, masEvent) transition
option) list
method searchTransition :
'myAgentType -> bool * 'myAgentType state * 'myAgentType state *
'myAgentType state * 'myAgentType state
method has : 'myAgentType state -> unit
method isTransitionValide : bool
method setAction : ('myAgentType -> unit) -> unit
method setBeginAction : ('myAgentType -> unit) -> unit
method setEndAction : ('myAgentType -> unit) -> unit
method setTransitions :
('myAgentType state option * ('myAgentType, masEvent) transition
option) list -> unit
end
class agent :
object
val mutable birthtime : int
val mutable currentState : agent state option
val mutable gid : int
val mutable id : int
val mutable emailStack : masEvent option list
val mutable pv : int
val mutable state_time : int
method cycle : agent -> unit
method getCurrentState : agent state
method setStartState : agent state -> unit
method setState : agent state -> agent state -> agent state -> agent ->
unit
end
Now, all the logic works fine.
My big problem, is I'm unable to make it working with a class which inherit
from agent. I'm forced to copy all the code for each type of agent I want.
For instance, if i define :
class inheritedDwarf = object (self)
inherit agent
val mutable currentState = ( None : inheritedDwarf state option)
end;;
it works.
But if I define :
class inheritedDwarf = object (self)
inherit agent
val mutable currentState = ( None : inheritedDwarf state option)
val specialValue=0
method getSpecialValue = specialValue
end;;
From the toplevel I get :
Error: The abbreviation inheritedDwarf expands to type
< cycle : dwarf -> unit; getCurrentState : dwarf state;
getMail : masEvent -> unit; getPileEmail : masEvent option list;
getPv : int; getSpecialValue : int;
receiveEMail : masEvent option -> unit; removeOldEmail : unit;
setPileEmail : masEvent option list -> unit; setPv : int -> unit;
setStartState : dwarf state -> unit;
setState : dwarf state -> dwarf state -> dwarf state -> dwarf ->
unit >
but is used with type
< cycle : dwarf -> unit; getCurrentState : dwarf state;
getMail : masEvent -> unit; getPileEmail : masEvent option list;
getPv : int; receiveEMail : masEvent option -> unit;
removeOldEmail : unit; setPileEmail : masEvent option list -> unit;
setPv : int -> unit; setStartState : dwarf state -> unit;
setState : dwarf state -> dwarf state -> dwarf state -> dwarf ->
unit >
So, it doesn't want to accept currentState to become a inheritedDwarf state
option and thus, I can't use my inheritedDwarf
Is there a solution ?
Thans for your help,
Regards,
Pierre-Alexandre
--
---------------------
Isaac Project - http://www.lisaac.org/
[-- Attachment #2: Type: text/html, Size: 6796 bytes --]
next reply other threads:[~2011-04-07 16:49 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-07 16:48 Pierre-Alexandre Voye [this message]
2011-04-07 21:44 ` Guillaume Yziquel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='BANLkTi=7a9+hjgYEDHD3Kbj2krmR9B7dGQ@mail.gmail.com' \
--to=ontologiae@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox