Skip to content

Commit ead7a3f

Browse files
Implement blocking of vector operations using postulates (#746)
1 parent 9c36083 commit ead7a3f

File tree

21 files changed

+153
-119
lines changed

21 files changed

+153
-119
lines changed

Diff for: vehicle-syntax/src/Vehicle/Syntax/AST.hs

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ import Vehicle.Syntax.AST.Name as X
1313
import Vehicle.Syntax.AST.Prog as X
1414
import Vehicle.Syntax.AST.Provenance as X
1515
import Vehicle.Syntax.AST.Relevance as X
16+
import Vehicle.Syntax.AST.Type as X
1617
import Vehicle.Syntax.AST.Visibility as X

Diff for: vehicle-syntax/src/Vehicle/Syntax/AST/Binder.hs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import GHC.Generics (Generic)
1010
import Vehicle.Syntax.AST.Name (HasName (..), Name)
1111
import Vehicle.Syntax.AST.Provenance (HasProvenance (..), Provenance)
1212
import Vehicle.Syntax.AST.Relevance (HasRelevance (..), Relevance (..))
13+
import Vehicle.Syntax.AST.Type
1314
import Vehicle.Syntax.AST.Visibility (HasVisibility (..), Visibility (..))
1415

1516
--------------------------------------------------------------------------------
@@ -111,6 +112,9 @@ instance HasRelevance (GenericBinder expr) where
111112
instance HasName (GenericBinder expr) (Maybe Name) where
112113
nameOf = nameOf . binderNamingForm
113114

115+
instance HasType (GenericBinder expr) expr where
116+
typeOf = binderValue
117+
114118
--------------------------------------------------------------------------------
115119
-- Binders
116120

Diff for: vehicle-syntax/src/Vehicle/Syntax/AST/Decl.hs

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import GHC.Generics (Generic)
77
import Prettyprinter (Pretty (..))
88
import Vehicle.Syntax.AST.Name
99
import Vehicle.Syntax.AST.Provenance
10+
import Vehicle.Syntax.AST.Type
1011

1112
--------------------------------------------------------------------------------
1213
-- Declarations
@@ -45,6 +46,11 @@ instance HasIdentifier (GenericDecl expr) where
4546
instance HasName (GenericDecl expr) Name where
4647
nameOf = nameOf . identifierOf
4748

49+
instance HasType (GenericDecl expr) expr where
50+
typeOf = \case
51+
DefAbstract _ _ _ t -> t
52+
DefFunction _ _ _ t _ -> t
53+
4854
bodyOf :: GenericDecl expr -> Maybe expr
4955
bodyOf = \case
5056
DefFunction _ _ _ _ e -> Just e
@@ -124,6 +130,10 @@ isExternalResourceSort = \case
124130
ParameterDef {} -> True
125131
PostulateDef -> False
126132

133+
convertToPostulate :: GenericDecl expr -> GenericDecl expr
134+
convertToPostulate d =
135+
DefAbstract (provenanceOf d) (identifierOf d) PostulateDef (typeOf d)
136+
127137
--------------------------------------------------------------------------------
128138
-- Annotations options
129139

Diff for: vehicle-syntax/src/Vehicle/Syntax/AST/Type.hs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Vehicle.Syntax.AST.Type where
2+
3+
--------------------------------------------------------------------------------
4+
-- HasType
5+
6+
class HasType expr typ | expr -> typ where
7+
typeOf :: expr -> typ

Diff for: vehicle-syntax/vehicle-syntax.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ library
113113
Vehicle.Syntax.AST.Prog
114114
Vehicle.Syntax.AST.Provenance
115115
Vehicle.Syntax.AST.Relevance
116+
Vehicle.Syntax.AST.Type
116117
Vehicle.Syntax.AST.Visibility
117118
Vehicle.Syntax.BNFC.Delaborate.External
118119
Vehicle.Syntax.BNFC.Delaborate.Internal

Diff for: vehicle/src/Vehicle/Backend/Queries.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ compilePropertyDecl ::
127127
m (Name, MultiProperty ())
128128
compilePropertyDecl prog queryFormat networkCtx queryFreeCtx p ident expr outputLocation = do
129129
logCompilerPass MinDetail ("property" <+> quotePretty ident) $ do
130-
normalisedExpr <- eval (mkNBEOptions vectorStructureOperations) emptyEnv expr
130+
normalisedExpr <- whilePreservingOperations vectorOperations $ normaliseInEmptyEnv expr
131131

132132
let computeProperty = compileMultiProperty queryFormat networkCtx queryFreeCtx p ident outputLocation normalisedExpr
133133

Diff for: vehicle/src/Vehicle/Backend/Queries/QuerySetStructure.hs

+23-12
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ module Vehicle.Backend.Queries.QuerySetStructure
22
( UnreducedAssertion,
33
compileSetQueryStructure,
44
eliminateNot,
5-
vectorStructureOperations,
5+
whilePreservingOperations,
66
isVectorEquals,
7+
vectorOperations,
78
)
89
where
910

1011
import Control.Monad.Except (MonadError (..))
1112
import Control.Monad.Reader (MonadReader (..), ReaderT (..))
13+
import Data.Data (Proxy (..))
1214
import Data.HashSet qualified as HashSet (fromList, intersection, null)
1315
import Data.Map qualified as Map (keysSet)
1416
import Data.Set (Set)
@@ -127,8 +129,8 @@ type MonadQueryStructure m =
127129

128130
-- | The set of vector operations that we sometimes want to avoid normalising
129131
-- out in the property for efficiency reasons.
130-
vectorStructureOperations :: Set StdLibFunction
131-
vectorStructureOperations =
132+
vectorOperations :: Set StdLibFunction
133+
vectorOperations =
132134
Set.fromList
133135
[ StdAddVector,
134136
StdSubVector,
@@ -138,15 +140,22 @@ vectorStructureOperations =
138140

139141
-- | The set of finite quantifier operations that we sometimes want to avoid normalising
140142
-- out in the property for efficiency reasons.
141-
finiteQuantifierFunctions :: Set StdLibFunction
142-
finiteQuantifierFunctions =
143+
finiteQuantifierOperations :: Set StdLibFunction
144+
finiteQuantifierOperations =
143145
Set.fromList
144146
[ StdForallIndex,
145147
StdExistsIndex
146148
]
147149

148-
queryStructureNBEOptions :: NBEOptions
149-
queryStructureNBEOptions = mkNBEOptions (vectorStructureOperations <> finiteQuantifierFunctions)
150+
vectorLikeOperations :: Set StdLibFunction
151+
vectorLikeOperations = vectorOperations <> finiteQuantifierOperations
152+
153+
-- | Alter the normalisation monads so that builtin vector operations are
154+
-- temporarily converted to postulates and therefore do not evaluate.
155+
whilePreservingOperations :: (MonadNorm Builtin m) => Set StdLibFunction -> m a -> m a
156+
whilePreservingOperations funcs = do
157+
let operations = Set.map identifierOf funcs
158+
locallyAdjustCtx (Proxy @Builtin) (alterKeys operations convertToPostulate)
150159

151160
compileBoolExpr ::
152161
forall m.
@@ -220,7 +229,7 @@ compileBoolExpr = go False
220229
Just (Just exprWithoutIf) -> do
221230
logDebug MaxDetail $ "If-lifted to:" <+> prettyFriendly (WithContext exprWithoutIf ctx)
222231
let env = variableCtxToEnv quantifiedVariables
223-
normExprWithoutIf <- reeval defaultNBEOptions env exprWithoutIf
232+
normExprWithoutIf <- renormalise env exprWithoutIf
224233
logDebug MaxDetail $ "Normalised to:" <+> prettyFriendly (WithContext normExprWithoutIf ctx)
225234
go True quantifiedVariables normExprWithoutIf
226235
decrCallDepth
@@ -321,9 +330,11 @@ compileFiniteQuantifier quantifiedVariables q quantSpine binder env body = do
321330
return $ Right (mempty, Query foldedExpr, mempty)
322331
else do
323332
logDebug MaxDetail $ "Unfolding finite quantifier:" <+> pretty q <+> prettyVerbose binder
324-
quantImplementation <- lookupIdentValueInEnv defaultNBEOptions env (fromFiniteQuantifier q)
333+
quantImplementation <- lookupIdentValueInEnv noAbstractFreeVarInterpretation env (fromFiniteQuantifier q)
325334
let quantifierExpr = VFiniteQuantifierSpine quantSpine binder env body
326-
normResult <- evalApp queryStructureNBEOptions quantImplementation quantifierExpr
335+
normResult <-
336+
whilePreservingOperations vectorLikeOperations $
337+
normaliseApp quantImplementation quantifierExpr
327338
compileBoolExpr quantifiedVariables normResult
328339

329340
-- | This is a sound, inexpensive, but incomplete, check for whether
@@ -375,7 +386,7 @@ compileInfiniteQuantifier quantifiedVariables binder env body = do
375386

376387
-- First of all optimistically try not to normalise the quantified variable.
377388
let optimisticEnv = extendEnvWithBound binder env
378-
optimisiticBody <- eval queryStructureNBEOptions optimisticEnv body
389+
optimisiticBody <- whilePreservingOperations vectorLikeOperations $ normaliseInEnv optimisticEnv body
379390

380391
let unreducedPassDoc = "Trying without reducing dimensions of" <+> variableDoc
381392
optimisticResult <-
@@ -398,7 +409,7 @@ compileInfiniteQuantifier quantifiedVariables binder env body = do
398409

399410
let updatedQuantifiedVars = newQuantifiedVariables <> quantifiedVariables
400411

401-
normBody <- eval queryStructureNBEOptions newEnv body
412+
normBody <- whilePreservingOperations vectorLikeOperations $ normaliseInEnv newEnv body
402413

403414
let reducedPassDoc = "Compiling while reducing dimensions of" <+> quotePretty variableName
404415
normalisedResult <-

Diff for: vehicle/src/Vehicle/Backend/Queries/UserVariableElimination.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Vehicle.Backend.Queries.Variable
3333
import Vehicle.Compile.Context.Free (MonadFreeContext)
3434
import Vehicle.Compile.Error
3535
import Vehicle.Compile.Normalise.Builtin (evalMul)
36-
import Vehicle.Compile.Normalise.NBE (defaultNBEOptions, reeval)
36+
import Vehicle.Compile.Normalise.NBE (renormalise)
3737
import Vehicle.Compile.Prelude
3838
import Vehicle.Compile.Print (prettyVerbose)
3939
import Vehicle.Compile.Type.Subsystem.Standard
@@ -408,7 +408,7 @@ substituteReducedVariablesThroughSolutions partialEnv solutions solvedVariablePo
408408
where
409409
f :: (UserVariable, WHNFValue Builtin) -> WHNFEnv Builtin -> m (WHNFEnv Builtin)
410410
f (var, solution) env = do
411-
normalisedSolution <- reeval defaultNBEOptions env solution
411+
normalisedSolution <- renormalise env solution
412412
let errorMsg = developerError $ "Environment index missing for solved variable" <+> quotePretty var
413413
let index = unIx $ fromMaybe errorMsg $ Map.lookup var solvedVariablePositions
414414
let newEntry = mkDefaultEnvEntry (layoutAsText $ pretty var) (Defined normalisedSolution)
@@ -493,7 +493,7 @@ compileReducedAssertion ::
493493
m (MaybeTrivial (BooleanExpr SolvableAssertion))
494494
compileReducedAssertion _originalVariables variables variableSubstEnv assertion = do
495495
-- First normalise the expression under the new environment of reduced variables
496-
normExpr <- reeval defaultNBEOptions variableSubstEnv assertion
496+
normExpr <- renormalise variableSubstEnv assertion
497497

498498
-- Then extract the relation and arguments
499499
splitAssertions <- splitUpAssertions False normExpr
@@ -546,7 +546,7 @@ compileReducedAssertion _originalVariables variables variableSubstEnv assertion
546546
Just Nothing -> compilerDeveloperError $ "Cannot lift 'if' over" <+> prettyVerbose expr
547547
Just (Just exprWithoutIf) -> do
548548
let env = variableCtxToEnv variableCtx
549-
normExprWithoutIf <- reeval defaultNBEOptions env exprWithoutIf
549+
normExprWithoutIf <- renormalise env exprWithoutIf
550550
splitUpAssertions True normExprWithoutIf
551551

552552
reduceAssertion ::

Diff for: vehicle/src/Vehicle/Compile/Context/Free/Class.hs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Vehicle.Compile.Context.Free.Class where
22

33
import Control.Monad.Reader (ReaderT (..), mapReaderT)
4-
import Control.Monad.State (StateT, mapStateT)
4+
import Control.Monad.State (StateT (..), mapStateT)
55
import Control.Monad.Writer
66
import Data.Data (Proxy (..))
77
import Vehicle.Compile.Context.Bound
@@ -19,22 +19,27 @@ import Vehicle.Data.BuiltinInterface (HasStandardData)
1919
class (PrintableBuiltin builtin, HasStandardData builtin, MonadCompile m) => MonadFreeContext builtin m where
2020
addDeclToContext :: Decl Ix builtin -> m a -> m a
2121
getFreeCtx :: Proxy builtin -> m (FreeCtx builtin)
22+
locallyAdjustCtx :: Proxy builtin -> (FreeCtx builtin -> FreeCtx builtin) -> m a -> m a
2223

2324
instance (Monoid w, MonadFreeContext builtin m) => MonadFreeContext builtin (WriterT w m) where
2425
addDeclToContext = mapWriterT . addDeclToContext
2526
getFreeCtx = lift . getFreeCtx
27+
locallyAdjustCtx p = mapWriterT . locallyAdjustCtx p
2628

2729
instance (MonadFreeContext builtin m) => MonadFreeContext builtin (ReaderT w m) where
2830
addDeclToContext = mapReaderT . addDeclToContext
2931
getFreeCtx = lift . getFreeCtx
32+
locallyAdjustCtx p = mapReaderT . locallyAdjustCtx p
3033

3134
instance (MonadFreeContext builtin m) => MonadFreeContext builtin (StateT w m) where
3235
addDeclToContext = mapStateT . addDeclToContext
3336
getFreeCtx = lift . getFreeCtx
37+
locallyAdjustCtx p = mapStateT . locallyAdjustCtx p
3438

3539
instance (MonadFreeContext builtin m) => MonadFreeContext builtin (BoundContextT builtin m) where
3640
addDeclToContext = mapBoundContextT . addDeclToContext
3741
getFreeCtx = lift . getFreeCtx
42+
locallyAdjustCtx p = mapBoundContextT . locallyAdjustCtx p
3843

3944
--------------------------------------------------------------------------------
4045
-- Operations

Diff for: vehicle/src/Vehicle/Compile/Context/Free/Instance.hs

+4
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ instance
8181
local updateCtx (unFreeContextT cont)
8282

8383
getFreeCtx _ = FreeContextT ask
84+
85+
locallyAdjustCtx _ f x =
86+
FreeContextT $
87+
local f (unFreeContextT x)

0 commit comments

Comments
 (0)