Re: A Taste of cKanren (via a coding challenge)

2011-11-13 Thread David Nolen
On Sun, Nov 13, 2011 at 10:46 AM, Jules wrote: > Here is a slight modification of the previous program that can be > executed in two directions: > > N = [40] > A = range(1,40) > B = range(1,40) > C = range(1,40) > D = range(1,40) > > def valid(a,b,c,d,n): > weights = set(w*a+x*b+y*c+z*d for w

Re: A Taste of cKanren (via a coding challenge)

2011-11-13 Thread Laurent PETIT
So, if one stays in the imperative world, why not just the generic problem instead of the specific one ? This clojure solution can solve the problem for any N (total weight) and any M (any number of parts the stone can be spit into): https://gist.github.com/1332020 The implementation is a bit lo

Re: A Taste of cKanren (via a coding challenge)

2011-11-13 Thread Jules
Here is a slight modification of the previous program that can be executed in two directions: N = [40] A = range(1,40) B = range(1,40) C = range(1,40) D = range(1,40) def valid(a,b,c,d,n): weights = set(w*a+x*b+y*c+z*d for w in [-1,0,1] for x in [-1,0,1]

Re: A Taste of cKanren (via a coding challenge)

2011-11-12 Thread David Nolen
checko and subchecko work together to determine whether an ordered set of weights can produce integers 1 to 40 in reverse. It's more verbose since we are preserving the ability to run our program backwards - which is simply a byproduct of writing a purely relational program. I'm curious if it can

Re: A Taste of cKanren (via a coding challenge)

2011-11-12 Thread Ambrose Bonnaire-Sergeant
I'll just clarify, the "matches" function body jumps out at me. "checko" doesn't, but it's role in the problem is still clear, even if its implementation is not. I think that's still an important trait. Can checko be improved? I'm not sure. What does subchecko do, David? Ambrose On Sun, Nov 13,

Re: A Taste of cKanren (via a coding challenge)

2011-11-12 Thread Ambrose Bonnaire-Sergeant
"Understandability" is subjective. The meaning of the cKanren really jumps out at me, the python program takes a lot more for me to think through. There is nothing wrong with pruning the search space in this program. As you said, doing so reduced the Python program execution time to 2ms! Ambrose

Re: A Taste of cKanren (via a coding challenge)

2011-11-12 Thread David Nolen
Write a version in Python that can infer 40 from the inputs then maybe we can talk about declarative. I have no idea what you mean by "cheating" and even less of an idea what you mean by "nice." On Saturday, November 12, 2011, Jules wrote: > The time difference is largely due to using the product

Re: A Taste of cKanren (via a coding challenge)

2011-11-12 Thread Jules
The time difference is largely due to using the product library function instead of for comprehensions and the fact that the cKanren version cheats by hardcoding part of the solution, and hardcoding an extra constraint alldiff(a,b,c,d). The following code takes ~12ms with PyPy on my computer: def

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread David Nolen
Also note that even given all this generality over the Python code - the earlier Python implementation takes ~300ms and this implementation takes >900ms on my machine. Quite a bit slower than ~12ms. Inferring 40 takes even less time of course - ~8ms. But really the execution time is just icing on

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread David Nolen
Excellent point Ambrose ;) And here it is: (define (subchecko w sl r o n) (conde ((== sl ()) (fresh (a d) (domfd a (range 1 100)) (conde ((conso a d r) (plusfd a 1 w) (conso w r o)) ((== r '()) (conso w r o) ((fresh (a b c

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Ambrose Bonnaire-Sergeant
I assume the cKanren version can run backwards with a little tweak? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with y

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread David Nolen
(define (matches n) (run #f (q) (fresh (a b c d s1 s2) (== q `(,a ,b ,c ,d)) (domfd a b c d s1 s2 (range 1 n)) (all-differentfd `(,a ,b ,c ,d)) (== a 1) (<=fd a b) (<=fd b c) (<=fd c d) (plusfd a b s1) (plusfd s1 c s2) (plusfd s2 d n) (checko `

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread David Nolen
Can you reorder your statements without changing the meaning of your program? For example you cannot move the placement of the "return" expression. David On Fri, Nov 11, 2011 at 8:09 PM, Jules wrote: > Here is a new program. Perhaps you would consider this declarative: > > def valid(a,b,c,d): >

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Jules
Here is a new program. Perhaps you would consider this declarative: def valid(a,b,c,d): weights = set(w*a+x*b+y*c+z*d for (w,x,y,z) in product([-1,0,1],repeat=4)) return weights >= set(range(1,41)) ws = [(a,b,c,d) for (a,b,c,d) in product(range(1,41),repeat=4) if a <= b <=

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Jules
Are we reading the same cKanren code? I'll give you that the matches definition is declarative, but then read checko and subchecko. They are all about (recursive) control flow. Where does the specification say anything remotely close to the checko and subchecko relations? In contrast to this, the P

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Timothy Baldridge
> My Python code is much more declarative than the given > cKanren code in that regard. Just compare: > http://dosync.posterous.com/another-taste-of-ckanren I don't think you understand what declarative programming is at its core. Declarative programming To borrow from the ever-present wikipedi

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Jules
In the same way the cKanren version is syntactic sugar around imperative code. Declarative is not a property of a language, it's a property of code that says how close to a mathematical specification the code is. My Python code is much more declarative than the given cKanren code in that regard. Ju

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Timothy Baldridge
>I wonder if you can make the cKanren version just as declarative as >this one (cKanren's purpose being declarative). I don't think the Python version could be considered declarative. One of the concepts behind logic programming (and to some extent declarative programming) is that you can simply p

Re: A Taste of cKanren (via a coding challenge)

2011-11-11 Thread Jules
Here is a Python version (http://pastebin.com/reW5eaCy): def valid(a,b,c,d): return set(w*a + x*b + y*c + z*d for w in [-1,0,1] for x in [-1,0,1] for y in [-1,0,1] for z in [-1,0,1]) > set(range(1,41)) ws = [(a,b,c,d) for a in range(

Re: A Taste of cKanren (via a coding challenge)

2011-10-31 Thread David Nolen
A blog post explaining the solution step by step http://dosync.posterous.com/another-taste-of-ckanren. On Mon, Oct 31, 2011 at 9:36 PM, David Nolen wrote: > Here's a correct version that solves the puzzle in ~12ms, > https://gist.github.com/1329580. A bit longer but it fun to combine > constrain

Re: A Taste of cKanren (via a coding challenge)

2011-10-31 Thread David Nolen
Here's a correct version that solves the puzzle in ~12ms, https://gist.github.com/1329580. A bit longer but it fun to combine constraints w/ search. Will try to find some time to write a more detailed explanation. On Sun, Oct 30, 2011 at 9:00 PM, David Nolen wrote: > Heh, as someone pointed out

Re: A Taste of cKanren (via a coding challenge)

2011-10-30 Thread Ambrose Bonnaire-Sergeant
Wow! Shows off the advantages of embedding in a language like Scheme too. I'm planning to join you in the implementation in core.logic after November. Keep me (us) posted :) Would CLP be more useful for a predicate dispatch implementation than what core.logic provides currently? Ambrose On Mon

Re: A Taste of cKanren (via a coding challenge)

2011-10-30 Thread David Nolen
Heh, as someone pointed out this doesn't actually solve the puzzle since I'm not considering putting stones on either side of the scale. Still I think the idea of what cKanren can do is communicated :) On Sun, Oct 30, 2011 at 8:34 PM, Brent Millare wrote: > Looks really cool. Can't wait to see th

Re: A Taste of cKanren (via a coding challenge)

2011-10-30 Thread Brent Millare
Looks really cool. Can't wait to see the talk. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To u

A Taste of cKanren (via a coding challenge)

2011-10-30 Thread David Nolen
Larent Petit pointed out this fun challenge: http://beust.com/weblog/2011/10/30/a-new-coding-challenge/ To get a sense of how cool cKanren and constraint logic programming is I recommend reading my post about solving it using cKanren http://dosync.posterous.com/a-taste-of-ckanren. This is what Wi