@@ -35,6 +35,8 @@ import Text.URI
35
35
36
36
import qualified Cryptol.Eval.Type as TV
37
37
import qualified Cryptol.Backend.Monad as V
38
+ import qualified Cryptol.Backend.SeqMap as V
39
+ import qualified Cryptol.Backend.WordValue as V
38
40
import qualified Cryptol.Eval.Value as V
39
41
import qualified Cryptol.Eval.Concrete as V
40
42
import Cryptol.Eval.Type (evalValType )
@@ -45,6 +47,7 @@ import qualified Cryptol.ModuleSystem.Name as C
45
47
import qualified Cryptol.Utils.Ident as C
46
48
( Ident , PrimIdent (.. ), mkIdent , prelPrim , floatPrim , arrayPrim
47
49
, ModName , modNameToText , identText , interactiveName
50
+ , ModPath (.. ), modPathSplit
48
51
)
49
52
import qualified Cryptol.Utils.RecordMap as C
50
53
import Cryptol.TypeCheck.TypeOf (fastTypeOf , fastSchemaOf )
@@ -740,7 +743,8 @@ prelPrims =
740
743
741
744
-- -- Sequences primitives
742
745
, (" #" , flip scGlobalDef " Cryptol.ecCat" ) -- {a,b,d} (fin a) => [a] d -> [b] d -> [a + b] d
743
- , (" splitAt" , flip scGlobalDef " Cryptol.ecSplitAt" ) -- {a,b,c} (fin a) => [a+b] c -> ([a]c,[b]c)
746
+ , (" take" , flip scGlobalDef " Cryptol.ecTake" ) -- {front, back, a} [front + back]a -> [front]a
747
+ , (" drop" , flip scGlobalDef " Cryptol.ecDrop" ) -- {front, back, a} (fin front) => [front + back]a -> [back]a
744
748
, (" join" , flip scGlobalDef " Cryptol.ecJoin" ) -- {a,b,c} (fin b) => [a][b]c -> [a * b]c
745
749
, (" split" , flip scGlobalDef " Cryptol.ecSplit" ) -- {a,b,c} (fin b) => [a * b] c -> [a][b] c
746
750
, (" reverse" , flip scGlobalDef " Cryptol.ecReverse" ) -- {a,b} (fin a) => [a] b -> [a] b
@@ -1231,18 +1235,20 @@ importName cnm =
1231
1235
case C. nameInfo cnm of
1232
1236
C. Parameter -> fail (" Cannot import non-top-level name: " ++ show cnm)
1233
1237
C. Declared modNm _
1234
- | modNm == C. interactiveName ->
1238
+ | modNm == C. TopModule C. interactiveName ->
1235
1239
let shortNm = C. identText (C. nameIdent cnm)
1236
1240
aliases = [shortNm]
1237
1241
uri = cryptolURI [shortNm] (Just (C. nameUnique cnm))
1238
1242
in pure (ImportedName uri aliases)
1239
1243
1240
1244
| otherwise ->
1241
- let modNmTxt = C. modNameToText modNm
1242
- modNms = Text. splitOn " ::" modNmTxt
1243
- shortNm = C. identText (C. nameIdent cnm)
1244
- aliases = [shortNm, modNmTxt <> " ::" <> shortNm]
1245
- uri = cryptolURI (modNms ++ [shortNm]) Nothing
1245
+ let (topMod, nested) = C. modPathSplit modNm
1246
+ modNmTxt = C. modNameToText topMod
1247
+ modNms = (Text. splitOn " ::" modNmTxt) ++ map C. identText nested
1248
+ shortNm = C. identText (C. nameIdent cnm)
1249
+ longNm = Text. intercalate " ::" ([modNmTxt] ++ map C. identText nested ++ [shortNm])
1250
+ aliases = [shortNm, longNm]
1251
+ uri = cryptolURI (modNms ++ [shortNm]) Nothing
1246
1252
in pure (ImportedName uri aliases)
1247
1253
1248
1254
-- | Currently this imports declaration groups by inlining all the
@@ -1639,22 +1645,22 @@ scCryptolEq sc x y =
1639
1645
1640
1646
-- | Convert from SAWCore's Value type to Cryptol's, guided by the
1641
1647
-- Cryptol type schema.
1642
- exportValueWithSchema :: C. Schema -> SC. CValue -> V. Value
1648
+ exportValueWithSchema :: C. Schema -> SC. CValue -> V. Eval V. Value
1643
1649
exportValueWithSchema (C. Forall [] [] ty) v = exportValue (evalValType mempty ty) v
1644
- exportValueWithSchema _ _ = V. VPoly mempty (error " exportValueWithSchema" )
1650
+ exportValueWithSchema _ _ = pure ( V. VPoly mempty (error " exportValueWithSchema" ) )
1645
1651
-- TODO: proper support for polymorphic values
1646
1652
1647
- exportValue :: TV. TValue -> SC. CValue -> V. Value
1653
+ exportValue :: TV. TValue -> SC. CValue -> V. Eval V. Value
1648
1654
exportValue ty v = case ty of
1649
1655
1650
1656
TV. TVBit ->
1651
- V. VBit (SC. toBool v)
1657
+ pure ( V. VBit (SC. toBool v) )
1652
1658
1653
1659
TV. TVInteger ->
1654
- V. VInteger (case v of SC. VInt x -> x; _ -> error " exportValue: expected integer" )
1660
+ pure ( V. VInteger (case v of SC. VInt x -> x; _ -> error " exportValue: expected integer" ) )
1655
1661
1656
1662
TV. TVIntMod _modulus ->
1657
- V. VInteger (case v of SC. VIntMod _ x -> x; _ -> error " exportValue: expected intmod" )
1663
+ pure ( V. VInteger (case v of SC. VIntMod _ x -> x; _ -> error " exportValue: expected intmod" ) )
1658
1664
1659
1665
TV. TVArray {} -> error $ " exportValue: (on array type " ++ show ty ++ " )"
1660
1666
@@ -1666,28 +1672,29 @@ exportValue ty v = case ty of
1666
1672
case v of
1667
1673
SC. VWord w -> V. word V. Concrete (toInteger (width w)) (unsigned w)
1668
1674
SC. VVector xs
1669
- | TV. isTBit e -> V. VWord (toInteger (Vector. length xs)) (V. ready (V. LargeBitsVal (fromIntegral (Vector. length xs))
1670
- (V. finiteSeqMap . map (V. ready . V. VBit . SC. toBool . SC. runIdentity . force) $ Fold. toList xs)))
1671
- | otherwise -> V. VSeq (toInteger (Vector. length xs)) $ V. finiteSeqMap $
1672
- map (V. ready . exportValue e . SC. runIdentity . force) $ Vector. toList xs
1675
+ | TV. isTBit e -> V. VWord (toInteger (Vector. length xs)) <$>
1676
+ V. bitmapWordVal V. Concrete (toInteger (Vector. length xs))
1677
+ (V. finiteSeqMap V. Concrete . map (V. ready . SC. toBool . SC. runIdentity . force) $ Fold. toList xs)
1678
+ | otherwise -> pure . V. VSeq (toInteger (Vector. length xs)) $ V. finiteSeqMap V. Concrete $
1679
+ map (\ x -> exportValue e (SC. runIdentity (force x))) (Vector. toList xs)
1673
1680
_ -> error $ " exportValue (on seq type " ++ show ty ++ " )"
1674
1681
1675
1682
-- infinite streams
1676
1683
TV. TVStream e ->
1677
1684
case v of
1678
- SC. VExtra (SC. CStream trie) -> V. VStream (V. IndexSeqMap $ \ i -> V. ready $ exportValue e (IntTrie. apply trie i))
1685
+ SC. VExtra (SC. CStream trie) -> pure $ V. VStream (V. indexSeqMap $ \ i -> exportValue e (IntTrie. apply trie i))
1679
1686
_ -> error $ " exportValue (on seq type " ++ show ty ++ " )"
1680
1687
1681
1688
-- tuples
1682
- TV. TVTuple etys -> V. VTuple ( exportTupleValue etys v)
1689
+ TV. TVTuple etys -> pure $ V. VTuple $ exportTupleValue etys v
1683
1690
1684
1691
-- records
1685
1692
TV. TVRec fields ->
1686
- V. VRecord ( C. recordFromFieldsWithDisplay (C. displayOrder fields) $ exportRecordValue (C. canonicalFields fields) v)
1693
+ pure . V. VRecord . C. recordFromFieldsWithDisplay (C. displayOrder fields) $ exportRecordValue (C. canonicalFields fields) v
1687
1694
1688
1695
-- functions
1689
1696
TV. TVFun _aty _bty ->
1690
- V. VFun mempty (error " exportValue: TODO functions" )
1697
+ pure $ V. VFun mempty (error " exportValue: TODO functions" )
1691
1698
1692
1699
-- abstract types
1693
1700
TV. TVAbstract {} ->
@@ -1702,8 +1709,8 @@ exportTupleValue :: [TV.TValue] -> SC.CValue -> [V.Eval V.Value]
1702
1709
exportTupleValue tys v =
1703
1710
case (tys, v) of
1704
1711
([] , SC. VUnit ) -> []
1705
- ([t] , _ ) -> [V. ready $ exportValue t v]
1706
- (t : ts, SC. VPair x y) -> (V. ready $ exportValue t (run x)) : exportTupleValue ts (run y)
1712
+ ([t] , _ ) -> [exportValue t v]
1713
+ (t : ts, SC. VPair x y) -> (exportValue t (run x)) : exportTupleValue ts (run y)
1707
1714
_ -> error $ " exportValue: expected tuple"
1708
1715
where
1709
1716
run = SC. runIdentity . force
@@ -1712,12 +1719,11 @@ exportRecordValue :: [(C.Ident, TV.TValue)] -> SC.CValue -> [(C.Ident, V.Eval V.
1712
1719
exportRecordValue fields v =
1713
1720
case (fields, v) of
1714
1721
([] , SC. VUnit ) -> []
1715
- ([(n, t)] , _ ) -> [(n, V. ready $ exportValue t v)]
1716
- ((n, t) : ts, SC. VPair x y) ->
1717
- (n, V. ready $ exportValue t (run x)) : exportRecordValue ts (run y)
1722
+ ([(n, t)] , _ ) -> [(n, exportValue t v)]
1723
+ ((n, t) : ts, SC. VPair x y) -> (n, exportValue t (run x)) : exportRecordValue ts (run y)
1718
1724
(_, SC. VRecordValue (alistAllFields
1719
1725
(map (C. identText . fst ) fields) -> Just ths)) ->
1720
- zipWith (\ (n,t) x -> (n, V. ready $ exportValue t (run x))) fields ths
1726
+ zipWith (\ (n,t) x -> (n, exportValue t (run x))) fields ths
1721
1727
_ -> error $ " exportValue: expected record"
1722
1728
where
1723
1729
run = SC. runIdentity . force
@@ -1726,20 +1732,23 @@ fvAsBool :: FirstOrderValue -> Bool
1726
1732
fvAsBool (FOVBit b) = b
1727
1733
fvAsBool _ = error " fvAsBool: expected FOVBit value"
1728
1734
1729
- exportFirstOrderValue :: FirstOrderValue -> V. Value
1735
+ exportFirstOrderValue :: FirstOrderValue -> V. Eval V. Value
1730
1736
exportFirstOrderValue fv =
1731
1737
case fv of
1732
- FOVBit b -> V. VBit b
1733
- FOVInt i -> V. VInteger i
1734
- FOVIntMod _ i -> V. VInteger i
1735
- FOVWord w x -> V. word V. Concrete (toInteger w) x
1738
+ FOVBit b -> pure ( V. VBit b)
1739
+ FOVInt i -> pure ( V. VInteger i)
1740
+ FOVIntMod _ i -> pure ( V. VInteger i)
1741
+ FOVWord w x -> V. word V. Concrete (toInteger w) x
1736
1742
FOVVec t vs
1737
- | t == FOTBit -> V. VWord len (V. ready (V. LargeBitsVal len (V. finiteSeqMap . map (V. ready . V. VBit . fvAsBool) $ vs)))
1738
- | otherwise -> V. VSeq len (V. finiteSeqMap (map (V. ready . exportFirstOrderValue) vs))
1743
+ | t == FOTBit -> V. VWord len <$> (V. bitmapWordVal V. Concrete len
1744
+ (V. finiteSeqMap V. Concrete . map (V. ready . fvAsBool) $ vs))
1745
+ | otherwise -> pure (V. VSeq len (V. finiteSeqMap V. Concrete (map exportFirstOrderValue vs)))
1739
1746
where len = toInteger (length vs)
1740
1747
FOVArray {} -> error $ " exportFirstOrderValue: unsupported FOT Array"
1741
- FOVTuple vs -> V. VTuple (map (V. ready . exportFirstOrderValue) vs)
1742
- FOVRec vm -> V. VRecord $ C. recordFromFields [ (C. mkIdent n, V. ready $ exportFirstOrderValue v) | (n, v) <- Map. assocs vm ]
1748
+ FOVTuple vs -> pure $ V. VTuple $ map exportFirstOrderValue vs
1749
+ FOVRec vm ->
1750
+ do let vm' = fmap exportFirstOrderValue vm
1751
+ pure $ V. VRecord $ C. recordFromFields [ (C. mkIdent n, v) | (n, v) <- Map. assocs vm' ]
1743
1752
1744
1753
importFirstOrderValue :: FirstOrderType -> V. Value -> IO FirstOrderValue
1745
1754
importFirstOrderValue t0 v0 = V. runEval mempty (go t0 v0)
@@ -1748,7 +1757,7 @@ importFirstOrderValue t0 v0 = V.runEval mempty (go t0 v0)
1748
1757
go t v = case (t,v) of
1749
1758
(FOTBit , V. VBit b) -> return (FOVBit b)
1750
1759
(FOTInt , V. VInteger i) -> return (FOVInt i)
1751
- (FOTVec _ FOTBit , V. VWord w wv) -> FOVWord (fromIntegral w) . V. bvVal <$> (V. asWordVal V. Concrete =<< wv)
1760
+ (FOTVec _ FOTBit , V. VWord w wv) -> FOVWord (fromIntegral w) . V. bvVal <$> (V. asWordVal V. Concrete wv)
1752
1761
(FOTVec _ ty , V. VSeq len xs) -> FOVVec ty <$> traverse (go ty =<< ) (V. enumerateSeqMap len xs)
1753
1762
(FOTTuple tys , V. VTuple xs) -> FOVTuple <$> traverse (\ (ty, x) -> go ty =<< x) (zip tys xs)
1754
1763
(FOTRec fs , V. VRecord xs) ->
0 commit comments