简体   繁体   English

我们如何将 Haskell 元组匹配到 Agda 数据类型?

[英]How can we match Haskell tuples to an Agda datatype?

I would like to use Haskell code in Agda, for example, something like a function that gives back a list of pairs of integers and strings.我想在 Agda 中使用 Haskell 代码,例如,类似于 function 的代码,它返回一个整数和字符串对列表。

I saw this documentation: https://agda.readthedocs.io/en/v2.6.1.1/language/foreign-function-interface.html我看到了这个文档: https://agda.readthedocs.io/en/v2.6.1.1/language/foreign-function-interface.html

But I don't know how to map Haskell tuples to an Agda type, because for example in a mapping like但我不知道如何将 map Haskell 元组转换为 Agda 类型,因为例如在像这样的映射中

{-# COMPILE GHC APair = data ?????? #-}

I don't know how to fill the????-s, as I don't have the definition of the tuple data type.我不知道如何填写??????-s,因为我没有元组数据类型的定义。

However, pairs are not listed at the builtin pairings either.但是,内置配对中也未列出配对。

How should I proceed?我应该如何进行?

The standard library does this in Foreign.Haskell.Pair ( https://agda.github.io/agda-stdlib/Foreign.Haskell.Pair.html ). The standard library does this in Foreign.Haskell.Pair ( https://agda.github.io/agda-stdlib/Foreign.Haskell.Pair.html ). The relevant code is相关代码是

record Pair (A : Set a) (B : Set b) : Set (a ⊔ b) where
  constructor _,_
  field  fst : A
         snd : B
open Pair public

{-# FOREIGN GHC type AgdaPair l1 l2 a b = (a , b) #-}
{-# COMPILE GHC Pair = data MAlonzo.Code.Foreign.Haskell.Pair.AgdaPair ((,)) #-}

There's a little bit of fiddling to account for the universe levels in the Agda type, which don't appear in the Haskell pairs.考虑到 Agda 类型中的 Universe 级别有一些麻烦,这些级别不会出现在 Haskell 对中。 If you don't need that, this should suffice:如果你不需要,这应该足够了:

data Pair (A B : Set) : Set where
  _,_ : A → B → Pair A B

{-# COMPILE GHC Pair = data (,) ((,)) #-}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM