Skip to content

Commit 5557609

Browse files
author
Andrew Farmer
committed
String -> HermitName
1 parent 2f2e502 commit 5557609

File tree

19 files changed

+207
-172
lines changed

19 files changed

+207
-172
lines changed

src/HERMIT/Dictionary/Common.hs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import Data.List
4848
import Data.Monoid
4949

5050
import Control.Arrow
51-
import Control.Monad
5251
import Control.Monad.IO.Class
5352

5453
import HERMIT.Context
@@ -75,6 +74,7 @@ callT = contextfreeT $ \ e -> case e of
7574
App {} -> return (collectArgs e)
7675
_ -> fail "not an application or variable occurence."
7776

77+
-- | Succeeds if we are looking at an application matching the given predicate.
7878
callPredT :: Monad m => (Id -> [CoreExpr] -> Bool) -> Transform c m CoreExpr (CoreExpr, [CoreExpr])
7979
callPredT p = do
8080
call@(Var i, args) <- callT
@@ -84,14 +84,10 @@ callPredT p = do
8484
-- | Succeeds if we are looking at an application of given function
8585
-- returning zero or more arguments to which it is applied.
8686
--
87-
-- Note: function name is found using findIdT, then resulting Id is compared.
88-
-- So callNameT "Data.Function.fix" works as expected.
89-
-- If findIdT cannot find an Id, falls back to string comparison.
90-
callNameT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
91-
=> String -> Transform c m CoreExpr (CoreExpr, [CoreExpr])
92-
callNameT nm = prefixFailMsg ("callNameT failed: not a call to '" ++ nm ++ ".") $ do
93-
p <- liftM (==) (findIdT nm) <+ return (cmpString2Var nm)
94-
callPredT (const . p)
87+
-- Note: comparison is performed with cmpHN2Var.
88+
callNameT :: MonadCatch m => HermitName -> Transform c m CoreExpr (CoreExpr, [CoreExpr])
89+
callNameT nm = prefixFailMsg ("callNameT failed: not a call to '" ++ show nm ++ ".")
90+
$ callPredT (const . cmpHN2Var nm)
9591

9692
-- | Succeeds if we are looking at a fully saturated function call.
9793
callSaturatedT :: Monad m => Transform c m CoreExpr (CoreExpr, [CoreExpr])
@@ -100,9 +96,8 @@ callSaturatedT = callPredT (\ i args -> idArity i == length args)
10096
-- idArity is conservatively set to zero by default.
10197

10298
-- | Succeeds if we are looking at an application of given function
103-
callNameG :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
104-
=> String -> Transform c m CoreExpr ()
105-
callNameG nm = prefixFailMsg "callNameG failed: " $ callNameT nm >>= \_ -> return ()
99+
callNameG :: MonadCatch m => HermitName -> Transform c m CoreExpr ()
100+
callNameG nm = prefixFailMsg "callNameG failed: " $ callNameT nm >> return ()
106101

107102
-- | Succeeds if we are looking at an application of a data constructor.
108103
callDataConT :: MonadCatch m => Transform c m CoreExpr (DataCon, [Type], [CoreExpr])
@@ -200,22 +195,28 @@ findBoundVarT p = do
200195
--------------------------------------------------------------------------------------------------
201196

202197
-- | Lookup the name in the context first, then, failing that, in GHC's global reader environment.
203-
findIdT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m) => String -> Transform c m a Id
204-
findIdT nm = prefixFailMsg ("Cannot resolve name " ++ nm ++ ", ") $ contextonlyT (findId nm)
198+
findIdT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
199+
=> HermitName -> Transform c m a Id
200+
findIdT nm = prefixFailMsg ("Cannot resolve name " ++ show nm ++ ", ") $ contextonlyT (findId nm)
205201

206202
-- | Lookup the name in the context first, then, failing that, in GHC's global reader environment.
207-
findVarT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m) => String -> Transform c m a Var
208-
findVarT nm = prefixFailMsg ("Cannot resolve name " ++ nm ++ ", ") $ contextonlyT (findVar nm)
203+
findVarT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
204+
=> HermitName -> Transform c m a Var
205+
findVarT nm = prefixFailMsg ("Cannot resolve name " ++ show nm ++ ", ") $ contextonlyT (findVar nm)
209206

210207
-- | Lookup the name in the context first, then, failing that, in GHC's global reader environment.
211-
findTyConT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m) => String -> Transform c m a TyCon
212-
findTyConT nm = prefixFailMsg ("Cannot resolve name " ++ nm ++ ", ") $ contextonlyT (findTyCon nm)
208+
findTyConT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
209+
=> HermitName -> Transform c m a TyCon
210+
findTyConT nm = prefixFailMsg ("Cannot resolve name " ++ show nm ++ ", ") $ contextonlyT (findTyCon nm)
213211

214212
-- | Lookup the name in the context first, then, failing that, in GHC's global reader environment.
215-
findTypeT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m) => String -> Transform c m a Type
216-
findTypeT nm = prefixFailMsg ("Cannot resolve name " ++ nm ++ ", ") $ contextonlyT (findType nm)
213+
findTypeT :: (BoundVars c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
214+
=> HermitName -> Transform c m a Type
215+
findTypeT nm = prefixFailMsg ("Cannot resolve name " ++ show nm ++ ", ") $ contextonlyT (findType nm)
217216

218217
-- TODO: "inScope" was defined elsewhere, but I've moved it here. Should it be combined with the above functions?
218+
-- Used in Dictionary.Inline to check if variables an in scope.
219+
-- Used in Dictionary.Fold
219220

220221
-- | Determine whether a variable is in scope.
221222
inScope :: ReadBindings c => c -> Var -> Bool

src/HERMIT/Dictionary/Composite.hs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ module HERMIT.Dictionary.Composite
1414
) where
1515

1616
import Control.Arrow
17-
import Control.Monad.IO.Class
17+
18+
import Data.String (fromString)
1819

1920
import HERMIT.Context
2021
import HERMIT.Core
22+
import HERMIT.External
2123
import HERMIT.GHC
22-
import HERMIT.Monad
2324
import HERMIT.Kure
24-
import HERMIT.External
25+
import HERMIT.Monad
26+
import HERMIT.Name
2527

2628
import HERMIT.Dictionary.Debug hiding (externals)
2729
import HERMIT.Dictionary.GHC hiding (externals)
@@ -56,19 +58,19 @@ externals =
5658

5759
------------------------------------------------------------------------------------------------------
5860

59-
basicCombinators :: [String]
60-
basicCombinators = ["$",".","id","flip","const","fst","snd","curry","uncurry"]
61+
basicCombinators :: [HermitName]
62+
basicCombinators = map fromString ["$",".","id","flip","const","fst","snd","curry","uncurry"]
6163

6264
-- | Unfold the current expression if it is one of the basic combinators:
6365
-- ('$'), ('.'), 'id', 'flip', 'const', 'fst', 'snd', 'curry', and 'uncurry'.
6466
-- This is intended to be used as a component of simplification traversals such as 'simplifyR' or 'bashR'.
65-
unfoldBasicCombinatorR :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
66-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
67+
unfoldBasicCombinatorR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
68+
, MonadCatch m )
6769
=> Rewrite c m CoreExpr
6870
unfoldBasicCombinatorR = setFailMsg "unfold-basic-combinator failed." $ unfoldNamesR basicCombinators
6971

70-
simplifyR :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
71-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
72+
simplifyR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
73+
, MonadCatch m, MonadUnique m )
7274
=> Rewrite c m Core
7375
simplifyR = setFailMsg "Simplify failed: nothing to simplify." $
7476
innermostR ( promoteBindR recToNonrecR
@@ -87,14 +89,14 @@ simplifyR = setFailMsg "Simplify failed: nothing to simplify." $
8789
-- basic combinators. See 'bashComponents' for a list of rewrites performed.
8890
-- Bash also performs occurrence analysis and de-zombification on the result, to update
8991
-- IdInfo attributes relied-upon by GHC.
90-
bashR :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
91-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
92+
bashR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
93+
, MonadCatch m, MonadUnique m )
9294
=> Rewrite c m Core
9395
bashR = bashExtendedWithR []
9496

9597
-- | An extensible bash. Given rewrites are performed before normal bash rewrites.
96-
bashExtendedWithR :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
97-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
98+
bashExtendedWithR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
99+
, MonadCatch m, MonadUnique m )
98100
=> [Rewrite c m Core] -> Rewrite c m Core
99101
bashExtendedWithR rs = bashUsingR (rs ++ map fst bashComponents)
100102

@@ -104,14 +106,13 @@ bashExtendedWithR rs = bashUsingR (rs ++ map fst bashComponents)
104106
-- Note: core fragment which fails linting is still returned! Otherwise would behave differently than bashR.
105107
-- Useful for debugging the bash command itself.
106108
bashDebugR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
107-
, HasDebugChan m, HasDynFlags m, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m
108-
, MonadThings m, MonadUnique m )
109+
, HasDebugChan m, HasDynFlags m, MonadCatch m, MonadUnique m )
109110
=> Rewrite c m Core
110111
bashDebugR = bashUsingR [ bracketR nm r >>> catchM (promoteT lintExprT >> idR) traceR
111112
| (r,nm) <- bashComponents ]
112113

113114
-- | Perform the 'bash' algorithm with a given list of rewrites.
114-
bashUsingR :: (ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, HasEmptyContext c, MonadCatch m)
115+
bashUsingR :: (AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadPath c Crumb, MonadCatch m)
115116
=> [Rewrite c m Core] -> Rewrite c m Core
116117
bashUsingR rs = setFailMsg "bash failed: nothing to do." $
117118
repeatR (occurAnalyseR >>> onetdR (catchesT rs)) >+> anytdR (promoteExprR dezombifyR) >+> occurAnalyseChangedR
@@ -136,8 +137,8 @@ bashHelp = "Iteratively apply the following rewrites until nothing changes:"
136137
: map snd (bashComponents :: [(RewriteH Core,String)] -- to resolve ambiguity
137138
)
138139
-- TODO: Think about a good order for bash.
139-
bashComponents :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
140-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
140+
bashComponents :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
141+
, MonadCatch m, MonadUnique m )
141142
=> [(Rewrite c m Core, String)]
142143
bashComponents =
143144
[ -- (promoteExprR occurAnalyseExprChangedR, "occur-analyse-expr") -- ??
@@ -173,13 +174,13 @@ bashComponents =
173174
-- | Smash is a more powerful but less efficient version of bash.
174175
-- Unlike bash, smash is not concerned with whether it duplicates work,
175176
-- and is intended for use during proving tasks.
176-
smashR :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
177-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
177+
smashR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
178+
, MonadCatch m, MonadUnique m )
178179
=> Rewrite c m Core
179180
smashR = smashExtendedWithR []
180181

181-
smashExtendedWithR :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
182-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
182+
smashExtendedWithR :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
183+
, MonadCatch m, MonadUnique m )
183184
=> [Rewrite c m Core] -> Rewrite c m Core
184185
smashExtendedWithR rs = smashUsingR (rs ++ map fst smashComponents1) (map fst smashComponents2)
185186

@@ -197,8 +198,8 @@ smashHelp = "A more powerful but less efficient version of \"bash\", intended fo
197198

198199

199200
-- | As bash, but with "let-nonrec-subst" instead of "let-nonrec-subst-safe".
200-
smashComponents1 :: ( ExtendPath c Crumb, ReadPath c Crumb, AddBindings c, ReadBindings c, HasEmptyContext c
201-
, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m )
201+
smashComponents1 :: ( AddBindings c, ExtendPath c Crumb, HasEmptyContext c, ReadBindings c, ReadPath c Crumb
202+
, MonadCatch m, MonadUnique m )
202203
=> [(Rewrite c m Core, String)]
203204
smashComponents1 =
204205
[ -- (promoteExprR occurAnalyseExprChangedR, "occur-analyse-expr") -- ??

src/HERMIT/Dictionary/FixPoint.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import Control.Arrow
1919
import Control.Monad
2020
import Control.Monad.IO.Class
2121

22+
import Data.String (fromString)
23+
2224
import HERMIT.Context
2325
import HERMIT.Core
2426
import HERMIT.Monad
@@ -224,12 +226,12 @@ isFixExprT :: TransformH CoreExpr (Type,CoreExpr)
224226
isFixExprT = withPatFailMsg (wrongExprForm "fix t f") $ -- fix :: forall a. (a -> a) -> a
225227
do (Var fixId, [Type ty, f]) <- callT
226228
fixId' <- findIdT fixLocation
227-
guardMsg (fixId == fixId') (unqualifiedName fixId ++ " does not match " ++ fixLocation)
229+
guardMsg (fixId == fixId') (unqualifiedName fixId ++ " does not match " ++ show fixLocation)
228230
return (ty,f)
229231

230232
--------------------------------------------------------------------------------------------------
231233

232-
fixLocation :: String
233-
fixLocation = "Data.Function.fix"
234+
fixLocation :: HermitName
235+
fixLocation = fromString "Data.Function.fix"
234236

235237
--------------------------------------------------------------------------------------------------

src/HERMIT/Dictionary/Fold.hs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module HERMIT.Dictionary.Fold
88
, stashFoldAnyR
99
-- * Unlifted fold interface
1010
, fold
11-
, unifyTypes
11+
, unifyTypes -- TODO: remove in favor of GHC's unification
1212
, tyMatchesToCoreExpr
1313
) where
1414

@@ -21,10 +21,11 @@ import qualified Data.Map as Map
2121

2222
import HERMIT.Core
2323
import HERMIT.Context
24-
import HERMIT.Monad
25-
import HERMIT.Kure
2624
import HERMIT.External
2725
import HERMIT.GHC
26+
import HERMIT.Kure
27+
import HERMIT.Monad
28+
import HERMIT.Name
2829

2930
import HERMIT.Dictionary.Common (varBindingDepthT,inScope,findIdT)
3031
import HERMIT.Dictionary.Inline hiding (externals)
@@ -35,7 +36,7 @@ import Prelude hiding (exp)
3536

3637
externals :: [External]
3738
externals =
38-
[ external "fold" (promoteExprR . foldR :: String -> RewriteH Core)
39+
[ external "fold" (promoteExprR . foldR :: HermitName -> RewriteH Core)
3940
[ "fold a definition"
4041
, ""
4142
, "double :: Int -> Int"
@@ -69,13 +70,11 @@ stashFoldAnyR = setFailMsg "Fold failed: no definitions could be folded." $
6970
catchesM =<< liftM (map stashFoldR) (liftM Map.keys (constT getStash))
7071

7172
foldR :: (ReadBindings c, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m, MonadUnique m)
72-
=> String -> Rewrite c m CoreExpr
73-
foldR nm = prefixFailMsg "Fold failed: " $ do
74-
v <- findIdT nm
75-
foldVarR v Nothing
73+
=> HermitName -> Rewrite c m CoreExpr
74+
foldR nm = prefixFailMsg "Fold failed: " $ findIdT nm >>= foldVarR Nothing
7675

77-
foldVarR :: (ReadBindings c, MonadCatch m, MonadUnique m) => Var -> Maybe BindingDepth -> Rewrite c m CoreExpr
78-
foldVarR v md = do
76+
foldVarR :: (ReadBindings c, MonadCatch m, MonadUnique m) => Maybe BindingDepth -> Var -> Rewrite c m CoreExpr
77+
foldVarR md v = do
7978
case md of
8079
Nothing -> return ()
8180
Just depth -> do depth' <- varBindingDepthT v

src/HERMIT/Dictionary/Function.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Control.Monad.IO.Class
1717

1818
import Data.List (nub, intercalate, intersect, partition, transpose)
1919
import Data.Maybe (isNothing)
20+
import Data.String (fromString)
2021

2122
import HERMIT.Context
2223
import HERMIT.Core
@@ -133,7 +134,7 @@ appArgM n e | n < 0 = fail "appArgM: arg must be non-negative"
133134
buildCompositionT :: (BoundVars c, HasDynFlags m, HasHermitMEnv m, HasHscEnv m, MonadCatch m, MonadIO m, MonadThings m)
134135
=> CoreExpr -> CoreExpr -> Transform c m x CoreExpr
135136
buildCompositionT f g = do
136-
composeId <- findIdT "Data.Function.."
137+
composeId <- findIdT $ fromString "Data.Function.."
137138
fDot <- buildApplicationM (varToCoreExpr composeId) f
138139
buildApplicationM fDot g
139140

@@ -168,15 +169,15 @@ buildFixT :: (BoundVars c, HasHscEnv m, HasHermitMEnv m, MonadCatch m, MonadIO m
168169
=> CoreExpr -> Transform c m x CoreExpr
169170
buildFixT f = do
170171
(tvs, ty) <- endoFunExprTypeM f
171-
fixId <- findIdT "Data.Function.fix"
172+
fixId <- findIdT $ fromString "Data.Function.fix"
172173
f' <- substOrApply f [ (v, varToCoreExpr v) | v <- tvs ]
173174
return $ mkCoreLams tvs $ mkCoreApps (varToCoreExpr fixId) [Type ty, f']
174175

175176
-- | Build an expression that is the monomorphic id function for given type.
176177
buildIdT :: (BoundVars c, HasHscEnv m, HasHermitMEnv m, MonadCatch m, MonadIO m, MonadThings m)
177178
=> Type -> Transform c m x CoreExpr
178179
buildIdT ty = do
179-
idId <- findIdT "Data.Function.id"
180+
idId <- findIdT $ fromString "Data.Function.id"
180181
return $ mkCoreApp (varToCoreExpr idId) (Type ty)
181182

182183
------------------------------------------------------------------------------

0 commit comments

Comments
 (0)