+\end{code}
+
+Ordering
+~~~~~~~~
+@Insts@ are ordered by their class/type info, rather than by their
+unique. This allows the context-reduction mechanism to use standard finite
+maps to do their stuff.
+
+\begin{code}
+instance Ord Inst where
+ compare = cmpInst
+instance Ord PredType where
+ compare = cmpPred
+
+instance Eq Inst where
+ (==) i1 i2 = case i1 `cmpInst` i2 of
+ EQ -> True
+ other -> False
+instance Eq PredType where
+ (==) p1 p2 = case p1 `cmpPred` p2 of
+ EQ -> True
+ other -> False
+
+cmpInst (Dict _ pred1 _) (Dict _ pred2 _)
+ = (pred1 `cmpPred` pred2)
+cmpInst (Dict _ _ _) other
+ = LT
+
+cmpInst (Method _ _ _ _ _ _) (Dict _ _ _)
+ = GT
+cmpInst (Method _ id1 tys1 _ _ _) (Method _ id2 tys2 _ _ _)
+ = (id1 `compare` id2) `thenCmp` (tys1 `compare` tys2)
+cmpInst (Method _ _ _ _ _ _) other
+ = LT
+
+cmpInst (LitInst _ lit1 ty1 _) (LitInst _ lit2 ty2 _)
+ = (lit1 `cmpOverLit` lit2) `thenCmp` (ty1 `compare` ty2)
+cmpInst (LitInst _ _ _ _) (FunDep _ _ _)
+ = LT
+cmpInst (LitInst _ _ _ _) other
+ = GT
+
+cmpInst (FunDep clas1 fds1 _) (FunDep clas2 fds2 _)
+ = (clas1 `compare` clas2) `thenCmp` (fds1 `compare` fds2)
+cmpInst (FunDep _ _ _) other
+ = GT
+
+cmpPred (Class c1 tys1) (Class c2 tys2)
+ = (c1 `compare` c2) `thenCmp` (tys1 `compare` tys2)
+cmpPred (IParam n1 ty1) (IParam n2 ty2)
+ = (n1 `compare` n2) `thenCmp` (ty1 `compare` ty2)
+cmpPred (Class _ _) (IParam _ _) = LT
+cmpPred _ _ = GT
+
+cmpOverLit (OverloadedIntegral i1) (OverloadedIntegral i2) = i1 `compare` i2
+cmpOverLit (OverloadedFractional f1) (OverloadedFractional f2) = f1 `compare` f2
+cmpOverLit (OverloadedIntegral _) (OverloadedFractional _) = LT
+cmpOverLit (OverloadedFractional _) (OverloadedIntegral _) = GT
+\end{code}
+
+
+Selection
+~~~~~~~~~
+\begin{code}
+instLoc (Dict u pred loc) = loc
+instLoc (Method u _ _ _ _ loc) = loc
+instLoc (LitInst u lit ty loc) = loc
+instLoc (FunDep _ _ loc) = loc
+
+getDictPred_maybe (Dict _ p _) = Just p
+getDictPred_maybe _ = Nothing
+
+getMethodTheta_maybe (Method _ _ _ theta _ _) = Just theta
+getMethodTheta_maybe _ = Nothing
+
+getDictClassTys (Dict u (Class clas tys) _) = (clas, tys)
+
+getFunDeps (FunDep clas fds _) = Just (clas, fds)
+getFunDeps _ = Nothing
+
+getFunDepsOfLIE lie = catMaybes (map getFunDeps (lieToList lie))
+
+getIPsOfPred (IParam n ty) = [(n, ty)]
+getIPsOfPred _ = []
+getIPsOfTheta theta = concatMap getIPsOfPred theta
+
+getIPs (Dict u (IParam n ty) loc) = [(n, ty)]
+getIPs (Method u id _ theta t loc) = getIPsOfTheta theta
+getIPs _ = []