假设我们定义了一个函数
f : N \to N
f 0 = 0
f (s n) = f (n/2) -- this / operator is implemented as floored division.
Run Code Online (Sandbox Code Playgroud)
Agda将在三文鱼中绘制f,因为它无法判断n/2是否小于n.我不知道怎么告诉Agda的终止检查器.我在标准库中看到它们有一个2的分区和n/2 <n的证明.但是,我仍然没有看到如何让终止检查器意识到已经在较小的子问题上进行了递归.
我正在使用凯特编辑器.这是一个显示我的问题的最小示例:
我有一个文件,出现了一堆:
\command{stuff}
Run Code Online (Sandbox Code Playgroud)
东西是一些任意字母串.我想替换它
\disobey{stuff}
Run Code Online (Sandbox Code Playgroud)
东西不变的地方.正则表达式:
\\command\{[a-zA-Z]*\}
Run Code Online (Sandbox Code Playgroud)
匹配这样的表达.所以我用CTRL-r拉出替换对话框,然后输入
Find: \\command\{[a-zA-Z]*\}
Replace: \\disobey\{\1\}
Run Code Online (Sandbox Code Playgroud)
所以在文档中,有一个实际的例子,
\command{exchange}
Run Code Online (Sandbox Code Playgroud)
当我点击更换按钮更改为
\disobey{1}
Run Code Online (Sandbox Code Playgroud)
在Kate文档中:附录B:正则表达式,\ 1应与使用的第一个模式匹配.这确实是正确的语法吗?我也试过$ 1,#1和其他各种各样的东西.
我有一个计算基本上做了以下几点:
f :: [a] -> ([b],Bool)
Run Code Online (Sandbox Code Playgroud)
实际上可以编写此函数
f = foldr h ([],False) . map g
where h (b,bool) (bs,boolSoFar) = (b:bs,bool || boolSoFar)
Run Code Online (Sandbox Code Playgroud)
哪些g :: a -> (b,Bool)是需要花费大量时间的功能.此外,f通常在小列表上调用,因此尝试并行计算地图似乎很有趣.这可以通过Control.Parallel.Strategies parMap来完成.所以现在我们使用
f = foldr h ([],False) . parMap rseq g
where h (b,bool) (bs,boolSoFar) = (b:bs, bool || boolSoFar)
Run Code Online (Sandbox Code Playgroud)
一切都很好.现在,您将注意到可以在第一个定义中执行顺序优化f.也就是说,我可以使用map-fold fusion将其作为单个折叠编写,因此在列表中循环一次.但是,我失去了做并行的好处.
现在,有人可能会说,在第二个定义中f,再次循环列表并不是那么糟糕,所以为什么不这样做呢.我想我的想法是,如果Haskell有可变变量,那么可以在map的主体中更新这个布尔变量(我想你必须锁定并解锁它).做这样的事情有什么建议吗?
我想考虑以下三个(相关的?)Coq定义.
Inductive nat1: Prop :=
| z1 : nat1
| s1 : nat1 -> nat1.
Inductive nat2 : Set :=
| z2 : nat2
| s2 : nat2 -> nat2.
Inductive nat3 : Type :=
| z3 : nat3
| s3 : nat3 -> nat3.
Run Code Online (Sandbox Code Playgroud)
这三种类型都给出了归纳原则来证明一个命题.
nat1_ind
: forall P : Prop, P -> (nat1 -> P -> P) -> nat1 -> P
nat2_ind
: forall P : nat2 -> Prop,
P z2 -> (forall n : nat2, P n …Run Code Online (Sandbox Code Playgroud) 有人告诉我 Rust 在仿射逻辑中具有语义——所以有删除/弱化但没有重复/收缩。
编译如下:
fn throw_away<A, B>(x: A, _y: B) -> A {
x
}
Run Code Online (Sandbox Code Playgroud)
由于不允许重复,因此无法编译以下内容:
fn dup<A>(x: A) -> (A, A) {
(x, x)
}
Run Code Online (Sandbox Code Playgroud)
同样,这些都不编译:
fn throw_away3<A, B>(x: A, f: fn(A) -> B) -> A {
x;
f(x)
}
fn throw_away4<A, B>(x: A, f: fn(A) -> B) -> A {
throw_away(x, f(x))
}
Run Code Online (Sandbox Code Playgroud)
弱化也是有目共睹的
fn weaken<A, B, C>(f: fn(A) -> B) -> impl Fn(A, C) -> B {
move |x: A, y: C| f(x)
}
Run Code Online (Sandbox Code Playgroud)
fn(A, C) -> …
我想知道关于LLVM的Compiler-RT项目的以下内容:从它调用的程序.根据我的理解,Compiler-RT是一组函数,它们处理LLVM中的指令,这些指令实际上没有硬件对应物(它还有更多的东西吗?).因此,如果我在LLVM中使用除法,则应该用适当的Compiler-RT函数替换它.首先,如果这不正确,请纠正我!
其次,我很好奇谁生成Compiler-RT用法.它是Clang还是LLVM直接.我可以为LLVM编写不同的前端,LLVM会在适当的时候自动处理Compiler-RT的使用吗?
我正在尝试开始使用Haskell的LLVM绑定.一个很好的起点是Hello World.
以下是来自绑定作者的博客.
bldGreet :: CodeGenModule (Function (IO ()))
bldGreet = do
puts <- newNamedFunction ExternalLinkage "puts" :: TFunction (Ptr Word8 -> IO Word32)
greetz <- createStringNul "Hello, World!"
func <- createFunction ExternalLinkage $ do
tmp <- getElementPtr greetz (0::Word32, (0::Word32, ()))
call puts tmp -- Throw away return value.
ret ()
return func
Run Code Online (Sandbox Code Playgroud)
它不编译.
相反,我得到"模糊类型变量n0' in the constraint:
(type-level-0.2.4:Data.TypeLevel.Num.Sets.NatI n0)
arising from a use ofgetElementPtr0'可能修复:添加修复这些类型变量的类型签名"
这是一个可行的变体
llvmModule :: TFunction (IO Word32)
llvmModule =
withStringNul "Hello world!" $ …Run Code Online (Sandbox Code Playgroud) 我对LLVM-Haskell绑定的问题是我得到了"重复"的名字.我认为解释我的问题的最好方法是用一个小的具体例子(注意这个例子是设计的,对于这样一个小例子,有很简单的方法可以解决它......但它确实指出了我的问题).
putc :: TFunction (Int32 -> IO Word32)
putc = newNamedFunction ExternalLinkage "putchar"
simple :: TFunction (Int32 -> IO Word32)
simple = do
internalputc <- putc
createNamedFunction ExternalLinkage "simple" $ \x -> do
call internalputc x
call internalputc x
ret (0 :: Word32)
easy :: TFunction (Int32 -> IO Word32)
easy = do
internalputc <- putc
internalsimple <- simple
createNamedFunction ExternalLinkage "easy" $ \x -> do
call internalsimple x
y <- add x (42 :: Int32)
call internalputc y
ret …Run Code Online (Sandbox Code Playgroud) 我想在Haskell中实现术语图,这样我就可以实现一个使用共享的术语重写引擎.就像是
data TG f v = Var v | Op f [TG f v] | P (Ptr (TG f v))
Run Code Online (Sandbox Code Playgroud)
而且我希望以下内容有意义:
let
t' = Op 'f' [Var 'x', Var 'y']
t = getPointer t'
in
Op 'g' [P t,P t]
Run Code Online (Sandbox Code Playgroud)
然后在重写期间,我只需要重写一次.
但是,我注意到两件事:(1)模块被称为Foreign.Storable,因此它应该只用于FFI的东西,(2)对于像列表这样的任何类型都没有Foreign.Storable的实例; 为什么是这样?
我试图使用channels/STM在Haskell中实现消息传递.也许这是一个糟糕的想法,并且有一种更好的方法来实现/使用Haskell中的消息传递.如果是这种情况,请告诉我; 然而,我的任务已经打开了关于并发Haskell的一些基本问题.
我听说过STM很棒的东西,尤其是Haskell中的实现.既然它支持阅读和写作,并且有一些安全上的好处,我认为可以从那里开始.这提出了我最大的问题:是吗
msg <- atomically $ readTChan chan
Run Code Online (Sandbox Code Playgroud)
其中chan是一个TChan Int,导致等待通道等待它的等待?
考虑以下程序:
p chan = do
atomically $ writeTChan chan 1
atomically $ writeTChan chan 2
q chan = do
msg1 <- atomically $ readTChan chan
msg2 <- atomically $ readTChan chan
-- for testing purposes
putStrLn $ show msg1
putStrLn $ show msg2
main = do
chan <- atomically $ newTChan
p chan
q chan
Run Code Online (Sandbox Code Playgroud)
用ghc --make -threaded编译它,然后运行程序,实际上你得到1然后打印到控制台2.现在,假设我们这样做
main = do
chan <- atomically $ newTChan
forkIO $ p …Run Code Online (Sandbox Code Playgroud)