Skip to content

Commit 146f57f

Browse files
committed
improve and add haddock and other comments and FIXMEs
per @RyanGlScott code review.
1 parent 16b183c commit 146f57f

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

cryptol-saw-core/src/CryptolSAWCore/CryptolEnv.hs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,24 @@ data InputText = InputText
115115

116116
--------------------------------------------------------------------------------
117117

118-
-- | Should a given import result in all symbols being visible (as they
119-
-- are for focused modules in the Cryptol REPL) or only public symbols?
120-
-- Making all symbols visible is useful for verification and code
121-
-- generation.
118+
-- | 'ImportVisibility' - Should a given import (see 'importModule')
119+
-- result in all symbols being visible (as they are for focused
120+
-- modules in the Cryptol REPL) or only public symbols? Making all
121+
-- symbols visible is useful for verification and code generation.
122+
--
123+
-- NOTE: this notion of public vs. private symbols is specific to
124+
-- SAWScript and distinct from Cryptol's notion of private
125+
-- definitions.
122126
--
123-
-- NOTE: PublicAndPrivate is specific to SAWScript (Cryptol cannot do such).
124127
data ImportVisibility
125-
= OnlyPublic
126-
| PublicAndPrivate
128+
= OnlyPublic -- ^ behaves like a normal Cryptol "import"
129+
| PublicAndPrivate -- ^ allows viewing of both "private" sections and (arbitrarily nested) submodules.
127130
deriving (Eq, Show)
128131

129132

130133
-- | The environment for capturing the Cryptol interpreter state as well as the
131134
-- SAWCore translations and associated state.
132135
--
133-
-- FIXME[D]: The differences in function between this and the similar
134-
-- C.Env?
135-
136136
data CryptolEnv = CryptolEnv
137137
{ eImports :: [(ImportVisibility, P.Import)]
138138
-- ^ Declarations of imported Cryptol modules
@@ -185,6 +185,14 @@ nameMatcher xs =
185185

186186
-- Initialize ------------------------------------------------------------------
187187

188+
-- | initCryptolEnv - Create initial CryptolEnv, this involves loading the built-in modules
189+
-- (preludeName, arrayName, preludeReferenceName) and translating them into SAWCore, and
190+
-- putting them into scope.
191+
--
192+
-- NOTE: submodules in these built-in modules would be suppported here.
193+
--
194+
-- FIXME: There is some code duplication between this and `loadCryptolModule` and `importModule`.
195+
188196
initCryptolEnv ::
189197
(?fileReader :: FilePath -> IO ByteString) =>
190198
SharedContext -> IO CryptolEnv
@@ -226,7 +234,6 @@ initCryptolEnv sc = do
226234
let refDecls = T.mDecls refMod
227235
let nms = Set.toList (MI.ifsPublic (TIface.genIfaceNames refMod))
228236

229-
-- FIXME: assuming we have same "bug" were we to have submodules in prelude. ?
230237
let refPrims = Map.fromList
231238
[ (prelPrim (identText (MN.nameIdent nm)), T.EWhere (T.EVar nm) refDecls)
232239
| nm <- nms ]
@@ -285,8 +292,7 @@ ioParseResult res = case res of
285292

286293
-- NamingEnv and Related -------------------------------------------------------
287294

288-
-- | getNamingEnv env - get the full MR.NamingEnv based on all the `eImports`
289-
--
295+
-- | @'getNamingEnv' env@ - get the full 'MR.NamingEnv' based on all the 'eImports'
290296

291297
getNamingEnv :: CryptolEnv -> MR.NamingEnv
292298
getNamingEnv env =
@@ -504,9 +510,6 @@ loadCryptolModule sc env path = do
504510
-- user binds the `cryptolModule` returned here at the saw
505511
-- command line.
506512

507-
-- FIXME: A better CLI API could simplify both this code and the SAWScript
508-
-- semantics.
509-
510513
return ( cryptolModule
511514
, updateFFITypes m
512515
env { eModuleEnv = modEnv''
@@ -544,7 +547,7 @@ mkCryptolModule m types newTermEnv =
544547
(\k _ -> Set.member k (MEx.exported C.NSType (T.mExports m)))
545548
(T.mTySyns m)
546549
)
547-
-- FIXME: ensure type synonym in submodule is included.
550+
-- FIXME: TODO: ensure type synonym in submodule is included.
548551

549552
-- create the map of symbols:
550553
( Map.filterWithKey (\k _ -> Set.member k names)
@@ -576,34 +579,32 @@ updateFFITypes m env = env { eFFITypes = eFFITypes' }
576579
"Cannot find foreign function in term environment: " <> Text.pack (show nm)
577580
]
578581

579-
-- | bindCryptolModule - ad hoc function called when we do `D <-cryptol_load`
582+
-- | bindCryptolModule - ad hoc function called when `D <-cryptol_load` is seen
580583
-- on the command line.
581584
--
582585
-- FIXME:
583586
-- - submodules are not handled correctly below.
584-
-- - the code is duplicating functionality that we have with imports.
587+
-- - the code is duplicating functionality that we have with `importModule`
585588
--
586589
bindCryptolModule :: (P.ModName, CryptolModule) -> CryptolEnv -> CryptolEnv
587590
bindCryptolModule (modName, CryptolModule sm tm) env =
588591
env { eExtraNames = flip (foldr addName) (Map.keys tm') $
589592
flip (foldr addTSyn) (Map.keys sm) $
590593
flip (foldr addSubModule) (Map.keys tm') $
591-
-- FIXME: This added function doesn't really
592-
-- fix the submodule support bugs.
593594
eExtraNames env
594595
, eExtraTSyns = Map.union sm (eExtraTSyns env)
595596
, eExtraTypes = Map.union (fmap fst tm') (eExtraTypes env)
596597
, eTermEnv = Map.union (fmap snd tm') (eTermEnv env)
597598
}
598599
where
599-
-- select out those typed terms from `tm' that have Cryptol schemas
600+
-- | `tm'` is the typed terms from `tm` that have Cryptol schemas
600601
tm' = Map.mapMaybe f tm
601602
where
602603
f (TypedTerm (TypedTermSchema s) x) = Just (s,x)
603604
f _ = Nothing
604605

605606
addName name = MN.shadowing (MN.singletonNS C.NSValue (P.mkQual modName (MN.nameIdent name)) name)
606-
-- FIXME: suspicious. (we need to do any C.NSModule?)
607+
-- FIXME: suspicious. (Do we need to do any C.NSModule?)
607608

608609
addSubModule name = MN.shadowing (MN.singletonNS C.NSModule (P.mkQual modName (MN.nameIdent name)) name)
609610

@@ -623,6 +624,13 @@ extractDefFromCryptolModule (CryptolModule _ tm) name =
623624

624625
--------------------------------------------------------------------------------
625626

627+
-- | @'importModule' sc env src as vis imps@ - extend the Cryptol
628+
-- environment with a module. Closely mirrors the sawscript command "import".
629+
--
630+
-- NOTE:
631+
-- - the module can be qualified or not (per 'as' argument).
632+
-- - per 'vis' we can import public definitions or *all* (i.e., kinternal and public) definitions.
633+
626634
importModule ::
627635
(?fileReader :: FilePath -> IO ByteString) =>
628636
SharedContext {- ^ Shared context for creating terms -} ->
@@ -793,7 +801,7 @@ pExprToTypedTerm sc env pexpr = do
793801
let nameEnv = getNamingEnv env
794802
let npe' = MR.rename npe
795803
re <- MM.interactive (MB.rename interactiveName nameEnv npe')
796-
-- NOTE: if a Value not in scope, reported here.
804+
-- NOTE: if a name is not in scope, it is reported here.
797805

798806
-- Infer types
799807
let ifDecls = getAllIfaceDecls modEnv

intTests/test_saw_submodule_access1/test.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ $SAW test_import_errors.saw
55

66
$SAW test_import_D.saw
77

8-
# $SAW test_load_D.saw # currently not working!
8+
# $SAW test_load_D.saw
9+
# - This is a test that will fail till till code is fixed.
10+
# - TODO: uncomment after fixing code.
911

1012
$SAW test_UseFunctors.saw

saw-central/src/SAWCentral/Prover/Exporter.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ writeCoqCryptolModule mon inputFile outputFile notations skips = io $ do
530530
env <- initCryptolEnv sc
531531
cryptolPrimitivesForSAWCoreModule <- scFindModule sc nameOfCryptolPrimitivesForSAWCoreModule
532532
(cm, _) <- loadCryptolModule sc env inputFile
533-
-- NOTE: this allows 'unknownPrimitives', see definition
533+
-- NOTE: implementation of loadCryptolModule, now uses this default:
534+
-- defaultPrimitiveOptions = ImportPrimitiveOptions{allowUnknownPrimitives=True}
534535
cry_env <- mkCryEnv env
535536
mm <- scGetModuleMap sc
536537
let ?mm = mm

0 commit comments

Comments
 (0)