66import Control.Monad.Reader (ReaderT )
77import Data.Maybe (maybeToList )
88import Data.Proxy (Proxy (.. ))
9+ import Data.Set (Set )
10+ import Data.Set qualified as Set
911import Vehicle.Backend.Loss.Core
1012import Vehicle.Backend.Loss.Domain (compileQuantifier )
1113import Vehicle.Backend.Loss.LogicCompilation (findAndCompileLogic )
@@ -18,6 +20,7 @@ import Vehicle.Compile.Prelude
1820import Vehicle.Data.Builtin.Loss
1921import Vehicle.Data.Builtin.Standard
2022import Vehicle.Data.Builtin.Standard.Normalise ()
23+ import Vehicle.Data.Code.BooleanExpr (unDisjunctAll )
2124import Vehicle.Data.Code.ForcedValue
2225import Vehicle.Data.DifferentiableLogic
2326import Vehicle.Data.Variable.Bound.Context.Tensor (TensorBoundContextT )
@@ -26,49 +29,52 @@ import Vehicle.Data.Variable.Free.Context (MonadFreeContext (..), addDeclEntryTo
2629convertToLossTensors ::
2730 (MonadCompile m ) =>
2831 DifferentiableLogicID ->
32+ Set Name ->
2933 Prog Builtin ->
3034 m (Prog LossBuiltin )
31- convertToLossTensors logicID prog@ (Main ds) = do
35+ convertToLossTensors logicID requestedDecls prog@ (Main ds) = do
3236 -- First find and compile the logic
3337 logic <- logCompilerPass LossLogic $ findAndCompileLogic logicID prog
3438
3539 -- Then compile the program using that logic
3640 runFreshFreeContextT (Proxy @ Builtin ) $ do
3741 runFreshFreeContextT (Proxy @ LossBuiltin ) $
3842 logCompilerPass Loss $ do
39- Main <$> convertDecls logicID logic ds
43+ Main <$> convertDecls logicID logic requestedDecls ds
4044
4145convertDecls ::
4246 (MonadCompile m , MonadFreeContext Builtin m , MonadFreeContext LossBuiltin m ) =>
4347 DifferentiableLogicID ->
4448 DifferentiableLogicImplementation ->
49+ Set Name ->
4550 [Decl Builtin ] ->
4651 m [Decl LossBuiltin ]
47- convertDecls logicID logic = \ case
52+ convertDecls logicID logic requestedDecls = \ case
4853 [] -> return []
4954 decl : decls -> do
50- maybeLossDecl <- convertDecl logicID logic decl
55+ maybeLossDecl <- convertDecl logicID logic requestedDecls decl
5156 decls' <-
5257 maybe id addDeclToContext maybeLossDecl $
5358 addDeclEntryToContext decl $
54- convertDecls logicID logic decls
59+ convertDecls logicID logic requestedDecls decls
5560 return $ maybeToList maybeLossDecl ++ decls'
5661
5762convertDecl ::
5863 forall m .
5964 (MonadCompile m , MonadFreeContext Builtin m , MonadFreeContext LossBuiltin m ) =>
6065 DifferentiableLogicID ->
6166 DifferentiableLogicImplementation ->
67+ Set Name ->
6268 Decl Builtin ->
6369 m (Maybe (Decl LossBuiltin ))
64- convertDecl logicID logic decl = case decl of
70+ convertDecl logicID logic requestedDecls decl = case decl of
6571 DefAbstract p ident sort typ
6672 | isAnnotatedAsExternalResource sort -> do
6773 let normType = Unforced emptyBoundEnv typ
6874 runConversion $ convertResourceDecl p ident sort normType
6975 | otherwise -> return Nothing
7076 DefFunction p ident ann typ expr
71- | isAnnotatedAsProperty ann -> do
77+ | isAnnotatedAsProperty ann || nameOf decl `Set.member` requestedDecls -> do
7278 let normType = Unforced emptyBoundEnv typ
7379 let normExpr = Unforced emptyBoundEnv expr
7480 runConversion $ convertPropertyDecl p ident ann normType normExpr
@@ -104,11 +110,14 @@ convertPropertyDecl ::
104110convertPropertyDecl p ident ann typ body = do
105111 lossType <- convertDeclType typ
106112 lossBody <- convertMultiProperty body
107- let lossTensorDecl = DefFunction p ident ann lossType lossBody
108- return lossTensorDecl
113+ return $ DefFunction p ident ann lossType lossBody
109114
110115convertDeclType :: (MonadLogic m ) => UnforcedType Builtin -> m (Type LossBuiltin )
111116convertDeclType typ = unnormalise 0 <$> convertThunk Nothing typ
112117
113118convertMultiProperty :: (MonadLogic m ) => Thunk Builtin -> m (Expr LossBuiltin )
114- convertMultiProperty body = unnormalise 0 <$> convertThunk (Just compileQuantifier) body
119+ convertMultiProperty body = do
120+ let compQuantifier args = do
121+ disjuncts <- compileQuantifier args
122+ foldrM1 orLossValue $ unDisjunctAll disjuncts
123+ unnormalise 0 <$> convertThunk (Just compQuantifier) body
0 commit comments