3

Michelson specification declares the following rule for expansion of SET_CDR macro

SET_CDR @var %field => CAR %field ; PAIR @var

However, %field is supposed to be a field annotation of right element of a pair, but it's passed to the CAR which fetches the first one.

The same problem for SET_CAR.

It seems that this rule can be fixed like:

SET_CDR @var %field => CDR %field; SWAP; PAIR @var;

However, this approach loses a field annotation of the left element, but if I test the following script on the alphanet:

parameter unit;
storage unit;
code { DROP;
       PUSH int 1;
       PUSH int 2;
       PUSH int 3;
       PAIR %l %r;
       SET_CDR %r;
       DROP;
       UNIT; NIL operation; PAIR; };

I get

Well typed
Gas remaining: 399452 units remaining
{ parameter unit ;
  storage unit ;
  code { /* [ pair (unit @parameter) (unit @storage) ] */
         DROP
         /* [] */ ;
         PUSH int 1
         /* [ int ] */ ;
         PUSH int 2
         /* [ int : int ] */ ;
         PUSH int 3
         /* [ int : int : int ] */ ;
         PAIR %l %r
         /* [ pair (int %l) (int %r) : int ] */ ;
         SET_CDR %r
         /* [ pair (int %l @l) (int %r) ] */ ;
         DROP
         /* [] */ ;
         UNIT
         /* [ unit ] */ ;
         NIL operation
         /* [ list operation : unit ] */ ;
         PAIR
         /* [ pair (list operation) unit ] */ } }

So, the question is: how SET_CAR/SET_CDR should be expanded in pure Michelson instructions? Is it possible?

1

It appears that, in fact:

SET_CAR @var %field => { DUP; CAR %field; DROP; CDR @%%; SWAP; PAIR %field %@; }
SET_CDR @var %field => { DUP; CDR %field; DROP; CAR @%%; PAIR %@ %field; }

So, the @var is ignored (and indeed looks to be lost upon macro unexpansion).

You can see for yourself. One easy hack is to tezos-client -l typecheck script <script>. Due to the -l you will see (in the request to the typecheck_code RPC) the raw expanded code in JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.