@@ -77,20 +77,40 @@ runConstraintEnv ::
77 Members '[State VariableMap, State (MKM.Map Pred)] effs =>
77 Members '[State VariableMap, State (MKM.Map Pred)] effs =>
78 Eff (SubstituteEnv : Writer Substitution : Writer Constraint : State (Seq Constraint) : effs) a ->
78 Eff (SubstituteEnv : Writer Substitution : Writer Constraint : State (Seq Constraint) : effs) a ->
79 Eff effs ((a, Substitution), Seq Constraint)
79 Eff effs ((a, Substitution), Seq Constraint)
80−runConstraintEnv =
80+runConstraintEnv f = do
81− runState @(Seq Constraint) Empty
82+ runState @(Seq Constraint) Empty
82− . interpret @(Writer Constraint) (\(Tell x) -> modify (x :<|))
83+ . interpret @(Writer Constraint) (\(Tell x) -> modify (x :<|))
83− . runWriter @Substitution
84+ . runWriter @Substitution
84− . interpret @SubstituteEnv
85+ . interpret @SubstituteEnv
85− ( \(SubstituteEnv x) -> do
86+ ( \(SubstituteEnv x) -> do
86− modify @(Seq Constraint) (sub x <$>)
87+ -- modify @(Seq Constraint) (sub x <$>)
87− -- TODO: I have a feeling that variables in the env should already be closed over.
88+ -- TODone: I have a feeling that variables in the env should already be closed over.
88− -- (aka this is redundant)
89+ -- (aka this is redundant)
91+ -- No, they can have type variables. E g when they are arguments in a function.
89− modify @VariableMap (sub x <$>)
92+ modify @VariableMap (sub x <$>)
90− modify @(MKM.Map Pred) (sub x)
93+ modify @(MKM.Map Pred) (sub x)
92− return ()
95+ return ()
98+ return ((x, subs), sub subs <$> cs)
100+captureSubstitutions ::
101+ Members '[SubstituteEnv] effs =>
102+ Eff (Writer Substitution : effs) a ->
103+ Eff effs (a, Substitution)
104+captureSubstitutions f = do
106+ runWriter @Substitution
107+ . interpose @SubstituteEnv
108+ ( \(SubstituteEnv x) -> do
95data SubstituteEnv a where
115data SubstituteEnv a where
96 SubstituteEnv :: Substitution -> SubstituteEnv ()
116 SubstituteEnv :: Substitution -> SubstituteEnv ()
@@ -166,7 +186,7 @@ prettySubstitution = A.toJSON . M.mapKeysMonotonic (TL.pack . show) . fmap rende
167-- | @older <> newer@
187-- | @older <> newer@
168instance Semigroup Substitution where
188instance Semigroup Substitution where
169− x'@(Substitution x) <> (Substitution y) = Substitution (fmap (sub x') y <> x)
189+ x'@(Substitution x) <> y'@(Substitution y) = Substitution (M.unionWithKey (error "invariant") (fmap (sub y') x) (fmap (sub x') y))
171instance Monoid Substitution where
191instance Monoid Substitution where
172 mempty = Substitution mempty
192 mempty = Substitution mempty
@@ -598,8 +618,12 @@ inferComponents n ((S.toList -> paths) : otherComponents) = do
598 return $ fmap fst <$> tvsTypes
618 return $ fmap fst <$> tvsTypes
599 -- TODO: There should probably be some smarter predicate filtering
619 -- TODO: There should probably be some smarter predicate filtering
600 schemes <- types & traverse . traverse %%~ (\t -> solveAndClose p $ sub s $ preds :=> t)
620 schemes <- types & traverse . traverse %%~ (\t -> solveAndClose p $ sub s $ preds :=> t)
622+ (otherBindings, s') <-
623+ captureSubstitutions $
601− fmap (M.unionWith mergeBindings schemes) . withBindings (toScheme' <$> schemes) $
624+ withBindings (toScheme' <$> schemes) $
602− inferComponents n otherComponents
625+ inferComponents n otherComponents
626+ return $ M.unionWith mergeBindings (fmap (sub s') <$> schemes) otherBindings
604 bindingToNExpr :: NonEmpty VarName -> NExprLoc -> Binding NExpr
628 bindingToNExpr :: NonEmpty VarName -> NExprLoc -> Binding NExpr
605 bindingToNExpr name expr = NamedVar (StaticKey <$> name) (stripAnnotation expr) nullPos
629 bindingToNExpr name expr = NamedVar (StaticKey <$> name) (stripAnnotation expr) nullPos
@@ -724,6 +748,7 @@ inferGeneral' toPretty f = do
724 traceValue "substitutions_after_solving" $ prettySubstitution s'
748 traceValue "substitutions_after_solving" $ prettySubstitution s'
725 let s = subs <> s'
749 let s = subs <> s'
726 returnedS = Substitution . M.filterWithKey (\k _ -> k `RS.notMember` range) . unSubstitution $ s
750 returnedS = Substitution . M.filterWithKey (\k _ -> k `RS.notMember` range) . unSubstitution $ s
751+ traceValue "composed_substitution" $ prettySubstitution s
727 traceSubtree "returned" $ do
752 traceSubtree "returned" $ do
728 traceValue "substitutions" $ prettySubstitution returnedS
753 traceValue "substitutions" $ prettySubstitution returnedS
729 -- Filter for optimization purposes
754 -- Filter for optimization purposes