Skip to content

Commit 17b2e26

Browse files
committed
Implement streamScanl in Coq, and set up the translation to use
it instead of the SAWCore implementation.
1 parent 10b55c5 commit 17b2e26

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

saw-core-coq/coq/generated/CryptolToCoq/CryptolPrimitivesForSAWCore.v

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Import ListNotations.
1010
(** Post-preamble section specified by you *)
1111
From CryptolToCoq Require Import SAWCorePrelude.
1212
Import SAWCorePrelude.
13+
From CryptolToCoq Require Import SAWCorePreludeExtra.
1314

1415

1516
(** Code generated by saw-core-coq *)
@@ -684,7 +685,7 @@ Definition ecFoldlPrime : forall (n : Num), forall (a : Type), forall (b : Type)
684685
fun (n : Num) (a : Type) (b : Type) {Inh_b : SAWCoreScaffolding.Inhabited b} (pa : RecordTypeCons "eq" (a -> a -> SAWCoreScaffolding.Bool) RecordTypeNil) => ecFoldl n a b.
685686

686687
Definition ecScanl : forall (n : Num), forall (a : Type), forall (b : Type), (a -> b -> a) -> a -> seq n b -> seq (tcAdd (TCNum 1) n) a :=
687-
fun (n : Num) (a : Type) (b : Type) (f : a -> b -> a) (z : a) => CryptolPrimitivesForSAWCore.Num_rect (fun (n1 : Num) => seq n1 b -> seq (tcAdd (TCNum 1) n1) a) (fun (n1 : SAWCoreScaffolding.Nat) (xs : SAWCoreVectorsAsCoqVectors.Vec n1 b) => SAWCoreVectorsAsCoqVectors.scanl b a n1 f z xs) (fun (xs : SAWCorePrelude.Stream b) => SAWCorePrelude.MkStream a (SAWCorePrelude.Nat__rec (fun (i : SAWCoreScaffolding.Nat) => a) z (fun (i : SAWCoreScaffolding.Nat) (x : a) => f x (SAWCorePrelude.streamGet b xs i)))) n.
688+
fun (n : Num) (a : Type) (b : Type) (f : a -> b -> a) (z : a) => CryptolPrimitivesForSAWCore.Num_rect (fun (n1 : Num) => seq n1 b -> seq (tcAdd (TCNum 1) n1) a) (fun (n1 : SAWCoreScaffolding.Nat) (xs : SAWCoreVectorsAsCoqVectors.Vec n1 b) => SAWCoreVectorsAsCoqVectors.scanl b a n1 f z xs) (fun (xs : SAWCorePrelude.Stream b) => SAWCorePreludeExtra.streamScanl b a f z xs) n.
688689

689690
Definition TCFloat : Num -> Num -> Type :=
690691
fun (_1 : Num) (_2 : Num) => unit : Type.

saw-core-coq/coq/generated/CryptolToCoq/SAWCorePrelude.v

+6
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ Definition streamShiftL : forall (a : Type), Stream a -> SAWCoreScaffolding.Nat
830830
Definition streamShiftR : forall (a : Type), a -> Stream a -> SAWCoreScaffolding.Nat -> Stream a :=
831831
fun (a : Type) (z : a) (xs : Stream a) (i : SAWCoreScaffolding.Nat) => streamAppend a i (replicate i a z) xs.
832832

833+
(* Prelude.streamScanl was skipped *)
834+
833835
(* Prelude.Integer was skipped *)
834836

835837
(* Prelude.intAdd was skipped *)
@@ -952,6 +954,10 @@ Definition foldW64List : unfoldedW64List -> W64List :=
952954
either (unit : Type) (prod (@sigT (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (fun (_1 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => unit : Type)) (prod W64List (unit : Type))) W64List (fun (_1 : unit : Type) => W64Nil) (fun (bv_l : prod (@sigT (SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) (fun (_1 : SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool) => unit : Type)) (prod W64List (unit : Type))) => let var__0 := SAWCoreVectorsAsCoqVectors.Vec 64 SAWCoreScaffolding.Bool in
953955
W64Cons (@projT1 var__0 (fun (_1 : var__0) => unit : Type) (SAWCoreScaffolding.fst bv_l)) (SAWCoreScaffolding.fst (SAWCoreScaffolding.snd bv_l))).
954956

957+
(* Prelude.unsafeAssertBVULt was skipped *)
958+
959+
(* Prelude.unsafeAssertBVULe was skipped *)
960+
955961
Axiom bvEqWithProof : forall (n : SAWCoreScaffolding.Nat), forall (v1 : SAWCoreVectorsAsCoqVectors.Vec n SAWCoreScaffolding.Bool), forall (v2 : SAWCoreVectorsAsCoqVectors.Vec n SAWCoreScaffolding.Bool), Maybe (SAWCoreScaffolding.Eq (SAWCoreVectorsAsCoqVectors.Vec n SAWCoreScaffolding.Bool) v1 v2) .
956962

957963
Definition bvultWithProof : forall (n : SAWCoreScaffolding.Nat), forall (v1 : SAWCoreVectorsAsCoqVectors.Vec n SAWCoreScaffolding.Bool), forall (v2 : SAWCoreVectorsAsCoqVectors.Vec n SAWCoreScaffolding.Bool), Maybe (SAWCoreScaffolding.Eq SAWCoreScaffolding.Bool (SAWCoreVectorsAsCoqVectors.bvult n v1 v2) SAWCoreScaffolding.true) :=

saw-core-coq/coq/handwritten/CryptolToCoq/SAWCorePreludeExtra.v

+22
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ Proof.
4747
apply addNat_add.
4848
Defined.
4949

50+
Definition streamScanl (a b : sort 0) (f : b -> a -> b) (z:b) (xs:Stream a) : Stream b :=
51+
MkStream b
52+
(fix strm (n:nat) : b :=
53+
match n with
54+
| O => z
55+
| S n' => f (strm n') (streamGet a xs n')
56+
end).
57+
58+
Lemma streamScanl_zero a b f z xs : streamGet b (streamScanl a b f z xs) 0 = z.
59+
Proof.
60+
reflexivity.
61+
Qed.
62+
63+
Lemma streamScanl_succ a b f z xs : forall n,
64+
streamGet b (streamScanl a b f z xs) (S n) =
65+
f (streamGet b (streamScanl a b f z xs) n)
66+
(streamGet a xs n).
67+
Proof.
68+
intro n. reflexivity.
69+
Qed.
70+
71+
5072
Theorem fold_unfold_IRT As Ds D : forall x, foldIRT As Ds D (unfoldIRT As Ds D x) = x.
5173
Proof.
5274
induction x; simpl; unfold uncurry; f_equal; try easy.

saw-core-coq/src/Verifier/SAW/Translation/Coq/SpecialTreatment.hs

+8
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ cryptolPrimitivesModule = mkModuleName ["CryptolPrimitivesForSAWCore"]
179179
sawCoreScaffoldingModule :: ModuleName
180180
sawCoreScaffoldingModule = mkModuleName ["SAWCoreScaffolding"]
181181

182+
preludeExtraModule :: ModuleName
183+
preludeExtraModule = mkModuleName ["SAWCorePreludeExtra"]
184+
182185
cryptolPreludeSpecialTreatmentMap :: Map.Map String IdentSpecialTreatment
183186
cryptolPreludeSpecialTreatmentMap = Map.fromList $ []
184187

@@ -359,6 +362,11 @@ sawCorePreludeSpecialTreatmentMap configuration =
359362
, ("Vec", mapsTo vectorsModule "Vec")
360363
]
361364

365+
-- Streams
366+
++
367+
[ ("streamScanl", mapsTo preludeExtraModule "streamScanl")
368+
]
369+
362370
-- Integers
363371
++
364372
[ ("Integer", mapsTo sawDefinitionsModule "Integer")

saw-core/prelude/Prelude.sawcore

+3
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,9 @@ streamShiftR a z xs i = streamAppend a i (replicate i a z) xs;
13451345
-- implementation that just iterates "f" separate for each index would waste work.
13461346
-- The fixpoint is well founded because each element only refers to elements with
13471347
-- smaller indices.
1348+
--
1349+
-- For Coq support, this definition is replaced with a naive implementation
1350+
-- that performs independent iterations for each location.
13481351
streamScanl : (a b : sort 0) -> (b -> a -> b) -> b -> Stream a -> Stream b;
13491352
streamScanl a b f z as =
13501353
fix (Stream b)

src/SAWScript/Prover/Exporter.hs

+10
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,14 @@ withImportSAWCorePrelude config@(Coq.TranslationConfiguration { Coq.postPreamble
410410
]
411411
}
412412

413+
withImportSAWCorePreludeExtra :: Coq.TranslationConfiguration -> Coq.TranslationConfiguration
414+
withImportSAWCorePreludeExtra config@(Coq.TranslationConfiguration { Coq.postPreamble }) =
415+
config { Coq.postPreamble = postPreamble ++ unlines
416+
[ "From CryptolToCoq Require Import SAWCorePreludeExtra."
417+
]
418+
}
419+
420+
413421
withImportCryptolPrimitivesForSAWCore ::
414422
Coq.TranslationConfiguration -> Coq.TranslationConfiguration
415423
withImportCryptolPrimitivesForSAWCore config@(Coq.TranslationConfiguration { Coq.postPreamble }) =
@@ -478,6 +486,7 @@ writeCoqCryptolModule inputFile outputFile notations skips = io $ do
478486
let configuration =
479487
withImportCryptolPrimitivesForSAWCoreExtra $
480488
withImportCryptolPrimitivesForSAWCore $
489+
withImportSAWCorePreludeExtra $
481490
withImportSAWCorePrelude $
482491
coqTranslationConfiguration notations skips
483492
let nm = takeBaseName inputFile
@@ -518,6 +527,7 @@ writeCoqCryptolPrimitivesForSAWCore outputFile notations skips = do
518527
() <- scLoadModule sc (emptyModule (mkModuleName ["CryptolPrimitivesForSAWCore"]))
519528
m <- scFindModule sc nameOfCryptolPrimitivesForSAWCoreModule
520529
let configuration =
530+
withImportSAWCorePreludeExtra $
521531
withImportSAWCorePrelude $
522532
coqTranslationConfiguration notations skips
523533
let doc = Coq.translateSAWModule configuration m

0 commit comments

Comments
 (0)