我试图找到 GHC 中发生的某种内联的来源,其中作为参数传递给另一个函数的函数是内联的。例如,我可能会编写如下定义(使用我自己的 List 类型以避免重写规则):
data MyList a = Nil | Cons a (MyList a)
deriving (Show)
mapMyList :: (a -> b) -> MyList a -> MyList b
mapMyList f Nil = Nil
mapMyList f (Cons a rest) = Cons (f a) $ mapMyList f rest
Run Code Online (Sandbox Code Playgroud)
随后拨打电话
fromList :: [a] -> MyList a
fromList = ...
main = do
print $ mapMyList (*2) $ fromList [1..5]
Run Code Online (Sandbox Code Playgroud)
mapMyList是递归的,所以它不能直接内联。但是,在生成的 Core 中,我看到以下定义:
fromList :: [a] -> MyList a
fromList = ...
main …Run Code Online (Sandbox Code Playgroud)