combinators

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

Can clojure evaluate a chain of mixed arity functions and return a partial function if needed?

不问归期 提交于 2019-12-05 10:02:38
Suppose you have three functions of arity 1, 2 and 3 as below: (defn I [x] x) (defn K [x y] x) (defn S [x y z] (x z (y z))) Does clojure have an evaluation function or idiom for evaluating: (I K S I I) as (I (K (S (I (I))))) returning a parital function of arity 2? I am considering creating a macro that can take the simple function definitions above and expand them to multi-arity functions that can return partial results. I would not want to create the macro if there is already a built in or idiomatic way to accomplish this. Here is what the expanded macros would like for the above functions:

Scala: Can I nudge a combinator parser to be locally greedy?

余生颓废 提交于 2019-12-05 02:23:21
Suppose I have an ambiguous language expressed in combinator parser. Is there a way to make certain expressions locally greedy? Here's an example of what I mean. import scala.util.parsing.combinator._ object Example extends JavaTokenParsers { def obj: Parser[Any] = (shortchain | longchain) ~ anyrep def longchain: Parser[Any] = zero~zero~one~one def shortchain: Parser[Any] = zero~zero def anyrep: Parser[Any] = rep(any) def any: Parser[Any] = zero | one def zero: Parser[Any] = "0" def one: Parser[Any] = "1" def main(args: Array[String]) { println(parseAll(obj, args(0) )) } } After compiling, I

Haskell: some and many [duplicate]

筅森魡賤 提交于 2019-12-04 17:43:08
问题 This question already has answers here : What are Alternative's “some” and “many” useful for? (4 answers) Closed 12 months ago . What are some and many in Control.Applicative.Alternative good for? If I write something like some $ Just 42 , it seems to cause infinite recursion, which seems not very useful... 回答1: They make sense, when used as a parser combinator. some means, that the parser is applied as often as possible, but at least once. many is similar, but allows no parse. In case of

Fixed point combinator for mutually recursive functions?

对着背影说爱祢 提交于 2019-12-04 17:02:38
问题 Is there a fixed point combinator for creating tuples of mutually recursive functions? I.e. I'm looking for something like the Y-Combinator but which takes multiple "recursive"* functions, and will return a tuple of functions? *: not really recursive of course, as they are written to take themselves (and siblings) as arguments, in the usual Y-Combinator way. 回答1: The creature you are looking for is Y* combinator. Basing on this page by oleg-at-okmij.org I ported the Y* to Clojure: (defn Y* [&

Scala combinator parsers - distinguish between number strings and variable strings

◇◆丶佛笑我妖孽 提交于 2019-12-04 13:16:32
问题 I'm doing Cay Horstmann's combinator parser exercises, I wonder about the best way to distinguish between strings that represent numbers and strings that represent variables in a match statement: def factor: Parser[ExprTree] = (wholeNumber | "(" ~ expr ~ ")" | ident) ^^ { case a: wholeNumber => Number(a.toInt) case a: String => Variable(a) } The second line there, "case a: wholeNumber" is not legal. I thought about a regexp, but haven't found a way to get it to work with "case". 回答1: I would

Python implementation of Parsec?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 00:35:38
I recently wrote a parser in Python using Ply (it's a python reimplementation of yacc). When I was almost done with the parser I discovered that the grammar I need to parse requires me to do some look up during parsing to inform the lexer. Without doing a look up to inform the lexer I cannot correctly parse the strings in the language. Given than I can control the state of the lexer from the grammar rules I think I'll be solving my use case using a look up table in the parser module, but it may become too difficult to maintain/test. So I want to know about some of the other options. In Haskell

How to create a lazy-seq generating, anonymous recursive function in Clojure?

一世执手 提交于 2019-12-03 16:31:53
问题 Edit : I discovered a partial answer to my own question in the process of writing this, but I think it can easily be improved upon so I will post it anyway. Maybe there's a better solution out there? I am looking for an easy way to define recursive functions in a let form without resorting to letfn . This is probably an unreasonable request, but the reason I am looking for this technique is because I have a mix of data and recursive functions that depend on each other in a way requires a lot

How would you (re)implement iterate in Haskell?

↘锁芯ラ 提交于 2019-12-03 13:44:02
问题 iterate :: (a -> a) -> a -> [a] (As you probably know) iterate is a function that takes a function and starting value. Then it applies the function to the starting value, then it applies the same function to the last result, and so on. Prelude> take 5 $ iterate (^2) 2 [2,4,16,256,65536] Prelude> The result is an infinite list. (that's why I use take ). My question how would you implement your own iterate' function in Haskell, using only the basics ( (:) (++) lambdas, pattern mataching, guards

What are super combinators and constant applicative forms?

时光毁灭记忆、已成空白 提交于 2019-12-03 08:29:40
问题 I'm struggling with what Super Combinators are: A supercombinator is either a constant, or a combinator which contains only supercombinators as subexpressions. And also with what Constant Applicative Forms are: Any super combinator which is not a lambda abstraction. This includes truly constant expressions such as 12, ((+) 1 2), [1,2,3] as well as partially applied functions such as ((+) 4). Note that this last example is equivalent under eta abstraction to \ x -> (+) 4 x which is not a CAF.