对于函数 monad,我发现(<*>)和(>>=)/(=<<)有两个非常相似的类型。特别是,(=<<)使相似性更加明显:
(<*>) :: (r -> a -> b) -> (r -> a) -> (r -> b)
(=<<) :: (a -> r -> b) -> (r -> a) -> (r -> b)
Run Code Online (Sandbox Code Playgroud)
所以它就像(<*>)和(>>=)/(=<<)取一个二元函数和一个一元函数,并限制前者的两个参数之一通过后者从另一个参数中确定。毕竟,我们知道对于函数 applicative/monad,
f <*> g = \x -> f x (g x)
f =<< g = \x -> f (g x) x
Run Code Online (Sandbox Code Playgroud)
它们看起来非常相似(或者对称,如果你愿意的话),我不禁想到标题中的问题。
关于 monad 比应用函子“更强大”,在LYAH 的For a few Monads …
monads haskell functional-programming applicative combinatory-logic
我正在尝试在scala中使用非常轻量级的组合子演算编码.最初,我只是实现S和K组合器,应用程序和常量值.后来我希望提升scala函数并允许将表达式作为scala函数进行求值.但是,这是为了以后.这是我到目前为止所拥有的.
/** Combinator expression */
sealed abstract class CE
/** Application: CE| (x y) <=> LC| (x:(A=>B) y:A) : B */
case class Ap[A <: CE, B <: CE, X](e1: A, e2: B) extends CE
/** A raw value with type */
case class Value[T](v: T) extends CE
/** Some combinator */
sealed abstract class Comb extends CE
/** The S combinator: CE| S x y z
* LC| ?x:(A=>B=>C).?y:(A=>B).?z:A.(x z (y z)) : C
* S : ?A.?B.?C. (A …Run Code Online (Sandbox Code Playgroud) 在之前的问题SystemT编译器和处理Haskell中的无限类型时,我询问了如何将SystemT Lambda微积分解析为SystemT组合器.我决定使用普通代数数据类型来编码SystemT Lambda演算和SystemT Combinator演算(基于注释:SystemT编译器和处理Haskell中的无限类型).
SystemTCombinator.hs:
module SystemTCombinator where
data THom = Id
| Unit
| Zero
| Succ
| Compose THom THom
| Pair THom THom
| Fst
| Snd
| Curry THom
| Eval
| Iter THom THom
deriving (Show)
Run Code Online (Sandbox Code Playgroud)
SystemTLambda.hs:
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE TypeSynonymInstances #-}
module SystemTLambda where
import Control.Monad.Catch
import Data.Either (fromRight)
import qualified SystemTCombinator as SystemTC
type …Run Code Online (Sandbox Code Playgroud) 虽然学习Haskell,我遇到了一个挑战,找到两个函数f和g,使得f g和f . g是等价的(总,所以像f = undefined或f = (.) f不计).给定的解决方案是f并且g都等于\x -> x . x(或join (.)).
(我注意到这不是Haskell特有的;它可以用纯粹的组合逻辑表示为"find fand gsuch that f g = B f g",然后给定的解决方案将转换为f = g = W B.)
我理解为什么给定的解决方案在扩展时会起作用,但我不明白如果你不知道它会怎么找到它.这是我能走多远:
f g = f . g (给予)f g z = (f . g) z (双方的扩张)f g z = f (g z) (简化RHS)而且我不知道如何从那里开始.在尝试寻找解决方案时,我会做什么?
haskell functional-programming lambda-calculus combinatory-logic
在搜索关于Raymond Smullyan的To Mock a Mockingbird的信息时,我偶然发现了Stephen Tetley的基本组合器的Haskell代码.我认为这是一个有趣的想法,并决定使用这些组合器从To Mock a Mockingbird的第24章实施"可以做算术的鸟".我得到了定义真理,虚假,继承者,前任和零检查组合的功能,以及将组合布尔变为Haskell布尔值的功能,反之亦然.但是当我尝试创建一个带有组合数并返回整数的函数时,我会遇到问题.我想知道如何使这个功能工作.这是我到目前为止所得到的:
-- | The first 7 combinators below are from Stephen Tetley's Data.Aviary.Birds
-- | (http://hackage.haskell.org/package/data-aviary-0.2.3/docs/Data-Aviary-Birds.html),
-- | with minor modifications.
-- | S combinator - starling.
-- Haskell: Applicative\'s @(\<*\>)@ on functions.
-- Not interdefined.
st :: (a -> b -> c) -> (a -> b) -> a -> c
st f g x = f x (g x)
-- | K combinator …Run Code Online (Sandbox Code Playgroud) 我正在摆弄JavaScript中的Cominators,当我偶然发现维基百科说:"Y组合子可以在SKI演算中表达为:Y = S(K(SII))时,我很自豪(希望)让S上班." S(S(KS)K)(K(SII)))",所以我必须尝试:
var I = function (x) {
return x;
};
var K = function (x) {
return function(){
return x;}
};
var S = function (x) {
return function (y) {
return function (z) {
return x(z)(y(z));
}
}
};
var Y = S (K(S(I)(I))) (S(S(K(S))(K)) (K(S(I)(I))));
Y; //evals to:
//function (z) {return x(z)(y(z));}
//And this (lifted from Crockford's Site):
var factorial = Y(function (fac) {
return function (n) {
return n <= 2 ? n : …Run Code Online (Sandbox Code Playgroud) 我现在明白了类型签名s (s k):
s (s k) :: ((t1 -> t2) -> t1) -> (t1 -> t2) -> t1
Run Code Online (Sandbox Code Playgroud)
我可以在Haskell WinGHCi工具中创建无错误的示例:
示例:
s (s k) (\g -> 2) (\x -> 3)
Run Code Online (Sandbox Code Playgroud)
回报2.
示例:
s (s k) (\g -> g 3) successor
Run Code Online (Sandbox Code Playgroud)
回报4.
其中successor定义如下:
successor = (\x -> x + 1)
Run Code Online (Sandbox Code Playgroud)
尽管如此,我仍然没有一个直观的感受了什么s (s k)呢.
该组合子s (s k)采取任何两个函数f和g.什么是s (s k)用做f …
lambda haskell combinators lambda-calculus combinatory-logic
我正在实现一个解释器,允许用户定义任意组合器并将它们应用于任意术语.例如,用户可以通过输入以下组合子定义来定义对的Church编码:
pair a b c ? c a b
true a b ? a
first a ? a true
Run Code Online (Sandbox Code Playgroud)
然后用户可以输入first (pair a b),根据先前定义的规则逐步减少:
first (pair a b)
? pair a b true
? true a b
? a
Run Code Online (Sandbox Code Playgroud)
S x y z ? x z (y z)
K x y ? x
I x ? x
Run Code Online (Sandbox Code Playgroud)
身份组合子也可以在头两个组合子条款所限定I ? S S K K或I ? S K …
binary-tree interpreter functional-programming combinators combinatory-logic
考虑这个组合子:
S (S K)
Run Code Online (Sandbox Code Playgroud)
将其应用于参数XY:
S (S K) X Y
Run Code Online (Sandbox Code Playgroud)
它签订合同:
X Y
Run Code Online (Sandbox Code Playgroud)
我将S(SK)转换为相应的Lambda术语并得到了这个结果:
(\x y -> x y)
Run Code Online (Sandbox Code Playgroud)
我使用Haskell WinGHCi工具获取(\ xy - > xy)的类型签名,并返回:
(t1 -> t) -> t1 -> t
Run Code Online (Sandbox Code Playgroud)
这对我来说很有意义.
接下来,我使用WinGHCi获取s(sk)的类型签名并返回:
((t -> t1) -> t) -> (t -> t1) -> t
Run Code Online (Sandbox Code Playgroud)
这对我来说没有意义.为什么类型签名不同?
注意:我将s,k和i定义为:
s = (\f g x -> f x (g x))
k = (\a x -> a)
i = (\f -> f)
Run Code Online (Sandbox Code Playgroud) haskell combinators lambda-calculus combinatory-logic type-signature
我想快速正确地减少函数以在Haskell中指向自由格式.我更愿意产生相当可读的结果.我该怎么办呢?
haskell ×7
combinators ×5
interpreter ×2
applicative ×1
binary-tree ×1
implicit ×1
javascript ×1
lambda ×1
monads ×1
ocaml ×1
pointfree ×1
scala ×1
y-combinator ×1