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

[DMS-3,9] Motoko san/arrays #5

Closed
wants to merge 16 commits into from
Closed

[DMS-3,9] Motoko san/arrays #5

wants to merge 16 commits into from

Conversation

GoPavel
Copy link

@GoPavel GoPavel commented Apr 23, 2024

No description provided.

@@ -121,11 +156,17 @@ let rec extract_concurrency (seq : seqn) : stmt' list * seqn =
let conc, stmts = List.fold_left extr ([], []) stmts in
rev conc, { seq with it = fst seq.it, rev stmts }

let prelude =
let (!!) p = !!! Source.no_region p in
[ !! (ImportI (!! "../../src/viper/lib/array.vpr")) ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is temporary, right? We need to decide how to ship the viper lib with moc (probably ask the Dfinity team)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's temporary. But I think it should be in this shape.
We will discuss with Dfinity team

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tweaked path since it wasn't working for the test runner.
Now lib/ should be next to .mo file.

and dec ctxt d =
let (!!) p = !!! (d.at) p in
match d.it with
(* TODO AnnotE could be nested *)
| M.VarD (x, {it=M.AnnotE ({it=M.ArrayE (mut, es); note; _}, _); _})
| M.LetD ({it=M.VarP x;_}, {it=M.AnnotE ({it=M.ArrayE (mut, es); note; _}, _); _}, None) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to suggest moving this logic to assign_stmt, but I see you have a TODO for that

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -90,8 +92,21 @@ and stmt' =

and typ = (typ', info) Source.annotated_phrase


and mut = Const | Mut
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this flag for future use? Looks like the translation ignores it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually used in array utility functions. Check array_acc.
I use it for identifying which kind of an access predicate should be used. Namely ArrayT Mut requires write access into each location so it should be array_acc_var which is a macro for forall ... acc(loc(a, i).val, write)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I did it only for convenience. We can eliminate it and pass this information as a separate parameter into utility functions. But I put it this way.

])
| M.WhileE(e, s1) ->
let (invs, s1') = extract_loop_invariants s1 in
let invs' = List.map (fun inv -> exp ctxt inv) invs in
!!([],
[ !!(WhileS(exp ctxt e, invs', stmt ctxt s1')) ])
(* TODO: remove by Motoko->Motoko transformation *)
Copy link
Member

@int-index int-index Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give an example of the Motoko->Motoko transformation that you want?

As I understand it, this clause deals with arr := [1,2,3]. What should it be transformed to?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr := [1, 2, 3] could be transformed into

let t1 := [1, 2, 3]
arr := t1

So all array literal will be allocated once and have fresh name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that arr := [...] could not produced just by array_alloc. We have to use temporary variable t1.
Otherwise two assignment could produce inconsistent:

arr := []
arr := [1]

translated (without temporary declaration) to:

inhale len(arr) == 0 && array_acc_mut(arr)
exhale array_acc_mut(arr) // we cannot exhale len(arr) == 0
inhale len(arr) == 1 && array_acc_mut(arr)
assert 1 == 0

reassignment arr := t1 solve this problem

@@ -485,6 +571,9 @@ and exp ctxt e =
!!(BoolLitE b)
| M.IntLit i ->
!!(IntLitE i)
(* TODO: support Nat type *)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add [DMS-34] reference in the comment.

@GoPavel GoPavel changed the title Motoko san/arrays [DMS-3,9] Motoko san/arrays Apr 24, 2024
@@ -542,4 +633,6 @@ and tr_typ' typ =
| T.Prim T.Int -> IntT
| T.Prim T.Nat -> IntT (* Viper has no native support for Nat, so translate to Int *)
| T.Prim T.Bool -> BoolT
| T.Array (T.Mut typ) -> ArrayT (Mut , tr_typ' (T.normalize typ))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call to T.normalize looks redundant, because tr_typ' already does normalization.

@int-index
Copy link
Member

Looks like the build is still failing due to path-related issues

error: builder for '/nix/store/sb8fp5h5xnal5z6gaca8wzss2zadl15j-test-viper.drv' failed with exit code 2;
       last 25 log lines:
       > +++ private.silicon.ret (actual)
       > @@ -0,0 +1 @@
       > +Return code 1
       > --- private.silicon (expected)
       > +++ private.silicon (actual)
       > @@ -0,0 +1 @@
       > +  [0] Parse error: file "/build/test-viper-src/viper/_out/viper_lib/array.vpr" does not exist (private.vpr@1.1)
       > simple-funs:ln: failed to create symbolic link '_out/viper_lib': File exists
       >  [tc] [vpr] [silicon]
       > --- simple-funs.silicon.ret (expected)
       > +++ simple-funs.silicon.ret (actual)
       > @@ -0,0 +1 @@
       > +Return code 1
       > --- simple-funs.silicon (expected)
       > +++ simple-funs.silicon (actual)
       > @@ -1,2 +1 @@
       > -Parse warning: In macro $Perm, the following parameters were defined but not used: $Self  (array.vpr@2.1)
       > -Parse warning: In macro $Inv, the following parameters were defined but not used: $Self  (array.vpr@3.1)
       > +  [0] Parse error: file "/build/test-viper-src/viper/_out/viper_lib/array.vpr" does not exist (simple-funs.vpr@1.1)
       > unsupported:ln: failed to create symbolic link '_out/viper_lib': File exists
       >  [tc] [vpr]
       > Some tests failed:
       > array.mo assertions.mo async.mo claim-broken.mo claim-reward-naive.mo claim-simple.mo claim.mo counter.mo invariant.mo loop-invariant.mo method-call.mo nats.mo private.mo simple-funs.mo
       > make: *** [Makefile:5: all] Error 1
       > make: Leaving directory '/build/test-viper-src/viper'
       For full logs, run 'nix log /nix/store/sb8fp5h5xnal5z6gaca8wzss2zadl15j-test-viper.drv'.

I can confirm locally, nix-build does not work. Let's try a different approach and simply inline the array code without any imports. I'll do that.

and typ' =
| IntT
| BoolT
| RefT
| ArrayT of mut * typ'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having thought about this some more, I don't think this is right. This is supposed to be the Viper type, and the Array domain defined in array.vpr is not parameterized by anything. So this type should be simply ArrayT (no of)


and unit_decl' ctxt (d : M.dec_field') at =
let self_id = !!! (Source.no_region) "$Self" in
let self_var at = !!! at (LocalVar (!!! at self_id.it, !!! at RefT)) in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the preexisting self helper function to get the self-var from the ctxt?

@int-index
Copy link
Member

OK, I've addressed my own comments and submitted #8.

@GoPavel
Copy link
Author

GoPavel commented Apr 30, 2024

see newer version of this PR in the #8

@GoPavel GoPavel closed this Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants