backtrack: some cleanup.
parent
e2bead3328
commit
6480a46c1e
|
@ -1,8 +1,8 @@
|
||||||
! Copyright (C) 2008 William Schlieper
|
! Copyright (C) 2008 William Schlieper
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
|
||||||
USING: kernel continuations combinators sequences quotations arrays namespaces
|
USING: assocs combinators continuations fry kernel macros math
|
||||||
fry summary assocs math math.order macros ;
|
namespaces quotations sequences summary ;
|
||||||
|
|
||||||
IN: backtrack
|
IN: backtrack
|
||||||
|
|
||||||
|
@ -13,16 +13,18 @@ ERROR: amb-failure ;
|
||||||
M: amb-failure summary drop "Backtracking failure" ;
|
M: amb-failure summary drop "Backtracking failure" ;
|
||||||
|
|
||||||
: fail ( -- )
|
: fail ( -- )
|
||||||
failure get [ continue ]
|
failure get [ continue ] [ amb-failure ] if* ;
|
||||||
[ amb-failure ] if* ;
|
|
||||||
|
|
||||||
: require ( ? -- )
|
: require ( ? -- )
|
||||||
[ fail ] unless ;
|
[ fail ] unless ;
|
||||||
|
|
||||||
MACRO: checkpoint ( quot -- quot' )
|
MACRO: checkpoint ( quot -- quot' )
|
||||||
'[ failure get _
|
'[
|
||||||
'[ '[ failure set _ continue ] callcc0
|
failure get _ '[
|
||||||
_ failure set @ ] callcc0 ] ;
|
'[ failure set _ continue ] callcc0
|
||||||
|
_ failure set @
|
||||||
|
] callcc0
|
||||||
|
] ;
|
||||||
|
|
||||||
: number-from ( from -- from+n )
|
: number-from ( from -- from+n )
|
||||||
[ 1 + number-from ] checkpoint ;
|
[ 1 + number-from ] checkpoint ;
|
||||||
|
@ -43,10 +45,11 @@ MACRO: checkpoint ( quot -- quot' )
|
||||||
length 1 - 0 number-from-to nip ;
|
length 1 - 0 number-from-to nip ;
|
||||||
|
|
||||||
MACRO: unsafe-amb ( seq -- quot )
|
MACRO: unsafe-amb ( seq -- quot )
|
||||||
dup length 1 =
|
dup length 1 = [
|
||||||
[ first 1quotation ]
|
first 1quotation
|
||||||
[ [ first ] [ rest ] bi
|
] [
|
||||||
'[ _ [ drop _ unsafe-amb ] checkpoint ] ] if ;
|
unclip swap '[ _ [ drop _ unsafe-amb ] checkpoint ]
|
||||||
|
] if ;
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -54,8 +57,7 @@ PRIVATE>
|
||||||
[ amb-integer ] [ nth ] bi ;
|
[ amb-integer ] [ nth ] bi ;
|
||||||
|
|
||||||
: amb ( seq -- elt )
|
: amb ( seq -- elt )
|
||||||
[ fail f ]
|
[ fail f ] [ unsafe-amb ] if-empty ; inline
|
||||||
[ unsafe-amb ] if-empty ; inline
|
|
||||||
|
|
||||||
MACRO: amb-execute ( seq -- quot )
|
MACRO: amb-execute ( seq -- quot )
|
||||||
[ length 1 - ] [ <enum> [ 1quotation ] assoc-map ] bi
|
[ length 1 - ] [ <enum> [ 1quotation ] assoc-map ] bi
|
||||||
|
|
Loading…
Reference in New Issue