如何使用Haskell单例库的列表?

时间:2018-01-03 22:57:56

标签: haskell singleton-type

我尝试使用我使用列表的singletons库来创建类型:

import Data.Singletons.TH (singletons)

$(singletons [d|

    data LLVMType
        = IntegerType
        | FloatType
        | FunctionType { argumentTypes :: [LLVMType] }

    |])

foo :: SLLVMType ('FunctionType '[ 'IntegerType ])
foo = ???

我尝试将foo设为:SFunctionType [SIntegerType],但会导致此错误:

• Couldn't match expected type ‘Data.Singletons.Sing '['IntegerType]’
              with actual type ‘[Data.Singletons.Sing 'IntegerType]’
• In the first argument of ‘SFunctionType’, namely ‘[SIntegerType]’
  In the expression: SFunctionType [SIntegerType]
  In an equation for ‘foo’: foo = SFunctionType [SIntegerType]

如何为foo制作类似于typechecks的表达式?或者我怎样才能以另一种方式实现这一目标?

Github repo with the code

1 个答案:

答案 0 :(得分:3)

你非常接近,但你需要使用list singletons

{-# language TemplateHaskell, DataKinds, GADTs, TypeFamilies #-}
module SingList where

import Data.Singletons.TH (singletons)
import Data.Singletons.Prelude.List (Sing (SNil, SCons))

$(singletons [d|

    data LLVMType
        = IntegerType
        | FloatType
        | FunctionType { argumentTypes :: [LLVMType] }

    |])

foo :: SLLVMType ('FunctionType '[ 'IntegerType ])
foo = SFunctionType (SCons SIntegerType SNil)