lan*_*key 9 haskell compile-time-constant
有没有办法确保在编译时评估如下表达式?
myList :: [Int]
myList = sort [3,2,0,1]
Run Code Online (Sandbox Code Playgroud)
如果您正在评估的是一个实例Lift,您可以在编译时使用TemplateHaskell以下方法对其进行评估:
{-# LANGUAGE TemplateHaskell #-}
module Sort where
import Data.List
import Language.Haskell.TH.Syntax
myList :: [Int]
myList = $(lift (sort [3,2,0,1] :: [Int]))
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以检查编译到的内容-ddump-splices:
$ ghc -ddump-splices sort
[1 of 1] Compiling Sort ( sort.hs, sort.o )
sort.hs:9:12-41: Splicing expression
lift (sort [3, 2, 0, 1] :: [Int]) ======> [0, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)