@@ -15,10 +15,12 @@ import GHC.Runtime.Interpreter as Interp
1515import GHC.Types.Name (nameOccName )
1616import GHC.Types.Name.Occurrence (occNameString )
1717import qualified GHC.Debugger.Logger as Logger
18- import GHC.Utils.Outputable (text , (<+>) , ppr )
18+ import GHC.Utils.Outputable (text , (<+>) , ppr , ($$) )
19+ import GHC.Utils.Panic
1920import Control.Monad.Reader
2021import GHC.Core.TyCo.Compare
2122import GHC.Driver.Config
23+ import GHC.Stack
2224
2325
2426
@@ -104,22 +106,41 @@ checkType ty = do
104106 t <- anyTerm
105107 unless (termType t `eqType` ty) (parseError (TermParseError " ty mismatch" ))
106108
109+ traceTerm :: HasCallStack => TermParser ()
110+ traceTerm = do
111+ t <- anyTerm
112+ liftDebugger $ logSDoc Logger. Debug (ppr t $$ callStackDoc)
113+
107114-- | Evaluate the currently focused term
108- seqTermP :: TermParser a -> TermParser a
115+ seqTermP :: HasCallStack => TermParser a -> TermParser a
109116seqTermP term_parser = do
110117 t <- anyTerm
111118 hsc_env <- liftDebugger $ getSession
112119 focus (liftIO $ seqTerm hsc_env t)
113120 term_parser
114121
122+ refreshTerm :: TermParser Term
123+ refreshTerm = do
124+ t <- anyTerm
125+ hsc_env <- liftDebugger $ getSession
126+ case t of
127+ Suspension {} -> do
128+ liftDebugger $ logSDoc Logger. Debug (ppr t <+> ppr (ty t))
129+ t' <- liftDebugger $ liftIO $ cvObtainTerm hsc_env 2 False (ty t) (val t)
130+ liftDebugger $ logSDoc Logger. Debug (ppr t' <+> ppr (ty t'))
131+ return t'
132+ _ -> return t
133+
134+
135+
115136-- | Change the focus of the term parser onto the specified term.
116137focus :: TermParser Term -> TermParser a -> TermParser a
117138focus parse_term term_parser =
118139 parse_term >>= \ t ->
119140 TermParser $ \ _ -> runTermParser term_parser t
120141
121142-- | Focus on a new subtree, after forcing it to WHNF.
122- focusSeq :: TermParser Term -> TermParser a -> TermParser a
143+ focusSeq :: HasCallStack => TermParser Term -> TermParser a -> TermParser a
123144focusSeq parse_term term_parser = focus parse_term (seqTermP term_parser)
124145
125146subtermTerm :: Int -> TermParser Term
@@ -128,6 +149,7 @@ subtermTerm idx = do
128149 case t of
129150 Term {subTerms}
130151 | idx < length subTerms -> do
152+ liftDebugger $ logSDoc Logger. Debug (ppr subTerms)
131153 pure (subTerms !! idx)
132154 | otherwise -> parseError (TermParseError $ " missing subterm index " <> show idx)
133155 other -> parseError (TermParseError $ " expected Term with subterms, got " <> termTag other)
@@ -188,8 +210,9 @@ newtypeWrapParser = do
188210
189211-- | Is the current focus a suspension?
190212isSuspension :: TermParser Bool
191- isSuspension = do
213+ isSuspension = focus refreshTerm $ do
192214 t <- anyTerm
215+ traceTerm
193216 case t of
194217 Suspension {} -> pure True
195218 other -> do
@@ -292,7 +315,8 @@ programTermParser =
292315 programAskThunkParser = do
293316 matchConstructorTerm " ProgramAskThunk"
294317 -- Get what we need to check THUNKiness for
295- is_thunk <- focus (subtermTerm 0 ) isSuspension
318+ traceTerm
319+ is_thunk <- focus (subtermTerm 1 ) isSuspension
296320 liftDebugger $ logSDoc Logger. Debug (text " is_thunk" <+> ppr is_thunk )
297321 bool_fv <- liftDebugger $ reifyBool is_thunk
298322 hsc_env <- liftDebugger $ getSession
0 commit comments