Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the most mettaful way to work with "global" variables? #63

Open
CICS-Oleg opened this issue Feb 4, 2025 · 1 comment
Open
Assignees
Labels
help wanted Extra attention is needed question Further information is requested tutorial Tutorial question

Comments

@CICS-Oleg
Copy link
Contributor

Suppose we have accounts that are associated with some users.

!(bind! &acc1 (new-state (usdt 100)))

(= (asset s1) &acc1)

We can change the number of assets by changing the state.

!(change-state! &acc1 (usdt 200))

Is this representation the right one for the described model?

Suppose we have to implement a loop for buying assets by all the users. Buying process can only be made by some fixed portions. Thus we need reasonable amount of iterations to satisy some critical value thresh that is simply a sum of all assets.

!(bind! &acc2 (new-state (usdt 300)))

(= (asset s2) &acc2)

!(bind! &quant (new-state (usdt 1)))

(= (buy $agent $quant)
    (let ($usdt $num) (asset $agent) $num 
    (change-state! (asset $agent) (usdt 200)) ...)
)

(= (buy_loop $threshold $agent_list)
    (if (<= (get_all_assets $agent_list) $threshold)
       (for_every_agent_in_agent_list (buy $agent))
       )
)

How to do that iterations in the most effective way?

@CICS-Oleg CICS-Oleg assigned CICS-Oleg, Necr0x0Der and vsbogd and unassigned CICS-Oleg Feb 4, 2025
@CICS-Oleg CICS-Oleg added help wanted Extra attention is needed question Further information is requested tutorial Tutorial question labels Feb 4, 2025
@Necr0x0Der
Copy link
Contributor

Well, as a minor comment, I'd propose to avoid introducing new tokens with bind! unless it is really desirable.
I'd propose to use

!(add-reduct &self
  (= (asset s1) (new-state (usdt 100)))

instead of

!(bind! &acc1 (new-state (usdt 100)))
(= (asset s1) &acc1)

but for some weird reason (see trueagi-io/hyperon-experimental#846 ) it doesn't work now. There are, however, other ways:

!(let $x (new-state (usdt 100))
   (add-atom &self
      (= (asset s1) $x)))

or to not wrapping 100 into usdt (it works unlike when it is wrapped into expression):

!(add-reduct &self
  (= (asset s1) (new-state 100))

I'd go with let option until the bug is fixed.

Another minor thing is that (let ($usdt $num) (asset $agent) will not work. You need (get-state (asset $agent)). Also, you may want using (let (usdt $num) .... That is usdt is not a variable. Now, it looks a little bit strange that you ignore whether it is usdt or not, and then changes the state into usdt : (change-state! (asset $agent) (usdt 200)).

I'd also note that whether to use states or add/remove atom is the question for deeper analysis. But states seem ok in your case if you are not querying them. However, I'm not sure I got the semantics of your main question precisely, so it depends.

Who is buying what? This code

(= (buy $agent $quant)
    (let ($usdt $num) (asset $agent) $num 
    (change-state! (asset $agent) (usdt 200)) ...)
)

seems unclear. Does agent spend some quantity of usdt, which should result in (- $num $quant) state? Or is it agent, who buys usdt, so it should be (+ $num $quant)?
In this loop:

(= (buy_loop $threshold $agent_list)
    (if (<= (get_all_assets $agent_list) $threshold)
       (for_every_agent_in_agent_list (buy $agent))
       )
)

the summed assets of agents in the list should be less than $threshold, right? What does it mean? Are these assets that agents have, so these assets are bought from the agents? And all the assets from all the agents should be bought? Or what? What does it mean (buy $agent)? Buy what? There is no $quant parameter. These are important details, because if you need just to reduce all the assents of all the agents to zero (???) or change them independently, then you can just do this independently. If operations over assets of one agent condition operations over other agent's assets, then you need to do folding over collapsed tuple of atoms (as in the previous issue). The latter is also true if you need to modify the same state (agent assets) in multiple nondeterministic branches. So, it would be nice to get an extended example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested tutorial Tutorial question
Projects
None yet
Development

No branches or pull requests

3 participants