@@ -263,8 +263,7 @@ replaceTypeClassDictionaries mn = everywhereM' (mkM go)
263263 where
264264 go (TypeClassDictionary trySuperclasses constraint dicts) = do
265265 env <- getEnv
266− let dicts' = fromMaybe (typeClassDictionaries env) dicts
267− entails env mn dicts' constraint trySuperclasses
266+ entails env mn dicts constraint trySuperclasses
268267 go other = return other
269268
270269-- |
@@ -287,7 +286,7 @@ data DictionaryValue
287286 -- |
288287 -- A subclass dictionary
289288 --
290− | SubclassDictionaryValue DictionaryValue Integer
289+ | SubclassDictionaryValue DictionaryValue (Qualified ProperName) Integer
291290 deriving (Show, Ord, Eq)
292291
293292-- |
@@ -307,12 +306,12 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
307306 filterModule _ = False
308307
309308 solve context' (className, tys) trySuperclasses =
310− case chooseSimplestDictionaries (go className tys) of
309+ case chooseSimplestDictionaries (go trySuperclasses className tys) of
311310 [] -> throwError . strMsg $ "No instance found for " ++ show className ++ " " ++ unwords (map prettyPrintTypeAtom tys)
312311 [dict] -> return (dictionaryValueToValue dict)
313312 _ -> throwError . strMsg $ "Overlapping instances found for " ++ show className ++ " " ++ unwords (map prettyPrintTypeAtom tys)
314313 where
315− go className' tys' =
314+ go trySuperclasses' className' tys' =
316315 -- Look for regular type instances
317316 [ mkDictionary (canonicalizeDictionary tcd) args
318317 | tcd <- context'
@@ -324,8 +323,8 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
324323 , args <- solveSubgoals subst (tcdDependencies tcd) ] ++
325324
326325 -- Look for implementations via superclasses
327− [ SubclassDictionaryValue suDict index
328− | trySuperclasses
326+ [ SubclassDictionaryValue suDict superclass index
327+ | trySuperclasses'
329328 , (subclassName, (args, _, implies)) <- M.toList (typeClasses env)
330329 -- Try each superclass
331330 , (index, (superclass, suTyArgs)) <- zip [0..] implies
@@ -334,7 +333,7 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
334333 -- Make sure the types unify with the types in the superclass implication
335334 , subst <- maybeToList . (>>= verifySubstitution) . fmap concat $ zipWithM (typeHeadsAreEqual moduleName env) tys' suTyArgs
336335 -- Finally, satisfy the subclass constraint
337− , suDict <- go subclassName (map (applySubst subst . TypeVar) args) ]
336+ , suDict <- go trySuperclasses' subclassName (map (applySubst subst . TypeVar) args) ]
338337
339338 -- Create dictionaries for subgoals which still need to be solved by calling go recursively
340339 -- E.g. the goal (Show a, Show b) => Show (Either a b) can be satisfied if the current type
@@ -342,7 +341,7 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
342341 solveSubgoals :: [(String, Type)] -> Maybe [(Qualified ProperName, [Type])] -> [Maybe [DictionaryValue]]
343342 solveSubgoals _ Nothing = return Nothing
344343 solveSubgoals subst (Just subgoals) = do
345− dict <- mapM (uncurry go . second (map (replaceAllTypeVars subst))) subgoals
344+ dict <- mapM (uncurry (go True) . second (map (replaceAllTypeVars subst))) subgoals
346345 return $ Just dict
347346 -- Make a dictionary from subgoal dictionaries by applying the correct function
348347 mkDictionary :: Qualified Ident -> Maybe [DictionaryValue] -> DictionaryValue
@@ -354,10 +353,9 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
354353 dictionaryValueToValue (LocalDictionaryValue fnName) = Var fnName
355354 dictionaryValueToValue (GlobalDictionaryValue fnName) = App (Var fnName) (ObjectLiteral [])
356355 dictionaryValueToValue (DependentDictionaryValue fnName dicts) = foldl App (Var fnName) (map dictionaryValueToValue dicts)
357− dictionaryValueToValue (SubclassDictionaryValue dict index) =
358− App (App (App (Var (Qualified (Just (ModuleName [ProperName C.prelude])) (Ident (C.!!))))
356+ dictionaryValueToValue (SubclassDictionaryValue dict superclassName index) =
357+ App (Accessor (show superclassName ++ "_" ++ show index)
359358 (Accessor C.__superclasses (dictionaryValueToValue dict)))
360− (NumericLiteral (Left index)))
361359 (ObjectLiteral [])
362360 -- Ensure that a substitution is valid
363361 verifySubstitution :: [(String, Type)] -> Maybe [(String, Type)]
@@ -500,7 +498,7 @@ instantiatePolyTypeWithUnknowns val (ForAll ident ty _) = do
500498instantiatePolyTypeWithUnknowns val (ConstrainedType constraints ty) = do
501499 dicts <- getTypeClassDictionaries
502500 (_, ty') <- instantiatePolyTypeWithUnknowns (error "Types under a constraint cannot themselves be constrained") ty
503− return (foldl App val (map (flip (TypeClassDictionary True) (Just dicts)) constraints), ty')
501+ return (foldl App val (map (flip (TypeClassDictionary True) dicts) constraints), ty')
504502instantiatePolyTypeWithUnknowns val ty = return (val, ty)
505503
506504-- |
@@ -624,7 +622,7 @@ infer' (Var var) = do
624622 case ty of
625623 ConstrainedType constraints ty' -> do
626624 dicts <- getTypeClassDictionaries
627− return $ TypedValue True (foldl App (Var var) (map (flip (TypeClassDictionary True) (Just dicts)) constraints)) ty'
625+ return $ TypedValue True (foldl App (Var var) (map (flip (TypeClassDictionary True) dicts) constraints)) ty'
628626 _ -> return $ TypedValue True (Var var) ty
629627infer' v@(Constructor c) = do
630628 env <- getEnv
@@ -646,6 +644,9 @@ infer' (IfThenElse cond th el) = do
646644infer' (Let ds val) = do
647645 (ds', val'@(TypedValue _ _ valTy)) <- inferLetBinding [] ds val infer
648646 return $ TypedValue True (Let ds' val') valTy
647+infer' (SuperClassDictionary className tys) = do
648+ dicts <- getTypeClassDictionaries
649+ return $ TypeClassDictionary False (className, tys) dicts
649650infer' (TypedValue checkType val ty) = do
650651 Just moduleName <- checkCurrentModule <$> get
651652 kind <- liftCheck $ kindOf moduleName ty
@@ -783,6 +784,17 @@ newSkolemScope = SkolemScope . runUnknown <$> fresh'
783784skolemize :: String -> Int -> SkolemScope -> Type -> Type
784785skolemize ident sko scope = replaceTypeVars ident (Skolem sko scope)
785786
787+-- |
788+-- This function has one purpose - to skolemize type variables appearing in a
789+-- SuperClassDictionary placeholder. These type variables are somewhat unique since they are the
790+-- only example of scoped type variables.
791+--
792+skolemizeTypesInValue :: String -> Int -> SkolemScope -> Value -> Value
793+skolemizeTypesInValue ident sko scope = everywhere (mkT go)
794+ where
795+ go (SuperClassDictionary c ts) = SuperClassDictionary c (map (skolemize ident sko scope) ts)
796+ go other = other
797+
786798-- |
787799-- Introduce skolem scope at every occurence of a ForAll
788800--
@@ -812,7 +824,8 @@ check' val (ForAll ident ty _) = do
812824 scope <- newSkolemScope
813825 sko <- newSkolemConstant
814826 let sk = skolemize ident sko scope ty
815− val' <- check val sk
827+ let skVal = skolemizeTypesInValue ident sko scope val
828+ val' <- check skVal sk
816829 return $ TypedValue True val' (ForAll ident ty (Just scope))
817830check' val t@(ConstrainedType constraints ty) = do
818831 dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
@@ -858,6 +871,19 @@ check' v@(Var var) ty = do
858871 case v' of
859872 Nothing -> throwError . strMsg $ "Unable to check type subsumption"
860873 Just v'' -> return $ TypedValue True v'' ty'
874+check' (SuperClassDictionary className tys) _ = do
875+ -- |
876+ -- Here, we replace a placeholder for a superclass dictionary with a regular
877+ -- TypeClassDictionary placeholder. The reason we do this is that it is necessary to have the
878+ -- correct super instance dictionaries in scope, and these are not available when the type class
879+ -- declaration gets desugared.
880+ --
881+ -- Note also that the first argument to TypeClassDictionary is False, meaning we _do not_ want
882+ -- to consider superclass instances when searching for this dictionary - doing so might lead
883+ -- to traversing a cycle in the instance graph.
884+ --
885+ dicts <- getTypeClassDictionaries
886+ return $ TypeClassDictionary False (className, tys) dicts
861887check' (TypedValue checkType val ty1) ty2 = do
862888 Just moduleName <- checkCurrentModule <$> get
863889 kind <- liftCheck $ kindOf moduleName ty1
@@ -990,7 +1016,7 @@ checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg ret = do
9901016 checkFunctionApplication fn ty arg ret
9911017checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do
9921018 dicts <- getTypeClassDictionaries
993− checkFunctionApplication' (foldl App fn (map (flip (TypeClassDictionary True) (Just dicts)) constraints)) fnTy arg ret
1019+ checkFunctionApplication' (foldl App fn (map (flip (TypeClassDictionary True) dicts) constraints)) fnTy arg ret
9941020checkFunctionApplication' _ fnTy arg _ = throwError . strMsg $ "Cannot apply a function of type "
9951021 ++ prettyPrintType fnTy
9961022 ++ " to argument " ++ prettyPrintValue arg
@@ -1033,7 +1059,7 @@ subsumes' val ty1 (SaturatedTypeSynonym name tyArgs) = do
10331059subsumes' (Just val) (ConstrainedType constraints ty1) ty2 = do
10341060 dicts <- getTypeClassDictionaries
10351061 _ <- subsumes' Nothing ty1 ty2
1036− return . Just $ foldl App val (map (flip (TypeClassDictionary True) (Just dicts)) constraints)
1062+ return . Just $ foldl App val (map (flip (TypeClassDictionary True) dicts) constraints)
10371063subsumes' val (TypeApp f1 r1) (TypeApp f2 r2) | f1 == tyObject && f2 == tyObject = do
10381064 let
10391065 (ts1, r1') = rowToList r1