使用 GHC 泛型定义类似“mempty”的函数?

2024-03-22

我正在为 Zoho REST API 编写一个客户端库,并且有一堆不同的记录类型,其中包含所有内容Maybe a字段,即:

data Approval = Approval
  { apDelegate :: Maybe Bool
  , apApprove :: Maybe Bool
  , apReject :: Maybe Bool
  , apResubmit :: Maybe Bool
  } deriving (Eq, Show, Generic)

data ContactSpecialFields = ContactSpecialFields
  { csfCurrencySymbol :: Maybe Text -- $currency_symbol
  , csfState :: Maybe Text -- $state
  , csfProcessFlow :: Maybe Bool -- $process_flow
  , csfApproved :: Maybe Bool -- $approved
  , csfApproval :: Approval  -- $approval
  , csfEditable :: Maybe Bool -- $editable
  } deriving (Eq, Show)

-- and so on

我需要一种方法来定义此类类型的“空”记录,例如:

emptyApproval :: Approval
emptyApproval = Approval
  { apDelegate = Nothing
  , apApprove = Nothing
  , apReject = Nothing
  , apResubmit = Nothing
  }

因此,我伸出手来GHC.Generics并得到一些工作(这是有缺陷的!):

-- These parts seem logically correct to me...

class GEmptyZohoStructure f where
  gEmptyZohoStructure :: f p

instance (GEmptyZohoStructure f, GEmptyZohoStructure g) => GEmptyZohoStructure (f :*: g) where
  gEmptyZohoStructure = (gEmptyZohoStructure :: f p) :*: (gEmptyZohoStructure :: g p)

instance GEmptyZohoStructure Maybe where
  gEmptyZohoStructure = Nothing

class EmptyZohoStructure a where
  emptyZohoStructure :: a

  default emptyZohoStructure :: (Generic a, (GEmptyZohoStructure (Rep a))) => a
  emptyZohoStructure = GHC.Generics.to gEmptyZohoStructure

-- Whereas these parts are random type-class instances that I've written, just 
-- to get the code to compile.

instance (GEmptyZohoStructure f) => GEmptyZohoStructure (M1 i t f) where
  gEmptyZohoStructure = (gEmptyZohoStructure :: f p)

instance (GEmptyZohoStructure f) => GEmptyZohoStructure (K1 i (f p)) where
  gEmptyZohoStructure = gEmptyZohoStructure

instance EmptyZohoStructure Approval

当代码编译时,以下(可以理解)会导致运行时堆栈溢出:

ghci> emptyZohoStructure  :: Approval
*** Exception: stack overflow

我正在关注encode教程位于https://www.stackage.org/haddock/lts-12.1/base-4.11.1.0/GHC-Generics.html#g:12 https://www.stackage.org/haddock/lts-12.1/base-4.11.1.0/GHC-Generics.html#g:12,由于参数被传递给encode功能,它允许人们有机会打开M1 / K1构造函数并构建一些有意义的递归层次结构。我如何编写泛型M1 and K1对于我的用例(通用函数实际上没有任何参数)?


没有任何定义的意义GEmptyZohoStructure Maybe在泛型的类型类中。

class G f where 
  gempty' :: f p
instance (G f, G g) => G ( f :*: g) where 
  gempty' = gempty' :*: gempty'

instance G c => G (D1 x c) where
  gempty' = M1 gempty'
instance G s => G (C1 x s) where
  gempty' = M1 gempty'    
instance E t => G (S1 m (Rec0 t)) where -- the key instance
  gempty' = M1 (K1 gempty)

class E a where
  gempty :: a
  default gempty :: (Generic a, G (Rep a)) => a
  gempty = to gempty'    
instance E (Maybe a) where 
  gempty = Nothing

之后,您可以定义由 Maybe 值组成的任何产品类型。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 GHC 泛型定义类似“mempty”的函数? 的相关文章

随机推荐

  • 我们可以使用具有相同字段名的 group by 和 where 条件吗

    我有一个要求 比如必须提取用户选择的日期范围内的所有记录 选择从 2011 年 1 月 15 日到 2011 年 8 月 20 日开始的所有员工并按日期分组 我应该如何为此编写 SQL 查询 SELECT FROM employees WH
  • Matlab:从命令窗口重命名工作区元素?

    Matlab 的 GUI 允许我通过右键单击元素并选择 重命名 选项来重命名工作区中的任何元素 是否也可以从命令窗口执行此操作 这些是您可以轻松亲自测试的事情 并且您应该这样做 这是学习 发现的最佳方式 无论如何 答案是否定的 您不能从命令
  • 为什么具有非常高索引号的 JavaScript 数组会导致崩溃/减慢/麻烦?

    基本上破坏我的 Node js Express 服务器的代码是这样的 resultArr resultArr test resultArr test 2015073012 someObject 当我将其更改为此时 它运行没有问题 resul
  • 大量 XML 编辑

    我需要将一个中等大小的 XML 文件加载到内存中 对该文件进行多次随机访问修改 可能是数十万次 然后将结果写入 STDIO 大多数这些修改将是节点插入 删除 以及文本节点内的字符插入 删除 这些 XML 文件足够小 可以放入内存 但也足够大
  • 使用 dplyr 将一个 data.frame 的输出通过管道传输到另一个 data.frame

    我有两个 data frames 一个查找表 它告诉我一组中包含的一组产品 每组有at least类型 1 和类型 2 的一种产品 第二个 data frame 告诉我有关交易的详细信息 每笔交易可以有以下产品之一 a 唯一的产品s来自其中
  • Elastic Beanstalk -> 使用 Grails 的 RDS 连接错误

    我正在将 Grails 应用程序部署到 Amazon Web Services Elastic Beanstalk 并利用 RDS 我正在使用 Grails Spring Security RDS 表 该应用程序似乎工作正常 RDS 中的登
  • Flutter SDK 使用哪个渠道?

    预览版 1 的公告位于 宣布 Flutter 发布预览 1 Flutter Medium https medium com flutter io flutter release preview 1 943a9b6ee65a 下载 Flutt
  • 如何在 R 中进行条件“左连接”?

    我发现自己在 R 中多次执行 条件左连接 用一个例子来说明 如果您有两个数据框 例如 gt df a b 1 1 0 2 2 0 gt other df a b 1 2 3 目标是最终得到这个数据框 gt final df a b 1 1
  • 如何在另一个函数中使用 calc()

    是否可以使用calc CSS 函数内部 例如transform or translate 我似乎无法让它发挥作用 这是一个演示 大家可以玩一下 http jsfiddle net qdJwY 1 http jsfiddle net qdJw
  • 如何在 Ruby 中使用条件运算符 (?:)?

    条件运算符 在 Ruby 中使用 例如 这是正确的吗 它是三元运算符 http en wikipedia org wiki Ternary operation 其工作方式与 C 中类似 不需要括号 这是一个表达式 其工作原理如下 if th
  • C# ClickOnce 应用程序的“添加或删除程序”图标

    我已经尝试过Stack Overflow问题中的解决方案 添加或删除程序 中 ClickOnce 应用程序的自定义图标 https stackoverflow com questions 10927109 icon for click on
  • 有什么好的可视化 HTML5 编辑器或 IDE? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 看起来 Dreamweaver CS5 将在几年内 实际上是几周 尝试使 HTML5 变得更加流畅 似
  • Rails:嵌套资源冲突,如何根据调用的路由确定索引操作的范围

    假设您有两条定义的路线 map resources articles map resources categories has many gt articles 两者都可以通过助手 路径访问 articles path articles c
  • 切换列表字典中的键和值

    大家好 Stackoverflow 我有一个嵌套字典 其中列表作为值 我想创建一个字典 其中所有列表条目都将其相应的键作为值 举例时间 what I have dict1 A 1 2 3 B 4 5 6 C 7 8 9 what I wan
  • 检测请求是否是 ESI - Symfony2

    这可能是不可能的 如果是这种情况 那么我将不得不寻找其他解决方案 所以如果无法完成 请告诉我 我知道我可以获得请求类型 即 1 主请求或 2 子请求 但是有没有办法检测该请求是否是 ESI 请求 我的理解是ESI永远是一个子请求 但是有很多
  • “无法加载配置中指定的 X.509 证书身份”

    我将工作中的 WCF 服务从我的开发环境转移到 QA 环境 包括证书 根授权 根身份验证撤销列表 服务证书 包括其 PK 之后 我使用 FindPrivateKey 找到了私钥 并授予所有相关帐户在文件系统级别访问私钥文件的完全权限 我的应
  • 使用 jq 解析嵌套 json

    我正在解析嵌套的 json 以从 json 响应中获取特定值 json响应如下 custom classes 2 images classifiers classes class football score 0 867376 classi
  • 错误:无法为具有多个返回值的查询创建 TypedQuery

    我尝试用java和jpa来实现searchBook功能 我有两门课 即媒体课和图书课 书籍延伸了媒体 我将数据保存在不同的表中 我尝试从下面的查询中选择数据 TypedQuery
  • 如何分析 AngularJS 组件的性能基准?

    作为一项要求 我必须根据模拟数据分析几个 AngularJS 组件 例如 ng grid IE8 Chrome 和 FF 中的数据表 的性能基准 我有模拟数据 现在 当使用 IE8 Profiler 时 我得到了几个函数的时间 毫秒 根据
  • 使用 GHC 泛型定义类似“mempty”的函数?

    我正在为 Zoho REST API 编写一个客户端库 并且有一堆不同的记录类型 其中包含所有内容Maybe a字段 即 data Approval Approval apDelegate Maybe Bool apApprove Mayb