lambda-calculus

Church Numerals in haskell

浪子不回头ぞ 提交于 2019-12-05 02:34:26
I am trying to print church numerals in haskell using the definions: 0 := λfx.x 1 := λfx.f x Haskell code: c0 = \f x -> x c1 = \f x -> f x When I enter it in the haskell console I get an error which says test> c1 <interactive>:1:0: No instance for (Show ((t -> t1) -> t -> t1)) arising from a use of `print' at <interactive>:1:0-1 Possible fix: add an instance declaration for (Show ((t -> t1) -> t -> t1)) In a stmt of an interactive GHCi command: print it I am not able to exactly figure out what error says. Thank you! C. A. McCann The problem here is that, by default, it's not possible to print

Is there any efficient way to convert an unary number to a binary number?

三世轮回 提交于 2019-12-04 19:30:50
问题 Let those datatypes represent unary and binary natural numbers, respectively: data UNat = Succ UNat | Zero data BNat = One BNat | Zero BNat | End u0 = Zero u1 = Succ Zero u2 = Succ (Succ Zero) u3 = Succ (Succ (Succ Zero)) u4 = Succ (Succ (Succ (Succ Zero))) b0 = End // 0 b1 = One End // 1 b2 = One (Zero End) // 10 b3 = One (One End) // 11 b4 = One (Zero (Zero End)) // 100 (Alternatively, one could use `Zero End` as b1, `One End` as b2, `Zero (Zero End)` as b3...) My question is: is there any

Is My Lambda Calculus Grammar Unambiguous?

北慕城南 提交于 2019-12-04 18:17:49
I am trying to write a small compiler for a language that handles lambda calculus. Here is the ambiguous definition of the language that I've found: E → ^ v . E | E E | ( E ) | v The symbols ^, ., (, ) and v are tokens. ^ represents lambda and v represents a variable. An expression of the form ^v.E is a function definition where v is the formal parameter of the function and E is its body. If f and g are lambda expressions, then the lambda expression fg represents the application of the function f to the argument g. I'm trying to write an unambiguous grammar for this language, under the

Is it possible to evaluate lambda calculus terms efficiently?

此生再无相见时 提交于 2019-12-04 08:29:51
问题 I've been writing a lot of programs in the lambda calculus recently and I wish I could run some of them in realtime. Yet, as much as the trending functional paradigm is based on the lambda calculus and the rule of B-reductions, I couldn't find a single evaluator that isn't a toy, not meant for efficiency. Functional languages are supposed to be fast, but those I know don't actually provide access to normal forms (see Haskell's lazy evaluator, Scheme's closures and so on), so don't work as LC

Is it possible to build a comparatively fast untyped lambda calculus machine?

扶醉桌前 提交于 2019-12-04 07:46:37
问题 Pure untyped lambda calculus is a powerful concept. However, building a machine or interpreter for real-world use is often described as (close to) impossible. I want to investigate this. Is it theoretically possible to build a comparatively fast untyped lambda calculus machine? By comparatively fast I generally mean comparable to modern Turing-like architectures for a similar range of tasks, within a similar amount of resources (gates, operations, physical space, power use, etc). I place no

Why is Haskell (GHC) so darn fast?

こ雲淡風輕ζ 提交于 2019-12-04 07:18:06
问题 Haskell (with the GHC compiler) is a lot faster than you'd expect. Used correctly, it can get close-ish to low-level languages. (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an inefficient C program, since GHC compiles Haskell to C).) My question is, why? Haskell is declarative and based on lambda calculus. Machine architectures are clearly imperative, being based on turing machines, roughly. Indeed, Haskell doesn't even

Why is a built-in function applied to too few arguments considered to be in weak head normal form?

会有一股神秘感。 提交于 2019-12-04 02:50:27
问题 The Haskell definition says: An expression is in weak head normal form (WHNF), if it is either: a constructor (eventually applied to arguments) like True, Just (square 42) or (:) 1 a built-in function applied to too few arguments (perhaps none) like (+) 2 or sqrt. or a lambda abstraction \x -> expression. Why do built-in functions receive special treatment? According to lambda calculus, there is no difference between a partially applied function and any other function, because at the end we

How to implement a recursive function in lambda calculus using a subset of Clojure language?

泪湿孤枕 提交于 2019-12-03 13:55:13
问题 I'm studying lambda calculus with the book "An Introduction to Functional Programming Through Lambda Calculus" by Greg Michaelson. I implement examples in Clojure using only a subset of the language. I only allow : symbols one-arg lambda functions function application var definition for convenience. So far I have those functions working : (def identity (fn [x] x)) (def self-application (fn [s] (s s))) (def select-first (fn [first] (fn [second] first))) (def select-second (fn [first] (fn

Is there any efficient way to convert an unary number to a binary number?

旧时模样 提交于 2019-12-03 12:36:04
Let those datatypes represent unary and binary natural numbers, respectively: data UNat = Succ UNat | Zero data BNat = One BNat | Zero BNat | End u0 = Zero u1 = Succ Zero u2 = Succ (Succ Zero) u3 = Succ (Succ (Succ Zero)) u4 = Succ (Succ (Succ (Succ Zero))) b0 = End // 0 b1 = One End // 1 b2 = One (Zero End) // 10 b3 = One (One End) // 11 b4 = One (Zero (Zero End)) // 100 (Alternatively, one could use `Zero End` as b1, `One End` as b2, `Zero (Zero End)` as b3...) My question is: is there any way to implement the function: toBNat :: UNat -> BNat That works in O(N) , doing only one pass through

How to implement a recursive function in lambda calculus using a subset of Clojure language?

送分小仙女□ 提交于 2019-12-03 03:09:53
I'm studying lambda calculus with the book "An Introduction to Functional Programming Through Lambda Calculus" by Greg Michaelson. I implement examples in Clojure using only a subset of the language. I only allow : symbols one-arg lambda functions function application var definition for convenience. So far I have those functions working : (def identity (fn [x] x)) (def self-application (fn [s] (s s))) (def select-first (fn [first] (fn [second] first))) (def select-second (fn [first] (fn [second] second))) (def make-pair (fn [first] (fn [second] (fn [func] ((func first) second))))) ;; def make