Skip to content

Commit 3667b04

Browse files
committed
Add a special case to the translation of cryptol comprehensions
when we can easily tell that the length of the branches are the same. This avoids unneccessary coercions. Fixes #1507
1 parent 1c70236 commit 3667b04

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

cryptol-saw-core/saw/Cryptol.sawcore

+10
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,16 @@ seqZip a b m n =
403403
n)
404404
m;
405405

406+
zipSame : (a b : isort 0) -> (n : Nat) -> Vec n a -> Vec n b -> Vec n (a * b);
407+
zipSame a b n x y = gen n (a*b) (\ (i : Nat) -> (at n a x i, at n b y i));
408+
409+
seqZipSame : (a b : isort 0) -> (n : Num) -> seq n a -> seq n b -> seq n (a * b);
410+
seqZipSame a b n =
411+
Num#rec
412+
(\ (n : Num) -> seq n a -> seq n b -> seq n (a * b))
413+
(\ (n : Nat) -> zipSame a b n)
414+
(streamMap2 a b (a*b) (\ (x:a) -> \ (y:b) -> (x,y)))
415+
n;
406416

407417
--------------------------------------------------------------------------------
408418
-- Ring and Logic functions

cryptol-saw-core/src/Verifier/SAW/Cryptol.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1483,10 +1483,14 @@ importComp sc env lenT elemT expr mss =
14831483
m <- importType sc env len
14841484
a <- importType sc env ty
14851485
(ys, n, b, argss, len') <- zipAll branches
1486-
zs <- scGlobalApply sc "Cryptol.seqZip" [a, b, m, n, xs, ys]
1487-
mn <- scGlobalApply sc "Cryptol.tcMin" [m, n]
14881486
ab <- scTupleType sc [a, b]
1489-
return (zs, mn, ab, args : argss, C.tMin len len')
1487+
if len == len' then
1488+
do zs <- scGlobalApply sc "Cryptol.seqZipSame" [a, b, m, xs, ys]
1489+
return (zs, m, ab, args : argss, len)
1490+
else
1491+
do zs <- scGlobalApply sc "Cryptol.seqZip" [a, b, m, n, xs, ys]
1492+
mn <- scGlobalApply sc "Cryptol.tcMin" [m, n]
1493+
return (zs, mn, ab, args : argss, C.tMin len len')
14901494
(xs, n, a, argss, lenT') <- zipAll mss
14911495
f <- lambdaTuples sc env elemT expr argss
14921496
b <- importType sc env elemT

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

+6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ Definition mlet : forall (a : Type), forall {Inh_a : SAWCoreScaffolding.Inhabite
182182
Definition seqZip : forall (a : Type), forall {Inh_a : SAWCoreScaffolding.Inhabited a}, forall (b : Type), forall {Inh_b : SAWCoreScaffolding.Inhabited b}, forall (m : Num), forall (n : Num), seq m a -> seq n b -> seq (tcMin m n) (prod a b) :=
183183
fun (a : Type) {Inh_a : SAWCoreScaffolding.Inhabited a} (b : Type) {Inh_b : SAWCoreScaffolding.Inhabited b} (m : Num) (n : Num) => CryptolPrimitivesForSAWCore.Num_rect (fun (m1 : Num) => seq m1 a -> seq n b -> seq (tcMin m1 n) (prod a b)) (fun (m1 : SAWCoreScaffolding.Nat) => CryptolPrimitivesForSAWCore.Num_rect (fun (n1 : Num) => SAWCoreVectorsAsCoqVectors.Vec m1 a -> seq n1 b -> seq (tcMin (TCNum m1) n1) (prod a b)) (fun (n1 : SAWCoreScaffolding.Nat) => SAWCorePrelude.zip a b m1 n1) (fun (xs : SAWCoreVectorsAsCoqVectors.Vec m1 a) (ys : SAWCorePrelude.Stream b) => SAWCoreVectorsAsCoqVectors.gen m1 (prod a b) (fun (i : SAWCoreScaffolding.Nat) => pair (SAWCorePrelude.sawAt m1 a xs i) (SAWCorePrelude.streamGet b ys i))) n) (CryptolPrimitivesForSAWCore.Num_rect (fun (n1 : Num) => SAWCorePrelude.Stream a -> seq n1 b -> seq (tcMin TCInf n1) (prod a b)) (fun (n1 : SAWCoreScaffolding.Nat) (xs : SAWCorePrelude.Stream a) (ys : SAWCoreVectorsAsCoqVectors.Vec n1 b) => SAWCoreVectorsAsCoqVectors.gen n1 (prod a b) (fun (i : SAWCoreScaffolding.Nat) => pair (SAWCorePrelude.streamGet a xs i) (SAWCorePrelude.sawAt n1 b ys i))) (SAWCorePrelude.streamMap2 a b (prod a b) (fun (x : a) (y : b) => pair x y)) n) m.
184184

185+
Definition zipSame : forall (a : Type), forall {Inh_a : SAWCoreScaffolding.Inhabited a}, forall (b : Type), forall {Inh_b : SAWCoreScaffolding.Inhabited b}, forall (n : SAWCoreScaffolding.Nat), SAWCoreVectorsAsCoqVectors.Vec n a -> SAWCoreVectorsAsCoqVectors.Vec n b -> SAWCoreVectorsAsCoqVectors.Vec n (prod a b) :=
186+
fun (a : Type) {Inh_a : SAWCoreScaffolding.Inhabited a} (b : Type) {Inh_b : SAWCoreScaffolding.Inhabited b} (n : SAWCoreScaffolding.Nat) (x : SAWCoreVectorsAsCoqVectors.Vec n a) (y : SAWCoreVectorsAsCoqVectors.Vec n b) => SAWCoreVectorsAsCoqVectors.gen n (prod a b) (fun (i : SAWCoreScaffolding.Nat) => pair (SAWCorePrelude.sawAt n a x i) (SAWCorePrelude.sawAt n b y i)).
187+
188+
Definition seqZipSame : forall (a : Type), forall {Inh_a : SAWCoreScaffolding.Inhabited a}, forall (b : Type), forall {Inh_b : SAWCoreScaffolding.Inhabited b}, forall (n : Num), seq n a -> seq n b -> seq n (prod a b) :=
189+
fun (a : Type) {Inh_a : SAWCoreScaffolding.Inhabited a} (b : Type) {Inh_b : SAWCoreScaffolding.Inhabited b} (n : Num) => CryptolPrimitivesForSAWCore.Num_rect (fun (n1 : Num) => seq n1 a -> seq n1 b -> seq n1 (prod a b)) (fun (n1 : SAWCoreScaffolding.Nat) => zipSame a b n1) (SAWCorePrelude.streamMap2 a b (prod a b) (fun (x : a) (y : b) => pair x y)) n.
190+
185191
Definition seqBinary : forall (n : Num), forall (a : Type), forall {Inh_a : SAWCoreScaffolding.Inhabited a}, (a -> a -> a) -> seq n a -> seq n a -> seq n a :=
186192
fun (num : Num) (a : Type) {Inh_a : SAWCoreScaffolding.Inhabited a} (f : a -> a -> a) => CryptolPrimitivesForSAWCore.Num_rect (fun (n : Num) => seq n a -> seq n a -> seq n a) (fun (n : SAWCoreScaffolding.Nat) => SAWCorePrelude.zipWith a a a f n) (SAWCorePrelude.streamMap2 a a a f) num.
187193

0 commit comments

Comments
 (0)