- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
据我了解,表达某事物是自由幺半群的一种方法是使用这样的类:
class (Foldable s, forall a. Monoid (s a)) => Sequence s where
singleton :: a -> s a
以及以下“通用建筑”法:
Monoid m
和任何功能
f :: a -> m
,
foldMap f . singleton = f
foldMap f
是幺半群态射。 foldMap f
是唯一同时满足 1 和 2 的函数。data ViewL s a
= EmptyL
| a :< s a
instance Sequence s => Semigroup (ViewL s a) where
EmptyL <> y = y
x <> EmptyL = x
(x :< xs) <> (y :< ys) = x :< (xs <> singleton y <> ys)
instance Sequence s => Monoid (ViewL s a) where
mempty = EmptyL
viewl :: Sequence s => s a -> ViewL s a
viewl = foldMap (:< mempty)
我将如何显示以下内容?
viewl s = EmptyL ==> s == mempty
viewl s == a :< s' <==> s == singleton a <> s'
foldMap f
之后。是所有
f
的幺半群态射,但我真的不知道如何处理上述问题。
最佳答案
我们将从几个关于 foldMap
的一般引理开始。和 singleton
.第一个引理显然来自任何Foldable
的参数性。 ,但我们可以使用它们的定律直接证明它。
引理(foldMap
组成):假设 Sequence s
, Monoid m
, 和 Monoid n
.让 f :: a -> m
成为一个函数并让 g :: m -> n
是一个幺半群态射。然后
g . foldMap @s f = foldMap @s (g . f)
证明:
g . foldMap @s f . singleton @s
= -- foldMap/singleton
g . f
通过
foldMap
的唯一性,
g . foldMap @s f = foldMap @s (g . f)
Sequence s
,
foldMap @s (singleton @s) = id @s
.
id @s . singleton @s = singleton @s
.通过
foldMap
的唯一性,
id @s = foldMap @s (singleton @s)
.
Semigroup (ViewL s a)
以一种更容易在下面使用的形式进行实例化。只需一点等式推理(使用一般
case
变换和幺半群恒等式)就足以证明以下实例是等价的(忽略惰性,我将自始至终)。
instance Sequence s => Semigroup (ViewL s a) where
EmptyL <> y = y
(x :< xs) <> v = x :< (xs <> unviewl v)
unviewl :: Sequence s => ViewL s a -> s a
unviewl EmptyL = mempty
unviewl (x :< xs) = singleton x <> xs
unviewl
是幺半群态射。
unviewl mempty = mempty
对于任何
v1
和
v2
,
unviewl v1 <> unviewl v2 = unviewl (v1 <> v2)
.第一个是微不足道的。
unviewl @s mempty
= -- def of mempty for ViewL s
unviewl @s EmptyL
= -- def of unviewl
mempty
其次,我们按案例工作:
unviewl (EmptyL <> v2)
= -- definition of <> (or the monoid law)
unviewl v2
= -- monoid law
mempty <> unviewl v2
= -- definition of unviewl
unviewl EmptyL <> unviewl v2
第二种情况:
unviewl ((x :< xs) <> v)
= -- definition of <>
unviewl (x :< (xs <> unviewl v))
= -- definition of unviewl
singleton x <> xs <> unviewl v
= -- definition of unviewl
unviewl (x :< xs) <> unviewl v
viewl
和
unviewl
是(满)逆。
unviewl . viewl = id
证明:
unviewl . viewl
= -- definition of viewl
unviewl . foldMap (:< mempty)
= -- foldMap composition lemma and the fact that unviewl
-- is a monoid morphism.
foldMap (unviewl . (:< mempty))
=
foldMap (\x -> unviewl (x :< mempty))
= -- definition of unviewl
foldMap (\x -> singleton x <> mempty)
= -- monoid law and eta reduction
foldMap singleton
= -- rebuilding lemma
id
引理(
viewl
的
singleton
):对于任何
a
,
viewl (singleton a) = a :< mempty
.
viewl (singleton a)
= -- definition of viewl
foldMap (:< mempty) (singleton a)
= -- foldMap/singleton law
a :< mempty
引理:
viewl . unviewl = id
证明:设
v :: ViewL s a
.我们在
v
上按案例进行。 .
viewl (unviewl EmptyL)
= -- definition of unviewl
viewl mempty
= -- viewl is a monoid morphism (because it is a fold)
mempty
= -- definition of mempty
EmptyL
第二种情况:
viewl (unviewl (x :< xs))
= -- definition of unviewl
viewl (singleton x <> xs)
= -- viewl is a monoid morphism
viewl (singleton x) <> viewl xs
= -- viewl of singleton lemma
(x :< mempty) <> viewl xs
= -- definition of <>
x :< (mempty <> unviewl (viewl xs))
= -- monoid law
x :< unviewl (viewl xs)
= -- by lemma above, unviewl . viewl = id
x :< xs
Sequence s
,
xs :: s a
, 和
viewl xs = EmptyL
,然后
xs = mempty
.
viewl xs = EmptyL
.因此
unviewl (viewl xs) = unviewl EmptyL
.由于
unviewl . viewl = id
根据
unviewl
的定义,
xs = mempty
.
Sequence s
,
x :: a
, 和
xs :: s a
,然后
viewl (singleton x <> xs) = x :< xs
.
viewl (singleton x <> xs)
= -- viewl is a monoid morphism
viewl (singleton x) <> viewl xs
= -- lemma above
(x :< mempty) <> viewl xs
= -- definition of <>
x :< (mempty <> unviewl (viewl xs))
= -- monoid law and unviewl . viewl = id
x :< xs
推论:如果
viewl xs = y :< ys
,然后
xs = singleton y <> ys
.
unviewl
到前提两边,
unviewl (viewl xs) = unviewl (y :< ys)
.由于
unview l . viewl = id
根据
unviewl
的定义,定理成立。
Sequence
的这个公式的其他几件事。 .我收集了
in this gist .
关于haskell - 如何证明基本序列性质,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68808980/
(这不是关于定理证明,而是关于实践中的测试,例如 quickCheck) 让f一些通用函数 f :: RESTRICTIONS => GENERICS 具有一些“理想的”属性(即不是 hack,是不可
给定数组 arr 和索引数组 ind,以下算法就地重新排列 arr 以满足给定的索引: function swap(arr, i, k) { var temp = arr[i]; arr[i]
我有兴趣创建一个具有运行时间和空间限制的简单数组问题。看来我找到了解决问题的方法。请阅读以下java代码中问题的初始描述注释: /* * Problem: Given two integer ar
我是 isabelle 的新手,并试图证明以下简单的不等式: lemma ineq: "(a::real) > 0 ⟹ a 0 ⟹ b 0" proof have "1/a + 1/b >
是否有任何理论说缓存应该比文件系统更快? 我认为,由于文件系统也使用缓存,因此没有科学证据表明当文件系统的概念有些松散时,我们应该将内容从文件系统移动到诸如 memcache 之类的缓存中——比如下载
我正在做一个证明,我的一个子目标看起来有点像这样: Goal forall (a b : bool) (p: Prop) (H1: p -> a = b) (H2: p), neg
我有定义的归纳类型: Inductive InL (A:Type) (y:A) : list A -> Prop := | InHead : forall xs:list A, InL y (co
我知道 CRC 是一个线性函数,这意味着 CRC(x xor y) = CRC(x) xor CRC(y),但我不知道如何证明 CRC 的这个属性。 有谁有想法吗? 非常感谢! 最佳答案 这通常不是真
我是 Coq 的初学者。 虽然计算机为我验证了证明令人满意,但众所周知,满足 Coq 的证明对人类来说难以阅读。这是一个简单的例子,假设您没有看到任何评论: Theorem add_comm : fo
我试图了解是什么决定了类型参数是否必须是标称的。 虽然 GADT 和类型家族在某种意义上看起来不同,但它们不是“简单容器”,因为它们的实例定义可以“查看”它们的参数,但简单类型是否可以明显需要名义参数
我想使用 function 关键字定义来证明函数定义的正确性。以下是自然数的通常归纳定义上的加法函数的定义: theory FunctionDefinition imports Main begin
我定义了一个 Sygma-Type,如下所示: { R : nat -> nat -> bool | Reflexive R } 我有两个元素 r1 r2 : { R : nat -> nat ->
我有以下数据: new_pairs x y Freq start.latittude start.longitude start.station end.la
出于教育目的,我一直试图通过使用各种语言扩展和单例类型,在 Haskell 中重建《Type-Driven Development with Idris》(即 RemoveElem.idr )一书中的
我定义了一个 Sygma-Type,如下所示: { R : nat -> nat -> bool | Reflexive R } 我有两个元素 r1 r2 : { R : nat -> nat ->
我正在使用Ax DevTools,并且试图弄清楚如何使用相同的构建信息标记多个扫描。现在,我的测试运行如下: class MyTestCase : XCTestCase { func myTest
我正在尝试证明一个函数的正确性,该函数检查数组是否按递增/递减顺序排序或未排序。行为是返回 -1,如果按降序排序,1,如果按升序排序,大小为 1,或包含相同的值,0,如果没有已排序或为空。运行:Fra
我试图证明 Z3(Microsoft 的 SMT 求解器)中的一个归纳事实。我知道 Z3 通常不提供此功能,如 Z3 guide 中所述。 (第 8 节:数据类型),但是当我们限制要证明事实的域时,这
问题已编辑: 如代码中所述,HashSet 和 HashMap 是快速失败的(但这不是保证): void goHashSet() { Set set = new HashSet();
我试图使导航栏中的链接延伸到导航栏的全长。我环顾四周,发现了一些有用的信息,但无法使其正常工作 HTML: To
我是一名优秀的程序员,十分优秀!