Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #341: HTTPD Error 666 : BOFH was here


comp / comp.lang.lisp / Re: Non-determinism

SubjectAuthor
* Non-determinismB. Pym
`* Re: Non-determinismB. Pym
 +* Re: Non-determinismB. Pym
 |`- Re: Non-determinismKaz Kylheku
 `- Re: Non-determinismB. Pym

1
Subject: Non-determinism
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Tue, 23 Jul 2024 22:44 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Non-determinism
Date: Tue, 23 Jul 2024 22:44:48 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 146
Message-ID: <v7pboq$1d67r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 24 Jul 2024 00:44:48 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="797cc67a814b886922527dba050c3ff4";
logging-data="1480955"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18khxJzdHDue1M0nYEopy5J"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:aaQ1waV2XEi30aC4dNugFDCKuFg=
View all headers

> From: Jeffrey Mark Siskind
> Subject: Re: Permutations - lisp like
> Date: 1998/10/12
> Newsgroups: comp.lang.lisp
>
> One elegant way of generating permutations (or any other form of combinatoric
> enumeration) is to write a nondeterministic description of the combinatoric
> structure. This can be done with Screamer, a nondeterministic extension to
> Common Lisp.
>
> (defun a-split-of-internal (x y)
> (if (null? y)
> (list x y)
> (either (list x y)
> (a-split-of-internal (append x (list (first y))) (rest y)))))
>
> (defun a-split-of (l) (a-split-of-internal '() l))
>
> (defun a-permutation-of (l)
> (if (null l)
> l
> (let ((split (a-split-of (a-permutation-of (rest l)))))
> (append (first split) (cons (first l) (second split))))))
>
> (defun permutations-of (l) (all-values (a-permutation-of l)))
>
> You can get Screamer from my home page.

Using Takafumi SHIDO's "amb". (Tested with Gauche Scheme
and Racket Scheme.)

(define (a-split-of-internal x y)
(if (null? y)
(list x y)
(amb (list x y)
(a-split-of-internal (append x (list (car y))) (cdr y)))))

(define (a-split-of l)
(a-split-of-internal '() l))

(define (a-permutation-of l)
(if (null? l)
l
(let ((split (a-split-of (a-permutation-of (cdr l)))))
(append (car split) (cons (car l) (cadr split))))))

(define (permutations-of l)
(amb-set-of (a-permutation-of l)))

(permutations-of '(a b c))

===>
((a b c) (b a c) (b c a) (a c b) (c a b) (c b a))

(permutations-of '(a b c d))

===>
((a b c d) (b a c d) (b c a d) (b c d a) (a c b d) (c a b d) (c b a d)
(c b d a) (a c d b) (c a d b) (c d a b) (c d b a) (a b d c) (b a d c)
(b d a c) (b d c a) (a d b c) (d a b c) (d b a c) (d b c a) (a d c b)
(d a c b) (d c a b) (d c b a))

;; Modified from the excellent code found here
;; http://www.shido.info/lisp/scheme_amb_e.html
;; and written by
;; SHIDO, Takafumi

;; [ SHIDO's comment ]
;; Notice that you cannot use the code shown in this chapter if
;; the searching path has loops. See SICP 4.3. for detailed
;; information on this matter.

;;; This function is re-assigned in `amb-choose' and `amb-fail' itself.
(define amb-fail #f)

;;; function for nondeterminism
(define (amb-choose . ls)
(if (null? ls)
(amb-fail)
(let ((fail0 amb-fail))
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(set! amb-fail fail0)
(cc (apply amb-choose (cdr ls)))))
(cc (car ls)))))))

;;; nondeterminism macro operator
(define-syntax amb
(syntax-rules ()
((_) (amb-fail))
((_ a) a)
((_ a b ...)
(let ((fail0 amb-fail))
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(set! amb-fail fail0)
(cc (amb b ...))))
(cc a)))))))

;;; returning all possibilities
(define-syntax amb-set-of
(syntax-rules ()
((_ s)
(let ((acc '()))
(amb (let ((v s))
(set! acc (cons v acc))
(amb-fail))
(reverse acc))))))
;; (reverse! acc))))))

;;; if not bool backtrack
(define (amb-assert bool)
(or bool (amb)))

;;; returns arbitrary number larger or equal to n
(define (amb-integer-starting-from n)
(amb n (amb-integer-starting-from (+ 1 n))))

;;; returns arbitrary number between a and b
(define (amb-number-between a b)
(let loop ((i a))
(if (> i b)
(amb)
(amb i (loop (+ 1 i))))))
;; (amb i (loop (1+ i))))))

;;; write following at the end of file
;;; initial value for amb-fail
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(cc 'no-choice)))))

Subject: Re: Non-determinism
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Tue, 23 Jul 2024 23:30 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Non-determinism
Date: Tue, 23 Jul 2024 23:30:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 158
Message-ID: <v7peea$1djes$1@dont-email.me>
References: <v7pboq$1d67r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 24 Jul 2024 01:30:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0324fe81369ea30485cf84037c5c04c3";
logging-data="1494492"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vsfOqhS5R8mSCQS2xNAwh"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:v2buYKM10eqbC6GJZRsMQ8EHv8g=
View all headers

B. Pym wrote:

> ;; Modified from the excellent code found here
> ;; http://www.shido.info/lisp/scheme_amb_e.html
> ;; and written by
> ;; SHIDO, Takafumi
>
>
> ;; [ SHIDO's comment ]
> ;; Notice that you cannot use the code shown in this chapter if
> ;; the searching path has loops. See SICP 4.3. for detailed
> ;; information on this matter.
>
>
> ;;; This function is re-assigned in `amb-choose' and `amb-fail' itself.
> (define amb-fail #f)
>
>
> ;;; function for nondeterminism
> (define (amb-choose . ls)
> (if (null? ls)
> (amb-fail)
> (let ((fail0 amb-fail))
> (call/cc
> (lambda (cc)
> (set! amb-fail
> (lambda ()
> (set! amb-fail fail0)
> (cc (apply amb-choose (cdr ls)))))
> (cc (car ls)))))))
>
> ;;; nondeterminism macro operator
> (define-syntax amb
> (syntax-rules ()
> ((_) (amb-fail))
> ((_ a) a)
> ((_ a b ...)
> (let ((fail0 amb-fail))
> (call/cc
> (lambda (cc)
> (set! amb-fail
> (lambda ()
> (set! amb-fail fail0)
> (cc (amb b ...))))
> (cc a)))))))
>
>
> ;;; returning all possibilities
> (define-syntax amb-set-of
> (syntax-rules ()
> ((_ s)
> (let ((acc '()))
> (amb (let ((v s))
> (set! acc (cons v acc))
> (amb-fail))
> (reverse acc))))))
> ;; (reverse! acc))))))
>
>
> ;;; if not bool backtrack
> (define (amb-assert bool)
> (or bool (amb)))
>
> ;;; returns arbitrary number larger or equal to n
> (define (amb-integer-starting-from n)
> (amb n (amb-integer-starting-from (+ 1 n))))
>
> ;;; returns arbitrary number between a and b
> (define (amb-number-between a b)
> (let loop ((i a))
> (if (> i b)
> (amb)
> (amb i (loop (+ 1 i))))))
> ;; (amb i (loop (1+ i))))))
>
>
> ;;; write following at the end of file
> ;;; initial value for amb-fail
> (call/cc
> (lambda (cc)
> (set! amb-fail
> (lambda ()
> (cc 'no-choice)))))

Problem 4.42 in SICP

Five school girls took an exam. As they think thattheir
parents are too much interested in their score, they promise
that they write one correct and one wrong informations to
their parents. Followings are parts of their letters
concerning their result:

Betty: Kitty was the second and I third.
Ethel: I won the top and Joan the second.
Joan: I was the third and poor Ethel the last.
Kitty: I was the second and Mary the fourth.
Mary: I was the fourth. Betty won the top.

Guess the real order of the five school girls.

Some additional useful functions:

;; ----------------------------------------------
;; Extra functions that don't involve
;; non-determinism.
;; ----------------------------------------------

(define (amb-all-different? . ls)
(let loop ((obj (car ls)) (ls (cdr ls)))
(or (null? ls)
(and (not (member obj ls))
(loop (car ls) (cdr ls))))))

;; First position is numbered 1. [Written by me.]
(define (amb-index x xs)
(let ((tail (member x xs)))
(and tail (- (length xs) -1 (length tail)))))

;; Takes into consideration that y may appear
;; more than once. [Written by me.]
(define (amb-before? x y lst)
(let ((a (member x lst)))
(and a
(let ((b (member y lst)))
(or (not b)
(> (length a) (length b)))))))

Now the problem.

(define (xor a b)
(if a (not b) b))

(define (either a m b n lst)
(xor (= m (amb-index a lst))
(= n (amb-index b lst))))

(define (girls-exam)
(amb-set-of
(let* ((girls '(kitty betty ethel joan mary))
(answer (list
(apply amb-choose girls)
(apply amb-choose girls)
(apply amb-choose girls)
(apply amb-choose girls)
(apply amb-choose girls))))
(amb-assert (apply amb-all-different? answer))
(amb-assert (either 'kitty 2 'betty 3 answer))
(amb-assert (either 'kitty 2 'mary 4 answer))
(amb-assert (either 'mary 4 'betty 1 answer))
(amb-assert (either 'ethel 1 'joan 2 answer))
;; Next line not needed.
;; (amb-assert (either 'joan 3 'ethel 5 answer))
answer)))

(girls-exam)
===>
'((kitty joan betty mary ethel))

Subject: Re: Non-determinism
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Wed, 24 Jul 2024 04:41 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Non-determinism
Date: Wed, 24 Jul 2024 04:41:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 154
Message-ID: <v7q0m7$1jrar$1@dont-email.me>
References: <v7pboq$1d67r$1@dont-email.me> <v7peea$1djes$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 24 Jul 2024 06:41:59 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="dc013996b5bb46648d47418209f67001";
logging-data="1699163"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Hya/Rt1Tj4/o2APooeoDh"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:pc4wnI7x3QTGREHJZZ3lX2XHKb0=
View all headers

B. Pym wrote:

> Problem 4.42 in SICP
>
> Five school girls took an exam. As they think thattheir
> parents are too much interested in their score, they promise
> that they write one correct and one wrong informations to
> their parents. Followings are parts of their letters
> concerning their result:
>
> Betty: Kitty was the second and I third.
> Ethel: I won the top and Joan the second.
> Joan: I was the third and poor Ethel the last.
> Kitty: I was the second and Mary the fourth.
> Mary: I was the fourth. Betty won the top.
>
> Guess the real order of the five school girls.

Shorter:

(define (xor a b)
(if a (not b) b))

(define (either a m b n lst)
(xor (= m (amb-index a lst))
(= n (amb-index b lst))))

(define (girls-exam)
(amb-set-of
(let* ((girls '(kitty betty ethel joan mary))
(answer (amb-permutation girls)))
(amb-assert (either 'kitty 2 'betty 3 answer))
(amb-assert (either 'kitty 2 'mary 4 answer))
(amb-assert (either 'mary 4 'betty 1 answer))
(amb-assert (either 'ethel 1 'joan 2 answer))
;; Next line not needed.
;; (amb-assert (either 'joan 3 'ethel 5 answer))
answer)))

(girls-exam)
===>
((kitty joan betty mary ethel))

Supporting code:

;; Modified from the excellent code found here
;; http://www.shido.info/lisp/scheme_amb_e.html
;; and written by
;; SHIDO, Takafumi

;; [ SHIDO's comment ]
;; Notice that you cannot use the code shown in this chapter if
;; the searching path has loops. See SICP 4.3. for detailed
;; information on this matter.

;;; This function is re-assigned in `amb-choose' and `amb-fail' itself.
(define amb-fail #f)

;;; function for nondeterminism
(define (amb-choose . ls)
(if (null? ls)
(amb-fail)
(let ((fail0 amb-fail))
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(set! amb-fail fail0)
(cc (apply amb-choose (cdr ls)))))
(cc (car ls)))))))

;;; nondeterminism macro operator
(define-syntax amb
(syntax-rules ()
((_) (amb-fail))
((_ a) a)
((_ a b ...)
(let ((fail0 amb-fail))
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(set! amb-fail fail0)
(cc (amb b ...))))
(cc a)))))))

;;; returning all possibilities
(define-syntax amb-set-of
(syntax-rules ()
((_ s)
(let ((acc '()))
(amb (let ((v s))
(set! acc (cons v acc))
(amb-fail))
(reverse acc))))))
;; (reverse! acc))))))

;;; if not bool backtrack
(define (amb-assert bool)
(or bool (amb)))

;;; returns arbitrary number larger or equal to n
(define (amb-integer-starting-from n)
(amb n (amb-integer-starting-from (+ 1 n))))

;;; returns arbitrary number between a and b
(define (amb-number-between a b)
(let loop ((i a))
(if (> i b)
(amb)
(amb i (loop (+ 1 i))))))
;; (amb i (loop (1+ i))))))

;;; write following at the end of file
;;; initial value for amb-fail
(call/cc
(lambda (cc)
(set! amb-fail
(lambda ()
(cc 'no-choice)))))

(define (amb-all-different? . ls)
(let loop ((obj (car ls)) (ls (cdr ls)))
(or (null? ls)
(and (not (member obj ls))
(loop (car ls) (cdr ls))))))

;; [Written by me.]
(define (amb-permutation lst)
(let ((tmp (map (lambda(_) (apply amb-choose lst))
lst)))
(amb-assert (apply amb-all-different? tmp))
tmp))

;; First position is numbered 1. [Written by me.]
(define (amb-index x xs)
(let ((tail (member x xs)))
(and tail (- (length xs) -1 (length tail)))))

;; Takes into consideration that y may appear
;; more than once. [Written by me.]
(define (amb-before? x y lst)
(let ((a (member x lst)))
(and a
(let ((b (member y lst)))
(or (not b)
(> (length a) (length b)))))))

Subject: Re: Non-determinism
From: Kaz Kylheku
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Wed, 24 Jul 2024 08:18 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Non-determinism
Date: Wed, 24 Jul 2024 08:18:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <20240723231718.796@kylheku.com>
References: <v7pboq$1d67r$1@dont-email.me> <v7peea$1djes$1@dont-email.me>
<v7q0m7$1jrar$1@dont-email.me>
Injection-Date: Wed, 24 Jul 2024 10:18:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="22dbec4c97aef40d7cd38abdf24b02a3";
logging-data="1763569"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+cMESt+dRkbEokqQ1L2J/M5VH03FNTONc="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:hFr4LbidT/oPXItvQ45jwPsiGDM=
View all headers

On 2024-07-24, B. Pym <Nobody447095@here-nor-there.org> wrote:
> B. Pym wrote:
>
>> Problem 4.42 in SICP
>>
>> Five school girls took an exam. As they think thattheir
>> parents are too much interested in their score, they promise
>> that they write one correct and one wrong informations to
>> their parents. Followings are parts of their letters
>> concerning their result:
>>
>> Betty: Kitty was the second and I third.
>> Ethel: I won the top and Joan the second.
>> Joan: I was the third and poor Ethel the last.
>> Kitty: I was the second and Mary the fourth.
>> Mary: I was the fourth. Betty won the top.
>>
>> Guess the real order of the five school girls.
>
> Shorter:

[ snip astonishing 130 line continuation-driven spaghetti behemoth ]

> ((kitty joan betty mary ethel))

$ txr girls.tl
#(kitty joan betty mary ethel)

$ cat girls.tl
(defun one-truth (g i1 n1 i2 n2)
(neq (eq [g i1] n1) (eq [g i2] n2)))

(each ((g (perm #(kitty ethel joan mary betty) 5)))
(when (and (one-truth g 1 'kitty 2 'betty)
(one-truth g 0 'ethel 1 'joan)
(one-truth g 2 'joan 4 'ethel)
(one-truth g 1 'kitty 3 'mary)
(one-truth g 3 'mary 0 'betty))
(prinl g)))

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: Non-determinism
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Mon, 5 Aug 2024 21:35 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Non-determinism
Date: Mon, 5 Aug 2024 21:35:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <v8rgi7$13t0c$1@dont-email.me>
References: <v7pboq$1d67r$1@dont-email.me> <v7peea$1djes$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 05 Aug 2024 23:35:07 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="4d0da7ed839683006bcf3d49bfd7bbbd";
logging-data="1176588"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wqP9CBQrp6JY0QsGteakb"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:e6YmaX7l7uWQ61kgFrPKqg/Pz38=
View all headers

B. Pym wrote:

> Problem 4.42 in SICP
>
> Five school girls took an exam. As they think thattheir
> parents are too much interested in their score, they promise
> that they write one correct and one wrong informations to
> their parents. Followings are parts of their letters
> concerning their result:
>
> Betty: Kitty was the second and I third.
> Ethel: I won the top and Joan the second.
> Joan: I was the third and poor Ethel the last.
> Kitty: I was the second and Mary the fourth.
> Mary: I was the fourth. Betty won the top.
>
> Guess the real order of the five school girls.

newLISP

;; Iterate over all permutations of a list, and
;; call a function on each.
(define (permute permute.seq permute.func (permute.built '()))
(if (null? permute.seq)
(permute.func permute.built)
(let (seq (copy permute.seq))
(dotimes (i (length seq))
(unless (zero? i) (rotate seq -1))
(permute
(rest seq)
permute.func
(cons (first seq) permute.built))))))

(define (xor a b) (if a (not b) b))

(define (find* x xs) (+ 1 (find x xs)))

(define (either a m b n lst)
(xor (= m (find* a lst))
(= n (find* b lst))))

(define (check answer)
(if
(and
(either 'kitty 2 'betty 3 answer)
(either 'kitty 2 'mary 4 answer)
(either 'mary 4 'betty 1 answer)
(either 'ethel 1 'joan 2 answer))
(println answer)))

(permute '(kitty betty ethel joan mary) check)
===>
(kitty joan betty mary ethel)

1

rocksolid light 0.9.8
clearnet tor