unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* CPS mystery
@ 2018-05-28 13:49 Matt Wette
  2018-05-29 17:54 ` Mark H Weaver
  0 siblings, 1 reply; 3+ messages in thread
From: Matt Wette @ 2018-05-28 13:49 UTC (permalink / raw)
  To: guile-user

Hi All,

I'm trying to generate CPS to feed to the compiler tower.
The following program is supposed to evaluate, expressed in Scheme, `(+ 1 2)'.
That is, essentially,
   scheme@(guile-user)> (apply + '(1 2))
   $1 = 3
I get the error message following.  Any clues what is going on?

(0 . #<cps (kfun () 0 1 9)>)
(1 . #<cps (ktail)>)
(2 . #<cps (kargs (rval) (1) (continue 1 (values 1)))>)
(3 . #<cps (kargs (arg) (4) (continue 2 (call 2 3 4)))>)
(4 . #<cps (kargs (arg) (3) (continue 3 (const 2)))>)
(5 . #<cps (kargs (arg) (2) (continue 4 (const 1)))>)
(6 . #<cps (kargs (t) (6) (continue 5 (primcall resolve 5 6)))>)
(7 . #<cps (kargs (name) (5) (continue 6 (const #t)))>)
(8 . #<cps (kargs () () (continue 7 (const +)))>)
(9 . #<cps (kclause (() () #f () #f) 8)>)

While compiling expression:
Throw to key `match-error' with args `("match" "no matching pattern"
  #<cps (kargs (rval) (6) (continue 10 (primcall handle-interrupts)))>)'.


And if I fire up Guile and turn off optimization, I get this, where I don't
even see the + resolved.
scheme@(guile-user)> (define v (compile '(+ 1 2) #:from 'scheme #:to 'cps))
scheme@(guile-user)> (disp-cps v)
(0 . #<cps (kfun () 0 1 4)>)
(1 . #<cps (ktail)>)
(2 . #<cps (kargs (val) (1) (continue 1 (values 1)))>)
(3 . #<cps (kargs () () (continue 2 (const 3)))>)
(4 . #<cps (kclause (() () #f () #f) 3)>)

Matt




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: CPS mystery
  2018-05-28 13:49 CPS mystery Matt Wette
@ 2018-05-29 17:54 ` Mark H Weaver
  2018-05-30  2:58   ` Matt Wette
  0 siblings, 1 reply; 3+ messages in thread
From: Mark H Weaver @ 2018-05-29 17:54 UTC (permalink / raw)
  To: Matt Wette; +Cc: guile-user

Hi Matt,

Matt Wette <matt.wette@gmail.com> writes:

> I'm trying to generate CPS to feed to the compiler tower.
> The following program is supposed to evaluate, expressed in Scheme, `(+ 1 2)'.
> That is, essentially,
>   scheme@(guile-user)> (apply + '(1 2))
>   $1 = 3
> I get the error message following.  Any clues what is going on?
>
> (0 . #<cps (kfun () 0 1 9)>)
> (1 . #<cps (ktail)>)
> (2 . #<cps (kargs (rval) (1) (continue 1 (values 1)))>)
> (3 . #<cps (kargs (arg) (4) (continue 2 (call 2 3 4)))>)
> (4 . #<cps (kargs (arg) (3) (continue 3 (const 2)))>)
> (5 . #<cps (kargs (arg) (2) (continue 4 (const 1)))>)
> (6 . #<cps (kargs (t) (6) (continue 5 (primcall resolve 5 6)))>)

The problem here is that 'resolve' returns a variable object, but 'call'
expects a procedure.  So, you need another continuation between
continuations 6 and 5 above, which does (primcall box-ref <>) to extract
the procedure from the variable object.

Also, I believe the (values 1) in continuation 2 above is incorrect.  It
expects its argument to be a list, whereas in this case it would be a
number.  It's not needed here.

> And if I fire up Guile and turn off optimization, I get this, where I don't
> even see the + resolved.
> scheme@(guile-user)> (define v (compile '(+ 1 2) #:from 'scheme #:to 'cps))
> scheme@(guile-user)> (disp-cps v)
> (0 . #<cps (kfun () 0 1 4)>)
> (1 . #<cps (ktail)>)
> (2 . #<cps (kargs (val) (1) (continue 1 (values 1)))>)
> (3 . #<cps (kargs () () (continue 2 (const 3)))>)
> (4 . #<cps (kclause (() () #f () #f) 3)>)

'+' is converted into a primitive operation in an early phase of
compilation.  For these purposes, it's probably better to choose a
procedure that won't be recognized specially by the compiler.

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> ,use (system base compile)
scheme@(guile-user)> (define intmap->alist (@@ (language cps intmap) intmap->alist))
scheme@(guile-user)> (define v (compile '(floor/ 3 7) #:from 'scheme #:to 'cps))
scheme@(guile-user)> ,pp (intmap->alist v)
$2 = ((0 . #<cps (kfun () 0 1 9)>)
      (1 . #<cps (ktail)>)
      (2 . #<cps (kargs (arg) (3) (continue 1 (call 1 2 3)))>)
      (3 . #<cps (kargs (arg) (2) (continue 2 (const 7)))>)
      (4 . #<cps (kargs (arg) (1) (continue 3 (const 3)))>)
      (5 . #<cps (kargs (box) (4) (continue 4 (primcall box-ref 4)))>)
      (6 . #<cps (kargs (bound?) (6) (continue 5 (primcall resolve 5 6)))>)
      (7 . #<cps (kargs (name) (5) (continue 6 (const #t)))>)
      (8 . #<cps (kargs () () (continue 7 (const floor/)))>)
      (9 . #<cps (kclause (() () #f () #f) 8)>))
scheme@(guile-user)>
--8<---------------cut here---------------end--------------->8---

Admittedly, I manually cleaned up the output of ,pp here.  Anyway, see
the 'box-ref' expression above, which was missing from yours, and also
note that in the CPS above, 'call' returns directly to the $ktail,
without an intervening 'values'.

Hope this helps,

       Mark



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: CPS mystery
  2018-05-29 17:54 ` Mark H Weaver
@ 2018-05-30  2:58   ` Matt Wette
  0 siblings, 0 replies; 3+ messages in thread
From: Matt Wette @ 2018-05-30  2:58 UTC (permalink / raw)
  To: guile-user

On 05/29/2018 10:54 AM, Mark H Weaver wrote:

> Hi Matt,

> The problem here is that 'resolve' returns a variable object, but 'call'
> expects a procedure.  So, you need another continuation between
> continuations 6 and 5 above, which does (primcall box-ref <>) to extract
> the procedure from the variable object.
>
> Also, I believe the (values 1) in continuation 2 above is incorrect.  It
> expects its argument to be a list, whereas in this case it would be a
> number.  It's not needed here.

Thanks.  I got it working!

calc@(guile-user)> 1 + 2
(program
   (expr-stmt (add (fixed "1") (fixed "2"))))
(0 . #<cps (kfun () 0 1 9)>)
(1 . #<cps (ktail)>)
(2 . #<cps (kargs (arg) (4) (continue 1 (call 2 3 4)))>
(3 . #<cps (kargs (arg) (3) (continue 2 (const 2)))>)
(4 . #<cps (kargs (arg) (2) (continue 3 (const 1)))>)
(5 . #<cps (kargs (bx) (5) (continue 4 (primcall box-ref 5)))>)
(6 . #<cps (kargs (t) (7) (continue 5 (primcall resolve 6 7)))>)
(7 . #<cps (kargs (name) (6) (continue 6 (const #t)))>)
(8 . #<cps (kargs () () (continue 7 (const +)))>)
(9 . #<cps (kclause (() () #f () #f) 8)>)
$1 = 3




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-05-30  2:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-05-28 13:49 CPS mystery Matt Wette
2018-05-29 17:54 ` Mark H Weaver
2018-05-30  2:58   ` Matt Wette

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).