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

motoko-san: arguments and return values #1

Merged
merged 1 commit into from
Apr 8, 2024
Merged

Conversation

int-index
Copy link
Member

@int-index int-index commented Apr 4, 2024

Basic support for Int and Bool arguments and return values, incl. support for early return from a function.

The following example seems to work as expected:

example.mo

// @verify

actor Counter {
  var count = 0 : Int;

  public func increment() : async () {
    count += 1;
  };

  public func getZero() : async Int {
    if (true) {
      return 0;
    };
    return 1;
  };

  public func getMax(n : Int) : async Int {
    if (n > count) {
      return n;
    };
    return count;
  };

  public func getCount() : async Int {
    return count;
  };

  public func setCount(n : Int) : async () {
    count := n;
  };
};

It translates to the following Viper specification
example.vpr

define $Perm($Self) ((true && acc(($Self).count,write)))
define $Inv($Self) (true)
method __init__($Self: Ref)
    
    requires $Perm($Self)
    ensures $Perm($Self)
    ensures $Inv($Self)
    { 
      ($Self).count := 0 
    }
field count: Int
method increment($Self: Ref)
    
    requires $Perm($Self)
    requires $Inv($Self)
    ensures $Perm($Self)
    ensures $Inv($Self)
    { 
      ($Self).count := (($Self).count + 1)
      label $Ret 
    }
method getZero($Self: Ref)
     returns ($Res: Int)
    requires $Perm($Self)
    requires $Inv($Self)
    ensures $Perm($Self)
    ensures $Inv($Self)
    { 
      if (true)
         { 
           $Res := 0
           goto $Ret 
         }
      $Res := 1
      goto $Ret
      label $Ret 
    }
method getMax($Self: Ref, n: Int)
     returns ($Res: Int)
    requires $Perm($Self)
    requires $Inv($Self)
    ensures $Perm($Self)
    ensures $Inv($Self)
    { 
      if ((n > ($Self).count))
         { 
           $Res := n
           goto $Ret 
         }
      $Res := ($Self).count
      goto $Ret
      label $Ret 
    }
method getCount($Self: Ref)
     returns ($Res: Int)
    requires $Perm($Self)
    requires $Inv($Self)
    ensures $Perm($Self)
    ensures $Inv($Self)
    { 
      $Res := ($Self).count
      goto $Ret
      label $Ret 
    }
method setCount($Self: Ref, n: Int)
    
    requires $Perm($Self)
    requires $Inv($Self)
    ensures $Perm($Self)
    ensures $Inv($Self)
    { 
      ($Self).count := n
      label $Ret 
    }

Copy link

@GoPavel GoPavel left a comment

Choose a reason for hiding this comment

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

LGTM except one thing: This changes is not supported several arguments
I would suggest to support it by:

and args p = match p.it with
  | M.TupP ps ->
    List.concat (List.map args ps)
  | M.ParP p -> args p

@int-index int-index changed the title viper: int arguments and return values motoko-san: arguments and return values Apr 6, 2024
@int-index int-index requested a review from GoPavel April 6, 2024 16:27
New features:
* arguments and return values of primitive types (Int, Bool)
* multiple function arguments
* early return from functions

New test cases:
* test/viper/simple-funs.mo
* test/viper/counter.mo
@int-index int-index merged commit 1b071eb into master Apr 8, 2024
3 of 5 checks passed
@delete-merged-branch delete-merged-branch bot deleted the motoko-san/args-rets branch April 8, 2024 11:18
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.

2 participants