Top-level expression evaluation at compile time

泄露秘密 提交于 2019-12-05 02:47:42

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]))

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]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!