diff --git a/core/kernel/kernel.factor b/core/kernel/kernel.factor index 2540ee39cd..68feb7a94a 100755 --- a/core/kernel/kernel.factor +++ b/core/kernel/kernel.factor @@ -165,12 +165,16 @@ GENERIC: boa ( ... class -- tuple ) compose compose ; inline ! Booleans -: not ( obj -- ? ) f eq? ; inline +: not ( obj -- ? ) + #! Not inline because its special-cased by compiler. + f eq? ; + +: and ( obj1 obj2 -- ? ) + #! Not inline because its special-cased by compiler. + over ? ; : >boolean ( obj -- ? ) t f ? ; inline -: and ( obj1 obj2 -- ? ) over ? ; inline - : or ( obj1 obj2 -- ? ) dupd ? ; inline : xor ( obj1 obj2 -- ? ) dup not swap ? ; inline diff --git a/core/math/intervals/intervals-tests.factor b/core/math/intervals/intervals-tests.factor index 7aa8ae0679..5234f03ecf 100755 --- a/core/math/intervals/intervals-tests.factor +++ b/core/math/intervals/intervals-tests.factor @@ -217,7 +217,7 @@ IN: math.intervals.tests ] if ; : random-interval ( -- interval ) - 1000 random dup 2 1000 random + + + 2000 random 1000 - dup 2 1000 random + + 1 random zero? [ [ neg ] bi@ swap ] when 4 random { { 0 [ [a,b] ] } @@ -274,7 +274,7 @@ IN: math.intervals.tests : binary-test ( -- ? ) random-interval random-interval random-binary-op ! 3dup . . . - 0 pick interval-contains? over first { / /i } member? and [ + 0 pick interval-contains? over first { / /i mod rem } member? and [ 3drop t ] [ [ >r [ random-element ] bi@ ! 2dup . . @@ -310,3 +310,25 @@ IN: math.intervals.tests [ t ] [ -10 10 [a,b] -100 0 [a,b] assume<= -10 0 [a,b] = ] unit-test [ t ] [ -10 10 [a,b] 0 100 [a,b] assume<= -10 10 [a,b] = ] unit-test + +[ t ] [ -10 10 [a,b] interval-abs 0 10 [a,b] = ] unit-test + +! Test that commutative interval ops really are +: random-interval-or-empty ( -- ) + 10 random 0 = [ empty-interval ] [ random-interval ] if ; + +: random-commutative-op ( -- op ) + { + interval+ interval* + interval-bitor interval-bitand interval-bitxor + interval-max interval-min + } random ; + +[ t ] [ + 80000 [ + drop + random-interval-or-empty random-interval-or-empty + random-commutative-op + [ execute ] [ swapd execute ] 3bi = + ] all? +] unit-test diff --git a/core/math/intervals/intervals.factor b/core/math/intervals/intervals.factor index 4aa86d772b..1896943a71 100755 --- a/core/math/intervals/intervals.factor +++ b/core/math/intervals/intervals.factor @@ -235,11 +235,15 @@ TUPLE: interval { from read-only } { to read-only } ; : interval/f ( i1 i2 -- i3 ) [ [ [ /f ] interval-op ] interval-division-op ] do-empty-interval ; +: (interval-abs) ( i1 -- i2 ) + interval>points [ first2 [ abs ] dip 2array ] bi@ 2array ; + : interval-abs ( i1 -- i2 ) - dup empty-interval eq? [ - interval>points [ first2 [ abs ] dip 2array ] bi@ 2array - points>interval - ] unless ; + { + { [ dup empty-interval eq? ] [ ] } + { [ 0 over interval-contains? ] [ (interval-abs) { 0 t } suffix points>interval ] } + [ (interval-abs) points>interval ] + } cond ; : interval-mod ( i1 i2 -- i3 ) #! Inaccurate. @@ -307,30 +311,45 @@ SYMBOL: incomparable : interval>= ( i1 i2 -- ? ) swap interval<= ; +: interval-bitand-pos ( i1 i2 -- ? ) + [ to>> first ] bi@ min 0 swap [a,b] ; + +: interval-bitand-neg ( i1 i2 -- ? ) + dup from>> first 0 < [ drop ] [ nip ] if + 0 swap to>> first [a,b] ; + +: interval-nonnegative? ( i -- ? ) + from>> first 0 >= ; + : interval-bitand ( i1 i2 -- i3 ) - dup 1 [a,a] interval>= [ - 1 [a,a] interval- interval-rem - ] [ - 2drop [-inf,inf] - ] if ; + #! Inaccurate. + [ + { + { + [ 2dup [ interval-nonnegative? ] both? ] + [ interval-bitand-pos ] + } + { + [ 2dup [ interval-nonnegative? ] either? ] + [ interval-bitand-neg ] + } + [ 2drop [-inf,inf] ] + } cond + ] do-empty-interval ; : interval-bitor ( i1 i2 -- i3 ) #! Inaccurate. [ - 2dup [ 0 [a,a] interval>= ] both? - [ to>> first 0 swap [a,b] interval-intersect ] - [ 2drop [-inf,inf] ] - if + 2dup [ interval-nonnegative? ] both? + [ + [ interval>points [ first ] bi@ ] bi@ + 4array supremum 0 swap next-power-of-2 [a,b] + ] [ 2drop [-inf,inf] ] if ] do-empty-interval ; : interval-bitxor ( i1 i2 -- i3 ) #! Inaccurate. - [ - 2dup [ 0 [a,a] interval>= ] both? - [ nip to>> first 0 swap [a,b] ] - [ 2drop [-inf,inf] ] - if - ] do-empty-interval ; + interval-bitor ; : assume< ( i1 i2 -- i3 ) dup empty-interval eq? [ drop ] [ diff --git a/extra/24-game/24-game.factor b/extra/24-game/24-game.factor new file mode 100644 index 0000000000..569cef8302 --- /dev/null +++ b/extra/24-game/24-game.factor @@ -0,0 +1,38 @@ +! Copyright © 2008 Reginald Keith Ford II +! 24, the Factor game! + +USING: kernel random namespaces shuffle sequences +parser io math prettyprint combinators continuations +vectors words quotations accessors math.parser +backtrack math.ranges locals fry memoize macros assocs ; + +IN: 24-game + +: nop ; +: do-something ( a b -- c ) { + - * } amb-execute ; +: maybe-swap ( a b -- a b ) { nop swap } amb-execute ; +: some-rots ( a b c -- a b c ) + #! Try each permutation of 3 elements. + { nop rot -rot swap spin swapd } amb-execute ; +: makes-24? ( a b c d -- ? ) [ some-rots do-something some-rots do-something maybe-swap do-something 24 = ] [ 4drop ] if-amb ; +: vector-24-able? ( vector -- t/f ) [ makes-24? ] with-datastack first ; +: q ( -- obj ) "quit" ; +: show-commands ( -- ) "Commands: " write "commands" get unparse print ; +: report ( vector -- ) unparse print show-commands ; +: give-help ( -- ) "Command not found..." print show-commands ; +: find-word ( string choices -- word ) [ name>> = ] with find nip ; +: obtain-word ( -- word ) readln "commands" get find-word dup [ drop give-help obtain-word ] unless ; +: done? ( vector -- t/f ) 1 swap length = ; +: victory? ( vector -- t/f ) V{ 24 } = ; +: apply-word ( vector word -- vector ) 1quotation with-datastack >vector ; +: update-commands ( vector -- ) length 3 < [ "commands" [ \ rot swap remove ] change ] [ ] if ; +DEFER: check-status +: quit-game ( vector -- ) drop "you're a quitter" print ; +: quit? ( vector -- t/f ) peek "quit" = ; +: end-game ( vector -- ) dup victory? [ drop "You WON!" ] [ pop number>string " is not 24... You lose." append ] if print ; +: repeat ( vector -- ) dup report obtain-word apply-word dup update-commands check-status ; +: check-status ( object -- ) dup done? [ end-game ] [ dup quit? [ quit-game ] [ repeat ] if ] if ; +: build-quad ( -- vector ) 4 [ 10 random ] replicate >vector ; +: 24-able ( -- vector ) build-quad dup vector-24-able? [ drop build-quad ] unless ; +: set-commands ( -- ) { + - * / rot swap q } "commands" set ; +: play-game ( -- ) set-commands 24-able repeat ; \ No newline at end of file diff --git a/extra/locals/backend/backend.factor b/extra/locals/backend/backend.factor index 10bed8b5df..fd24631171 100644 --- a/extra/locals/backend/backend.factor +++ b/extra/locals/backend/backend.factor @@ -1,3 +1,5 @@ +! Copyright (C) 2008 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. USING: math kernel slots.private inference.known-words inference.backend sequences effects words ; IN: locals.backend diff --git a/extra/math/derivatives/derivatives-docs.factor b/extra/math/derivatives/derivatives-docs.factor new file mode 100644 index 0000000000..23847e82f7 --- /dev/null +++ b/extra/math/derivatives/derivatives-docs.factor @@ -0,0 +1,9 @@ +USING: help.markup help.syntax ; + +IN: math.derivatives + +HELP: derivative ( x function -- m ) +{ $values { "x" "the x-position on the function" } { "function" "a differentiable function" } } +{ $description "Finds the slope of the tangent line at the given x-position on the given function." } ; + +{ derivative-func } related-words diff --git a/extra/math/derivatives/derivatives.factor b/extra/math/derivatives/derivatives.factor new file mode 100644 index 0000000000..fef93cabc4 --- /dev/null +++ b/extra/math/derivatives/derivatives.factor @@ -0,0 +1,9 @@ +! Copyright © 2008 Reginald Keith Ford II +! Tool for computing the derivative of a function at a point +USING: kernel math math.points math.function-tools ; +IN: math.derivatives + +: small-amount ( -- n ) 1.0e-12 ; +: near ( x -- y ) small-amount + ; +: derivative ( x function -- m ) 2dup [ near ] dip [ eval ] 2bi@ slope ; +: derivative-func ( function -- function ) [ derivative ] curry ; \ No newline at end of file diff --git a/extra/math/function-tools/function-tools.factor b/extra/math/function-tools/function-tools.factor new file mode 100644 index 0000000000..042b5f0897 --- /dev/null +++ b/extra/math/function-tools/function-tools.factor @@ -0,0 +1,8 @@ +! Copyright © 2008 Reginald Keith Ford II +! Tools for quickly comparing, transforming, and evaluating mathematical Factor functions + +USING: kernel math arrays ; +IN: math.function-tools +: difference-func ( func func -- func ) [ bi - ] 2curry ; +: eval ( x func -- pt ) dupd call 2array ; +: eval3d ( x y func -- pt ) [ 2dup ] dip call 3array ; diff --git a/extra/math/newtons-method/newtons-method.factor b/extra/math/newtons-method/newtons-method.factor new file mode 100644 index 0000000000..b2778a2d85 --- /dev/null +++ b/extra/math/newtons-method/newtons-method.factor @@ -0,0 +1,11 @@ +! Copyright © 2008 Reginald Keith Ford II +! Newton's Method of approximating roots + +USING: kernel math math.derivatives ; +IN: math.newtons-method + + +: newton-method ( guess function -- x ) newton-precision [ [ newton-step ] keep ] times drop ; diff --git a/extra/math/points/points.factor b/extra/math/points/points.factor index 5efd6e07e0..c8654869e2 100644 --- a/extra/math/points/points.factor +++ b/extra/math/points/points.factor @@ -1,5 +1,4 @@ - -USING: kernel arrays math.vectors ; +USING: kernel arrays math.vectors sequences math ; IN: math.points @@ -20,3 +19,9 @@ PRIVATE> : v+z ( seq z -- seq ) Z v+ ; : v-z ( seq z -- seq ) Z v- ; +: rise ( pt2 pt1 -- n ) [ second ] bi@ - ; +: run ( pt2 pt1 -- n ) [ first ] bi@ - ; +: slope ( pt pt -- slope ) [ rise ] [ run ] 2bi / ; +: distance ( point point -- float ) v- norm ; +: midpoint ( point point -- point ) v+ 2 v/n ; +: linear-solution ( pt pt -- x ) [ drop first2 ] [ slope ] 2bi / - ; \ No newline at end of file diff --git a/extra/math/secant-method/secant-method.factor b/extra/math/secant-method/secant-method.factor new file mode 100644 index 0000000000..2089dde848 --- /dev/null +++ b/extra/math/secant-method/secant-method.factor @@ -0,0 +1,14 @@ +! Copyright © 2008 Reginald Keith Ford II +! Secant Method of approximating roots + +USING: kernel math math.function-tools math.points math.vectors ; +IN: math.secant-method + + +: secant-method ( left right function -- x ) secant-precision [ secant-step ] times drop v+ 2 v*n ; +! : close-enough? ( a b -- t/f ) - abs tiny-amount < ; +! : secant-method2 ( left right function -- x ) 2over close-enough? [ drop average ] [ secant-step secant-method ] if ; \ No newline at end of file diff --git a/extra/project-euler/ave-time/ave-time.factor b/extra/project-euler/ave-time/ave-time.factor index c8212b4009..56f7185095 100644 --- a/extra/project-euler/ave-time/ave-time.factor +++ b/extra/project-euler/ave-time/ave-time.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2007 Aaron Schaefer ! See http://factorcode.org/license.txt for BSD license. USING: arrays combinators io kernel math math.functions math.parser - math.statistics namespaces sequences tools.time ; + math.statistics namespaces sequences tools.time continuations ; IN: project-euler.ave-time : collect-benchmarks ( quot n -- seq ) diff --git a/extra/shell/shell.factor b/extra/shell/shell.factor index 45c6f1fb4d..e694b36007 100644 --- a/extra/shell/shell.factor +++ b/extra/shell/shell.factor @@ -50,7 +50,6 @@ DEFER: expansion METHOD: expand { back-quoted-expr } expr>> expr - ast>> command>> expansion utf8 @@ -122,7 +121,7 @@ DEFER: shell { [ dup f = ] [ drop ] } { [ dup "exit" = ] [ drop ] } { [ dup "" = ] [ drop shell ] } - { [ dup expr ] [ expr ast>> chant shell ] } + { [ dup expr ] [ expr chant shell ] } { [ t ] [ drop "ix: ignoring input" print shell ] } } cond ; diff --git a/extra/ui/gadgets/labelled/labelled.factor b/extra/ui/gadgets/labelled/labelled.factor index 686e940ae6..831ac1b1d8 100755 --- a/extra/ui/gadgets/labelled/labelled.factor +++ b/extra/ui/gadgets/labelled/labelled.factor @@ -12,9 +12,9 @@ TUPLE: labelled-gadget < track content ; : ( gadget title -- newgadget ) { 0 1 } labelled-gadget new-track - swap