Skip to content

Commit 02f2be2

Browse files
author
Brian Huffman
committed
Replace GlobalDef term constructor with Primitive.
The new `Primitive` constructor is for global constants that do not have definitions. Instead of an `Ident`, they are identified with an `ExtCns`, which includes a `NameInfo` and a type. Fixes #162.
1 parent f208f24 commit 02f2be2

File tree

10 files changed

+40
-19
lines changed

10 files changed

+40
-19
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ flatTermFToExpr ::
202202
m Coq.Term
203203
flatTermFToExpr tf = -- traceFTermF "flatTermFToExpr" tf $
204204
case tf of
205-
GlobalDef i -> translateIdent i
205+
Primitive (EC _ nmi _) ->
206+
case nmi of
207+
ModuleIdentifier i -> translateIdent i
208+
ImportedName{} -> errorTermM "Invalid name for saw-core primitive"
206209
UnitValue -> pure (Coq.Var "tt")
207210
UnitType -> pure (Coq.Var "unit")
208211
PairValue x y -> Coq.App (Coq.Var "pair") <$> traverse translateTerm [x, y]

saw-core/src/Verifier/SAW/ExternalFormat.hs

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ scWriteExternal t0 =
132132
pure $ unwords ["Constant", show (ecVarIndex ec), show (ecType ec), show e]
133133
FTermF ftf ->
134134
case ftf of
135-
GlobalDef ident -> pure $ unwords ["Global", show ident]
135+
Primitive ec ->
136+
do stashName ec
137+
pure $ unwords ["Primitive", show (ecVarIndex ec), show (ecType ec)]
136138
UnitValue -> pure $ unwords ["Unit"]
137139
UnitType -> pure $ unwords ["UnitT"]
138140
PairValue x y -> pure $ unwords ["Pair", show x, show y]
@@ -229,7 +231,7 @@ scReadExternal sc input =
229231
["Pi", s, t, e] -> Pi (Text.pack s) <$> readIdx t <*> readIdx e
230232
["Var", i] -> pure $ LocalVar (read i)
231233
["Constant",i,t,e] -> Constant <$> readEC i t <*> readIdx e
232-
["Global", x] -> pure $ FTermF (GlobalDef (parseIdent x))
234+
["Primitive", i, t] -> FTermF <$> (Primitive <$> readEC i t)
233235
["Unit"] -> pure $ FTermF UnitValue
234236
["UnitT"] -> pure $ FTermF UnitType
235237
["Pair", x, y] -> FTermF <$> (PairValue <$> readIdx x <*> readIdx y)

saw-core/src/Verifier/SAW/Recognizer.hs

+8-2
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,17 @@ asFTermF :: Recognizer Term (FlatTermF Term)
119119
asFTermF (unwrapTermF -> FTermF ftf) = return ftf
120120
asFTermF _ = Nothing
121121

122+
asModuleIdentifier :: Recognizer (ExtCns e) Ident
123+
asModuleIdentifier (EC _ nmi _) =
124+
case nmi of
125+
ModuleIdentifier ident -> Just ident
126+
_ -> Nothing
127+
122128
asGlobalDef :: Recognizer Term Ident
123129
asGlobalDef t =
124130
case unwrapTermF t of
125-
FTermF (GlobalDef ident) -> pure ident
126-
Constant (EC _ (ModuleIdentifier ident) _) _ -> pure ident
131+
FTermF (Primitive ec) -> asModuleIdentifier ec
132+
Constant ec _ -> asModuleIdentifier ec
127133
_ -> Nothing
128134

129135
isGlobalDef :: Ident -> Recognizer Term ()

saw-core/src/Verifier/SAW/Rewriter.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ rewriteSharedTermTypeSafe sc ss t0 =
627627
Sort{} -> return ftf -- doesn't matter
628628
NatLit{} -> return ftf -- doesn't matter
629629
ArrayValue t es -> ArrayValue t <$> traverse rewriteAll es
630-
GlobalDef{} -> return ftf
630+
Primitive{} -> return ftf
631631
StringLit{} -> return ftf
632632
ExtCns{} -> return ftf
633633
rewriteTop :: (?cache :: Cache IO TermIndex Term) =>

saw-core/src/Verifier/SAW/SCTypeCheck.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,8 @@ instance TypeInfer (TermF TypedTerm) where
389389
-- terms. Intuitively, this represents the case where each immediate subterm of
390390
-- a term has already been labeled with its (most general) type.
391391
instance TypeInfer (FlatTermF TypedTerm) where
392-
typeInfer (GlobalDef d) =
393-
do ty <- liftTCM scTypeOfGlobal d
394-
typeCheckWHNF ty
392+
typeInfer (Primitive ec) =
393+
typeCheckWHNF $ typedVal $ ecType ec
395394
typeInfer UnitValue = liftTCM scUnitType
396395
typeInfer UnitType = liftTCM scSort (mkSort 0)
397396
typeInfer (PairValue (TypedTerm _ tx) (TypedTerm _ ty)) =

saw-core/src/Verifier/SAW/SharedTerm.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ scTypeOf' sc env t0 = State.evalStateT (memo t0) Map.empty
927927
-> State.StateT (Map TermIndex Term) IO Term
928928
ftermf tf =
929929
case tf of
930-
GlobalDef d -> lift $ scTypeOfGlobal sc d
930+
Primitive ec -> return $ ecType ec
931931
UnitValue -> lift $ scUnitType sc
932932
UnitType -> lift $ scSort sc (mkSort 0)
933933
PairValue x y -> do

saw-core/src/Verifier/SAW/Simulator.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ evalTermF cfg lam recEval tf env =
139139
maybe (recEval t) id (simUninterpreted cfg tf ec')
140140
FTermF ftf ->
141141
case ftf of
142-
GlobalDef ident -> simGlobal cfg ident
142+
Primitive ec ->
143+
do ec' <- traverse (fmap toTValue . recEval) ec
144+
case ecName ec' of
145+
ModuleIdentifier ident -> simGlobal cfg ident
146+
_ -> simExtCns cfg tf ec'
143147
UnitValue -> return VUnit
144148
UnitType -> return $ TValue VUnitType
145149
PairValue x y -> do tx <- recEvalDelay x

saw-core/src/Verifier/SAW/Term/Functor.hs

+5-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ maxSort ss = maximum ss
140140
-- NB: If you add constructors to FlatTermF, make sure you update
141141
-- zipWithFlatTermF!
142142
data FlatTermF e
143-
= GlobalDef !Ident -- ^ Global variables are referenced by label.
143+
-- | A primitive or axiom without a definition.
144+
= Primitive !(ExtCns e)
144145

145146
-- Tuples are represented as nested pairs, grouped to the right,
146147
-- terminated with unit at the end.
@@ -219,7 +220,8 @@ zipWithFlatTermF :: (x -> y -> z) -> FlatTermF x -> FlatTermF y ->
219220
Maybe (FlatTermF z)
220221
zipWithFlatTermF f = go
221222
where
222-
go (GlobalDef x) (GlobalDef y) | x == y = Just $ GlobalDef x
223+
go (Primitive (EC xi xn xt)) (Primitive (EC yi _ yt))
224+
| xi == yi = Just (Primitive (EC xi xn (f xt yt)))
223225

224226
go UnitValue UnitValue = Just UnitValue
225227
go UnitType UnitType = Just UnitType
@@ -344,7 +346,7 @@ termToPat t =
344346
case unwrapTermF t of
345347
Constant ec _ -> Net.Atom (toAbsoluteName (ecName ec))
346348
App t1 t2 -> Net.App (termToPat t1) (termToPat t2)
347-
FTermF (GlobalDef d) -> Net.Atom (identText d)
349+
FTermF (Primitive ec) -> Net.Atom (toAbsoluteName (ecName ec))
348350
FTermF (Sort s) -> Net.Atom (Text.pack ('*' : show s))
349351
FTermF (NatLit _) -> Net.Var
350352
FTermF (DataTypeApp c ps ts) ->

saw-core/src/Verifier/SAW/Term/Pretty.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import Verifier.SAW.Term.Functor
6363
-- * Doc annotations
6464

6565
data SawStyle
66-
= GlobalDefStyle
66+
= PrimitiveStyle
6767
| ConstantStyle
6868
| ExtCnsStyle
6969
| LocalVarStyle
@@ -76,7 +76,7 @@ data SawStyle
7676
colorStyle :: SawStyle -> AnsiStyle
7777
colorStyle =
7878
\case
79-
GlobalDefStyle -> mempty
79+
PrimitiveStyle -> mempty
8080
ConstantStyle -> colorDull Blue
8181
ExtCnsStyle -> colorDull Red
8282
LocalVarStyle -> colorDull Green
@@ -422,7 +422,7 @@ ppDataType d (params, ((d_ctx,d_tp), ctors)) =
422422
ppFlatTermF :: Prec -> FlatTermF Term -> PPM SawDoc
423423
ppFlatTermF prec tf =
424424
case tf of
425-
GlobalDef i -> return $ annotate GlobalDefStyle $ ppIdent i
425+
Primitive ec -> annotate PrimitiveStyle <$> ppBestName (ecName ec)
426426
UnitValue -> return "(-empty-)"
427427
UnitType -> return "#(-empty-)"
428428
PairValue x y -> ppPair prec <$> ppTerm' PrecLambda x <*> ppTerm' PrecNone y
@@ -542,7 +542,7 @@ scTermCount doBinders t0 = execState (go [t0]) IntMap.empty
542542
shouldMemoizeTerm :: Term -> Bool
543543
shouldMemoizeTerm t =
544544
case unwrapTermF t of
545-
FTermF GlobalDef{} -> False
545+
FTermF Primitive{} -> False
546546
FTermF UnitValue -> False
547547
FTermF UnitType -> False
548548
FTermF (CtorApp _ [] []) -> False

saw-core/src/Verifier/SAW/Typechecker.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,12 @@ processDecls (Un.TypeDecl q (PosPair p nm) tp : rest) =
350350
void $ ensureSort $ typedType typed_tp
351351
mnm <- getModuleName
352352
let ident = mkIdent mnm nm
353-
t <- liftTCM scFlatTermF (GlobalDef ident)
353+
let nmi = ModuleIdentifier ident
354+
i <- liftTCM scFreshGlobalVar
355+
liftTCM scRegisterName i nmi
356+
let def_tp = typedVal typed_tp
357+
let ec = EC i nmi def_tp
358+
t <- liftTCM scFlatTermF (Primitive ec)
354359
liftTCM scRegisterGlobal ident t
355360
liftTCM scModifyModule mnm $ \m ->
356361
insDef m $ Def { defIdent = ident,

0 commit comments

Comments
 (0)