lambda-calculus

Church Numerals in haskell

一笑奈何 提交于 2019-12-06 23:27:57
问题 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

SystemT Compiler and dealing with Infinite Types in Haskell

会有一股神秘感。 提交于 2019-12-06 02:42:41
I'm following this blog post: http://semantic-domain.blogspot.com/2012/12/total-functional-programming-in-partial.html It shows a small OCaml compiler program for System T (a simple total functional language) . The entire pipeline takes OCaml syntax (via Camlp4 metaprogramming) transforms them to OCaml AST that is translated to SystemT Lambda Calculus (see: module Term ) and then finally SystemT Combinator Calculus (see: module Goedel ). The final step is also wrapped with OCaml metaprogramming Ast.expr type. I'm attempting to translate it to Haskell without the use of Template Haskell. For

How do you represent nested types using the Scott Encoding?

不羁岁月 提交于 2019-12-06 01:37:20
An ADT can be represented using the Scott Encoding by replacing products by tuples and sums by matchers. For example: data List a = Cons a (List a) | Nil Can be encoded using the Scott Encoding as: cons = (λ h t c n . c h t) nil = (λ c n . n) But I couldn't find how nested types can be encoded using SE: data Tree a = Node (List (Tree a)) | Leaf a How can it be done? If the Wikipedia article is correct, then data Tree a = Node (List (Tree a)) | Leaf a has Scott encoding node = λ a . λ node leaf . node a leaf = λ a . λ node leaf . leaf a It looks like the Scott encoding is indifferent to (nested

To prove SKK and II are beta equivalent, lambda calculus

孤街醉人 提交于 2019-12-05 20:10:16
I am new to lambda calculus and struggling to prove the following. SKK and II are beta equivalent. where S = lambda xyz.xz(yz) K = lambda xy.x I = lambda x.x I tried to beta reduce SKK by opening it up, but got nowhere, it becomes to messy. Dont think SKK can be reduced further without expanding S, K. SKK = (λxyz.xz(yz))KK → λz.Kz(Kz) (in two steps actually, for the two parameters) Kz = (λxy.x)z → λy.z λz.Kz(Kz) → λz.(λy.z)(λy.z) (again, several steps) → λz.z = I (You should be able to prove that II → I ) ;another approach with fewer steps, first reduce SK to λyz.z; SKK = (λxyz.xz(yz))KK → λyz

convert flip lambda into SKI terms

情到浓时终转凉″ 提交于 2019-12-05 19:37:08
I'm having trouble converting the lambda for flip into the SKI combinators (I hope that makes sense). Here is my conversion: /fxy.fyx /f./x./y.fyx /f./x.S (/y.fy) (/y.x) /f./x.S f (/y.x) /f./x.S f (K x) /f.S (/x.S f) (/x.K x) /f.S (/x.S f) K /f.S (S (/x.S) (/x.f)) K /f.S (S (K S) (K f)) K S (/f.S (S (K S) (K f))) (/f.K) S (/f.S (S (K S) (K f))) (K K) S (S (/f.S) (/f.S (K S) (K f))) (K K) S (S (K S) (/f.S (K S) (K f))) (K K) S (S (K S) (S (/f.S (K S)) (/f.K f))) (K K) S (S (K S) (S (/f.S (K S)) K)) (K K) S (S (K S) (S (S (/f.S) (/f.K S)) K)) (K K) S (S (K S) (S (S (K S) (/f.K S)) K)) (K K) S (S

Y Combinator implementation Scheme

╄→гoц情女王★ 提交于 2019-12-05 14:36:53
I am really new to scheme functional programming. I recently came across Y-combinator function in lambda calculus, something like this Y ≡ (λy.(λx.y(xx))(λx.y(xx))) . I wanted to implement it in scheme, i searched alot but i didn't find any implementation which exactly matches the above given structure. Some of them i found are given below: (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg))))))) and (define Y (lambda (r) ((lambda (f) (f f)) (lambda (y) (r (lambda (x) ((y y) x))))))) As you

Call by value in the lambda calculus

ぐ巨炮叔叔 提交于 2019-12-05 13:05:44
问题 I'm working my way through Types and Programming Languages, and Pierce, for the call by value reduction strategy, gives the example of the term id (id (λz. id z)) . The inner redex id (λz. id z) is reduced to λz. id z first, giving id (λz. id z) as the result of the first reduction, before the outer redex is reduced to the normal form λz. id z . But call by value order is defined as 'only outermost redexes are reduced', and 'a redex is reduced only when its right-hand side has already been

The type signature of a combinator does not match the type signature of its equivalent Lambda function

你说的曾经没有我的故事 提交于 2019-12-05 10:42:44
Consider this combinator: S (S K) Apply it to the arguments X Y: S (S K) X Y It contracts to: X Y I converted S (S K) to the corresponding Lambda terms and got this result: (\x y -> x y) I used the Haskell WinGHCi tool to get the type signature of (\x y -> x y) and it returned: (t1 -> t) -> t1 -> t That makes sense to me. Next, I used WinGHCi to get the type signature of s (s k) and it returned: ((t -> t1) -> t) -> (t -> t1) -> t That doesn't make sense to me. Why are the type signatures different? Note: I defined s, k, and i as: s = (\f g x -> f x (g x)) k = (\a x -> a) i = (\f -> f) We start

How would you implement a beta-reduction function in F#?

最后都变了- 提交于 2019-12-05 10:39:31
I am writing a lambda calculus in F#, but I am stuck on implementing the beta-reduction (substituting formal parameters with the actual parameters). (lambda x.e)f --> e[f/x] example of usage: (lambda n. n*2+3) 7 --> (n*2+3)[7/n] --> 7*2+3 So I'd love to hear some suggestions in regards to how others might go about this. Any ideas would be greatly appreciated. Thanks! Assuming your representation of an expression looks like type expression = App of expression * expression | Lambda of ident * expression (* ... *) , you have a function subst (x:ident) (e1:expression) (e2:expression) : expression

Why won't GHC reduce my type family?

邮差的信 提交于 2019-12-05 07:23:41
Here's an untyped lambda calculus whose terms are indexed by their free variables. I'm using the singletons library for singleton values of type-level strings. {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} import Data.Singletons import Data.Singletons.TypeLits data Expr (free :: [Symbol]) where Var :: Sing a -> Expr '[a] Lam :: Sing a -> Expr as -> Expr (Remove a as) App :: Expr free1 -> Expr free2 -> Expr (Union free1 free2) A Var introduces a free variable. A