This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
/
Inspect.hs
195 lines (170 loc) · 6.54 KB
/
Inspect.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{-# LANGUAGE PatternGuards, CPP,
FlexibleInstances, FlexibleContexts, MultiParamTypeClasses,
TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans -fno-warn-name-shadowing #-}
-- |
-- Module : Scion.Inspect
-- Copyright : (c) Thomas Schilling 2008
-- License : BSD-style
--
-- Maintainer : nominolo@gmail.com
-- Stability : experimental
-- Portability : portable
--
-- Functionality to inspect Haskell programs.
--
module Scion.Inspect
( typeOfResult, prettyResult
, typeDecls, classDecls, familyDecls
, toplevelNames
, module Scion.Inspect.Find
, module Scion.Inspect.TypeOf
) where
import Scion.Utils()
import Scion.Inspect.Find
import Scion.Inspect.TypeOf
import GHC
import Bag
import Var ( varType )
import DataCon ( dataConUserType )
import Type ( tidyType )
import VarEnv ( emptyTidyEnv )
import Data.Generics.Biplate
import Data.Generics.UniplateStr hiding ( Str (..) )
import qualified Data.Generics.Str as U
import Outputable
import GHC.SYB.Utils
#ifdef DEBUG
--import FastString
import Test.QuickCheck()
import Test.GHC.Gen()
--import Debug.Trace
--import StaticFlags ( initStaticOpts )
#endif
------------------------------------------------------------------------------
typeOfResult :: SearchResult Id -> Maybe Type
typeOfResult (FoundId i) = Just $ tidyType emptyTidyEnv $ varType i
typeOfResult (FoundCon _ c) = Just $ dataConUserType c
typeOfResult _ = Nothing
prettyResult :: OutputableBndr id => SearchResult id -> SDoc
prettyResult (FoundId i) = ppr i
prettyResult (FoundName n) = ppr n
prettyResult (FoundCon _ c) = ppr c
prettyResult r = ppr r
------------------------------------------------------------------------------
typeDecls :: TypecheckedMod m => m -> [LTyClDecl Name]
typeDecls m | Just (grp, _, _, _, _) <- renamedSource m =
[ t | t <- hs_tyclds grp
, isDataDecl (unLoc t)
|| isTypeDecl (unLoc t)
|| isSynDecl (unLoc t) ]
-- XXX: include families?
typeDecls _ = error "typeDecls: No renamer information available."
classDecls :: RenamedSource -> [LTyClDecl Name]
classDecls (grp, _, _, _, _) =
[ t | t <- hs_tyclds grp
, isClassDecl (unLoc t) ]
familyDecls :: RenamedSource -> [LTyClDecl Name]
familyDecls (grp, _, _, _, _) =
[ t | t <- hs_tyclds grp
, isFamilyDecl (unLoc t) ]
toplevelNames :: TypecheckedMod m => m -> [Name]
toplevelNames m | Just (grp, _imps, _exps, _doc, _hmi) <- renamedSource m =
[ n | L _ tycld <- hs_tyclds grp
, L _ n <- tyClDeclNames tycld
]
++
let ValBindsOut bind_grps _sigs = hs_valds grp
in [ n | (_, binds0) <- bind_grps
, L _ bind <- bagToList binds0
, n <- case bind of
FunBind {fun_id = L _ n} -> [n]
PatBind {pat_lhs = L _ p} -> pat_names p
_ -> []
]
where
-- return names bound by pattern
pat_names :: Pat Name -> [Name]
pat_names pat =
[ n | Just n <- map pat_bind_name
(trace (showData Renamer 2 (pat, universe pat)) (universe pat)) ]
pat_bind_name :: Pat Name -> Maybe Name
pat_bind_name (VarPat id) = Just id
pat_bind_name (AsPat (L _ id) _) = Just id
pat_bind_name _ = Nothing
toplevelNames _ = []
------------------------------------------------------------------------------
instance Uniplate a => Biplate a a where
biplate = uniplate
instance Uniplate (Pat n) where
uniplate pat = case pat of
WildPat _ -> (Zero, \Zero -> pat)
VarPat _ -> (Zero, \Zero -> pat)
VarPatOut _n _ -> (Zero, \Zero -> pat) --down binds (VarPatOut n)
LazyPat (L s p) -> (One p, \(One p') -> LazyPat (L s p'))
AsPat n (L s p) -> (One p, \(One p') -> AsPat n (L s p'))
ParPat (L s p) -> (One p, \(One p') -> ParPat (L s p'))
BangPat (L s p) -> (One p, \(One p') -> BangPat (L s p'))
ListPat ps t -> down ps (\ps' -> ListPat ps' t)
TuplePat ps b t -> down ps (\ps' -> TuplePat ps' b t)
PArrPat ps t -> down ps (\ps' -> PArrPat ps' t)
ConPatIn c details -> down details (ConPatIn c)
ConPatOut dcon tvs pds binds args ty -> -- also look inside binds?
down args (\args' -> ConPatOut dcon tvs pds binds args' ty)
ViewPat e (L s p) t -> (One p, \(One p') -> ViewPat e (L s p') t)
QuasiQuotePat _ -> (Zero, \Zero -> pat)
LitPat _ -> (Zero, \Zero -> pat)
NPat _ _ _ -> (Zero, \Zero -> pat)
NPlusKPat _ _ _ _ -> (Zero, \Zero -> pat)
TypePat _ -> (Zero, \Zero -> pat)
SigPatIn (L s p) t -> (One p, \(One p') -> SigPatIn (L s p') t)
SigPatOut (L s p) t -> (One p, \(One p') -> SigPatOut (L s p') t)
CoPat w p t -> (One p, \(One p') -> CoPat w p' t)
instance Biplate (HsConDetails (LPat id) (HsRecFields id (LPat id))) (Pat id) where
biplate (PrefixCon ps) = down ps PrefixCon
biplate (RecCon fs) = down fs RecCon
biplate (InfixCon (L sl l) (L sr r)) =
(Two (One l) (One r),
\(Two (One l') (One r')) -> InfixCon (L sl l') (L sr r'))
instance (Uniplate arg) => Biplate (HsRecFields id (Located arg)) arg where
biplate (HsRecFields flds dotdot) =
down flds (\flds' -> HsRecFields flds' dotdot)
instance (Uniplate arg) => Biplate (HsRecField id (Located arg)) arg where
biplate (HsRecField lid (L sl arg) b) =
(One arg, \(One arg') -> HsRecField lid (L sl arg') b)
instance Biplate b a => Biplate (Bag b) a where
biplate = uniplateOnBag biplate
--instance Biplate (HsBindLr n n)
uniplateOnBag :: (a -> (U.Str b, U.Str b -> a))
-> Bag a -> (U.Str b, U.Str b -> Bag a)
uniplateOnBag f bag = (as, \ns -> listToBag (bs ns))
where (as, bs) = uniplateOnList f (bagToList bag)
down :: Biplate from to => from -> (from -> c) -> (U.Str to, U.Str to -> c)
down b f = (ps, \ps' -> f (set ps'))
where (ps, set) = biplate b
-- BiplateType from to = (Str to, Str to -> from)
instance Biplate b a => Biplate (Located b) a where
biplate (L s b) = down b (L s)
{-
instance Uniplate a => Biplate (Located a) a where
biplate (L s a) = (One a, \(One a') -> L s a')
-}
instance Biplate b a => Biplate [b] a where
biplate = uniplateOnList biplate
{-
fmap (fst . biplate) (listStr bs),
strList . fmap (snd . biplate))
-}
{-
-- biplate :: (Str a, Str a -> [b])
-- biplate :: (Str a, Str a -> b)
-}
{-
instance Biplate [(Located a)] a where
biplate las = (unLoc `fmap` listStr las,
zipWith (\(L s _) a -> L s a) las . strList)
-}
{-
instance Biplate a b => Biplate (Bag a) b where
biplate b = (foldBag
-}