lambda-calculus

Why `id id` is not a value in OCaml?

南楼画角 提交于 2019-12-11 02:55:14
问题 I am still trying to understand the value restriction in OCaml and I was reading through Wright's paper. And in it states (fun x -> x) (fun y -> y) is not a syntactic value while it is also stating lambda expression should be a value. I am a bit confused here, isn't id id in its essence also a lambda expression? What really counts as a syntactic value in OCaml? I also tried it in utop and found these: utop # let x = let x = (fun y -> y) (fun z -> z) in x ;; val x : '_a -> '_a = <fun> Here id

Implementing Alpha-Equivalence in haskell

瘦欲@ 提交于 2019-12-11 02:43:15
问题 I want to define Alpha-Equivalence using this data definition type Sym = Char data Exp = Var Sym | App Term Exp | Lam Sym Exp deriving (Eq, Read, Show) What is the best way to do this? 回答1: One way is to convert names to De Bruijn indices, where e.g. 0 refers to the variable bound by the innermost enclosing lambda, 1 the next enclosing lambda, and so on. So instead of absolute names, you use relative indices, giving you alpha-equivalence and capture-avoiding substitution for free: type Sym =

Beta reduction in lambda calculus: Order of evaluation important?

喜欢而已 提交于 2019-12-10 20:47:01
问题 Given the following lambda expression, where \ resembles lambda : (\kf.f(\c.co)km)(\x.dox)(\le.le) Is it wrong if I convert (\c.co)k into ko ? I did that and apparently, it was wrong. The right way to go would have been to evaluate the outer function first, meaning (\f.f(\c.co)(\x.dox)m)(\le.le) would have been the desired solution. Is that true, because I can't find any rule that could indicate that in our lecture notes? If yes, why can't I evaluate inner functions first? I've done it like

How do you translate from lambda terms to interaction nets?

馋奶兔 提交于 2019-12-10 15:56:57
问题 On this paper, the author suggests a translation between lambda terms: data Term = Zero | Succ Term | App Term Term | Lam Term and interaction nets: data Net = -- if I understood correctly Apply Net Net Net | Abstract Net Net Net | Delete Net Net Int | Duplicate Net Net Net Int | Erase Net Unfortunately I can not understand his compilation algorithm. It seems like the actual algorithm is missing, and I have no idea what he means with the images on the third page. I've tried understanding it

Integer division using only addition, multiplication, subtraction and maximum

China☆狼群 提交于 2019-12-10 13:47:59
问题 Suppose we have a programming language ℤ which has the following syntax: ℤ := 0 | 1 | (+ ℤ ℤ) | (* ℤ ℤ) | (- ℤ ℤ) | (max ℤ ℤ) For convenience, we can define new binding forms in our language as follows: (not x) = (- 1 x) (abs x) = (- (max 0 (+ x x)) x) (min x y) = (- 0 (max (- 0 x) (- 0 y))) (nil x) = (not (min 1 (abs x))) This language is powerful enough to express branching and comparison operators: (if x y z) = (+ (* x y) (* (not x) z)) (eq x y) = (nil (- x y)) (ne x y) = (not (eq x y))

How do you represent nested types using the Scott Encoding?

浪子不回头ぞ 提交于 2019-12-07 17:03:46
问题 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? 回答1: If the Wikipedia article is correct, then data Tree a = Node (List (Tree a)) | Leaf a has Scott encoding node = λ a . λ

Implement in Haskell the Church encoding of the pair for polymorphic λ-calculus/System F

主宰稳场 提交于 2019-12-07 09:48:14
问题 I want to implement the Church encoding of the pair in polymorphic lambda calculus in Haskell. On page 77, section 8.3.3 of Peter Selinger's notes on lambda calculus, he gives a construction of the cartesian product of two types as A×B = ∀α.(A→B→α)→α ⟨M,N⟩ = Λα.λf A→B→α .fMN For another source, on page 54, section 4.2.3 of Dider Rémy's notes on lambda calculus, he defines the Church encoding of the pair in polymorphic λ-calculus/System F as Λα₁.Λα₂.λx₁∶α₁.λx₂∶α₂.Λβ.λy∶α₁→α₂→β. y x₁ x₂ I think

Y Combinator implementation Scheme

和自甴很熟 提交于 2019-12-07 07:58:33
问题 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

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

ⅰ亾dé卋堺 提交于 2019-12-07 04:17:48
问题 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

Why won't GHC reduce my type family?

倾然丶 夕夏残阳落幕 提交于 2019-12-07 02:31:55
问题 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