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

Support toSignedInteger and deepseq in the reference evaluator #1784

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
were incorrectly computed.
([#1773](https://github.com/GaloisInc/cryptol/issues/1773))

* The reference evaluator now evaluates the `toSignedInteger` and `deepseq`
primitives instead of panicking.

## New features

* REPL command `:dumptests <FILE> <EXPR>` updated to write to stdout when
Expand Down
34 changes: 33 additions & 1 deletion src/Cryptol/Eval/Reference.lhs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,28 @@ Operations on Values
> where
> g (Nat n) = f n
> g Inf = evalPanic "vFinPoly" ["Expected finite numeric type"]

>
> -- | Reduce a value to normal form.
> forceValue :: Value -> E ()
> forceValue v =
> case v of
> -- Values where the field is already is normal form
> VBit{} -> pure ()
> VInteger{} -> pure ()
> VRational{} -> pure ()
> VFloat{} -> pure ()
> -- Values with fields containing other values to reduce to normal form
> VList _ xs -> forceValues xs
> VTuple xs -> forceValues xs
> VRecord fs -> forceValues $ map snd fs
> VEnum _ xs -> forceValues xs
> -- Lambdas and other abstractions are already in normal form
> VFun{} -> pure ()
> VPoly{} -> pure ()
> VNumPoly{} -> pure ()
> where
> forceValues :: [E Value] -> E ()
> forceValues = mapM_ (\x -> forceValue =<< x)

Environments
------------
Expand Down Expand Up @@ -758,6 +779,10 @@ by corresponding type classes:
> , "lg2" ~> vFinPoly $ \n -> pure $
> VFun $ \v ->
> vWord n <$> appOp1 lg2Wrap (fromVWord =<< v)
> , "toSignedInteger"
> ~> vFinPoly $ \_n -> pure $
> VFun $ \x ->
> VInteger <$> (fromSignedVWord =<< x)
> -- Rational
> , "ratio" ~> VFun $ \l -> pure $
> VFun $ \r ->
Expand Down Expand Up @@ -944,6 +969,13 @@ by corresponding type classes:
> -- executes parmap sequentially
> pure $ VList n (map f' xs')
>
> , "deepseq" ~> VPoly $ \_a -> pure $
> VPoly $ \_b -> pure $
> VFun $ \x -> pure $
> VFun $ \y ->
> do forceValue =<< x
> y
>
> , "error" ~> VPoly $ \_a -> pure $
> VNumPoly $ \_ -> pure $
> VFun $ \s ->
Expand Down
17 changes: 17 additions & 0 deletions tests/issues/issue1249.icry
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// toSignedInteger

toSignedInteger (0 : [64])
:eval toSignedInteger (0 : [64])
toSignedInteger (1 : [64])
:eval toSignedInteger (1 : [64])
toSignedInteger (2^^63 - 1 : [64])
:eval toSignedInteger (2^^63 - 1 : [64])
toSignedInteger (-1 : [64])
:eval toSignedInteger (-1 : [64])
toSignedInteger (-(2^^63) : [64])
:eval toSignedInteger (-(2^^63) : [64])

// deepseq

deepseq ((), undefined`{()}) True
:eval deepseq ((), undefined`{()}) True
18 changes: 18 additions & 0 deletions tests/issues/issue1249.icry.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Loading module Cryptol
0
0
1
1
9223372036854775807
9223372036854775807
-1
-1
-9223372036854775808
-9223372036854775808

Run-time error: undefined
-- Backtrace --
Cryptol::error called at Cryptol:1062:13--1062:18
Cryptol::undefined called at issue1249.icry:16:14--16:23
Cryptol::deepseq called at issue1249.icry:16:1--16:8
Run-time error: undefined
Loading