@@ -49,6 +49,9 @@ module Constrained.Generation (
4949 EqW (.. ),
5050 SumSpec (.. ),
5151 pattern SumSpec ,
52+
53+ mapSpec ,
54+ forwardPropagateSpec ,
5255) where
5356
5457import Constrained.AbstractSyntax
@@ -833,17 +836,7 @@ mergeSolverStage (SolverStage x ps spec relevant) plan =
833836 normalizeSolverStage $ SolverStage
834837 y
835838 (ps ++ ps')
836- ( addToErrorSpec
837- ( NE. fromList
838- ( [ " Solving var " ++ show x ++ " fails."
839- , " Merging the Specs"
840- , " 1. " ++ show spec
841- , " 2. " ++ show spec'
842- ]
843- )
844- )
845- (spec <> spec')
846- )
839+ (spec <> spec')
847840 (relevant <> relevant')
848841 Nothing -> stage
849842 | stage@ (SolverStage y ps' spec' relevant') <- plan
@@ -897,27 +890,21 @@ backPropagation relevant (SolverPlan initplan) = SolverPlan (go [] (reverse init
897890 go acc [] = acc
898891 go acc (s@ (SolverStage (x :: Var a ) ps spec _) : plan) = go (s : acc) plan'
899892 where
900- newStages = concatMap ( newStage spec) ps
893+ newStages = concatMap newStage ps
901894 plan' = foldr mergeSolverStage plan newStages
895+
902896 -- Note use of the Term Pattern Equal
903- newStage specl (Assert (Equal (V x') t)) =
904- termVarEqCases specl x' t
905- newStage specr (Assert (Equal t (V x'))) =
906- termVarEqCases specr x' t
907- newStage _ _ = []
908-
909- termVarEqCases :: HasSpec b => Specification a -> Var b -> Term b -> [SolverStage ]
910- termVarEqCases (MemberSpec vs) x' t
911- | Set. singleton (Name x) == freeVarSet t =
912- [SolverStage x' [] (MemberSpec (NE. nub (fmap (\ v -> errorGE $ runTerm (Env. singleton x v) t) vs)))
913- (Set. insert (Name x') relevant)]
914- termVarEqCases specx x' t
915- | Just Refl <- eqVar x x'
916- , [Name y] <- Set. toList $ freeVarSet t
917- , Result ctx <- toCtx y t =
918- [SolverStage y [] (propagateSpec specx ctx)
919- (Set. insert (Name x') relevant)]
920- termVarEqCases _ _ _ = []
897+ newStage (Assert (Equal tl tr))
898+ | [Name xl] <- Set. toList $ freeVarSet tl
899+ , [Name xr] <- Set. toList $ freeVarSet tr
900+ , Name x `elem` [Name xl, Name xr]
901+ , Result ctxL <- toCtx xl tl
902+ , Result ctxR <- toCtx xr tr
903+ = case (eqVar x xl, eqVar x xr) of
904+ (Just Refl , _) -> [SolverStage xr [] (propagateSpec (forwardPropagateSpec spec ctxL) ctxR) (Set. insert (Name x) relevant)]
905+ (_, Just Refl ) -> [SolverStage xl [] (propagateSpec (forwardPropagateSpec spec ctxR) ctxL) (Set. insert (Name x) relevant)]
906+ _ -> error " The impossible happened"
907+ newStage _ = []
921908
922909-- | Function symbols for `(==.)`
923910data EqW :: [Type ] -> Type -> Type where
@@ -1329,3 +1316,34 @@ fromGESpec ge = case ge of
13291316 Result s -> s
13301317 GenError xs -> ErrorSpec (catMessageList xs)
13311318 FatalError es -> error $ catMessages es
1319+
1320+ -- TODO: move this somewhere sensible
1321+
1322+ -- | Functor like property for Specification, but instead of a Haskell function (a -> b),
1323+ -- it takes a function symbol (t '[a] b) from a to b.
1324+ -- Note, in this context, a function symbol is some constructor of a witnesstype.
1325+ -- Eg. ProdFstW, InjRightW, SingletonW, etc. NOT the lifted versions like fst_ singleton_,
1326+ -- which construct Terms. We had to wait until here to define this because it
1327+ -- depends on Semigroup property of Specification, and Asserting equality
1328+ mapSpec ::
1329+ forall t a b .
1330+ AppRequires t '[a ] b =>
1331+ t '[a ] b ->
1332+ Specification a ->
1333+ Specification b
1334+ mapSpec f (ExplainSpec es s) = explainSpec es (mapSpec f s)
1335+ mapSpec f TrueSpec = mapTypeSpec f (emptySpec @ a )
1336+ mapSpec _ (ErrorSpec err) = ErrorSpec err
1337+ mapSpec f (MemberSpec as) = MemberSpec $ NE. nub $ fmap (semantics f) as
1338+ mapSpec f (SuspendedSpec x p) =
1339+ constrained $ \ x' ->
1340+ Exists (\ _ -> fatalError " mapSpec" ) (x :-> fold [Assert $ (x' ==. appTerm f (V x)), p])
1341+ mapSpec f (TypeSpec ts cant) = mapTypeSpec f ts <> notMemberSpec (map (semantics f) cant)
1342+
1343+ -- TODO generalizeme!
1344+ forwardPropagateSpec :: HasSpec a => Specification a -> Ctx a b -> Specification b
1345+ forwardPropagateSpec s CtxHOLE = s
1346+ forwardPropagateSpec s (CtxApp f (c :? Nil ))
1347+ | Evidence <- ctxHasSpec c = mapSpec f (forwardPropagateSpec s c)
1348+ forwardPropagateSpec _ _ = TrueSpec
1349+
0 commit comments