python-agentspeak icon indicating copy to clipboard operation
python-agentspeak copied to clipboard

[DOC] add lists as part of a belief by constructing a literal with a tuple representing the list

Open ccarstens opened this issue 6 years ago • 4 comments

A belief containing a list as an argument can be constructed by passing tuples representing the list to the Literal constructor. e.g

my_literal = Literal("my_functor", ((12, 45), ))

is equivalent to the literal within ASL

my_functor([12, 45])

ccarstens avatar Sep 19 '19 20:09 ccarstens

That's correct. The embedding is as follows:

Python Agentspeak
int, float number
string string
tuple, agentspeak.LinkedList list
agentspeak.Literal literal
agentspeak.Var, agentspeak.Wildcard variable
any other object* opaque

*) That means it's valid to create a Literal("foo", (MyObject(), )). On the AgentSpeak side MyObject is completely opaque, but it can be used in unification and passed to Python actions that might be able to do something with it.

niklasf avatar Sep 19 '19 21:09 niklasf

About agentspeak.LinkedList, I saw that the constructor takes head and tail as arguments.

LinkedList([1, 2, 3], [4, 5, 6])

results in

[
    [1, 2, 3],
    [4, 5, 6]
]

and not in

[1, 2, 3, 4, 5, 6]

*) That means it's valid to create a Literal("foo", (MyObject(), )). On the AgentSpeak side MyObject is completely opaque, but it can be used in unification and passed to Python actions that might be able to do something with it.

That is incredibly good to know! Thank you! :)

ccarstens avatar Sep 19 '19 21:09 ccarstens

Avoid LinkedList, if you can.

It is only required to represent the [Head|Tail] syntax from AgentSpeak. We cannot use tuples for that, because Tail matches a list of unknown length.

For example:

>>> [Head|Tail] = [1,2,3,4].
Head = 1
Tail = [2, 3, 4]

The proper way to construct a LinkedList would be:

LinkedList(1, (2, 3, 4))
LinkedList(1, LinkedList(2, (3, 4)))
LinkedList(1, LinkedList(2, LinkedList(3, (4, ))))

niklasf avatar Sep 19 '19 22:09 niklasf

Oh okay, I see. I guess using tuples and knowing that they'll be cast to agent speak lists does the job! :)

ccarstens avatar Sep 19 '19 22:09 ccarstens