Skip to content

Commit 7a3442d

Browse files
author
Eddy Westbrook
committed
switched to using intToBv for writing bitvector literals instead of bvLit (as the two had the same definition anyway); also changed bitvector literals to use signed integers, and switched to using the bv-sized library to do this translation instead of rolling our own; all of this in support of #25
1 parent cf889e7 commit 7a3442d

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

coq/handwritten/CryptolToCoq/SAWCoreVectorsAsCoqVectors.v

+1-3
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,12 @@ Fixpoint bvNat (size : Nat) (number : Nat) : bitvector size :=
194194

195195
(* Arguments bvNat : simpl never. *)
196196

197-
(* Use this to write decimal number literals of bitvector type, e.g. bvLit 64 3 *)
198-
Definition bvLit (size : Nat) (lit : Z) : bitvector size := bitsToBv (fromZ lit).
199-
200197
Definition bvToNatFolder (n : nat) (b : bool) := b + n.*2.
201198

202199
Fixpoint bvToNat (size : Nat) (v : bitvector size) : Nat :=
203200
Vector.fold_left bvToNatFolder 0 v.
204201

202+
(* This is used to write literals of bitvector type, e.g. intToBv 64 3 *)
205203
Definition intToBv (n : Nat) (z : Z) : bitvector n := bitsToBv (fromZ z).
206204
Opaque intToBv.
207205

saw-core-coq.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ library
2424
lens,
2525
mtl,
2626
saw-core,
27+
parameterized-utils,
28+
bv-sized,
2729
vector
2830
hs-source-dirs: src
2931
exposed-modules:

src/Verifier/SAW/Translation/Coq/Term.hs

+12-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
{-# LANGUAGE RecordWildCards #-}
1313
{-# LANGUAGE TemplateHaskell #-}
1414
{-# LANGUAGE ViewPatterns #-}
15+
{-# LANGUAGE TypeApplications #-}
16+
{-# LANGUAGE DataKinds #-}
17+
{-# LANGUAGE PatternGuards #-}
1518

1619
{- |
1720
Module : Verifier.SAW.Translation.Coq
@@ -34,8 +37,10 @@ import Data.Maybe (fromMaybe)
3437
import Prelude hiding (fail)
3538
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>))
3639

37-
import Data.Bits ((.|.), bit)
38-
import qualified Data.Vector as Vector (reverse, imap)
40+
import Data.Parameterized.Pair
41+
import Data.Parameterized.NatRepr
42+
import qualified Data.BitVector.Sized as BV
43+
import qualified Data.Vector as Vector (reverse, toList)
3944
import qualified Language.Coq.AST as Coq
4045
import qualified Language.Coq.Pretty as Coq
4146
import Verifier.SAW.Recognizer
@@ -188,12 +193,11 @@ flatTermFToExpr tf = -- traceFTermF "flatTermFToExpr" tf $
188193
Coq.App rect_var <$> mapM translateTerm args
189194
Sort s -> pure (Coq.Sort (translateSort s))
190195
NatLit i -> pure (Coq.NatLit (toInteger i))
191-
ArrayValue (asBoolType -> Just ()) (traverse asBool -> Just bits) -> do
192-
return (Coq.App (Coq.Var "bvLit")
193-
[Coq.NatLit (toInteger $ length bits),
194-
Coq.ZLit (foldl (.|.) 0 $
195-
Vector.imap (\i b -> if b then bit i else 0) $
196-
Vector.reverse bits)])
196+
ArrayValue (asBoolType -> Just ()) (traverse asBool -> Just bits)
197+
| Pair w bv <- BV.bitsBE (Vector.toList bits)
198+
, Left LeqProof <- decideLeq (knownNat @1) w -> do
199+
return (Coq.App (Coq.Var "intToBv")
200+
[Coq.NatLit (intValue w), Coq.ZLit (BV.asSigned w bv)])
197201
ArrayValue _ vec -> do
198202
let addElement accum element = do
199203
elementTerm <- translateTerm element

0 commit comments

Comments
 (0)