From: ·········@gmail.com
Subject: Can lisp do amb as well?
Date: 
Message-ID: <1176735778.487417.245950@w1g2000hsg.googlegroups.com>
Hi

Thanks for all the answers I get for the macro question.
I couldn't get any help from scheme group.
If lisp can do amb as well then my problem is solved.
This is the particular problem (not homework) I'm trying to solve:
This is like solving variable length permutation which is hard to do
without in place code expansion. You can use a single number to map
permutation but it's ugly.

;; let n = number of initial conditions                n>=1
;;     l = number of non zero terms  (matrix size)     l>=1
;;     v = number of verification terms                v>=1
;; total terms = tt = n + l + v
;;                l = g (glob_terms) + c (constant term 0|1 )
;;                    g >= n (all initial conditions must be used)
(define (solve-gen tt)
    (define hd 2)
    (let ((n (number-between 1 tt))
          (v (number-between 1 tt))
          (l (number-between 1 tt))
         )
      (assert       (= (+ n l v) tt) )
      (let ((g (number-between 1 l))
            (c (number-between 0 1))
           )
         (assert       (= (+ g c) l)  )
         (list n v l g c)
         ;; all dimensions are locked in
;; I need to do in-place code expansion
;; (make-list g (make-list n (number-between 0 1)))
      )
    )
)

(bag-of (list (number-between 0 1) (number-between 0 1) ))
(bag-of (list (number-between 0 1) (number-between 0 1) (number-
between 0 1) ))
> ((0 0) (0 1) (1 0) (1 1))
> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))

;; as you can see the code would only work if can expand code in place
dynamically n times.

From: D Herring
Subject: Re: Can lisp do amb as well?
Date: 
Message-ID: <-p-dncL5asRwC77bnZ2dnUVZ_tadnZ2d@comcast.com>
·········@gmail.com wrote:
> Thanks for all the answers I get for the macro question.
> I couldn't get any help from scheme group.
> If lisp can do amb as well then my problem is solved.
> This is the particular problem (not homework) I'm trying to solve:
> This is like solving variable length permutation which is hard to do
> without in place code expansion. You can use a single number to map
> permutation but it's ugly.
> 
> ;; let n = number of initial conditions                n>=1
> ;;     l = number of non zero terms  (matrix size)     l>=1
> ;;     v = number of verification terms                v>=1
> ;; total terms = tt = n + l + v
> ;;                l = g (glob_terms) + c (constant term 0|1 )
> ;;                    g >= n (all initial conditions must be used)
> (define (solve-gen tt)
>     (define hd 2)
>     (let ((n (number-between 1 tt))
>           (v (number-between 1 tt))
>           (l (number-between 1 tt))
>          )
>       (assert       (= (+ n l v) tt) )
>       (let ((g (number-between 1 l))
>             (c (number-between 0 1))
>            )
>          (assert       (= (+ g c) l)  )
>          (list n v l g c)
>          ;; all dimensions are locked in
> ;; I need to do in-place code expansion
> ;; (make-list g (make-list n (number-between 0 1)))
>       )
>     )
> )
> 
> (bag-of (list (number-between 0 1) (number-between 0 1) ))
> (bag-of (list (number-between 0 1) (number-between 0 1) (number-
> between 0 1) ))
>> ((0 0) (0 1) (1 0) (1 1))
>> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
> 
> ;; as you can see the code would only work if can expand code in place
> dynamically n times.
> 

Sorry; I'm not familiar with your terminology...  Your "amb" is similar 
to the following link, right?
http://www.rubyquiz.com/quiz70.html

If so, then its readily implemented in Lisp.  I've done similar things 
to generate backtracking parsers from an EBNF grammar...  You might 
enjoy a couple books:
Paradigms of Artificial Intelligence Programming by Peter Norvig
- chapter 11, Logic Programming.
http://norvig.com/paip.html

On Lisp by Paul Graham
-  chapter 22, Nondeterminism.
http://paulgraham.com/onlisp.html

- Daniel
From: D Herring
Subject: Re: Can lisp do amb as well?
Date: 
Message-ID: <UoqdneAfL_R2BL7bnZ2dnUVZ_oOknZ2d@comcast.com>
D Herring wrote:
> ·········@gmail.com wrote:
>> Thanks for all the answers I get for the macro question.
>> I couldn't get any help from scheme group.
>> If lisp can do amb as well then my problem is solved.
...
> Sorry; I'm not familiar with your terminology...  Your "amb" is similar 
> to the following link, right?
> http://www.rubyquiz.com/quiz70.html

Oh how strongly Ruby programmers lust after Lisp:
http://www.rubyquiz.com/quiz95.html
From: Frank Buss
Subject: Re: Can lisp do amb as well?
Date: 
Message-ID: <ufo9wc05tpxx.191xsopz6qqss$.dlg@40tude.net>
·········@gmail.com wrote:

> (bag-of (list (number-between 0 1) (number-between 0 1) ))
> (bag-of (list (number-between 0 1) (number-between 0 1) (number-
> between 0 1) ))
>> ((0 0) (0 1) (1 0) (1 1))
>> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
> 
> ;; as you can see the code would only work if can expand code in place
> dynamically n times.

If you just want to calculate all possible combinations, you don't need
macros:

(defun combinations-caller (max count fun &optional (item '()))
  (if (> count 0)
      (loop for i from 0 to max do
            (combinations-caller max (1- count) fun (cons i item)))
    (funcall fun item)))

(defun combinations (max count)
  (let ((list '()))
    (combinations-caller max
                         count
                         (lambda (item) (push item list)))
    list))

CL-USER > (combinations 1 2)
((1 1) (0 1) (1 0) (0 0))

CL-USER > (combinations 1 3)
((1 1 1) (0 1 1) (1 0 1) (0 0 1) (1 1 0) (0 1 0) (1 0 0) (0 0 0))

CL-USER > (combinations 2 2)
((2 2) (1 2) (0 2) (2 1) (1 1) (0 1) (2 0) (1 0) (0 0))


If you are searching for a general list comprehension algorithm, macros
could be useful, like described in a talk at the ILC2007:

http://www.international-lisp-conference.org/2007/speakers#latendresse_mario

The talk was very interesting and it was impressive how small the macro was
to implement list comprehensions like used e.g. in Haskell.
(combinations 2 2) would look like this:
((list x y) (x <- '(0 1 2)) (y <- '(0 1 2)))

I have the paper in printed form (they gave all ILC participants a nice
book with all articles), maybe it is available on the web, too.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: ·········@gmail.com
Subject: Re: Can lisp do amb as well?
Date: 
Message-ID: <1176744077.049160.163320@e65g2000hsc.googlegroups.com>
On Apr 17, 12:56 am, Frank Buss <····@frank-buss.de> wrote:
> ·········@gmail.com wrote:
> > (bag-of (list (number-between 0 1) (number-between 0 1) ))
> > (bag-of (list (number-between 0 1) (number-between 0 1) (number-
> > between 0 1) ))
> >> ((0 0) (0 1) (1 0) (1 1))
> >> ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))
>
> > ;; as you can see the code would only work if can expand code in place
> > dynamically n times.
>
> If you just want to calculate all possible combinations, you don't need
> macros:

The problem is not really about generating combination or
permutations.
I can always map permutation with a single integer. (ugly)
(I don't want to use coroutines either, the code become difficult to
read. ugly)
It's about dynamic code expansion.
If I can use library I could've used Maple.
I've just got help from scheme group and the code is working.
So, no show this time, sorry.


>
> (defun combinations-caller (max count fun &optional (item '()))
>   (if (> count 0)
>       (loop for i from 0 to max do
>             (combinations-caller max (1- count) fun (cons i item)))
>     (funcall fun item)))
>
> (defun combinations (max count)
>   (let ((list '()))
>     (combinations-caller max
>                          count
>                          (lambda (item) (push item list)))
>     list))
>
> CL-USER > (combinations 1 2)
> ((1 1) (0 1) (1 0) (0 0))
>
> CL-USER > (combinations 1 3)
> ((1 1 1) (0 1 1) (1 0 1) (0 0 1) (1 1 0) (0 1 0) (1 0 0) (0 0 0))
>
> CL-USER > (combinations 2 2)
> ((2 2) (1 2) (0 2) (2 1) (1 1) (0 1) (2 0) (1 0) (0 0))
>
> If you are searching for a general list comprehension algorithm, macros
> could be useful, like described in a talk at the ILC2007:
>
> http://www.international-lisp-conference.org/2007/speakers#latendress...
>
> The talk was very interesting and it was impressive how small the macro was
> to implement list comprehensions like used e.g. in Haskell.
> (combinations 2 2) would look like this:
> ((list x y) (x <- '(0 1 2)) (y <- '(0 1 2)))
>
> I have the paper in printed form (they gave all ILC participants a nice
> book with all articles), maybe it is available on the web, too.
>
> --
> Frank Buss, ····@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de
From: Frank Buss
Subject: Re: Can lisp do amb as well?
Date: 
Message-ID: <1fumy0tabfkd.1cuupsrib43ew.dlg@40tude.net>
Frank Buss wrote:

> If you are searching for a general list comprehension algorithm, macros
> could be useful, like described in a talk at the ILC2007:
> 
> http://www.international-lisp-conference.org/2007/speakers#latendresse_mario
> 
> The talk was very interesting and it was impressive how small the macro was
> to implement list comprehensions like used e.g. in Haskell.
> (combinations 2 2) would look like this:
> ((list x y) (x <- '(0 1 2)) (y <- '(0 1 2)))
> 
> I have the paper in printed form (they gave all ILC participants a nice
> book with all articles), maybe it is available on the web, too.

I found it, really interesting:

http://www.iro.umontreal.ca/~latendre/publications/listCompFinal.pdf

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de