-
Notifications
You must be signed in to change notification settings - Fork 126
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 #1200 from GaloisInc/wip/rpc/feat/newtype
initial newtype support for rpc server
- Loading branch information
Showing
18 changed files
with
658 additions
and
293 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any | ||
|
||
class OpaqueValue(): | ||
"""A value from the Cryptol server which cannot be directly represented and/or | ||
marshalled to an RPC client. | ||
Note that as far as Python is concerned these values are only equal to | ||
themselves. If a richer notion of equality is required then the server should | ||
be consulted to compute the result.""" | ||
unique : int | ||
identifier : str | ||
|
||
def __init__(self, unique : int, identifier : str) -> None: | ||
self.unique = unique | ||
self.identifier = identifier | ||
|
||
def __str__(self) -> str: | ||
return self.identifier | ||
|
||
def __repr__(self) -> str: | ||
return f"Opaque({self.unique!r}, {self.identifier!r})" | ||
|
||
def __eq__(self, other : Any) -> bool: | ||
if not isinstance(other, OpaqueValue): | ||
return False | ||
else: | ||
return self.unique == other.unique and self.identifier == other.identifier | ||
|
||
def __hash__(self) -> int: | ||
return hash((self.unique, self.identifier)) |
50 changes: 50 additions & 0 deletions
50
cryptol-remote-api/python/tests/cryptol/test-files/CplxQNewtype.cry
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,50 @@ | ||
// This is a development of rational complex numbers | ||
|
||
type Q = Rational | ||
|
||
fortyTwo : Q | ||
fortyTwo = 42 | ||
|
||
// Complex rational numbers in rectangular coordinates | ||
newtype CplxQ = | ||
{ real : Q, imag : Q } | ||
|
||
embedQ : Q -> CplxQ | ||
embedQ x = CplxQ{ real = x, imag = 0 } | ||
|
||
cplxTwo : CplxQ | ||
cplxTwo = embedQ 2 | ||
|
||
cplxForty : CplxQ | ||
cplxForty = embedQ 40 | ||
|
||
cplxFortyTwo : CplxQ | ||
cplxFortyTwo = embedQ 42 | ||
|
||
|
||
cplxAdd : CplxQ -> CplxQ -> CplxQ | ||
cplxAdd x y = CplxQ { real = r, imag = i } | ||
where | ||
r = x.real + y.real | ||
i = x.imag + y.imag | ||
|
||
cplxMul : CplxQ -> CplxQ -> CplxQ | ||
cplxMul x y = CplxQ { real = r, imag = i } | ||
where | ||
r = x.real * y.real - x.imag * y.imag | ||
i = x.real * y.imag + x.imag * y.real | ||
|
||
cplxEq : CplxQ -> CplxQ -> Bit | ||
cplxEq x y = (x.real == y.real) && (x.imag == y.imag) | ||
|
||
property cplxAddAssoc x y z = | ||
cplxEq (cplxAdd (cplxAdd x y) z) | ||
(cplxAdd x (cplxAdd y z)) | ||
|
||
property cplxMulAssoc x y z = | ||
cplxEq (cplxMul (cplxMul x y) z) | ||
(cplxMul x (cplxMul y z)) | ||
|
||
property cplxMulDistrib x y z = | ||
cplxEq (cplxMul x (cplxAdd y z)) | ||
(cplxAdd (cplxMul x y) (cplxMul x z)) |
33 changes: 33 additions & 0 deletions
33
cryptol-remote-api/python/tests/cryptol/test_CplxQNewtype.py
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,33 @@ | ||
import unittest | ||
from pathlib import Path | ||
import unittest | ||
import cryptol | ||
from cryptol.bitvector import BV | ||
|
||
|
||
class TestCplxQ(unittest.TestCase): | ||
def test_CplxQ(self): | ||
c = cryptol.connect(reset_server=True) | ||
c.load_file(str(Path('tests','cryptol','test-files', 'CplxQNewtype.cry'))) | ||
|
||
forty_two = c.eval("fortyTwo").result() | ||
cplx_forty_two1 = c.call("embedQ", forty_two).result() | ||
cplx_forty_two2 = c.eval("CplxQ{ real = 42, imag = 0 }").result() | ||
cplx_two = c.eval("cplxTwo").result() | ||
cplx_forty = c.eval("cplxForty").result() | ||
cplx_forty_two3 = c.call("cplxAdd", cplx_two, cplx_forty).result() | ||
cplx_forty_two4 = c.eval("cplxMul (CplxQ{ real = 21, imag = 0 }) (CplxQ{ real = 2, imag = 0 })").result() | ||
cplx_forty_two5 = c.eval("cplxAdd (CplxQ{ real = 41, imag = 0 }) (CplxQ{ real = 1, imag = 0 })").result() | ||
cplx_forty_two6 = c.eval("CplxQ{ real = 42, imag = 0 }").result() | ||
|
||
self.assertTrue(c.call("cplxEq", cplx_forty_two1, cplx_forty_two2).result()) | ||
self.assertFalse(c.call("cplxEq", cplx_two, cplx_forty_two2).result()) | ||
self.assertTrue(c.call("cplxEq", cplx_forty_two1, cplx_forty_two3).result()) | ||
self.assertTrue(c.call("cplxEq", cplx_forty_two1, cplx_forty_two4).result()) | ||
self.assertTrue(c.call("cplxEq", cplx_forty_two1, cplx_forty_two5).result()) | ||
self.assertTrue(c.call("cplxEq", cplx_forty_two1, cplx_forty_two6).result()) | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Oops, something went wrong.