- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有标准Vect
类型:
module Vect where
data Nat = Z | S Nat
data Vect (n :: Nat) (a :: Type) where
VNil :: Vect Z a
VCons :: a -> Vect n a -> Vect (S n) a
foldVect :: forall (p :: Nat -> Type) n a.
p Z ->
(forall m. a -> p m -> p (S m)) ->
Vect n a -> p n
foldVect n c = go
where go :: forall l. Vect l a -> p l
go VNil = n
go (VCons x xs) = x `c` go xs
newtype FVect a n = FVect { unFVect :: Vect n a }
buildVect :: forall n a.
(forall (p :: Nat -> Type).
p Z ->
(forall m. a -> p m -> p (S m)) ->
p n
) -> Vect n a
buildVect f = unFVect $ f (FVect VNil) $ \x (FVect xs) -> FVect $ x `VCons` xs
base
重新创建(部分)机器允许列表融合:
instance Functor (Vect n) where
fmap = mapVect
{-# INLINE fmap #-}
mapVect :: forall n a b. (a -> b) -> (Vect n a -> Vect n b)
mapVect _f VNil = VNil
mapVect f (VCons x xs) = f x `VCons` mapVect f xs
mapFB :: forall (p :: Nat -> Type) n a b. (forall m. b -> p m -> p (S m)) -> (a -> b) -> a -> p n -> p (S n)
mapFB cons f = \x ys -> cons (f x) ys
{-# INLINE [0] mapFB #-}
{-# NOINLINE [0] mapVect #-}
{-# RULES "mapVect" [~1] forall f xs. mapVect f xs = buildVect (\nil cons -> foldVect nil (mapFB cons f) xs) #-}
{-# INLINE [0] foldVect #-}
-- base has this; I don't think it does anything without a "refolding" rule on mapVect
{-# INLINE [0] buildVect #-}
{-# RULES "foldVect/buildVect" forall (nil :: p Z)
(cons :: forall m. a -> p m -> p (S m))
(f :: forall (q :: Nat -> Type).
q Z ->
(forall m. a -> q m -> q (S m)) ->
q n
).
foldVect nil cons (buildVect f) = f nil cons
#-}
module Test where
import Vect
test :: Vect n Int -> Vect n Int
test = fmap (*5) . fmap (+2)
-ddump-simpl
,我看到了
foldVect
和
buildVect
已经被内联了,但是......
INLINE
s 是分阶段的,因此它们无论如何都不会干扰融合。 (毕竟,这就是 base
为 []
所做的) INLINE [0]
s 与 NOINLINE
描绘了一个相当惊人的图像:test
= \ (@ (n_a141 :: Nat)) (x_X1lK :: Vect n_a141 Int) ->
buildVect
@ n_a141
@ Int
(\ (@ (p_X1jl :: Nat -> *))
(nil_X11K [OS=OneShot] :: p_X1jl 'Z)
(cons_X11M [OS=OneShot]
:: forall (m :: Nat). Int -> p_X1jl m -> p_X1jl ('S m)) ->
foldVect
@ p_X1jl
@ n_a141
@ Int
nil_X11K
(\ (@ (m_a1i5 :: Nat))
(x1_aYI :: Int)
(ys_aYJ [OS=OneShot] :: p_X1jl m_a1i5) ->
cons_X11M
@ m_a1i5
(case x1_aYI of { GHC.Types.I# x2_a1l5 ->
GHC.Types.I# (GHC.Prim.*# x2_a1l5 5#)
})
ys_aYJ)
(buildVect
@ n_a141
@ Int
(\ (@ (p1_a1i0 :: Nat -> *))
(nil1_a10o [OS=OneShot] :: p1_a1i0 'Z)
(cons1_a10p [OS=OneShot]
:: forall (m :: Nat). Int -> p1_a1i0 m -> p1_a1i0 ('S m)) ->
foldVect
@ p1_a1i0
@ n_a141
@ Int
nil1_a10o
(\ (@ (m_a1i5 :: Nat))
(x1_aYI :: Int)
(ys_aYJ [OS=OneShot] :: p1_a1i0 m_a1i5) ->
cons1_a10p
@ m_a1i5
(case x1_aYI of { GHC.Types.I# x2_a1lh ->
GHC.Types.I# (GHC.Prim.+# x2_a1lh 2#)
})
ys_aYJ)
x_X1lK)))
"foldVect/buildVect"
forall (@ (p_aYG :: Nat -> *))
(@ (n_aYJ :: Nat))
(@ a_aYH)
(nil_aYD :: p_aYG 'Z)
(cons_aYE :: forall (m :: Nat). a_aYH -> p_aYG m -> p_aYG ('S m))
(f_aYF
:: forall (q :: Nat -> *).
q 'Z -> (forall (m :: Nat). a_aYH -> q m -> q ('S m)) -> q n_aYJ).
foldVect @ p_aYG
@ n_aYJ
@ a_aYH
nil_aYD
cons_aYE
(buildVect
@ n_aYJ
@ a_aYH
(\ (@ (p1_a156 :: Nat -> *))
(ds_d1io :: p1_a156 'Z)
(ds1_d1ip
:: forall (m :: Nat). a_aYH -> p1_a156 m -> p1_a156 ('S m)) ->
f_aYF @ p1_a156 ds_d1io ds1_d1ip))
= f_aYF @ p_aYG nil_aYD cons_aYE
buildVect
的论点需要是一个非常特定形式的 lambda 抽象,我在构建一个最终发生的重写系统时遇到了麻烦。
最佳答案
像往常一样,newtype
每当编译器被牛逼时,就可以节省一天:
module Vect where
-- everything else the same...
newtype VectBuilder n a = VectBuilder { runVectBuilder :: forall (p :: Nat -> Type).
p Z ->
(forall m. a -> p m -> p (S m)) ->
p n
}
buildVect' :: forall n a. VectBuilder n a -> Vect n a
buildVect' f = unFVect $
runVectBuilder f (FVect VNil) $ \x (FVect xs) -> FVect $ x `VCons` xs
{-# INLINE [0] buildVect' #-}
buildVect :: forall n a.
(forall (p :: Nat -> Type).
p Z ->
(forall m. a -> p m -> p (S m)) ->
p n
) -> Vect n a
buildVect f = buildVect' (VectBuilder f)
{-# INLINE buildVect #-}
{-# RULES "foldVect/buildVect'" forall (nil :: p Z)
(cons :: forall m. a -> p m -> p (S m))
(f :: VectBuilder n a).
foldVect nil cons (buildVect' f) = runVectBuilder f nil cons
#-}
-- compiler no longer has a chance to muck up the LHS by eta expanding f because
-- f "isn't" a function anymore
-- rule for mapVect goes unchanged, so I guess that's evidence that this is totally transparent
module Test where
import Vect
test :: Vect n Int -> Vect n Int
test = fmap (*5) . fmap (+2)
Rec {
-- RHS size: {terms: 19, types: 31, coercions: 13, joins: 0/0}
Test.test_go [Occ=LoopBreaker]
:: forall (l :: Nat). Vect l Int -> FVect Int l
[GblId, Arity=1, Caf=NoCafRefs, Str=<S,1*U>]
Test.test_go
= \ (@ (l_a14W :: Nat)) (ds_d1jk :: Vect l_a14W Int) ->
case ds_d1jk of {
VNil co_a14Y -> (Vect.$WVNil @ Int) `cast` <Co:4>;
VCons @ n2_a151 co_a152 x_aYE xs_aYF ->
(Vect.VCons
@ ('S n2_a151)
@ Int
@ n2_a151
@~ <Co:2>
(case x_aYE of { GHC.Types.I# x1_a1xr ->
GHC.Types.I# (GHC.Prim.*# (GHC.Prim.+# x1_a1xr 2#) 5#) -- success!
})
((Test.test_go @ n2_a151 xs_aYF) `cast` <Co:3>))
`cast` <Co:4>
}
end Rec }
-- RHS size: {terms: 4, types: 5, coercions: 0, joins: 0/0}
Test.test1 :: forall (n :: Nat). Vect n Int -> FVect Int n
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=<S,1*U>,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=1,unsat_ok=True,boring_ok=True)}]
Test.test1
= \ (@ (n_a1wd :: Nat)) (x_X1xa :: Vect n_a1wd Int) ->
Test.test_go @ n_a1wd x_X1xa
-- RHS size: {terms: 1, types: 0, coercions: 9, joins: 0/0}
test :: forall (n :: Nat). Vect n Int -> Vect n Int
[GblId,
Arity=1,
Caf=NoCafRefs,
Str=<S,1*U>,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True,
Guidance=ALWAYS_IF(arity=0,unsat_ok=True,boring_ok=True)}]
test = Test.test1 `cast` <Co:9>
RULES
系统内爆,所以给 GHC 一些帮助
newtype
s,即使它们不是必需的。
关于haskell - 长度索引链表的融合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50844444/
将 KLV 字符串拆分为键、长度、值作为元素的列表/元组的更有效方法是什么? 为了添加一点背景,前 3 位数字作为键,接下来的 2 位表示值的长度。 我已经能够使用以下代码解决该问题。但我不认为我的代
首先,我试图从文件中提取视频持续时间,然后在无需实际上传文件的情况下显示它。 当用户选择视频时 - 信息将显示在其下方,包括文件名、文件大小、文件类型。不管我的技能多么糟糕 - 我无法显示持续时间。我
我是 Scala 编程新手,这是我的问题:如何计算每行的字符串数量?我的数据框由一列 Array[String] 类型组成。 friendsDF: org.apache.spark.sql.DataF
我有一个React Web应用程序(create-react-app),该应用程序使用react-hook-forms上传歌曲并使用axios将其发送到我的Node / express服务器。 我想确
如果给你一个网络掩码(例如 255.255.255.0),你如何在 Java 中获得它的长度/位(例如 8)? 最佳答案 如果您想找出整数低端有多少个零位,请尝试 Integer.numberOfTr
我需要使用 jQuery 获取 div 数量的长度。 我可以得到它,但在两个单击事件中声明变量,但这似乎是错误的,然后我还需要使用它来根据数字显示隐藏按钮。我觉得我不必将代码加倍。 在这里摆弄 htt
我对此感到非常绝望,到目前为止我在 www 上找不到任何东西。 情况如下: 我正在使用 Python。 我有 3 个数组:x 坐标、y 坐标和半径。 我想使用给定的 x 和 y 坐标创建散点图。 到目
我有一个表单,我通过 jQuery 的加载函数动态添加新的输入和选择元素。有时加载的元素故意为空,在这种情况下我想隐藏容器 div,这样它就不会破坏样式。 问题是,我似乎无法计算加载的元素,因此不知道
我决定通过替换来使我的代码更清晰 if (wrappedSet.length > 0) 类似 if (wrappedSet.exists()) 是否有任何 native jq 函数可以实现此目的?或者
简单的问题。如果我有一个如下表: CREATE TABLE `exampletable` ( `id` int(11) NOT NULL AUTO_INCREMENT, `textfield`
我正在使用经典 ASP/MySQL 将长用户输入插入到我的数据库中,该输入是从富文本编辑器生成的。该列设置为 LONG-TEXT。 作为参数化查询(准备语句)的新手,我不确定用于此特定查询的数据长度。
我正在获取 Stripe 交易费用的值(value)并通过禁用的文本字段显示它。 由于输入文本域,句子出现较大空隙 This is the amount $3.50____________that n
我有一个 div,其背景图像的大小设置为包含。但是,图像是视网膜计算机(Macbook Pro 等)的双分辨率图像,所以我希望能够以某种方式让页面知道即使我说的是背景大小:包含 200x200 图像,
我正在开发一个具有“已保存”和“已完成”模块的小部件。当我删除元素时,它会从 dom 中删除/淡化它,但是当我将其标记为完成时,它会将其克隆到已完成的选项卡。这工作很棒,但顶部括号内的数字不适合我。这
我有一个来自 json 提要的数组,我知道在 jArray 中有一个联盟,但我需要计算出该数组的计数,以防稍后将第二个添加到提要中。目前 log cat 没有注销“teamFeedStructure”
目标:给定一个混合类型的数组,确定每个级别的元素数量。如果同一层有两个子数组,则它们的每个元素都计入该层元素的总数。 方法: Array.prototype.elementsAtLevels = fu
我需要帮助为 Java 中的单链表制作 int size(); 方法。 这是我目前所拥有的,但它没有返回正确的列表大小。 public int size() { int size = 0;
我正在为学校作业创建一个文件服务器应用程序。我目前拥有的是一个简单的 Client 类,它通过 TCP 发送图像,还有一个 Server 类接收图像并将其写入文件。 这是我的客户端代码 import
我有这对功能 (,) length :: Foldable t => t a -> b -> (Int, b) 和, head :: [a] -> a 我想了解的类型 (,) length he
我正在GitHub Pages上使用Jekyll来构建博客,并希望获得传递给YAML前题中Liquid模板的page.title字符串的长度,该字符串在每个帖子的YAML主题中。我还没有找到一种简单的
我是一名优秀的程序员,十分优秀!