Hi Alexander -
Regarding "Cannot apply “unsafe-amb” to a run-time computed value",
see `Optimizing compiler` in the source code browser,
& especially `bad-macro-input` under `Stack checker errors`:

"Most code you write will run with the optimizing compiler. Sometimes,
the non-optimizing compiler is used, for example for listener interactions,
or for running the quotation passed to call(."

I was bitten by the same compiler issue in my `spill-seq` word (see code
below).
My solution worked fine interactively in the Listener, but flunked out in a
source code compile. It took quite a bit of experimentation to compose an
idiom,
incorporating a MACRO: definiton & quotation calls, that would compile in
my source.
This seems to stem from the use of the optimizing compiler with source
files,
whereas the non-optimizing compiler used in the Listener is more permissive.

The solution to the puzzle that I wrote doesn't use `amb`. I approached the
problem
with `math.combinatorics` & `math.ranges` instead, so I'm not much help
there.
But I had to wrestle with the same conundrum, regarding compilation in the
Listener
versus source file compilation. See if the example helps. I posted the
(brief) source
file below.

I'd be very interested in an approach using `backtracking`!
Cheers,
cw

------------------ alternative solution follows ----------------

! Copyright (C) 2017 CW Alston.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit fry kernel kernel.private
locals macros math math.combinatorics math.functions math.ranges
multiline prettyprint sequences ;
IN: andrew-task*

! Problem suggested by Alexander Ilin:
! The task is to find digits A, B, C, D, E, and F, such that:
! all digits are 1..9,
! all of the digits are different,
! D < E < F,
! ABC*D*EE*FFF = n^2, where n is an integer.

! number of possible sequences of digits 1-9, taken 6 at a time:
! 9 6 nPk . => 60480

! put sequence elements on the stack
MACRO: spill-seq ( seq  -- quot )
    '[ _ [ length iota ] keep [ nth ] curry each ] ;

! check each permutation for solution to task specs
:: (check-6) ( perm-seq -- ? )
   [ perm-seq spill-seq
    :> F :> E :> D :> C :> B :> A
    {
        [ D E < E F < and ]
        [
          A 100 * B 10 * C + + :> ABC
          11 E * :> EE
          111 F * :> FFF
          { ABC D EE FFF } product :> prod
          prod sqrt >integer dup * prod =
        ]
    } 0&& ] ; inline

: 9P6 ( -- permutations )
    1 9 [a,b] { } clone-like      ! ( -- { 1 2 3 4 5 6 7 8 9 } )
    6 <k-permutations> ; inline   ! ( -- permutations )

: solve-andrew ( -- seq )
    9P6 [ (check-6) call( -- ? ) ] find nip ;

: solve-andrew. ( -- ) solve-andrew . ;

MAIN: solve-andrew.

/*
USE: andrew-task*
IN: scratchpad solve-andrew.
{ 8 1 4 2 3 9 }  ! solution

! checking:
IN: scratchpad 8 100 * 1 10 * 4 + + "abc" set
IN: scratchpad 11 3 * "ee" set
IN: scratchpad 111 9 * "fff" set
IN: scratchpad "abc" get 2 "ee" get "fff" get 4 narray

--- Data stack:
{ 814 2 33 999 }
IN: scratchpad product "prod" set
IN: scratchpad "prod" get sqrt >integer

--- Data stack:
7326
IN: scratchpad dup *

--- Data stack:
53670276
IN: scratchpad "prod" get = .
t
*/
! ---------------------

On Fri, Oct 6, 2017 at 10:30 AM, Alexander Ilin <ajs...@yandex.ru> wrote:

> Hello!
>
>   I found a perfect task to learn the `backtrack` vocab, and have
> successfully solved the problem (although the help system could use some
> example code). The only question I have left is why can't I compile the
> vocab below. I get the error that says "Cannot apply “unsafe-amb” to a
> run-time computed value" in the `solve` word. It works perfectly well in
> the listener, so why can't it work in a vocab?
>
>   Could someone point out the reason for the error? Which part of the
> quotation is run-time computed? I have inlined everything I could, but it
> didn't help.
>
> ```
> ! Copyright (C) 2017 Alexander Ilin.
> ! See http://factorcode.org/license.txt for BSD license.
> USING:
>     arrays backtrack combinators.short-circuit infix kernel
>     locals math math.functions prettyprint scratchpad
>     sequences sequences.private sets
> ;
>
> IN: andrew-task
>
> ! The task is to find digits A, B, C, D, E, and F, such that:
> ! all digits are 1..9,
> ! all of the digits are different,
> ! D < E < F,
> ! ABC*D*EE*FFF = n^2, where n is an integer.
>
> ALIAS: 0? zero?
>
> : last3 ( seq -- X Y Z )
>     unclip-last [ unclip-last [ last ] dip ] dip ; inline
>
> : 6array ( A B C D E F -- {ABCDEF} )
>     4array swap prefix swap prefix ; inline
>
> : check-success ( {ABCDEF} -- )
>     {
>         [ [ 0? ] count 0? ]
>         [ duplicates empty? ]
>         [ last3 [| D E F | D E < E F < and ] call ]
>         [
>             [ first3 ] [ last3 ] bi
>             [| A B C D E F |
>                 [infix A * 100 + B * 10 + C infix] :> ABC
>                 11  E * :> EE
>                 111 F * :> FFF
>                 { ABC D EE FFF } product :> prod
>                 prod sqrt >integer dup * prod =
>             ] call
>         ]
>     } 1&& must-be-true ; inline
>
> : solve ( -- )
>     [
>         10 <iota> amb 10 <iota> amb 10 <iota> amb
>         10 <iota> amb 10 <iota> amb 10 <iota> amb
>         6array dup check-success .
>     ] amb-all ;
>
> MAIN: solve
> ```
>
> ---=====---
>  Александр
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>



-- 
*~ Memento Amori*
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to