church-encoding

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

谁都会走 提交于 2020-01-23 04:34:09
问题 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? 回答1: In Coq,

Operations on Church Lists in Haskell

房东的猫 提交于 2020-01-14 12:41:36
问题 I am referring to this question type Churchlist t u = (t->u->u)->u->u In lambda calculus, lists are encoded as following: [] := λc. λn. n [1,2,3] := λc. λn. c 1 (c 2 (c 3 n)) mapChurch :: (t->s) -> (Churchlist t u) -> (Churchlist s u) mapChurch f l = \c n -> l (c.f) n I am thinking about what other list functions I could implement on Churchlists and successfully wrote a conc2 function that concatenates 2 church lists conc2Church l1 l2 c n = l1 c (l2 c n) I also tried a zipWithChurch that

Church encoding of lists using right folds and difference lists

点点圈 提交于 2020-01-11 13:08:45
问题 Here is the sequential question after How to store data of a functional chain of Monoidal List? and Extracting data from a function chain without arrays and here I would like to express my respect and appreciation for contributors to my Questions, especially by @Aadit M Shah and @user633183 Now, this question is opened to clarify the similarities and differences or relation between Difference list and Church list . Difference list https://stackoverflow.com/a/51320041/6440264 A difference list

Subtraction of church numerals in haskell

不羁岁月 提交于 2019-12-30 00:52:11
问题 I'm attempting to implement church numerals in Haskell, but I've hit a minor problem. Haskell complains of an infinite type with Occurs check: cannot construct the infinite type: t = (t -> t1) -> (t1 -> t2) -> t2 when I try and do subtraction. I'm 99% positive that my lambda calculus is valid (although if it isn't, please tell me). What I want to know, is whether there is anything I can do to make haskell work with my functions. module Church where type (Church a) = ((a -> a) -> (a -> a))

Church lists in Haskell

北战南征 提交于 2019-12-22 01:52:54
问题 I had to implement the haskell map function to work with church lists which are defined as following: type Churchlist t u = (t->u->u)->u->u In lambda calculus, lists are encoded as following: [] := λc. λn. n [1,2,3] := λc. λn. c 1 (c 2 (c 3 n)) The sample solution of this exercise is: mapChurch :: (t->s) -> (Churchlist t u) -> (Churchlist s u) mapChurch f l = \c n -> l (c.f) n I have NO idea how this solution works and I don't know how to create such a function. I have already experience with

Printing Church Booleans

我只是一个虾纸丫 提交于 2019-12-11 17:19:17
问题 The following code is meant to print Church encoding of booleans as Haskell's Bool : {-#LANGUAGE FlexibleInstances #-} instance Show (t -> t -> t) where show b = show $ b True False Which leads to this error: <interactive>:4:21: error: • Couldn't match expected type ‘t’ with actual type ‘Bool’ ‘t’ is a rigid type variable bound by the instance declaration at <interactive>:3:10-27 • In the first argument of ‘b’, namely ‘True’ In the second argument of ‘($)’, namely ‘b True False’ In the

Church encoding for dependent types: from Coq to Haskell

可紊 提交于 2019-12-10 07:41:31
问题 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? 回答1: Sure it is: {-#

More efficient tail of church encoded list

风格不统一 提交于 2019-12-09 14:42:44
问题 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 >

How do I use the Church encoding for Free Monads?

心已入冬 提交于 2019-12-09 09:33:43
问题 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

Sml Church numeral type inference

。_饼干妹妹 提交于 2019-12-08 11:31:39
问题 I have this expression in SML and need to find the most general type of it. When run through the compiler I get what it shows below. How would I go about finding what the most general type would be of not only this function but other functions like church numerals function "two". val one = fn f => (fn x => f x) Why is the type of this: ('a -> 'b) -> 'a -> 'b 回答1: What you do is, you apply a process called Hindley–Milner type inference . The general principle involves three steps: First, we