forked from dfinity/motoko
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
motoko-san: add basic support for Nat
New features: * translate Nat to Int New test cases: * test/viper/nats.mo Future work: encode in Viper that Nats are non-negative.
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// @verify | ||
|
||
actor Nats { | ||
|
||
var x = 42 : Nat; | ||
|
||
public func getZero() : async Nat { | ||
return 0; | ||
}; | ||
|
||
public func getX() : async Nat { | ||
return x; | ||
}; | ||
|
||
public func double() : async () { | ||
x := x * 2; | ||
}; | ||
|
||
public func natToInt(n : Nat) : async Int { | ||
return n; | ||
}; | ||
|
||
public func add(n : Nat, m : Int) : async Int { | ||
return n + m; | ||
}; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Parse warning: In macro $Inv, the following parameters were defined but not used: $Self ([email protected]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
define $Perm($Self) ((true && acc(($Self).x,write))) | ||
define $Inv($Self) (true) | ||
method __init__($Self: Ref) | ||
|
||
requires $Perm($Self) | ||
ensures $Perm($Self) | ||
ensures $Inv($Self) | ||
{ | ||
($Self).x := 42 | ||
} | ||
field x: Int | ||
method getZero($Self: Ref) | ||
returns ($Res: Int) | ||
requires $Perm($Self) | ||
requires $Inv($Self) | ||
ensures $Perm($Self) | ||
ensures $Inv($Self) | ||
{ | ||
$Res := 0 | ||
goto $Ret | ||
label $Ret | ||
} | ||
method getX($Self: Ref) | ||
returns ($Res: Int) | ||
requires $Perm($Self) | ||
requires $Inv($Self) | ||
ensures $Perm($Self) | ||
ensures $Inv($Self) | ||
{ | ||
$Res := ($Self).x | ||
goto $Ret | ||
label $Ret | ||
} | ||
method double($Self: Ref) | ||
|
||
requires $Perm($Self) | ||
requires $Inv($Self) | ||
ensures $Perm($Self) | ||
ensures $Inv($Self) | ||
{ | ||
($Self).x := (($Self).x * 2) | ||
label $Ret | ||
} | ||
method natToInt($Self: Ref, n: Int) | ||
returns ($Res: Int) | ||
requires $Perm($Self) | ||
requires $Inv($Self) | ||
ensures $Perm($Self) | ||
ensures $Inv($Self) | ||
{ | ||
$Res := n | ||
goto $Ret | ||
label $Ret | ||
} | ||
method add($Self: Ref, n: Int, m: Int) | ||
returns ($Res: Int) | ||
requires $Perm($Self) | ||
requires $Inv($Self) | ||
ensures $Perm($Self) | ||
ensures $Inv($Self) | ||
{ | ||
$Res := (n + m) | ||
goto $Ret | ||
label $Ret | ||
} |