referredNss (n, Just d, _) =
  let fds    = getFunDocs d
      ts     = concatMap types fds
      names  = concatMap (extractPTermNames) ts
  in  S.map getNs $ S.fromList names

  where getFunDocs (FunDoc f)                        = [f]
        getFunDocs (DataDoc f fs)                    = f:fs
        getFunDocs (InterfaceDoc _ _ fs _ _ _ _ _ _) = fs
        getFunDocs (RecordDoc _ _ f fs _)            = f:fs
        getFunDocs (NamedImplementationDoc _ fd)     = [fd]
        getFunDocs (ModDoc _ _)                      = []
        types (FD _ _ args t _)                      = t:(map second args)
        second (_, x, _, _)                          = x


-- | Returns an NsDict of containing all known namespaces and their contents
referredNss (n, Just d, _) =
  let fds    = getFunDocs d
      ts     = concatMap types fds
      names  = concatMap (extractPTermNames) ts
  in  S.map getNs $ S.fromList names

  where getFunDocs (FunDoc f)                        = [f]
        getFunDocs (DataDoc f fs)                    = f:fs
        getFunDocs (InterfaceDoc _ _ fs _ _ _ _ _ _) = fs
        getFunDocs (RecordDoc _ _ f fs _)            = f:fs
        getFunDocs (NamedImplementationDoc _ fd)     = [fd]
        getFunDocs (ModDoc _ _)                      = []
        types (FD _ _ args t _)                      = t:(map second args)
        second (_, x, _, _)                          = x


-- | Returns an NsDict of containing all known namespaces and their contents
referredNss (n, Just d, _) =
  let fds    = getFunDocs d
      ts     = concatMap types fds
      names  = concatMap (extractPTermNames) ts
  in  S.map getNs $ S.fromList names

  where getFunDocs (FunDoc f)                        = [f]
        getFunDocs (DataDoc f fs)                    = f:fs
        getFunDocs (InterfaceDoc _ _ fs _ _ _ _ _ _) = fs
        getFunDocs (RecordDoc _ _ f fs _)            = f:fs
        getFunDocs (NamedImplementationDoc _ fd)     = [fd]
        getFunDocs (ModDoc _ _)                      = []
        types (FD _ _ args t _)                      = t:(map second args)
        second (_, x, _, _)                          = x


-- | Returns an NsDict of containing all known namespaces and their contents