niklasf / python-agentspeak

A Python-based interpreter for the agent-oriented programming language JASON
GNU General Public License v3.0
48 stars 24 forks source link

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

Open ccarstens opened 5 years ago

ccarstens commented 5 years ago

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])
niklasf commented 5 years ago

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.

ccarstens commented 5 years ago

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! :)

niklasf commented 5 years ago

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, ))))
ccarstens commented 5 years ago

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