编译时的顶级表达式评估 [英] Top-level expression evaluation at compile time
本文介绍了编译时的顶级表达式评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以确保在编译时对以下表达式进行求值?
Is there any way to ensure, that an expression like the following would be evaluated at compile time?
myList :: [Int]
myList = sort [3,2,0,1]
推荐答案
如果您要评估的是 TemplateHaskell
:
If what you're evaluating is an instance of Lift
, you can evaluate it at compile time using TemplateHaskell
:
{-# LANGUAGE TemplateHaskell #-}
module Sort where
import Data.List
import Language.Haskell.TH.Syntax
myList :: [Int]
myList = $(lift (sort [3,2,0,1] :: [Int]))
如果需要,可以使用-ddump-splices
检查它已编译为什么:
If you want, you can check what it has compiled to with -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]
这篇关于编译时的顶级表达式评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文