    -- |
    -- Module      :  Cryptol.Parser.Names
    -- Copyright   :  (c) 2013-2016 Galois, Inc.
    -- License     :  BSD3
    -- Maintainer  :  cryptol@galois.com
    -- Stability   :  provisional
    -- Portability :  portable
    --
    -- This module defines the scoping rules for value- and type-level
    -- names in Cryptol.

    module Cryptol.Parser.Names where

    import Cryptol.Parser.AST

    import           Data.Set (Set)
    import qualified Data.Set as Set

    -- | The names defined by a newtype.
    tnamesNT :: Newtype name -> ([Located name], ())
    tnamesNT :: Newtype name -> ([Located name], ())
    tnamesNT x :: Newtype name
    x = ([ Newtype name -> Located name
    forall name. Newtype name -> Located name
    nName Newtype name
    x ], ())

    -- | The names defined and used by a group of mutually recursive declarations.
    namesDs :: Ord name => [Decl name] -> ([Located name], Set name)
    namesDs :: [Decl name] -> ([Located name], Set name)
    namesDs ds :: [Decl name]
    ds = ([Located name]
    defs, [Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames [Located name]
    defs ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions [Set name]
    frees))
      where
      defs :: [Located name]
    defs          = [[Located name]] -> [Located name]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat [[Located name]]
    defss
      (defss :: [[Located name]]
    defss,frees :: [Set name]
    frees) = [([Located name], Set name)] -> ([[Located name]], [Set name])
    forall a b. [(a, b)] -> ([a], [b])
    unzip ((Decl name -> ([Located name], Set name))
    -> [Decl name] -> [([Located name], Set name)]
    forall a b. (a -> b) -> [a] -> [b]
    map Decl name -> ([Located name], Set name)
    forall name. Ord name => Decl name -> ([Located name], Set name)
    namesD [Decl name]
    ds)

    -- | The names defined and used by a single declarations.
    namesD :: Ord name => Decl name -> ([Located name], Set name)
    namesD :: Decl name -> ([Located name], Set name)
    namesD decl :: Decl name
    decl =
      case Decl name
    decl of
        DBind b :: Bind name
    b       -> Bind name -> ([Located name], Set name)
    forall name. Ord name => Bind name -> ([Located name], Set name)
    namesB Bind name
    b
        DPatBind p :: Pattern name
    p e :: Expr name
    e  -> (Pattern name -> [Located name]
    forall name. Pattern name -> [Located name]
    namesP Pattern name
    p, Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e)
        DSignature {} -> ([],Set name
    forall a. Set a
    Set.empty)
        DFixity{}     -> ([],Set name
    forall a. Set a
    Set.empty)
        DPragma {}    -> ([],Set name
    forall a. Set a
    Set.empty)
        DType {}      -> ([],Set name
    forall a. Set a
    Set.empty)
        DProp {}      -> ([],Set name
    forall a. Set a
    Set.empty)
        DLocated d :: Decl name
    d _  -> Decl name -> ([Located name], Set name)
    forall name. Ord name => Decl name -> ([Located name], Set name)
    namesD Decl name
    d

    -- | The names defined and used by a single declarations in such a way
    -- that they cannot be duplicated in a file. For example, it is fine
    -- to use @x@ on the RHS of two bindings, but not on the LHS of two
    -- type signatures.
    allNamesD :: Ord name => Decl name -> [Located name]
    allNamesD :: Decl name -> [Located name]
    allNamesD decl :: Decl name
    decl =
      case Decl name
    decl of
        DBind b :: Bind name
    b         -> ([Located name], Set name) -> [Located name]
    forall a b. (a, b) -> a
    fst (Bind name -> ([Located name], Set name)
    forall name. Ord name => Bind name -> ([Located name], Set name)
    namesB Bind name
    b)
        DPatBind p :: Pattern name
    p _    -> Pattern name -> [Located name]
    forall name. Pattern name -> [Located name]
    namesP Pattern name
    p
        DSignature ns :: [Located name]
    ns _ -> [Located name]
    ns
        DFixity _ ns :: [Located name]
    ns    -> [Located name]
    ns
        DPragma ns :: [Located name]
    ns _    -> [Located name]
    ns
        DType ts :: TySyn name
    ts        -> [TySyn name -> Located name
    forall name. TySyn name -> Located name
    tsName TySyn name
    ts]
        DProp ps :: PropSyn name
    ps        -> [PropSyn name -> Located name
    forall name. PropSyn name -> Located name
    psName PropSyn name
    ps]
        DLocated d :: Decl name
    d _    -> Decl name -> [Located name]
    forall name. Ord name => Decl name -> [Located name]
    allNamesD Decl name
    d

    -- | The names defined and used by a single binding.
    namesB :: Ord name => Bind name -> ([Located name], Set name)
    namesB :: Bind name -> ([Located name], Set name)
    namesB b :: Bind name
    b = ([Bind name -> Located name
    forall name. Bind name -> Located name
    bName Bind name
    b], [Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames ([Pattern name] -> [Located name]
    forall name. [Pattern name] -> [Located name]
    namesPs (Bind name -> [Pattern name]
    forall name. Bind name -> [Pattern name]
    bParams Bind name
    b)) (BindDef name -> Set name
    forall name. Ord name => BindDef name -> Set name
    namesDef (Located (BindDef name) -> BindDef name
    forall a. Located a -> a
    thing (Bind name -> Located (BindDef name)
    forall name. Bind name -> Located (BindDef name)
    bDef Bind name
    b))))

    namesDef :: Ord name => BindDef name -> Set name
    namesDef :: BindDef name -> Set name
    namesDef DPrim     = Set name
    forall a. Set a
    Set.empty
    namesDef (DExpr e :: Expr name
    e) = Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e

    -- | The names used by an expression.
    namesE :: Ord name => Expr name -> Set name
    namesE :: Expr name -> Set name
    namesE expr :: Expr name
    expr =
      case Expr name
    expr of
        EVar x :: name
    x        -> name -> Set name
    forall a. a -> Set a
    Set.singleton name
    x
        ELit _        -> Set name
    forall a. Set a
    Set.empty
        ENeg e :: Expr name
    e        -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        EComplement e :: Expr name
    e -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        EGenerate e :: Expr name
    e   -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        ETuple es :: [Expr name]
    es     -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Expr name -> Set name) -> [Expr name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE [Expr name]
    es)
        ERecord fs :: [Named (Expr name)]
    fs    -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Named (Expr name) -> Set name)
    -> [Named (Expr name)] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE (Expr name -> Set name)
    -> (Named (Expr name) -> Expr name)
    -> Named (Expr name)
    -> Set name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Named (Expr name) -> Expr name
    forall a. Named a -> a
    value) [Named (Expr name)]
    fs)
        ESel e :: Expr name
    e _      -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        EUpd mb :: Maybe (Expr name)
    mb fs :: [UpdField name]
    fs    -> let e :: Set name
    e = Set name
    -> (Expr name -> Set name) -> Maybe (Expr name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Maybe (Expr name)
    mb
                         in [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions (Set name
    e Set name -> [Set name] -> [Set name]
    forall a. a -> [a] -> [a]
    : (UpdField name -> Set name) -> [UpdField name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map UpdField name -> Set name
    forall name. Ord name => UpdField name -> Set name
    namesUF [UpdField name]
    fs)
        EList es :: [Expr name]
    es      -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Expr name -> Set name) -> [Expr name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE [Expr name]
    es)
        EFromTo{}     -> Set name
    forall a. Set a
    Set.empty
        EInfFrom e :: Expr name
    e e' :: Maybe (Expr name)
    e' -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e) (Set name
    -> (Expr name -> Set name) -> Maybe (Expr name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Maybe (Expr name)
    e')
        EComp e :: Expr name
    e arms :: [[Match name]]
    arms  -> let (dss :: [[Located name]]
    dss,uss :: [Set name]
    uss) = [([Located name], Set name)] -> ([[Located name]], [Set name])
    forall a b. [(a, b)] -> ([a], [b])
    unzip (([Match name] -> ([Located name], Set name))
    -> [[Match name]] -> [([Located name], Set name)]
    forall a b. (a -> b) -> [a] -> [b]
    map [Match name] -> ([Located name], Set name)
    forall name. Ord name => [Match name] -> ([Located name], Set name)
    namesArm [[Match name]]
    arms)
                         in Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union ([Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames ([[Located name]] -> [Located name]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat [[Located name]]
    dss) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e))
                                      ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions [Set name]
    uss)
        EApp e1 :: Expr name
    e1 e2 :: Expr name
    e2    -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e1) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e2)
        EAppT e :: Expr name
    e _     -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        EIf e1 :: Expr name
    e1 e2 :: Expr name
    e2 e3 :: Expr name
    e3  -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e1) (Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e2) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e3))
        EWhere  e :: Expr name
    e ds :: [Decl name]
    ds  -> let (bs :: [Located name]
    bs,xs :: Set name
    xs) = [Decl name] -> ([Located name], Set name)
    forall name. Ord name => [Decl name] -> ([Located name], Set name)
    namesDs [Decl name]
    ds
                         in Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union ([Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames [Located name]
    bs (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e)) Set name
    xs
        ETyped e :: Expr name
    e _    -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        ETypeVal _    -> Set name
    forall a. Set a
    Set.empty
        EFun ps :: [Pattern name]
    ps e :: Expr name
    e     -> [Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames ([Pattern name] -> [Located name]
    forall name. [Pattern name] -> [Located name]
    namesPs [Pattern name]
    ps) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e)
        ELocated e :: Expr name
    e _  -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e

        ESplit e :: Expr name
    e      -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        EParens e :: Expr name
    e     -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e
        EInfix a :: Expr name
    a o :: Located name
    o _ b :: Expr name
    b-> name -> Set name -> Set name
    forall a. Ord a => a -> Set a -> Set a
    Set.insert (Located name -> name
    forall a. Located a -> a
    thing Located name
    o) (Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    a) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    b))

    namesUF :: Ord name => UpdField name -> Set name
    namesUF :: UpdField name -> Set name
    namesUF (UpdField _ _ e :: Expr name
    e) = Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e

    -- | The names defined by a group of patterns.
    namesPs :: [Pattern name] -> [Located name]
    namesPs :: [Pattern name] -> [Located name]
    namesPs = (Pattern name -> [Located name])
    -> [Pattern name] -> [Located name]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap Pattern name -> [Located name]
    forall name. Pattern name -> [Located name]
    namesP

    -- | The names defined by a pattern.  These will always be unqualified names.
    namesP :: Pattern name -> [Located name]
    namesP :: Pattern name -> [Located name]
    namesP pat :: Pattern name
    pat =
      case Pattern name
    pat of
        PVar x :: Located name
    x        -> [Located name
    x]
        PWild         -> []
        PTuple ps :: [Pattern name]
    ps     -> [Pattern name] -> [Located name]
    forall name. [Pattern name] -> [Located name]
    namesPs [Pattern name]
    ps
        PRecord fs :: [Named (Pattern name)]
    fs    -> [Pattern name] -> [Located name]
    forall name. [Pattern name] -> [Located name]
    namesPs ((Named (Pattern name) -> Pattern name)
    -> [Named (Pattern name)] -> [Pattern name]
    forall a b. (a -> b) -> [a] -> [b]
    map Named (Pattern name) -> Pattern name
    forall a. Named a -> a
    value [Named (Pattern name)]
    fs)
        PList ps :: [Pattern name]
    ps      -> [Pattern name] -> [Located name]
    forall name. [Pattern name] -> [Located name]
    namesPs [Pattern name]
    ps
        PTyped p :: Pattern name
    p _    -> Pattern name -> [Located name]
    forall name. Pattern name -> [Located name]
    namesP Pattern name
    p
        PSplit p1 :: Pattern name
    p1 p2 :: Pattern name
    p2  -> [Pattern name] -> [Located name]
    forall name. [Pattern name] -> [Located name]
    namesPs [Pattern name
    p1,Pattern name
    p2]
        PLocated p :: Pattern name
    p _  -> Pattern name -> [Located name]
    forall name. Pattern name -> [Located name]
    namesP Pattern name
    p

    -- | The names defined and used by a match.
    namesM :: Ord name => Match name -> ([Located name], Set name)
    namesM :: Match name -> ([Located name], Set name)
    namesM (Match p :: Pattern name
    p e :: Expr name
    e)  = (Pattern name -> [Located name]
    forall name. Pattern name -> [Located name]
    namesP Pattern name
    p, Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    namesE Expr name
    e)
    namesM (MatchLet b :: Bind name
    b) = Bind name -> ([Located name], Set name)
    forall name. Ord name => Bind name -> ([Located name], Set name)
    namesB Bind name
    b

    -- | The names defined and used by an arm of alist comprehension.
    namesArm :: Ord name => [Match name] -> ([Located name], Set name)
    namesArm :: [Match name] -> ([Located name], Set name)
    namesArm = (([Located name], Set name)
     -> ([Located name], Set name) -> ([Located name], Set name))
    -> ([Located name], Set name)
    -> [([Located name], Set name)]
    -> ([Located name], Set name)
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr ([Located name], Set name)
    -> ([Located name], Set name) -> ([Located name], Set name)
    forall a.
    Ord a =>
    ([Located a], Set a)
    -> ([Located a], Set a) -> ([Located a], Set a)
    combine ([],Set name
    forall a. Set a
    Set.empty) ([([Located name], Set name)] -> ([Located name], Set name))
    -> ([Match name] -> [([Located name], Set name)])
    -> [Match name]
    -> ([Located name], Set name)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Match name -> ([Located name], Set name))
    -> [Match name] -> [([Located name], Set name)]
    forall a b. (a -> b) -> [a] -> [b]
    map Match name -> ([Located name], Set name)
    forall name. Ord name => Match name -> ([Located name], Set name)
    namesM
      where combine :: ([Located a], Set a)
    -> ([Located a], Set a) -> ([Located a], Set a)
    combine (ds1 :: [Located a]
    ds1,fs1 :: Set a
    fs1) (ds2 :: [Located a]
    ds2,fs2 :: Set a
    fs2) =
              ( (Located a -> Bool) -> [Located a] -> [Located a]
    forall a. (a -> Bool) -> [a] -> [a]
    filter ((a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` (Located a -> a) -> [Located a] -> [a]
    forall a b. (a -> b) -> [a] -> [b]
    map Located a -> a
    forall a. Located a -> a
    thing [Located a]
    ds2) (a -> Bool) -> (Located a -> a) -> Located a -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Located a -> a
    forall a. Located a -> a
    thing) [Located a]
    ds1 [Located a] -> [Located a] -> [Located a]
    forall a. [a] -> [a] -> [a]
    ++ [Located a]
    ds2
              , Set a -> Set a -> Set a
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union Set a
    fs1 ([Located a] -> Set a -> Set a
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames [Located a]
    ds1 Set a
    fs2)
              )

    -- | Remove some defined variables from a set of free variables.
    boundLNames :: Ord name => [Located name] -> Set name -> Set name
    boundLNames :: [Located name] -> Set name -> Set name
    boundLNames = [name] -> Set name -> Set name
    forall name. Ord name => [name] -> Set name -> Set name
    boundNames ([name] -> Set name -> Set name)
    -> ([Located name] -> [name])
    -> [Located name]
    -> Set name
    -> Set name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Located name -> name) -> [Located name] -> [name]
    forall a b. (a -> b) -> [a] -> [b]
    map Located name -> name
    forall a. Located a -> a
    thing

    -- | Remove some defined variables from a set of free variables.
    boundNames :: Ord name => [name] -> Set name -> Set name
    boundNames :: [name] -> Set name -> Set name
    boundNames = Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    boundNamesSet (Set name -> Set name -> Set name)
    -> ([name] -> Set name) -> [name] -> Set name -> Set name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [name] -> Set name
    forall a. Ord a => [a] -> Set a
    Set.fromList

    -- | Remove some defined variables from a set of free variables.
    boundNamesSet :: Ord name => Set name -> Set name -> Set name
    boundNamesSet :: Set name -> Set name -> Set name
    boundNamesSet bs :: Set name
    bs xs :: Set name
    xs = Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.difference Set name
    xs Set name
    bs

    -- | The type names defined and used by a group of mutually recursive declarations.
    tnamesDs :: Ord name => [Decl name] -> ([Located name], Set name)
    tnamesDs :: [Decl name] -> ([Located name], Set name)
    tnamesDs ds :: [Decl name]
    ds = ([Located name]
    defs, [Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames [Located name]
    defs ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions [Set name]
    frees))
      where
      defs :: [Located name]
    defs          = [[Located name]] -> [Located name]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat [[Located name]]
    defss
      (defss :: [[Located name]]
    defss,frees :: [Set name]
    frees) = [([Located name], Set name)] -> ([[Located name]], [Set name])
    forall a b. [(a, b)] -> ([a], [b])
    unzip ((Decl name -> ([Located name], Set name))
    -> [Decl name] -> [([Located name], Set name)]
    forall a b. (a -> b) -> [a] -> [b]
    map Decl name -> ([Located name], Set name)
    forall name. Ord name => Decl name -> ([Located name], Set name)
    tnamesD [Decl name]
    ds)

    -- | The type names defined and used by a single declaration.
    tnamesD :: Ord name => Decl name -> ([Located name], Set name)
    tnamesD :: Decl name -> ([Located name], Set name)
    tnamesD decl :: Decl name
    decl =
      case Decl name
    decl of
        DSignature _ s :: Schema name
    s       -> ([], Schema name -> Set name
    forall name. Ord name => Schema name -> Set name
    tnamesS Schema name
    s)
        DFixity {}           -> ([], Set name
    forall a. Set a
    Set.empty)
        DPragma {}           -> ([], Set name
    forall a. Set a
    Set.empty)
        DBind b :: Bind name
    b              -> ([], Bind name -> Set name
    forall name. Ord name => Bind name -> Set name
    tnamesB Bind name
    b)
        DPatBind _ e :: Expr name
    e         -> ([], Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e)
        DLocated d :: Decl name
    d _         -> Decl name -> ([Located name], Set name)
    forall name. Ord name => Decl name -> ([Located name], Set name)
    tnamesD Decl name
    d
        DType (TySyn n :: Located name
    n _ ps :: [TParam name]
    ps t :: Type name
    t)
                             -> ([Located name
    n], Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.difference (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t)
                                      ([name] -> Set name
    forall a. Ord a => [a] -> Set a
    Set.fromList ((TParam name -> name) -> [TParam name] -> [name]
    forall a b. (a -> b) -> [a] -> [b]
    map TParam name -> name
    forall n. TParam n -> n
    tpName [TParam name]
    ps)))
        DProp (PropSyn n :: Located name
    n _ ps :: [TParam name]
    ps cs :: [Prop name]
    cs)
                             -> ([Located name
    n], Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.difference ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Prop name -> Set name) -> [Prop name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Prop name -> Set name
    forall name. Ord name => Prop name -> Set name
    tnamesC [Prop name]
    cs))
                                      ([name] -> Set name
    forall a. Ord a => [a] -> Set a
    Set.fromList ((TParam name -> name) -> [TParam name] -> [name]
    forall a b. (a -> b) -> [a] -> [b]
    map TParam name -> name
    forall n. TParam n -> n
    tpName [TParam name]
    ps)))

    -- | The type names used by a single binding.
    tnamesB :: Ord name => Bind name -> Set name
    tnamesB :: Bind name -> Set name
    tnamesB b :: Bind name
    b = [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions [Set name
    setS, Set name
    setP, Set name
    setE]
      where
        setS :: Set name
    setS = Set name
    -> (Schema name -> Set name) -> Maybe (Schema name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Schema name -> Set name
    forall name. Ord name => Schema name -> Set name
    tnamesS (Bind name -> Maybe (Schema name)
    forall name. Bind name -> Maybe (Schema name)
    bSignature Bind name
    b)
        setP :: Set name
    setP = [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Pattern name -> Set name) -> [Pattern name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP (Bind name -> [Pattern name]
    forall name. Bind name -> [Pattern name]
    bParams Bind name
    b))
        setE :: Set name
    setE = BindDef name -> Set name
    forall name. Ord name => BindDef name -> Set name
    tnamesDef (Located (BindDef name) -> BindDef name
    forall a. Located a -> a
    thing (Bind name -> Located (BindDef name)
    forall name. Bind name -> Located (BindDef name)
    bDef Bind name
    b))

    tnamesDef :: Ord name => BindDef name -> Set name
    tnamesDef :: BindDef name -> Set name
    tnamesDef DPrim     = Set name
    forall a. Set a
    Set.empty
    tnamesDef (DExpr e :: Expr name
    e) = Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e

    -- | The type names used by an expression.
    tnamesE :: Ord name => Expr name -> Set name
    tnamesE :: Expr name -> Set name
    tnamesE expr :: Expr name
    expr =
      case Expr name
    expr of
        EVar _          -> Set name
    forall a. Set a
    Set.empty
        ELit _          -> Set name
    forall a. Set a
    Set.empty
        ENeg e :: Expr name
    e          -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e
        EComplement e :: Expr name
    e   -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e
        EGenerate e :: Expr name
    e     -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e
        ETuple es :: [Expr name]
    es       -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Expr name -> Set name) -> [Expr name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE [Expr name]
    es)
        ERecord fs :: [Named (Expr name)]
    fs      -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Named (Expr name) -> Set name)
    -> [Named (Expr name)] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE (Expr name -> Set name)
    -> (Named (Expr name) -> Expr name)
    -> Named (Expr name)
    -> Set name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Named (Expr name) -> Expr name
    forall a. Named a -> a
    value) [Named (Expr name)]
    fs)
        ESel e :: Expr name
    e _        -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e
        EUpd mb :: Maybe (Expr name)
    mb fs :: [UpdField name]
    fs      -> let e :: Set name
    e = Set name
    -> (Expr name -> Set name) -> Maybe (Expr name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Maybe (Expr name)
    mb
                           in [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions (Set name
    e Set name -> [Set name] -> [Set name]
    forall a. a -> [a] -> [a]
    : (UpdField name -> Set name) -> [UpdField name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map UpdField name -> Set name
    forall name. Ord name => UpdField name -> Set name
    tnamesUF [UpdField name]
    fs)
        EList es :: [Expr name]
    es        -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Expr name -> Set name) -> [Expr name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE [Expr name]
    es)
        EFromTo a :: Type name
    a b :: Maybe (Type name)
    b c :: Type name
    c t :: Maybe (Type name)
    t -> Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    a
                           Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    `Set.union` Set name
    -> (Type name -> Set name) -> Maybe (Type name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Maybe (Type name)
    b
                           Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    `Set.union` Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    c
                           Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    `Set.union` Set name
    -> (Type name -> Set name) -> Maybe (Type name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Maybe (Type name)
    t
        EInfFrom e :: Expr name
    e e' :: Maybe (Expr name)
    e'   -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e) (Set name
    -> (Expr name -> Set name) -> Maybe (Expr name) -> Set name
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Set name
    forall a. Set a
    Set.empty Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Maybe (Expr name)
    e')
        EComp e :: Expr name
    e mss :: [[Match name]]
    mss     -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e) ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Match name -> Set name) -> [Match name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Match name -> Set name
    forall name. Ord name => Match name -> Set name
    tnamesM ([[Match name]] -> [Match name]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat [[Match name]]
    mss)))
        EApp e1 :: Expr name
    e1 e2 :: Expr name
    e2      -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e1) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e2)
        EAppT e :: Expr name
    e fs :: [TypeInst name]
    fs      -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e) ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((TypeInst name -> Set name) -> [TypeInst name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map TypeInst name -> Set name
    forall name. Ord name => TypeInst name -> Set name
    tnamesTI [TypeInst name]
    fs))
        EIf e1 :: Expr name
    e1 e2 :: Expr name
    e2 e3 :: Expr name
    e3    -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e1) (Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e2) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e3))
        EWhere  e :: Expr name
    e ds :: [Decl name]
    ds    -> let (bs :: [Located name]
    bs,xs :: Set name
    xs) = [Decl name] -> ([Located name], Set name)
    forall name. Ord name => [Decl name] -> ([Located name], Set name)
    tnamesDs [Decl name]
    ds
                           in Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union ([Located name] -> Set name -> Set name
    forall name. Ord name => [Located name] -> Set name -> Set name
    boundLNames [Located name]
    bs (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e)) Set name
    xs
        ETyped e :: Expr name
    e t :: Type name
    t      -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e) (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t)
        ETypeVal t :: Type name
    t      -> Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t
        EFun ps :: [Pattern name]
    ps e :: Expr name
    e       -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Pattern name -> Set name) -> [Pattern name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP [Pattern name]
    ps)) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e)
        ELocated e :: Expr name
    e _    -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e

        ESplit e :: Expr name
    e        -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e
        EParens e :: Expr name
    e       -> Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e
        EInfix a :: Expr name
    a _ _ b :: Expr name
    b  -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    a) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    b)

    tnamesUF :: Ord name => UpdField name -> Set name
    tnamesUF :: UpdField name -> Set name
    tnamesUF (UpdField _ _ e :: Expr name
    e) = Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e

    tnamesTI :: Ord name => TypeInst name -> Set name
    tnamesTI :: TypeInst name -> Set name
    tnamesTI (NamedInst f :: Named (Type name)
    f)  = Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT (Named (Type name) -> Type name
    forall a. Named a -> a
    value Named (Type name)
    f)
    tnamesTI (PosInst t :: Type name
    t)    = Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t

    -- | The type names used by a pattern.
    tnamesP :: Ord name => Pattern name -> Set name
    tnamesP :: Pattern name -> Set name
    tnamesP pat :: Pattern name
    pat =
      case Pattern name
    pat of
        PVar _        -> Set name
    forall a. Set a
    Set.empty
        PWild         -> Set name
    forall a. Set a
    Set.empty
        PTuple ps :: [Pattern name]
    ps     -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Pattern name -> Set name) -> [Pattern name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP [Pattern name]
    ps)
        PRecord fs :: [Named (Pattern name)]
    fs    -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Named (Pattern name) -> Set name)
    -> [Named (Pattern name)] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map (Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP (Pattern name -> Set name)
    -> (Named (Pattern name) -> Pattern name)
    -> Named (Pattern name)
    -> Set name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Named (Pattern name) -> Pattern name
    forall a. Named a -> a
    value) [Named (Pattern name)]
    fs)
        PList ps :: [Pattern name]
    ps      -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Pattern name -> Set name) -> [Pattern name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP [Pattern name]
    ps)
        PTyped p :: Pattern name
    p t :: Type name
    t    -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP Pattern name
    p) (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t)
        PSplit p1 :: Pattern name
    p1 p2 :: Pattern name
    p2  -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP Pattern name
    p1) (Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP Pattern name
    p2)
        PLocated p :: Pattern name
    p _  -> Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP Pattern name
    p

    -- | The type names used by a match.
    tnamesM :: Ord name => Match name -> Set name
    tnamesM :: Match name -> Set name
    tnamesM (Match p :: Pattern name
    p e :: Expr name
    e)  = Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Pattern name -> Set name
    forall name. Ord name => Pattern name -> Set name
    tnamesP Pattern name
    p) (Expr name -> Set name
    forall name. Ord name => Expr name -> Set name
    tnamesE Expr name
    e)
    tnamesM (MatchLet b :: Bind name
    b) = Bind name -> Set name
    forall name. Ord name => Bind name -> Set name
    tnamesB Bind name
    b

    -- | The type names used by a type schema.
    tnamesS :: Ord name => Schema name -> Set name
    tnamesS :: Schema name -> Set name
    tnamesS (Forall params :: [TParam name]
    params props :: [Prop name]
    props ty :: Type name
    ty _) =
        Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.difference (Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Prop name -> Set name) -> [Prop name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Prop name -> Set name
    forall name. Ord name => Prop name -> Set name
    tnamesC [Prop name]
    props)) (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    ty))
            ([name] -> Set name
    forall a. Ord a => [a] -> Set a
    Set.fromList ((TParam name -> name) -> [TParam name] -> [name]
    forall a b. (a -> b) -> [a] -> [b]
    map TParam name -> name
    forall n. TParam n -> n
    tpName [TParam name]
    params))

    -- | The type names used by a prop.
    tnamesC :: Ord name => Prop name -> Set name
    tnamesC :: Prop name -> Set name
    tnamesC prop :: Prop name
    prop =
      case Prop name
    prop of
        CType t :: Type name
    t        -> Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t

    -- | Compute the type synonyms/type variables used by a type.
    tnamesT :: Ord name => Type name -> Set name
    tnamesT :: Type name -> Set name
    tnamesT ty :: Type name
    ty =
      case Type name
    ty of
        TWild         -> Set name
    forall a. Set a
    Set.empty
        TFun t1 :: Type name
    t1 t2 :: Type name
    t2    -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t1) (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t2)
        TSeq t1 :: Type name
    t1 t2 :: Type name
    t2    -> Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t1) (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t2)
        TBit          -> Set name
    forall a. Set a
    Set.empty
        TNum _        -> Set name
    forall a. Set a
    Set.empty
        TChar __ :: Char
    __      -> Set name
    forall a. Set a
    Set.empty
        TTuple ts :: [Type name]
    ts     -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Type name -> Set name) -> [Type name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT [Type name]
    ts)
        TRecord fs :: [Named (Type name)]
    fs    -> [Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Named (Type name) -> Set name)
    -> [Named (Type name)] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT (Type name -> Set name)
    -> (Named (Type name) -> Type name)
    -> Named (Type name)
    -> Set name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Named (Type name) -> Type name
    forall a. Named a -> a
    value) [Named (Type name)]
    fs)
        TLocated t :: Type name
    t _  -> Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t
        TUser x :: name
    x ts :: [Type name]
    ts    -> name -> Set name -> Set name
    forall a. Ord a => a -> Set a -> Set a
    Set.insert name
    x ([Set name] -> Set name
    forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
    Set.unions ((Type name -> Set name) -> [Type name] -> [Set name]
    forall a b. (a -> b) -> [a] -> [b]
    map Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT [Type name]
    ts))
        TParens t :: Type name
    t     -> Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    t
        TInfix a :: Type name
    a x :: Located name
    x _ c :: Type name
    c-> name -> Set name -> Set name
    forall a. Ord a => a -> Set a -> Set a
    Set.insert (Located name -> name
    forall a. Located a -> a
    thing Located name
    x)
                                    (Set name -> Set name -> Set name
    forall a. Ord a => Set a -> Set a -> Set a
    Set.union (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    a) (Type name -> Set name
    forall name. Ord name => Type name -> Set name
    tnamesT Type name
    c))