{-# LANGUAGE NamedFieldPuns #-}
module Language.PureScript.TypeChecker.Entailment
( InstanceContext
, SolverOptions(..)
, replaceTypeClassDictionaries
, newDictionaries
, entails
) where
import Prelude.Compat
import Protolude (ordNub)
import Control.Arrow (second, (&&&))
import Control.Monad.Error.Class (MonadError(..))
import Control.Monad.State
import Control.Monad.Supply.Class (MonadSupply(..))
import Control.Monad.Writer
import Data.Foldable (for_, fold, toList)
import Data.Function (on)
import Data.Functor (($>))
import Data.List (minimumBy, groupBy, nubBy, sortBy)
import Data.Maybe (fromMaybe, mapMaybe)
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Traversable (for)
import Data.Text (Text, stripPrefix, stripSuffix)
import qualified Data.Text as T
import qualified Data.List.NonEmpty as NEL
import Language.PureScript.AST
import Language.PureScript.Crash
import Language.PureScript.Environment
import Language.PureScript.Errors
import Language.PureScript.Names
import Language.PureScript.TypeChecker.Monad
import Language.PureScript.TypeChecker.Unify
import Language.PureScript.TypeClassDictionaries
import Language.PureScript.Types
import Language.PureScript.Label (Label(..))
import Language.PureScript.PSString (PSString, mkString, decodeString)
import qualified Language.PureScript.Constants as C
data Evidence
= NamedInstance (Qualified Ident)
| WarnInstance SourceType
| IsSymbolInstance PSString
| EmptyClassInstance
deriving (Show, Eq)
namedInstanceIdentifier :: Evidence -> Maybe (Qualified Ident)
namedInstanceIdentifier (NamedInstance i) = Just i
namedInstanceIdentifier _ = Nothing
type TypeClassDict = TypeClassDictionaryInScope Evidence
type InstanceContext = M.Map (Maybe ModuleName)
(M.Map (Qualified (ProperName 'ClassName))
(M.Map (Qualified Ident) (NEL.NonEmpty NamedDict)))
type Matching a = M.Map Text a
combineContexts :: InstanceContext -> InstanceContext -> InstanceContext
combineContexts = M.unionWith (M.unionWith (M.unionWith (<>)))
replaceTypeClassDictionaries
:: forall m
. (MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m, MonadSupply m)
=> Bool
-> Expr
-> m (Expr, [(Ident, InstanceContext, SourceConstraint)])
replaceTypeClassDictionaries shouldGeneralize expr = flip evalStateT M.empty $ do
let loop e = do
(e', solved) <- deferPass e
if getAny solved
then loop e'
else return e'
loop expr >>= generalizePass
where
deferPass :: Expr -> StateT InstanceContext m (Expr, Any)
deferPass = fmap (second fst) . runWriterT . f where
f :: Expr -> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) Expr
(_, f, _) = everywhereOnValuesTopDownM return (go True) return
generalizePass :: Expr -> StateT InstanceContext m (Expr, [(Ident, InstanceContext, SourceConstraint)])
generalizePass = fmap (second snd) . runWriterT . f where
f :: Expr -> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) Expr
(_, f, _) = everywhereOnValuesTopDownM return (go False) return
go :: Bool -> Expr -> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) Expr
go deferErrors (TypeClassDictionary constraint context hints) =
rethrow (addHints hints) $ entails (SolverOptions shouldGeneralize deferErrors) constraint context hints
go _ other = return other
data EntailsResult a
= Solved a TypeClassDict
| Unsolved SourceConstraint
| Deferred
deriving Show
data SolverOptions = SolverOptions
{ solverShouldGeneralize :: Bool
, solverDeferErrors :: Bool
}
data Matched t
= Match t
| Apart
| Unknown
deriving (Eq, Show, Functor)
instance Semigroup t => Semigroup (Matched t) where
(Match l) <> (Match r) = Match (l <> r)
Apart <> _ = Apart
_ <> Apart = Apart
_ <> _ = Unknown
instance Monoid t => Monoid (Matched t) where
mempty = Match mempty
entails
:: forall m
. (MonadState CheckState m, MonadError MultipleErrors m, MonadWriter MultipleErrors m, MonadSupply m)
=> SolverOptions
-> SourceConstraint
-> InstanceContext
-> [ErrorMessageHint]
-> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) Expr
entails SolverOptions{..} constraint context hints =
solve constraint
where
forClassName :: InstanceContext -> Qualified (ProperName 'ClassName) -> [SourceType] -> [TypeClassDict]
forClassName ctx cn@C.Warn [msg] =
findDicts ctx cn Nothing ++ [TypeClassDictionaryInScope [] 0 (WarnInstance msg) [] C.Warn [msg] Nothing]
forClassName _ C.IsSymbol args | Just dicts <- solveIsSymbol args = dicts
forClassName _ C.SymbolCompare args | Just dicts <- solveSymbolCompare args = dicts
forClassName _ C.SymbolAppend args | Just dicts <- solveSymbolAppend args = dicts
forClassName _ C.SymbolCons args | Just dicts <- solveSymbolCons args = dicts
forClassName _ C.RowUnion args | Just dicts <- solveUnion args = dicts
forClassName _ C.RowNub args | Just dicts <- solveNub args = dicts
forClassName _ C.RowLacks args | Just dicts <- solveLacks args = dicts
forClassName _ C.RowCons args | Just dicts <- solveRowCons args = dicts
forClassName _ C.RowToList args | Just dicts <- solveRowToList args = dicts
forClassName ctx cn@(Qualified (Just mn) _) tys = concatMap (findDicts ctx cn) (ordNub (Nothing : Just mn : map Just (mapMaybe ctorModules tys)))
forClassName _ _ _ = internalError "forClassName: expected qualified class name"
ctorModules :: SourceType -> Maybe ModuleName
ctorModules (TypeConstructor _ (Qualified (Just mn) _)) = Just mn
ctorModules (TypeConstructor _ (Qualified Nothing _)) = internalError "ctorModules: unqualified type name"
ctorModules (TypeApp _ ty _) = ctorModules ty
ctorModules (KindedType _ ty _) = ctorModules ty
ctorModules _ = Nothing
findDicts :: InstanceContext -> Qualified (ProperName 'ClassName) -> Maybe ModuleName -> [TypeClassDict]
findDicts ctx cn = fmap (fmap NamedInstance) . foldMap NEL.toList . foldMap M.elems . (>>= M.lookup cn) . flip M.lookup ctx
valUndefined :: Expr
valUndefined = Var nullSourceSpan (Qualified (Just (ModuleName [ProperName C.prim])) (Ident C.undefined))
solve :: SourceConstraint -> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) Expr
solve con = go 0 con
where
go :: Int -> SourceConstraint -> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) Expr
go work (Constraint _ className' tys' _) | work > 1000 = throwError . errorMessage $ PossiblyInfiniteInstance className' tys'
go work con'@(Constraint _ className' tys' conInfo) = WriterT . StateT . (withErrorMessageHint (ErrorSolvingConstraint con') .) . runStateT . runWriterT $ do
latestSubst <- lift . lift $ gets checkSubstitution
let tys'' = map (substituteType latestSubst) tys'
inferred <- lift get
classesInScope <- lift . lift $ gets (typeClasses . checkEnv)
TypeClassData{ typeClassDependencies } <- case M.lookup className' classesInScope of
Nothing -> throwError . errorMessage $ UnknownClass className'
Just tcd -> pure tcd
let instances = do
chain <- groupBy ((==) `on` tcdChain) $
sortBy (compare `on` (tcdChain &&& tcdIndex)) $
forClassName (combineContexts context inferred) className' tys''
let found = for chain $ \tcd ->
case matches typeClassDependencies tcd tys'' of
Apart -> Right ()
Match substs -> Left (Just (substs, tcd))
Unknown -> Left Nothing
case found of
Right _ -> []
Left Nothing -> []
Left (Just substsTcd) -> [substsTcd]
solution <- lift . lift $ unique tys'' instances
case solution of
Solved substs tcd -> do
tell (Any True, mempty)
lift . lift . for_ substs $ pairwiseM unifyTypes
let subst = fmap head substs
currentSubst <- lift . lift $ gets checkSubstitution
subst' <- lift . lift $ withFreshTypes tcd (fmap (substituteType currentSubst) subst)
lift . lift $ zipWithM_ (\t1 t2 -> do
let inferredType = replaceAllTypeVars (M.toList subst') t1
unifyTypes inferredType t2) (tcdInstanceTypes tcd) tys''
currentSubst' <- lift . lift $ gets checkSubstitution
let subst'' = fmap (substituteType currentSubst') subst'
args <- solveSubgoals subst'' (tcdDependencies tcd)
initDict <- lift . lift $ mkDictionary (tcdValue tcd) args
let match = foldr (\(className, index) dict -> subclassDictionaryValue dict className index)
initDict
(tcdPath tcd)
return match
Unsolved unsolved -> do
ident <- freshIdent ("dict" <> runProperName (disqualify (constraintClass unsolved)))
let qident = Qualified Nothing ident
newDicts <- lift . lift $ newDictionaries [] qident unsolved
let newContext = mkContext newDicts
modify (combineContexts newContext)
tell (mempty, [(ident, context, unsolved)])
return (Var nullSourceSpan qident)
Deferred ->
return (TypeClassDictionary (srcConstraint className' tys'' conInfo) context hints)
where
withFreshTypes
:: TypeClassDict
-> Matching SourceType
-> m (Matching SourceType)
withFreshTypes TypeClassDictionaryInScope{..} subst = do
let onType = everythingOnTypes S.union fromTypeVar
typeVarsInHead = foldMap onType tcdInstanceTypes
<> foldMap (foldMap (foldMap onType . constraintArgs)) tcdDependencies
typeVarsInSubst = S.fromList (M.keys subst)
uninstantiatedTypeVars = typeVarsInHead S.\\ typeVarsInSubst
newSubst <- traverse withFreshType (S.toList uninstantiatedTypeVars)
return (subst <> M.fromList newSubst)
where
fromTypeVar (TypeVar _ v) = S.singleton v
fromTypeVar _ = S.empty
withFreshType s = do
t <- freshType
return (s, t)
unique :: [SourceType] -> [(a, TypeClassDict)] -> m (EntailsResult a)
unique tyArgs []
| solverDeferErrors = return Deferred
| solverShouldGeneralize && (null tyArgs || any canBeGeneralized tyArgs) = return (Unsolved (srcConstraint className' tyArgs conInfo))
| otherwise = throwError . errorMessage $ NoInstanceFound (srcConstraint className' tyArgs conInfo)
unique _ [(a, dict)] = return $ Solved a dict
unique tyArgs tcds
| pairwiseAny overlapping (map snd tcds) =
throwError . errorMessage $ OverlappingInstances className' tyArgs (tcds >>= (toList . namedInstanceIdentifier . tcdValue . snd))
| otherwise = return $ uncurry Solved (minimumBy (compare `on` length . tcdPath . snd) tcds)
canBeGeneralized :: Type a -> Bool
canBeGeneralized TUnknown{} = True
canBeGeneralized (KindedType _ t _) = canBeGeneralized t
canBeGeneralized _ = False
overlapping :: TypeClassDict -> TypeClassDict -> Bool
overlapping TypeClassDictionaryInScope{ tcdPath = _ : _ } _ = False
overlapping _ TypeClassDictionaryInScope{ tcdPath = _ : _ } = False
overlapping TypeClassDictionaryInScope{ tcdDependencies = Nothing } _ = False
overlapping _ TypeClassDictionaryInScope{ tcdDependencies = Nothing } = False
overlapping tcd1 tcd2 = tcdValue tcd1 /= tcdValue tcd2
solveSubgoals :: Matching SourceType -> Maybe [SourceConstraint] -> WriterT (Any, [(Ident, InstanceContext, SourceConstraint)]) (StateT InstanceContext m) (Maybe [Expr])
solveSubgoals _ Nothing = return Nothing
solveSubgoals subst (Just subgoals) =
Just <$> traverse (go (work + 1) . mapConstraintArgs (map (replaceAllTypeVars (M.toList subst)))) subgoals
useEmptyDict :: Maybe [Expr] -> Expr
useEmptyDict args = foldl (App . Abs (VarBinder nullSourceSpan UnusedIdent)) valUndefined (fold args)
mkDictionary :: Evidence -> Maybe [Expr] -> m Expr
mkDictionary (NamedInstance n) args = return $ foldl App (Var nullSourceSpan n) (fold args)
mkDictionary EmptyClassInstance args = return (useEmptyDict args)
mkDictionary (WarnInstance msg) args = do
tell . errorMessage $ UserDefinedWarning msg
return (useEmptyDict args)
mkDictionary (IsSymbolInstance sym) _ =
let fields = [ ("reflectSymbol", Abs (VarBinder nullSourceSpan UnusedIdent) (Literal nullSourceSpan (StringLiteral sym))) ] in
return $ TypeClassDictionaryConstructorApp C.IsSymbol (Literal nullSourceSpan (ObjectLiteral fields))
subclassDictionaryValue :: Expr -> Qualified (ProperName 'ClassName) -> Integer -> Expr
subclassDictionaryValue dict className index =
App (Accessor (mkString (superclassName className index)) dict) valUndefined
solveIsSymbol :: [SourceType] -> Maybe [TypeClassDict]
solveIsSymbol [TypeLevelString ann sym] = Just [TypeClassDictionaryInScope [] 0 (IsSymbolInstance sym) [] C.IsSymbol [TypeLevelString ann sym] Nothing]
solveIsSymbol _ = Nothing
solveSymbolCompare :: [SourceType] -> Maybe [TypeClassDict]
solveSymbolCompare [arg0@(TypeLevelString _ lhs), arg1@(TypeLevelString _ rhs), _] =
let ordering = case compare lhs rhs of
LT -> C.orderingLT
EQ -> C.orderingEQ
GT -> C.orderingGT
args' = [arg0, arg1, srcTypeConstructor ordering]
in Just [TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.SymbolCompare args' Nothing]
solveSymbolCompare _ = Nothing
solveSymbolAppend :: [SourceType] -> Maybe [TypeClassDict]
solveSymbolAppend [arg0, arg1, arg2] = do
(arg0', arg1', arg2') <- appendSymbols arg0 arg1 arg2
let args' = [arg0', arg1', arg2']
pure [TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.SymbolAppend args' Nothing]
solveSymbolAppend _ = Nothing
appendSymbols :: SourceType -> SourceType -> SourceType -> Maybe (SourceType, SourceType, SourceType)
appendSymbols arg0@(TypeLevelString _ lhs) arg1@(TypeLevelString _ rhs) _ = Just (arg0, arg1, srcTypeLevelString (lhs <> rhs))
appendSymbols arg0@(TypeLevelString _ lhs) _ arg2@(TypeLevelString _ out) = do
lhs' <- decodeString lhs
out' <- decodeString out
rhs <- stripPrefix lhs' out'
pure (arg0, srcTypeLevelString (mkString rhs), arg2)
appendSymbols _ arg1@(TypeLevelString _ rhs) arg2@(TypeLevelString _ out) = do
rhs' <- decodeString rhs
out' <- decodeString out
lhs <- stripSuffix rhs' out'
pure (srcTypeLevelString (mkString lhs), arg1, arg2)
appendSymbols _ _ _ = Nothing
solveSymbolCons :: [SourceType] -> Maybe [TypeClassDict]
solveSymbolCons [arg0, arg1, arg2] = do
(arg0', arg1', arg2') <- consSymbol arg0 arg1 arg2
let args' = [arg0', arg1', arg2']
pure [TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.SymbolCons args' Nothing]
solveSymbolCons _ = Nothing
consSymbol :: SourceType -> SourceType -> SourceType -> Maybe (SourceType, SourceType, SourceType)
consSymbol _ _ arg@(TypeLevelString _ s) = do
(h, t) <- T.uncons =<< decodeString s
pure (mkTLString (T.singleton h), mkTLString t, arg)
where mkTLString = srcTypeLevelString . mkString
consSymbol arg1@(TypeLevelString _ h) arg2@(TypeLevelString _ t) _ = do
h' <- decodeString h
t' <- decodeString t
guard (T.length h' == 1)
pure (arg1, arg2, srcTypeLevelString (mkString $ h' <> t'))
consSymbol _ _ _ = Nothing
solveUnion :: [SourceType] -> Maybe [TypeClassDict]
solveUnion [l, r, u] = do
(lOut, rOut, uOut, cst) <- unionRows l r u
pure [ TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.RowUnion [lOut, rOut, uOut] cst ]
solveUnion _ = Nothing
unionRows :: SourceType -> SourceType -> SourceType -> Maybe (SourceType, SourceType, SourceType, Maybe [SourceConstraint])
unionRows l r _ =
guard canMakeProgress $> (l, r, rowFromList out, cons)
where
(fixed, rest) = rowToList l
rowVar = srcTypeVar "r"
(canMakeProgress, out, cons) =
case rest of
REmpty _ -> (True, (fixed, r), Nothing)
_ -> (not (null fixed), (fixed, rowVar), Just [ srcConstraint C.RowUnion [rest, r, rowVar] Nothing ])
solveRowCons :: [SourceType] -> Maybe [TypeClassDict]
solveRowCons [TypeLevelString ann sym, ty, r, _] =
Just [ TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.RowCons [TypeLevelString ann sym, ty, r, srcRCons (Label sym) ty r] Nothing ]
solveRowCons _ = Nothing
solveRowToList :: [SourceType] -> Maybe [TypeClassDict]
solveRowToList [r, _] = do
entries <- rowToRowList r
pure [ TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.RowToList [r, entries] Nothing ]
solveRowToList _ = Nothing
rowToRowList :: SourceType -> Maybe SourceType
rowToRowList r =
guard (eqType rest $ REmpty ()) $>
foldr rowListCons (srcTypeConstructor C.RowListNil) fixed
where
(fixed, rest) = rowToSortedList r
rowListCons (RowListItem _ lbl ty) tl =
foldl srcTypeApp (srcTypeConstructor C.RowListCons)
[ srcTypeLevelString (runLabel lbl)
, ty
, tl ]
solveNub :: [SourceType] -> Maybe [TypeClassDict]
solveNub [r, _] = do
r' <- nubRows r
pure [ TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.RowNub [r, r'] Nothing ]
solveNub _ = Nothing
nubRows :: SourceType -> Maybe SourceType
nubRows r =
guard (eqType rest $ REmpty ()) $>
rowFromList (nubBy ((==) `on` rowListLabel) fixed, rest)
where
(fixed, rest) = rowToSortedList r
solveLacks :: [SourceType] -> Maybe [TypeClassDict]
solveLacks [TypeLevelString ann sym, r] = do
(r', cst) <- rowLacks sym r
pure [ TypeClassDictionaryInScope [] 0 EmptyClassInstance [] C.RowLacks [TypeLevelString ann sym, r'] cst ]
solveLacks _ = Nothing
rowLacks :: PSString -> SourceType -> Maybe (SourceType, Maybe [SourceConstraint])
rowLacks sym r =
guard (lacksSym && canMakeProgress) $> (r, cst)
where
(fixed, rest) = rowToList r
lacksSym =
not $ sym `elem` (runLabel . rowListLabel <$> fixed)
(canMakeProgress, cst) = case rest of
REmpty _ -> (True, Nothing)
_ -> (not (null fixed), Just [ srcConstraint C.RowLacks [srcTypeLevelString sym, rest] Nothing ])
matches :: [FunctionalDependency] -> TypeClassDict -> [SourceType] -> Matched (Matching [SourceType])
matches deps TypeClassDictionaryInScope{..} tys =
let matched = zipWith typeHeadsAreEqual tys tcdInstanceTypes in
if not (covers matched)
then if any ((==) Apart . fst) matched then Apart else Unknown
else
let determinedSet = foldMap (S.fromList . fdDetermined) deps
solved = map snd . filter ((`S.notMember` determinedSet) . fst) $ zipWith (\(_, ts) i -> (i, ts)) matched [0..]
in verifySubstitution (M.unionsWith (++) solved)
where
covers :: [(Matched (), subst)] -> Bool
covers ms = finalSet == S.fromList [0..length ms - 1]
where
initialSet :: S.Set Int
initialSet = S.fromList . map snd . filter ((==) (Match ()) . fst . fst) $ zip ms [0..]
finalSet :: S.Set Int
finalSet = untilFixedPoint applyAll initialSet
untilFixedPoint :: Eq a => (a -> a) -> a -> a
untilFixedPoint f = go
where
go a | a' == a = a'
| otherwise = go a'
where a' = f a
applyAll :: S.Set Int -> S.Set Int
applyAll s = foldr applyDependency s deps
applyDependency :: FunctionalDependency -> S.Set Int -> S.Set Int
applyDependency FunctionalDependency{..} xs
| S.fromList fdDeterminers `S.isSubsetOf` xs = xs <> S.fromList fdDetermined
| otherwise = xs
typeHeadsAreEqual :: Type a -> Type a -> (Matched (), Matching [Type a])
typeHeadsAreEqual (KindedType _ t1 _) t2 = typeHeadsAreEqual t1 t2
typeHeadsAreEqual t1 (KindedType _ t2 _) = typeHeadsAreEqual t1 t2
typeHeadsAreEqual (TUnknown _ u1) (TUnknown _ u2) | u1 == u2 = (Match (), M.empty)
typeHeadsAreEqual (Skolem _ _ s1 _) (Skolem _ _ s2 _) | s1 == s2 = (Match (), M.empty)
typeHeadsAreEqual t (TypeVar _ v) = (Match (), M.singleton v [t])
typeHeadsAreEqual (TypeConstructor _ c1) (TypeConstructor _ c2) | c1 == c2 = (Match (), M.empty)
typeHeadsAreEqual (TypeLevelString _ s1) (TypeLevelString _ s2) | s1 == s2 = (Match (), M.empty)
typeHeadsAreEqual (TypeApp _ h1 t1) (TypeApp _ h2 t2) =
both (typeHeadsAreEqual h1 h2) (typeHeadsAreEqual t1 t2)
typeHeadsAreEqual (REmpty _) (REmpty _) = (Match (), M.empty)
typeHeadsAreEqual r1@RCons{} r2@RCons{} =
foldr both (uncurry go rest) common
where
(common, rest) = alignRowsWith typeHeadsAreEqual r1 r2
go :: ([RowListItem a], Type a) -> ([RowListItem a], Type a) -> (Matched (), Matching [Type a])
go (l, KindedType _ t1 _) (r, t2) = go (l, t1) (r, t2)
go (l, t1) (r, KindedType _ t2 _) = go (l, t1) (r, t2)
go ([], REmpty _) ([], REmpty _) = (Match (), M.empty)
go ([], TUnknown _ u1) ([], TUnknown _ u2) | u1 == u2 = (Match (), M.empty)
go ([], TypeVar _ v1) ([], TypeVar _ v2) | v1 == v2 = (Match (), M.empty)
go ([], Skolem _ _ sk1 _) ([], Skolem _ _ sk2 _) | sk1 == sk2 = (Match (), M.empty)
go ([], TUnknown _ _) _ = (Unknown, M.empty)
go (sd, r) ([], TypeVar _ v) = (Match (), M.singleton v [rowFromList (sd, r)])
go _ _ = (Apart, M.empty)
typeHeadsAreEqual (TUnknown _ _) _ = (Unknown, M.empty)
typeHeadsAreEqual _ _ = (Apart, M.empty)
both :: (Matched (), Matching [Type a]) -> (Matched (), Matching [Type a]) -> (Matched (), Matching [Type a])
both (b1, m1) (b2, m2) = (b1 <> b2, M.unionWith (++) m1 m2)
verifySubstitution :: Matching [Type a] -> Matched (Matching [Type a])
verifySubstitution mts = foldMap meet mts $> mts where
meet = pairwiseAll typesAreEqual
typesAreEqual :: Type a -> Type a -> Matched ()
typesAreEqual (KindedType _ t1 _) t2 = typesAreEqual t1 t2
typesAreEqual t1 (KindedType _ t2 _) = typesAreEqual t1 t2
typesAreEqual (TUnknown _ u1) (TUnknown _ u2) | u1 == u2 = Match ()
typesAreEqual (Skolem _ _ s1 _) (Skolem _ _ s2 _) | s1 == s2 = Match ()
typesAreEqual (Skolem _ _ _ _) _ = Unknown
typesAreEqual _ (Skolem _ _ _ _) = Unknown
typesAreEqual (TypeVar _ v1) (TypeVar _ v2) | v1 == v2 = Match ()
typesAreEqual (TypeLevelString _ s1) (TypeLevelString _ s2) | s1 == s2 = Match ()
typesAreEqual (TypeConstructor _ c1) (TypeConstructor _ c2) | c1 == c2 = Match ()
typesAreEqual (TypeApp _ h1 t1) (TypeApp _ h2 t2) = typesAreEqual h1 h2 <> typesAreEqual t1 t2
typesAreEqual (REmpty _) (REmpty _) = Match ()
typesAreEqual r1 r2 | isRCons r1 || isRCons r2 =
let (common, rest) = alignRowsWith typesAreEqual r1 r2
in fold common <> uncurry go rest
where
go :: ([RowListItem a], Type a) -> ([RowListItem a], Type a) -> Matched ()
go (l, KindedType _ t1 _) (r, t2) = go (l, t1) (r, t2)
go (l, t1) (r, KindedType _ t2 _) = go (l, t1) (r, t2)
go ([], TUnknown _ u1) ([], TUnknown _ u2) | u1 == u2 = Match ()
go ([], Skolem _ _ s1 _) ([], Skolem _ _ s2 _) | s1 == s2 = Match ()
go ([], Skolem _ _ _ _) _ = Unknown
go _ ([], Skolem _ _ _ _) = Unknown
go ([], REmpty _) ([], REmpty _) = Match ()
go ([], TypeVar _ v1) ([], TypeVar _ v2) | v1 == v2 = Match ()
go _ _ = Apart
typesAreEqual _ _ = Apart
isRCons :: Type a -> Bool
isRCons RCons{} = True
isRCons _ = False
newDictionaries
:: MonadState CheckState m
=> [(Qualified (ProperName 'ClassName), Integer)]
-> Qualified Ident
-> SourceConstraint
-> m [NamedDict]
newDictionaries path name (Constraint _ className instanceTy _) = do
tcs <- gets (typeClasses . checkEnv)
let TypeClassData{..} = fromMaybe (internalError "newDictionaries: type class lookup failed") $ M.lookup className tcs
supDicts <- join <$> zipWithM (\(Constraint ann supName supArgs _) index ->
newDictionaries ((supName, index) : path)
name
(Constraint ann supName (instantiateSuperclass (map fst typeClassArguments) supArgs instanceTy) Nothing)
) typeClassSuperclasses [0..]
return (TypeClassDictionaryInScope [] 0 name path className instanceTy Nothing : supDicts)
where
instantiateSuperclass :: [Text] -> [SourceType] -> [SourceType] -> [SourceType]
instantiateSuperclass args supArgs tys = map (replaceAllTypeVars (zip args tys)) supArgs
mkContext :: [NamedDict] -> InstanceContext
mkContext = foldr combineContexts M.empty . map fromDict where
fromDict d = M.singleton Nothing (M.singleton (tcdClassName d) (M.singleton (tcdValue d) (pure d)))
pairwiseAll :: Monoid m => (a -> a -> m) -> [a] -> m
pairwiseAll _ [] = mempty
pairwiseAll _ [_] = mempty
pairwiseAll p (x : xs) = foldMap (p x) xs <> pairwiseAll p xs
pairwiseAny :: (a -> a -> Bool) -> [a] -> Bool
pairwiseAny _ [] = False
pairwiseAny _ [_] = False
pairwiseAny p (x : xs) = any (p x) xs || pairwiseAny p xs
pairwiseM :: Applicative m => (a -> a -> m ()) -> [a] -> m ()
pairwiseM _ [] = pure ()
pairwiseM _ [_] = pure ()
pairwiseM p (x : xs) = traverse (p x) xs *> pairwiseM p xs