-
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.
Merge pull request #7 from anirudhgray/refactor-contracts
Refactor contracts
- Loading branch information
Showing
17 changed files
with
204 additions
and
23 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
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
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
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
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
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
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 |
---|---|---|
@@ -1,11 +1,18 @@ | ||
scoop "larder/math"; | ||
|
||
// Random float between 0 and 1 | ||
fun random() { | ||
return _random(); | ||
fun random() | ||
postcondition: 0 <= result, result <= 1 | ||
{ | ||
var result = _random(); | ||
return result; | ||
} | ||
|
||
// Random integer between min and max | ||
fun randomInt(min, max) { | ||
return floor(random() * (max - min + 1) + min); | ||
fun randomInt(min, max) | ||
precondition: min <= max | ||
postcondition: min <= result, result <= max | ||
{ | ||
var result = floor(random() * (max - min + 1) + min); | ||
return result; | ||
} |
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,31 @@ | ||
fun safeSqrt(value) | ||
precondition: value >= 0, "value must be greater than or equal to 0" | ||
postcondition: x >= 0, "x must be greater than or equal to 0" | ||
{ | ||
var x = value; | ||
var tolerance = 0.00001; // Define a tolerance level for the approximation | ||
var difference = x; | ||
|
||
while (difference > tolerance) { | ||
var newX = 0.5 * (x + value / x); | ||
difference = x - newX; | ||
if (difference < 0) { | ||
difference = -difference; | ||
} | ||
x = newX; | ||
} | ||
|
||
return x; | ||
} | ||
|
||
var x = 25; | ||
var y = safeSqrt(x); // y will be approximately 5 | ||
print y; | ||
|
||
safeSqrt(-9); // This will trigger a precondition error since z < 0 | ||
print result; | ||
|
||
// 5 | ||
// RuntimeError: Precondition failed: value must be greater than or equal to 0 | ||
// [at line 1 in advcontract2.tah] in <fn safeSqrt> | ||
// [called at line 25 in advcontract2.tah] |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
var check = false; | ||
assertion: check, "Check should be true!"; | ||
var checkvar = false; | ||
assertion: checkvar, "Check should be true!"; | ||
|
||
// RuntimeError: assertion contract failed (Check should be true!) | ||
// [at line 2 in assert_outside.tah] |
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,9 @@ | ||
scoop "larder/math" into math; | ||
check: math::max(4, 5) == 54; | ||
var recipeName = "Muffins"; | ||
var sugar = 50; | ||
assertion: recipeName != nil, "Recipe name cannot be nil!"; | ||
check: sugar < 40, "Sugar might be too much."; | ||
|
||
// Warning (null) [check.tah:2] | ||
// Warning (Sugar might be too much.) [check.tah:6] |
Oops, something went wrong.