Skip to content

Commit ef4efcd

Browse files
committed
Fixes for issue #57 and issue #58
1 parent 6364659 commit ef4efcd

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/CodeGen/IL/Common.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ dictType = "dict_t"
198198
arrayType :: Text
199199
arrayType = "array_t"
200200

201+
uncurriedFnType :: Int -> Text
202+
uncurriedFnType i = "Fn" <> (T.pack . show $ i)
203+
201204
int :: Text
202205
int = "int"
203206

src/CodeGen/IL/Optimizer/Inliner.hs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
7575
, binary euclideanRingNumber opDiv Divide
7676

7777
-- , binary eqNumber opEq EqualTo
78-
, binary eqNumber opNotEq NotEqualTo
78+
-- , binary eqNumber opNotEq NotEqualTo
7979
, binary eqInt opEq EqualTo
8080
, binary eqInt opNotEq NotEqualTo
8181
, binary eqString opEq EqualTo
@@ -122,7 +122,7 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
122122

123123
, inlineNonClassFunction (isModFn (C.dataFunction, C.apply)) $ \f x -> App Nothing f [x]
124124
, inlineNonClassFunction (isModFn (C.dataFunction, C.applyFlipped)) $ \x f -> App Nothing f [x]
125-
, inlineNonClassFunction (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
125+
, inlineUnsafeIndex (isModFnWithDict (C.dataArray, C.unsafeIndex)) $ flip (Indexer Nothing)
126126
] -- ++
127127
-- [ fn | i <- [0..10], fn <- [ mkFn i, runFn i ] ] ++
128128
-- [ fn | i <- [0..10], fn <- [ mkEffFn C.controlMonadEffUncurried C.mkEffFn i, runEffFn C.controlMonadEffUncurried C.runEffFn i ] ] ++
@@ -186,13 +186,14 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
186186
runFn :: Int -> AST -> AST
187187
runFn = runFn' C.dataFunctionUncurried C.runFn (\ss f args ->
188188
let len = length args
189-
typ = mkString $ "Fn" <> (T.pack . show $ len) in
189+
typ = mkString $ uncurriedFnType len in
190190
if len > 0
191191
then App ss (App Nothing (StringLiteral Nothing typ) [f]) args
192192
else App ss f args)
193193
runEffFn :: Text -> Text -> Int -> AST -> AST
194194
runEffFn modName fnName = runFn' modName fnName $ \ss fn acc ->
195-
Function ss Nothing [] (Block ss [Return ss (App ss fn acc)])
195+
let typ = mkString . uncurriedFnType $ length acc in
196+
Function ss Nothing [] (Block ss [Return ss (App ss (App Nothing (StringLiteral Nothing typ) [fn]) acc)])
196197

197198
runFn' :: Text -> Text -> (Maybe SourceSpan -> AST -> [AST] -> AST) -> Int -> AST -> AST
198199
runFn' modName runFnName res n = convert where
@@ -211,6 +212,17 @@ inlineCommonOperators = everywhereTopDown $ applyAll $
211212
convert (App _ (App _ op' [x]) [y]) | p op' = f x y
212213
convert other = other
213214

215+
inlineUnsafeIndex :: (AST -> Bool) -> (AST -> AST -> AST) -> AST -> AST
216+
inlineUnsafeIndex p _ = convert where
217+
convert :: AST -> AST
218+
convert (App _ (App ss op' [x]) [y@NumericLiteral{}])
219+
| p op' = Indexer ss y x
220+
convert (App _ (App ss op' [x]) [y])
221+
| p op' = Indexer ss y' x
222+
where
223+
y' = App Nothing (StringLiteral Nothing $ mkString int) [y]
224+
convert other = other
225+
214226
isModFn :: (Text, PSString) -> AST -> Bool
215227
isModFn (m, op) (Indexer _ (Var _ op') (Var _ m')) =
216228
m == m' && decodeString op == Just op'
@@ -294,8 +306,8 @@ ringInt = (C.dataRing, C.ringInt)
294306
euclideanRingNumber :: forall a b. (IsString a, IsString b) => (a, b)
295307
euclideanRingNumber = (C.dataEuclideanRing, C.euclideanRingNumber)
296308

297-
eqNumber :: forall a b. (IsString a, IsString b) => (a, b)
298-
eqNumber = (C.dataEq, C.eqNumber)
309+
-- eqNumber :: forall a b. (IsString a, IsString b) => (a, b)
310+
-- eqNumber = (C.dataEq, C.eqNumber)
299311

300312
eqInt :: forall a b. (IsString a, IsString b) => (a, b)
301313
eqInt = (C.dataEq, C.eqInt)

0 commit comments

Comments
 (0)