小编Dan*_*ton的帖子

无法找到变量的实例

上下文:我正在从事软件基础的练习.

Theorem neg_move : forall x y : bool,
  x = negb y -> negb x = y.
Proof. Admitted.

Theorem evenb_n__oddb_Sn : forall n : nat,
  evenb n = negb (evenb (S n)).
Proof.
  intros n. induction n as [| n'].
  Case "n = 0".
    simpl. reflexivity.
  Case "n = S n'".
    rewrite -> neg_move.
Run Code Online (Sandbox Code Playgroud)

在最后一行之前,我的子目标是这样的:

evenb (S n') = negb (evenb (S (S n')))
Run Code Online (Sandbox Code Playgroud)

我想将其转化为:

negb (evenb (S n')) = evenb (S (S n'))
Run Code Online (Sandbox Code Playgroud)

rewrite -> …

coq

10
推荐指数
1
解决办法
1931
查看次数

严格的fmap只使用Functor,而不是Monad

懒惰IO的一个恼怒最近引起了我的注意

import System.IO
import Control.Applicative

main = withFile "test.txt" ReadMode getLines >>= mapM_ putStrLn
  where getLines h = lines <$> hGetContents h
Run Code Online (Sandbox Code Playgroud)

由于懒惰的IO,上面的程序什么都不打印.所以我想象这可以用严格的版本来解决fmap.事实上,我确实提出了这样一个组合器:

forceM :: Monad m => m a -> m a
forceM m = do v <- m; return $! v

(<$!>) :: Monad m => (a -> b) -> m a -> m b
f <$!> m = liftM f (forceM m)
Run Code Online (Sandbox Code Playgroud)

更换<$><$!>确实缓解该问题.但是,我不满意.<$!>有一个Monad约束,感觉太紧; 它的伴侣<$>只需要Functor …

monads haskell strictness

10
推荐指数
1
解决办法
725
查看次数

斯卡拉的letrec?("打结"的不可改变的方式?)

假设我有一个像这样的愚蠢的小案例类:

case class Foo(name: String, other: Foo)
Run Code Online (Sandbox Code Playgroud)

我如何定义ab不可改变这样a.otherb,而且b.othera?scala是否提供了"打结"的方法?我想做这样的事情:

val (a, b): (Foo, Foo) = (Foo("a", b), Foo("b", a)) // Doesn't work.
Run Code Online (Sandbox Code Playgroud)

可能性

在Haskell中,我会这样做:

data Foo = Foo { name :: String, other :: Foo }

a = Foo "a" b
b = Foo "b" a
Run Code Online (Sandbox Code Playgroud)

绑定到ab包含在同一let表达式中或顶层的绑定.

或者,在不滥用Haskell的自动化letrec功能的情况下:

(a, b) = fix (\ ~(a', b') -> Foo "a" b', Foo "b" …
Run Code Online (Sandbox Code Playgroud)

scala letrec tying-the-knot

10
推荐指数
1
解决办法
656
查看次数

将模块恢复为记录

假设我有一个任意模块

module Foo where
foo :: Moo -> Goo
bar :: Car -> Far
baz :: Can -> Haz
Run Code Online (Sandbox Code Playgroud)

在哪里foo,barbaz,正确实施等

我想将此模块重新生成为自动生成的数据类型和相应的对象:

import Foo (Moo, Goo, Car, Far, Can, Haz)
import qualified Foo

data FooModule = Foo
  { foo :: Moo -> Goo
  , bar :: Car -> Far
  , baz :: Can -> Haz
  }

_Foo_ = Foo
  { foo = Foo.foo
  , bar = Foo.bar
  , baz = Foo.baz
  }
Run Code Online (Sandbox Code Playgroud)

名称必须与原始模块完全相同.

我可以手动执行此操作,但这非常繁琐,所以我想编写一些代码来为我执行此任务.

我不确定如何处理这样的任务.Template …

haskell module ghc-api template-haskell

10
推荐指数
1
解决办法
370
查看次数

使用javascript禁用中间点击滚动

背景:我正在创建一个让人联想到whenisgood.net的表,因为它具有click-n-drag切换表元素.当左,中,右鼠标按钮激活mousedown事件时,我想调用不同类型的切换代码.

通过使用JQuery,我有一个良好的开端.

$(".togglable").bind("contextmenu", function() {return false;});
$(".togglable").bind("mousedown", function(e){
  e.preventDefault();
  toggle(this, e);
});
Run Code Online (Sandbox Code Playgroud)

toggle()我可以e.which用来确定点击了什么按钮的函数中.

妙语:我用e.preventDefault()希望将停止滚动的中间点击默认行为.它没有.我该怎么做才能阻止滚动动作激活?

另请参阅"使用中键单击触发onclick事件"

javascript jquery scroll javascript-events

9
推荐指数
3
解决办法
1万
查看次数

统一的HTML模板语言

似乎每个Web框架都有自己的宠物模板语言.Ruby有eRuby,Python的django使用Django模板语言,Haskell有HeistHamlet,Java有JSP,然后有PHP ...

我的问题是,有没有人试图创造一种模板语言来统治它们?是否有至少有任何这样的模板语言,一些跻身不同的Web框架的广泛支持?

html language-agnostic templating dynamic-html

9
推荐指数
1
解决办法
981
查看次数

将中缀运算符自动提升到monadic中缀运算符

Haskell的一个好处是能够使用中缀表示法.

1 : 2 : 3 : []    :: Num a => [a]
2 + 4 * 3 + 5     :: Num a => a
Run Code Online (Sandbox Code Playgroud)

但是当操作员需要被抬起时,这种力量突然而且可悲地丢失了.

liftM2 (*) (liftM2 (+) m2 m4) (liftM2 (+) m3 m5)
liftM2 (:) m1 (liftM2 (:) m2 (liftM2 (:) m3 mE))
Run Code Online (Sandbox Code Playgroud)

可以定义类似的运算符以重新获得此功率

(.*) = liftM2 (*)
(.+) = liftM2 (+)
(.:) = liftM2 (:)

m1, m2, m3, m4, m5 :: Monad m, Num a => m a
mE = return []     :: Monad m => …
Run Code Online (Sandbox Code Playgroud)

monads haskell infix-notation

9
推荐指数
2
解决办法
677
查看次数

将scala代码推广到函数中

所以我最近不小心写了一个关于Scala问题的Haskell答案.对Haskell比较熟悉,解决方案对我来说非常容易:

myMaxBy :: (a -> a -> Ordering) -> [a] -> [a]
myMaxBy _ [] = undefined
myMaxBy f (x:xs) = foldr step [x] xs
  where step y acc@(z:_) = case f y z of
          GT -> [y]
          EQ -> y:acc
          LT -> acc
Run Code Online (Sandbox Code Playgroud)

然后有人提醒我这是一个Scala问题.我开始将我的代码转换为Scala,经过多次努力后我决定:

(List(xs.head) /: xs.tail) { (acc, y) =>
  y compare acc.head match {
    1  => List(y)
    0  => y :: acc
    -1 => acc
  }
}
Run Code Online (Sandbox Code Playgroud)

但我不能为我的生活让Scala类型系统屈服于我的意愿并将其概括为一个函数,其中xscompare是输入(理想情况下,首先是比较器的咖喱输入).虽然这肯定是由于我对Scala的一般不熟悉,但我也略微责怪Scala的复杂(虽然非常强大)类型系统.你可以做一些手握,并指导我如何把它变成一个通用函数,类型签名类似于Haskell等价?(阅读:如同一般.)如果比它更复杂,也请证明用法myMaxBy(myCompare)(someList) …

generics haskell types scala function

9
推荐指数
1
解决办法
628
查看次数

记录访问者的语法默认值

在我刚刚写完答案的时候,我遇到了一个有趣的问题:

data Gender = Male | Female
            deriving (Eq, Show)

data Age = Baby | Child | PreTeen | Adult
         deriving (Eq, Show, Ord)

data Clothing = Pants Gender Age
              | Shirt Gender Age
              | Skirt Age         -- assumed to be Female
              deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)

假设我希望用记录语法编写最终数据类型:

data Clothing = Pants {gender :: Gender, age :: Age}
              | Shirt {gender :: Gender, age :: Age}
              | Skirt {age :: Age}
              deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)

问题是,我想gender $ Skirt foo总是评价 …

haskell record default-value

9
推荐指数
1
解决办法
650
查看次数

Haskell中的行多态:用"转换"编写Forth DSL的麻烦

我最近受到了Haskell博客活动1的启发,试图在Haskell中编写类似Forth的DSL.我采取的方法既简单又令人困惑:

{-# LANGUAGE TypeOperators, RankNTypes, ImpredicativeTypes #-}

-- a :~> b represents a "stack transformation"
--          from stack type "a" to stack type "b"
-- a :> b represents a "stack" where the top element is of type "b"
--          and the "rest" of the stack has type "a"
type s :~> s' = forall r. s -> (s' -> r) -> r
data a :> b = a :> b deriving Show
infixl 4 :>
Run Code Online (Sandbox Code Playgroud)

对于做简单的事情,这非常有效:

start …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell concatenative-language higher-order-functions impredicativetypes

9
推荐指数
1
解决办法
1037
查看次数