git.iko.soy/rowtype-yoga/purescript-native
on 61aea87 help sign in
84 lines 2.3 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Scope
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- Utility functions for working with names in scope
13--
14-----------------------------------------------------------------------------
16module Language.PureScript.Scope (
17 usedNamesDecl,
18 usedNamesValue,
19 usedNamesBinder,
20 usedNamesCaseAlternative,
21 usedNamesDoNotationElement,
22 unusedNames
23) where
25import Data.List ((\\), nub)
27import Language.PureScript.Declarations
28import Language.PureScript.Names
30usedNames :: (Declaration -> [Ident], Value -> [Ident], Binder -> [Ident], CaseAlternative -> [Ident], DoNotationElement -> [Ident])
31usedNames = everythingOnValues (++) f g h (const []) (const [])
32 where
33 f :: Declaration -> [Ident]
34 f (ValueDeclaration name _ _ _ _) = [name]
35 f _ = []
37 g :: Value -> [Ident]
38 g (Abs (Left arg) _) = [arg]
39 g (Var (Qualified Nothing name)) = [name]
40 g _ = []
42 h :: Binder -> [Ident]
43 h (VarBinder name) = [name]
44 h _ = []
46-- |
47-- Gather all used names appearing inside a declaration
48--
49usedNamesDecl :: Declaration -> [Ident]
50usedNamesDecl = let (f, _, _, _, _) = usedNames in nub . f
52-- |
53-- Gather all used names appearing inside a value
54--
55usedNamesValue :: Value -> [Ident]
56usedNamesValue = let (_, f, _, _, _) = usedNames in nub . f
58-- |
59-- Gather all used names appearing inside a binder
60--
61usedNamesBinder :: Binder -> [Ident]
62usedNamesBinder = let (_, _, f, _, _) = usedNames in nub . f
64-- |
65-- Gather all used names appearing inside a case alternative
66--
67usedNamesCaseAlternative :: CaseAlternative -> [Ident]
68usedNamesCaseAlternative = let (_, _, _, f, _) = usedNames in nub . f
70-- |
71-- Gather all used names appearing inside a do notation element
72--
73usedNamesDoNotationElement :: DoNotationElement -> [Ident]
74usedNamesDoNotationElement = let (_, _, _, _, f) = usedNames in nub . f
76-- |
77-- Generate a set of names which are unused inside a value, of the form @_{n}@ for an integer @n@
78--
79unusedNames :: [Ident] -> [Ident]
80unusedNames allNames =
81 let
82 varNames = map (Ident . ('_' :) . show) ([1..] :: [Int])
83 in
84 varNames \\ allNames