-- |
-- 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 Cryptol.Utils.RecordMap

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 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 [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
  ([[Located name]]
defss,[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 name
decl =
  case Decl name
decl of
    DBind Bind name
b       -> Bind name -> ([Located name], Set name)
forall name. Ord name => Bind name -> ([Located name], Set name)
namesB Bind name
b
    DPatBind Pattern name
p 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 Decl name
d Range
_  -> 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 name
decl =
  case Decl name
decl of
    DBind 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 Pattern name
p Expr name
_    -> Pattern name -> [Located name]
forall name. Pattern name -> [Located name]
namesP Pattern name
p
    DSignature [Located name]
ns Schema name
_ -> [Located name]
ns
    DFixity Fixity
_ [Located name]
ns    -> [Located name]
ns
    DPragma [Located name]
ns Pragma
_    -> [Located name]
ns
    DType TySyn name
ts        -> [TySyn name -> Located name
forall name. TySyn name -> Located name
tsName TySyn name
ts]
    DProp PropSyn name
ps        -> [PropSyn name -> Located name
forall name. PropSyn name -> Located name
psName PropSyn name
ps]
    DLocated Decl name
d Range
_    -> 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 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 BindDef name
DPrim     = Set name
forall a. Set a
Set.empty
namesDef (DExpr 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 name
expr =
  case Expr name
expr of
    EVar name
x        -> name -> Set name
forall a. a -> Set a
Set.singleton name
x
    ELit Literal
_        -> Set name
forall a. Set a
Set.empty
    ENeg Expr name
e        -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    EComplement Expr name
e -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    EGenerate Expr name
e   -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    ETuple [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 Rec (Expr name)
fs    -> [Set name] -> Set name
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (((Range, Expr name) -> Set name)
-> [(Range, 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)
-> ((Range, Expr name) -> Expr name)
-> (Range, Expr name)
-> Set name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Range, Expr name) -> Expr name
forall a b. (a, b) -> b
snd) (Rec (Expr name) -> [(Range, Expr name)]
forall a b. RecordMap a b -> [b]
recordElements Rec (Expr name)
fs))
    ESel Expr name
e Selector
_      -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    EUpd Maybe (Expr name)
mb [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 [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
    EFromToLessThan{} -> Set name
forall a. Set a
Set.empty
    EInfFrom Expr name
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 Expr name
e [[Match name]]
arms  -> let ([[Located name]]
dss,[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 Expr name
e1 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 Expr name
e [TypeInst name]
_     -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    EIf Expr name
e1 Expr name
e2 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  Expr name
e [Decl name]
ds  -> let ([Located name]
bs,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 Expr name
e Type name
_    -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    ETypeVal Type name
_    -> Set name
forall a. Set a
Set.empty
    EFun FunDesc name
_ [Pattern name]
ps 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 Expr name
e Range
_  -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e

    ESplit Expr name
e      -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    EParens Expr name
e     -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
namesE Expr name
e
    EInfix Expr name
a Located name
o Fixity
_ 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 UpdHow
_ [Located Selector]
_ 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 Pattern name
pat =
  case Pattern name
pat of
    PVar Located name
x        -> [Located name
x]
    Pattern name
PWild         -> []
    PTuple [Pattern name]
ps     -> [Pattern name] -> [Located name]
forall name. [Pattern name] -> [Located name]
namesPs [Pattern name]
ps
    PRecord Rec (Pattern name)
fs    -> [Pattern name] -> [Located name]
forall name. [Pattern name] -> [Located name]
namesPs (((Range, Pattern name) -> Pattern name)
-> [(Range, Pattern name)] -> [Pattern name]
forall a b. (a -> b) -> [a] -> [b]
map (Range, Pattern name) -> Pattern name
forall a b. (a, b) -> b
snd (Rec (Pattern name) -> [(Range, Pattern name)]
forall a b. RecordMap a b -> [b]
recordElements Rec (Pattern name)
fs))
    PList [Pattern name]
ps      -> [Pattern name] -> [Located name]
forall name. [Pattern name] -> [Located name]
namesPs [Pattern name]
ps
    PTyped Pattern name
p Type name
_    -> Pattern name -> [Located name]
forall name. Pattern name -> [Located name]
namesP Pattern name
p
    PSplit Pattern name
p1 Pattern name
p2  -> [Pattern name] -> [Located name]
forall name. [Pattern name] -> [Located name]
namesPs [Pattern name
p1,Pattern name
p2]
    PLocated Pattern name
p Range
_  -> 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 Pattern name
p 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 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 ([Located a]
ds1,Set a
fs1) ([Located a]
ds2,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 Set name
bs 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 [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
  ([[Located name]]
defss,[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 name
decl =
  case Decl name
decl of
    DSignature [Located name]
_ 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 Bind name
b              -> ([], Bind name -> Set name
forall name. Ord name => Bind name -> Set name
tnamesB Bind name
b)
    DPatBind Pattern name
_ Expr name
e         -> ([], Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e)
    DLocated Decl name
d Range
_         -> Decl name -> ([Located name], Set name)
forall name. Ord name => Decl name -> ([Located name], Set name)
tnamesD Decl name
d
    DType (TySyn Located name
n Maybe Fixity
_ [TParam name]
ps 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 Located name
n Maybe Fixity
_ [TParam name]
ps [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 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 BindDef name
DPrim     = Set name
forall a. Set a
Set.empty
tnamesDef (DExpr 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 name
expr =
  case Expr name
expr of
    EVar name
_          -> Set name
forall a. Set a
Set.empty
    ELit Literal
_          -> Set name
forall a. Set a
Set.empty
    ENeg Expr name
e          -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e
    EComplement Expr name
e   -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e
    EGenerate Expr name
e     -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e
    ETuple [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 Rec (Expr name)
fs      -> [Set name] -> Set name
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (((Range, Expr name) -> Set name)
-> [(Range, 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)
-> ((Range, Expr name) -> Expr name)
-> (Range, Expr name)
-> Set name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Range, Expr name) -> Expr name
forall a b. (a, b) -> b
snd) (Rec (Expr name) -> [(Range, Expr name)]
forall a b. RecordMap a b -> [b]
recordElements Rec (Expr name)
fs))
    ESel Expr name
e Selector
_        -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e
    EUpd Maybe (Expr name)
mb [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 [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 Type name
a Maybe (Type name)
b Type name
c 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
    EFromToLessThan Type name
a Type name
b 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` Type name -> Set name
forall name. Ord name => Type name -> Set name
tnamesT Type name
b
                                       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 Expr name
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 Expr name
e [[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 Expr name
e1 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 Expr name
e [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 Expr name
e1 Expr name
e2 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  Expr name
e [Decl name]
ds    -> let ([Located name]
bs,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 Expr name
e 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 Type name
t      -> Type name -> Set name
forall name. Ord name => Type name -> Set name
tnamesT Type name
t
    EFun FunDesc name
_ [Pattern name]
ps 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 Expr name
e Range
_    -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e

    ESplit Expr name
e        -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e
    EParens Expr name
e       -> Expr name -> Set name
forall name. Ord name => Expr name -> Set name
tnamesE Expr name
e
    EInfix Expr name
a Located name
_ Fixity
_ 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 UpdHow
_ [Located Selector]
_ 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 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 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 Pattern name
pat =
  case Pattern name
pat of
    PVar Located name
_        -> Set name
forall a. Set a
Set.empty
    Pattern name
PWild         -> Set name
forall a. Set a
Set.empty
    PTuple [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 Rec (Pattern name)
fs    -> [Set name] -> Set name
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (((Range, Pattern name) -> Set name)
-> [(Range, 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)
-> ((Range, Pattern name) -> Pattern name)
-> (Range, Pattern name)
-> Set name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Range, Pattern name) -> Pattern name
forall a b. (a, b) -> b
snd) (Rec (Pattern name) -> [(Range, Pattern name)]
forall a b. RecordMap a b -> [b]
recordElements Rec (Pattern name)
fs))
    PList [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 Pattern name
p 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 Pattern name
p1 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 Pattern name
p Range
_  -> 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 Pattern name
p 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 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 [TParam name]
params [Prop name]
props Type name
ty Maybe Range
_) =
    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 name
prop =
  case Prop name
prop of
    CType 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 Type name
ty =
  case Type name
ty of
    Type name
TWild         -> Set name
forall a. Set a
Set.empty
    TFun Type name
t1 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 Type name
t1 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)
    Type name
TBit          -> Set name
forall a. Set a
Set.empty
    TNum Integer
_        -> Set name
forall a. Set a
Set.empty
    TChar Char
__      -> Set name
forall a. Set a
Set.empty
    TTuple [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 Rec (Type name)
fs    -> [Set name] -> Set name
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions (((Range, Type name) -> Set name)
-> [(Range, 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)
-> ((Range, Type name) -> Type name)
-> (Range, Type name)
-> Set name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Range, Type name) -> Type name
forall a b. (a, b) -> b
snd) (Rec (Type name) -> [(Range, Type name)]
forall a b. RecordMap a b -> [b]
recordElements Rec (Type name)
fs))
    TTyApp [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 Type name
t Range
_  -> Type name -> Set name
forall name. Ord name => Type name -> Set name
tnamesT Type name
t
    TUser name
x [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 Type name
t     -> Type name -> Set name
forall name. Ord name => Type name -> Set name
tnamesT Type name
t
    TInfix Type name
a Located name
x Fixity
_ 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))