church-encoding

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

How to implement Binary numbers in Haskell

僤鯓⒐⒋嵵緔 提交于 2019-12-06 03:02:15
问题 I have seen the following data constructor for Church numerals data Nat = Zero | Succ Nat deriving Show But this is unary numbers. How do we implement a data constructor for Binary numbers in Haskell in this way? I have tried this: data Bin = Zero | One | BinC [Bin] deriving Show After this we can get, decimal 5 encoded as BinC [One,Zero,One] But I think I am missing something here. My solution seems not as clever as the Church's solution. No surprise, I am not Church. Little bit of thinking,

Church encoding for dependent types: from Coq to Haskell

一笑奈何 提交于 2019-12-05 13:29:54
In Coq I can define a Church encoding for lists of length n: Definition listn (A : Type) : nat -> Type := fun m => forall (X : nat -> Type), X 0 -> (forall m, A -> X m -> X (S m)) -> X m. Definition niln (A : Type) : listn A 0 := fun X n c => n. Definition consn (A : Type) (m : nat) (a : A) (l : listn A m) : listn A (S m) := fun X n c => c m a (l X n c). Is the type system of Haskell (including its extensions) strong enough to accommodate such definitions? If yes, how? Sure it is: {-# LANGUAGE RankNTypes #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-}

What does “Error: Universe inconsistency” mean in Coq?

前提是你 提交于 2019-12-05 01:25:03
I am working through Software Foundations and am currently doing the exercises on Church numerals. Here is the type signature of a natural number: Definition nat := forall X : Type, (X -> X) -> X -> X. I have defined a function succ of type nat -> nat . I would now like to define an addition function like so: Definition plus (n m : nat) : nat := n nat succ m. However, I get the following error message: Error: Universe inconsistency. What does this error message actually mean? In Coq, everything has a type. Type is no exception: if you ask Coq with the Check command, it will tell you that its

Is it possible to use church encodings without breaking equational reasoning?

风格不统一 提交于 2019-12-04 16:43:08
问题 Mind this program: {-# LANGUAGE RankNTypes #-} import Prelude hiding (sum) type List h = forall t . (h -> t -> t) -> t -> t sum_ :: (Num a) => List a -> a sum_ = \ list -> list (+) 0 toList :: [a] -> List a toList = \ list cons nil -> foldr cons nil list sum :: (Num a) => [a] -> a -- sum = sum_ . toList -- does not work sum = \ a -> sum_ (toList a) -- works main = print (sum [1,2,3]) Both definitions of sum are identical up to equational reasoning. Yet, compiling the second definition of

How to implement Binary numbers in Haskell

谁说胖子不能爱 提交于 2019-12-04 07:25:26
I have seen the following data constructor for Church numerals data Nat = Zero | Succ Nat deriving Show But this is unary numbers. How do we implement a data constructor for Binary numbers in Haskell in this way? I have tried this: data Bin = Zero | One | BinC [Bin] deriving Show After this we can get, decimal 5 encoded as BinC [One,Zero,One] But I think I am missing something here. My solution seems not as clever as the Church's solution. No surprise, I am not Church. Little bit of thinking, I found that my solution depends upon list, whereas the Nat doesn't depend upon any such external

More efficient tail of church encoded list

早过忘川 提交于 2019-12-04 02:13:30
This is a literate haskell post. Simply save it as "ChurchList.lhs" to run it. > {-# LANGUAGE Rank2Types #-} A Church encoded list is a way of representing a list via a function. It resembles both folding and continuation passing style. > newtype ChurchList a = CList {runCList :: forall r. (a -> r -> r) -> r -> r} For illustration as to how this corresponds to a list, here is a O(n) isomorphism > fromList :: [a] -> ChurchList a > fromList xs = CList $ \cons empty -> foldr cons empty xs > toList :: ChurchList a -> [a] > toList cl = runCList cl (:) [] > instance Show a => Show (ChurchList a)

How do I use the Church encoding for Free Monads?

戏子无情 提交于 2019-12-03 12:22:13
I've been using the Free datatype in Control.Monad.Free from the free package. Now I'm trying to convert it to use F in Control.Monad.Free.Church but can't figure out how to map the functions. For example, a simple pattern matching function using Free would look like this - -- Pattern match Free matchFree :: (a -> r) -> (f (Free f a) -> r) -> Free f a -> r matchFree kp _ (Pure a) = kp a matchFree _ kf (Free f) = kf f I can easily convert it to a function that uses F by converting to/from Free - -- Pattern match F (using toF and fromF) matchF :: Functor f => (a -> r) -> (f (F f a) -> r) -> F f

Is it possible to use church encodings without breaking equational reasoning?

左心房为你撑大大i 提交于 2019-12-03 10:36:57
Mind this program: {-# LANGUAGE RankNTypes #-} import Prelude hiding (sum) type List h = forall t . (h -> t -> t) -> t -> t sum_ :: (Num a) => List a -> a sum_ = \ list -> list (+) 0 toList :: [a] -> List a toList = \ list cons nil -> foldr cons nil list sum :: (Num a) => [a] -> a -- sum = sum_ . toList -- does not work sum = \ a -> sum_ (toList a) -- works main = print (sum [1,2,3]) Both definitions of sum are identical up to equational reasoning. Yet, compiling the second definition of works, but the first one doesn't, with this error: tmpdel.hs:17:14: Couldn't match type ‘(a -> t0 -> t0) ->

Closures and universal quantification

不问归期 提交于 2019-12-03 07:26:35
问题 I've been trying to work out how to implement Church-encoded data types in Scala. It seems that it requires rank-n types since you would need a first-class const function of type forAll a. a -> (forAll b. b -> b) . However, I was able to encode pairs thusly: import scalaz._ trait Compose[F[_],G[_]] { type Apply = F[G[A]] } trait Closure[F[_],G[_]] { def apply[B](f: F[B]): G[B] } def pair[A,B](a: A, b: B) = new Closure[Compose[({type f[x] = A => x})#f, ({type f[x] = B => x})#f]#Apply, Id] {