- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:我已经跟进了更多 specific question .谢谢这里的回答者,我认为后续问题可以更好地解释我在这里介绍的一些困惑。
TL;DR 我正在努力将约束证明纳入表达式,同时在构造函数上使用具有存在约束的 GADT。 (这是一个严重的嘴,对不起!)
我将一个问题提炼为以下内容。我有一个简单的 GADT,它表示称为 X
的点和功能应用程序调用 F
.积分X
被限制为 Objects
.
data GADT ix a where
X :: Object ix a => a -> GADT ix a
F :: (a -> b) -> GADT ix a -> GADT ix b
Constrained
指其对象受某事物约束的容器,
Object
那是什么。 (编辑:我的真正问题涉及
Category
和
Cartesian
来自
constrained-categories 的类)
-- | I can constrain the values within containers of kind `* -> *`
class Constrained (ix :: * -> *) where
type Object ix a :: Constraint
-- | Here's a trivial constraint. A more interesting one might include `Typeable a`, for ex
instance Constrained (GADT ix) where
type Object (GADT ix) a = (Constrained ix, Object ix a)
我想写一个表达式:
-- error: Could not deduce: Object ix Int arising from a use of ‘X’
ex0 :: GADT ix String
ex0 = F show (X (3 :: Int))
虽然显而易见的解决方案有效,但在构建更大的表达式时它很快就会变得冗长:
-- Typechecks, but eventually verbose
ex1 :: Object ix Int => GADT ix String
ex1 = F show (X (3 :: Int))
我认为正确的解决方案应该是这样的:
-- error: Could not deduce: Object ix Int arising from a use of ‘X’
ex2 :: Constrained ix => GADT ix String
ex2 = F show (X (3 :: Int))
但我仍然无法得到
Object ix Int
的证明。 .
Object
添加约束
GADT
中的约束族类实例。我尝试在表达式的签名中提供约束。我试过
QuantifiedConstraints
,虽然,我不确定我是否完全掌握它。请有智慧的人帮助我!
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE InstanceSigs #-}
module Test where
import Data.Kind
import Data.Functor.Identity
import Data.Functor.Const
-- | I can constrain the values within containers of kind `* -> *`
class Constrained (ix :: * -> *) where
type Object ix a :: Constraint
-- | Here's a trivial constraint. A more interesting one might include `Typeable a`, for instance
instance Constrained (GADT ix) where
type Object (GADT ix) a = (Constrained ix, Object ix a)
-- | A demo GADT that has function application ('F'), and points ('X'). The
-- points are constrained.
data GADT ix a where
X :: Object ix a => a -> GADT ix a
F :: (a -> b) -> GADT ix a -> GADT ix b
-- -- Broken
-- -- error: Could not deduce: Object ix Int arising from a use of ‘X’
-- ex0 :: GADT ix String
-- ex0 = F show (X (3 :: Int))
-- Typechecks
-- but for larger programs becomes verbose, requiring many explicit constraints
ex1 :: Object ix Int => GADT ix String
ex1 = F show (X (3 :: Int))
-- -- What I want, but, it's broken
-- ex2 :: Constrained ix => GADT ix String
-- ex2 = F show (X (3 :: Int))
最佳答案
如果没有更多上下文,很难说最好的解决方案是什么,但这里有几种可能性:
完全避免约束
就目前而言,您的 GADT 似乎没有太多理由限制 X
至Object
.也许这只是不需要?
data GADT ix a where
X :: a -> GADT ix a
F :: (a -> b) -> GADT ix a -> GADT ix b
相反,约束可能在需要时来自外部。
All
这样的帮助器。
ex2' :: All (Object ix) '[Int] => GADT ix String
ex2' = F show (X (3 :: Int))
除了
Int
之外,列表中还可以有更多类型;和/或您可以进行同义词约束,例如
type StdObjs ix = (Object ix Int, Object x Bool, ...)
ex2'' :: StdObjs ix => GADT ix String
ex2'' = F show (X (3 :: Int))
通过数据结构本身向后传播约束
X
上的约束值,但仍有可能在 GADT 中以另一种方式表达。例如,如果函数不是通用函数,而是已经被限制为只接受
Object
的函数。 s,你可以这样:
data YourFunc ix a b where
YourFunc :: Object ix a => (a->b) -> YourFunc ix a b
show' :: Object ix Int => YourFunc ix Int String
show' = YourFunc show
这并不能直接帮助您解决您所询问的问题,但也许该功能是共享的或其他东西。你甚至可以有类似的东西
class Object ix a => InferrenceChain ix a where
type PreElem ix a :: Type
propInferrence :: (InferrenceChain ix (PreElem a) => r) -> r
接着
data YourFunc ix a b where
YourFunc :: InferrenceChain ix a
=> (PreElem a -> a) -> YourFunc (PreElem a) a
然后最后你可以证明
X
仅放入
Object ix String
的约束在外部并在
propInferrence
上递归.但这可能会变得非常繁琐。
关于haskell - 如何从范围内的约束族派生类型类实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70075414/
我有三个 td,并且正在尝试将每个内部的函数限制为仅该 td。我该怎么做呢?使用此代码,它会获取所有 3 个图像并将它们全部插入到 h2 之前: jQuery("td.frontpage_news")
这是所需的通用公式:if((b2-b1)=c1,True,False但是,我需要 b2-b1约等于 c1 , 在 5 内大约单位(在本例中为秒)。有没有可以处理这个的函数? 最佳答案 你也可以试试这个
我有三个整数,作为命令行参数传入后赋值给变量。我想验证每个整数都在 1-5 范围内。有没有一种方法可以在不使用如下所示的 if 语句的情况下在 Java 中完成此操作?我想避免这样做(注意伪代码):
检查某个变量 X 是否在某个变量 Z 的 n 个数字之内的最简洁方法是什么。n 是任意定义的数字(即 3)。 所以我想要 if (z {something} x){ // run code i
我的顶级 build.gradle (Gradle 2.2) 中有类似的东西 ext.repo = "https://my-artifactory-repo" buildscript { re
我只是在我的 jsp 页面中进行随机技巧和测试。我想使用 Attributes 将 request 范围对象存储在 session 范围对象中。存储后,当尝试从请求属性中提取值(存储在 session
我正在使用 Spring 。我有一个外部化属性文件。我正在按如下方式加载它。 现在我如何将 session 中的属性作为键值对保存? 我尝试编写一个扩展 ServletContextListene
我有以下范围: scope :billable, -> (range_start = nil, range_end = nil) { joins(:bids) .where("au
请看我的示例代码: var testObject = new SomeClass(); using (testObject) { //At this point how can the te
我目前在保持在 vector 范围内时遇到一些问题。 在下面的代码中,我试图检查正在检查的数字是否小于或等于它后面的数字 #include #include #include bool fun(
有人可以帮我解决下面的(简化的)代码吗?我试图从幻灯片事件函数中调用 doTheSlide() 函数。我对 JS 范围的理解仍然有点可疑。 实现这一目标的正确方法是什么?我收到此错误: Uncaugh
如何在 Swift 中检查时间是否在下午 6 点到晚上 11 点之间?我在使用 NSDateFormatter 时遇到了困难,我觉得一定有更简单的方法。 最佳答案 使用NSCalendar: let
我目前正在尝试创建一个 2D 横向滚动条,并且我目前有我的“世界”绘图(暂时是一个大白框),但我无法弄清楚世界地图的边缘与边缘之间的任何关系确保视口(viewport)始终完全被 map 覆盖。 我的
我正在学习李普曼,而且我只是在学习。我在这里尝试编写一个代码,该代码将返回 vector 中的最小元素。当我在 Codeblocks 中编译我的代码时,它说:“模板声明不能出现在 block 范围内”
我有三个日期对象。我该如何比较它们才能确定它们之间的相对差异。 oldDate = newDate() - 5; midDate = newDate() - 2.5; newDate = newDat
我需要检查对象“objCR”是否存在于当前范围内。我尝试使用以下代码。 if(objCR == null) alert("object is not defined"); 让我知道哪里错了。 最佳答案
如何检查 IP 地址是否属于私有(private)类别? if(isPrivateIPAddress(ipAddress)) { //do something } 如有
我正在开发一个 Firefox 插件,它可以转换用户通过用户选择突出显示的屏幕温度。转换后,用户选择将替换为 ID 为 alreadyconverted 的 span HTML 元素,其中包含原始温度
我正在开发一个邮资应用程序,该应用程序需要根据多个邮政编码范围检查整数邮政编码,并根据邮政编码匹配的范围返回不同的代码。 每个代码都有多个邮政编码范围。例如,如果邮政编码在 1000-2429、254
我正在使用 excel 范围进行连接:Set rng = Range("A1:A8")如果范围内的单元格之一为空,则会添加一个空格。 你如何阻止这个空间被添加? 最佳答案 假设您在那些非空单元格中有常
我是一名优秀的程序员,十分优秀!