]> gitweb.factorcode.org Git - factor.git/commitdiff
generic fixes; range and slice sequences
authorSlava Pestov <slava@factorcode.org>
Sun, 15 May 2005 01:15:50 +0000 (01:15 +0000)
committerSlava Pestov <slava@factorcode.org>
Sun, 15 May 2005 01:15:50 +0000 (01:15 +0000)
16 files changed:
CHANGES.txt
TODO.FACTOR.txt
library/bootstrap/boot-stage1.factor
library/bootstrap/image.factor
library/bootstrap/primitives.factor
library/collections/sequences-epilogue.factor
library/generic/generic.factor
library/generic/tuple.factor
library/httpd/browser-responder.factor
library/inference/stack.factor
library/inference/words.factor
library/syntax/generic.factor
library/syntax/see.factor
library/test/generic.factor
library/test/sequences.factor [new file with mode: 0644]
library/test/test.factor

index 72d8a08bb5b6024a8793b22d9a67c84055d1012d..90ffbe89503d2a60da4fa503f008ab64571d94f7 100644 (file)
@@ -33,6 +33,10 @@ Note that GENERIC: foo is the same as
 
  G: foo [ dup ] [ type ] ;
 
+Added two new types of 'virtual' sequences: a range sequence containing
+a range of integers, and a slice sequence containing a subsequence of
+another sequence.
+
 Factor 0.74:
 ------------
 
index a01eba61d5ccea0018c72d11f57c55df453a33b3..f404851bd1d0620ad2c1159a8c428faedbaa1a4e 100644 (file)
 \r
 + kernel:\r
 \r
+- delegating generic words with a non-standard picker\r
 - powerpc has weird callstack residue\r
 - instances: do not use make-list\r
 - unions containing tuples do not work properly\r
-- need G: combinations\r
 - method doc strings\r
 - clean up metaclasses\r
 - vectors: ensure its ok with bignum indices\r
index 253687495ba0e3590d34fb868fc444ea75ea4d55..cc74427ebc72b84bc400ef6bbb4e469eefdfd98e 100644 (file)
@@ -2,7 +2,7 @@
 ! See http://factor.sf.net/license.txt for BSD license.
 IN: image
 USING: lists parser namespaces stdio kernel vectors words
-hashtables ;
+hashtables sequences ;
 
 "Bootstrap stage 1..." print
 
index 31445a8ce41eb3b9abd649862a35cfa7704bb5dc..bbf43f4a0390ac7fe709d8c04cd6c034e58bdf73 100644 (file)
@@ -263,16 +263,17 @@ M: hashtable ' ( hashtable -- pointer )
 
 ( End of the image )
 
+: vocabulary, ( hash -- )
+    dup hashtable? [
+        [
+            cdr dup word? [ word, ] [ drop ] ifte
+        ] hash-each
+    ] [
+        drop
+    ] ifte ;
+
 : vocabularies, ( vocabularies -- )
-    [
-        cdr dup hashtable? [
-            [
-                cdr dup word? [ word, ] [ drop ] ifte
-            ] hash-each
-        ] [
-            drop
-        ] ifte
-    ] hash-each ;
+    [ cdr vocabulary, ] hash-each ;
 
 : global, ( -- )
     vocabularies get
index a809827b6d1542db6761bd4931d00180a1f22e55..df6df4c5fc894e0eb92cfdf9d5daf74828c857ff 100644 (file)
@@ -4,8 +4,8 @@ IN: image
 USING: kernel lists math memory namespaces parser words vectors
 hashtables generic alien assembler compiler errors files generic
 io-internals kernel kernel-internals lists math math-internals
-parser profiler strings unparser vectors words
-hashtables ;
+parser profiler strings unparser vectors words hashtables
+sequences ;
 
 ! This symbol needs the same hashcode in the target as in the
 ! host.
index 5aa737c8bbafb2a24f80e09f388d032ff5034302..b8620f84198938461dabc3fb57eef3f09bd47359 100644 (file)
@@ -172,6 +172,34 @@ TUPLE: repeated length object ;
 M: repeated length repeated-length ;
 M: repeated nth nip repeated-object ;
 
+! A range of integers
+TUPLE: range from to step ;
+
+C: range ( from to -- range )
+    >r 2dup > -1 1 ? r>
+    [ set-range-step ] keep
+    [ set-range-to ] keep
+    [ set-range-from ] keep ;
+
+M: range length ( range -- n )
+    dup range-to swap range-from - abs 1 + ;
+
+M: range nth ( n range -- n )
+    [ range-step * ] keep range-from + ;
+
+! A slice of another sequence.
+TUPLE: slice seq ;
+
+C: slice ( from to seq -- )
+    [ set-slice-seq ] keep
+    [ >r <range> r> set-delegate ] keep ;
+
+M: slice nth ( n slice -- obj )
+    [ delegate nth ] keep slice-seq nth ;
+
+M: slice set-nth ( obj n slice -- )
+    [ delegate nth ] keep slice-seq set-nth ;
+
 IN: kernel
 
 : depth ( -- n )
index 58e587a647ff7220943e3001bc2a90ff5a2f055f..b850d7f88e2bca8c2532d7bcc9869a73e3ff148a 100644 (file)
@@ -57,9 +57,20 @@ math-internals ;
         [ "Metaclass is missing add-method" throw ]
     ] unless* call ;
 
+: picker% "picker" word-prop % ;
+
+: dispatcher% "dispatcher" word-prop % ;
+
+: empty-method ( generic -- method )
+    [
+        [ dup delegate ] %
+        [ dup , ] make-list ,
+        [ literal, \ no-method , ] make-list ,
+        \ ?ifte ,
+    ] make-list ;
+
 : <empty-vtable> ( generic -- vtable )
-    [ literal, \ no-method , ] make-list
-    num-types swap <repeated> >vector ;
+    empty-method num-types swap <repeated> >vector ;
 
 : <vtable> ( generic -- vtable )
     dup <empty-vtable> over methods [
@@ -67,15 +78,38 @@ math-internals ;
         >r 2dup r> unswons add-method
     ] each nip ;
 
-: make-generic ( word -- )
-    #! (define-compound) is used to avoid resetting generic
-    #! word properties.
+: (small-generic) ( word methods -- quot )
+    [
+        2dup cdr (small-generic) [
+            >r >r picker%
+            r> car unswons "predicate" word-prop %
+            , r> , \ ifte ,
+        ] make-list
+    ] [
+        empty-method
+    ] ifte* ;
+
+: small-generic ( word -- def )
+    dup methods reverse (small-generic) ;
+
+: big-generic ( word -- def )
     [
-        dup "picker" word-prop %
-        dup "dispatcher" word-prop %
-        dup <vtable> ,
+        dup picker%
+        dup dispatcher%
+        <vtable> ,
         \ dispatch ,
-    ] make-list (define-compound) ;
+    ] make-list ;
+
+: small-generic? ( word -- ? )
+    dup "methods" word-prop hash-size 3 <=
+    swap "dispatcher" word-prop [ type ] = and ;
+
+: make-generic ( word -- )
+    dup dup small-generic? [
+        small-generic
+    ] [
+        big-generic
+    ] ifte  (define-compound) ;
 
 : define-method ( class generic definition -- )
     -rot
@@ -101,17 +135,9 @@ math-internals ;
     >r [ dup ] [ type ] r> define-generic* ;
 
 PREDICATE: compound generic ( word -- ? )
-    dup "dispatcher" word-prop [ type ] =
-    swap "picker" word-prop [ dup ] = and ;
-M: generic definer drop \ GENERIC: ;
-
-: define-2generic ( word -- )
-    >r [ ] [ arithmetic-type ] r> define-generic* ;
+    "dispatcher" word-prop ;
 
-PREDICATE: compound 2generic ( word -- ? )
-    dup "dispatcher" word-prop [ arithmetic-type ] =
-    swap "picker" word-prop not and ;
-M: 2generic definer drop \ 2GENERIC: ;
+M: generic definer drop \ G: ;
 
 ! Maps lists of builtin type numbers to class objects.
 SYMBOL: typemap
index 7b46257806328b2aecffaa3dd0c6910cb086810f..6714434e3d767d790c2b1ee854a419e32aa91c45 100644 (file)
@@ -32,7 +32,7 @@ M: tuple-seq length ( tuple-seq -- len )
 IN: generic
 
 DEFER: tuple?
-BUILTIN: tuple 18 tuple? [ 1 length f ] ;
+BUILTIN: tuple 18 tuple? ;
 
 ! So far, only tuples can have delegates, which also must be
 ! tuples (the UI uses numbers as delegates in a couple of places
@@ -69,8 +69,13 @@ UNION: arrayed array tuple ;
 : tuple-predicate ( word -- )
     #! Make a foo? word for testing the tuple class at the top
     #! of the stack.
-    dup predicate-word swap [
-        literal, [ swap class eq? ] %
+    dup predicate-word
+    2dup unit "predicate" set-word-prop
+    swap [
+        [ dup tuple? ] %
+        [ \ class-tuple , literal, \ eq? , ] make-list ,
+        [ drop f ] ,
+        \ ifte ,
     ] make-list define-compound ;
 
 : check-shape ( word slots -- )
@@ -148,10 +153,7 @@ UNION: arrayed array tuple ;
         drop object over hash* dup [
             2nip cdr
         ] [
-            2drop [ dup delegate ] swap
-            dup unit swap
-            unit [ car ] cons [ no-method ] append
-            \ ?ifte 3list append
+            2drop empty-method
         ] ifte
     ] ifte ;
 
index 55092be988cb2b401622f46007f0ee92c1a61443..8631cbbf56bc66186ec55404cfa604d56d411c33 100644 (file)
@@ -131,15 +131,7 @@ sequences ;
 : vocabulary-uses ( vocab -- list )
   #! Return a list of vocabularies that all words in a vocabulary
   #! uses.
-  <namespace> [
-    "result" off
-    words [
-      word-uses [
-        "result" [ unique ] change
-      ] each
-    ] each 
-    "result" get
-  ] bind ;
+  words [ word-uses ] map prune ;
 
 : build-eval-string ( vocab to-eval -- string )
   #! Build a string that can evaluate the string 'to-eval'
index 8088999558ef6aac5f25d273040c540f542c9852..0c177c5f17ca342337eea7fc911e5922f7959d0a 100644 (file)
@@ -1,36 +1,7 @@
-! :folding=indent:collapseFolds=1:
-
-! $Id$
-!
-! Copyright (C) 2004 Slava Pestov.
-! 
-! Redistribution and use in source and binary forms, with or without
-! modification, are permitted provided that the following conditions are met:
-! 
-! 1. Redistributions of source code must retain the above copyright notice,
-!    this list of conditions and the following disclaimer.
-! 
-! 2. Redistributions in binary form must reproduce the above copyright notice,
-!    this list of conditions and the following disclaimer in the documentation
-!    and/or other materials provided with the distribution.
-! 
-! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
-! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
+! Copyright (C) 2004, 2005 Slava Pestov.
+! See http://factor.sf.net/license.txt for BSD license.
 IN: inference
-USE: interpreter
-USE: kernel
-USE: lists
-USE: namespaces
-USE: words
+USING: interpreter kernel namespaces words ;
 
 \ >r [
     f \ >r dataflow, [ 1 0 node-inputs ] extend
@@ -44,14 +15,13 @@ USE: words
     [ 1 0 node-outputs ] bind
 ] "infer" set-word-prop
 
-: partial-eval ( word -- )
-    #! Partially evaluate a word.
+: infer-shuffle ( word -- )
     f over dup
     "infer-effect" word-prop
     [ host-word ] with-dataflow ;
 
-\ drop [ \ drop partial-eval ] "infer" set-word-prop
-\ dup  [ \ dup  partial-eval ] "infer" set-word-prop
-\ swap [ \ swap partial-eval ] "infer" set-word-prop
-\ over [ \ over partial-eval ] "infer" set-word-prop
-\ pick [ \ pick partial-eval ] "infer" set-word-prop
+\ drop [ \ drop infer-shuffle ] "infer" set-word-prop
+\ dup  [ \ dup  infer-shuffle ] "infer" set-word-prop
+\ swap [ \ swap infer-shuffle ] "infer" set-word-prop
+\ over [ \ over infer-shuffle ] "infer" set-word-prop
+\ pick [ \ pick infer-shuffle ] "infer" set-word-prop
index b1cfa1d80028ada9c23bf9340aa94ad7bb92a5e6..831151c887af308132bd046780367648f7f9f1bb 100644 (file)
@@ -170,8 +170,12 @@ M: word apply-object ( word -- )
 \ set-no-method-generic [ [ object tuple ] [ ] ] "infer-effect" set-word-prop
 \ set-no-method-object [ [ object tuple ] [ ] ] "infer-effect" set-word-prop
 \ car [ [ general-list ] [ object ] ] "infer-effect" set-word-prop
+\ real [ [ number ] [ real ] ] "infer-effect" set-word-prop
+\ imaginary [ [ number ] [ real ] ] "infer-effect" set-word-prop
+\ delegate [ [ object ] [ object ] ] "infer-effect" set-word-prop
 
 \ no-method t "terminator" set-word-prop
 \ no-method [ [ object word ] [ ] ] "infer-effect" set-word-prop
+\ <no-method> [ [ object word ] [ tuple ] ] "infer-effect" set-word-prop
 \ not-a-number t "terminator" set-word-prop
 \ throw t "terminator" set-word-prop
index da1c710d0eb9dc4409064dc4c8b1a1d9c1839172..4a85175cb0003a3bf515a17dcee8f259c69e69f5 100644 (file)
@@ -9,12 +9,6 @@ USING: syntax generic kernel lists namespaces parser words ;
     #! GENERIC: bar == G: bar [ dup ] [ type ] ;
     CREATE define-generic ; parsing
 
-: 2GENERIC:
-    #! 2GENERIC: bar == G: bar [ ] [ arithmetic-type ] ;
-    #! 2GENERIC words dispatch on arithmetic types and should
-    #! not be used for non-numerical types.
-    CREATE define-2generic ; parsing
-
 : G:
     #! G: word picker dispatcher ;
     CREATE [ 2unlist rot define-generic* ] [ ] ; parsing
index 4035f67d537e718973cd6c6e6c7442f16d3dc606..bfb006732ac7618d7f9109d995f3a0c326dbd36b 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2003, 2005 Slava Pestov.
 ! See http://factor.sf.net/license.txt for BSD license.
 IN: prettyprint
-USING: generic hashtables kernel lists math namespaces
-sequences stdio streams strings unparser words ;
+USING: #<unknown> generic hashtables kernel lists math
+namespaces sequences stdio streams strings unparser words ;
 
 ! Prettyprinting words
 : vocab-actions ( search -- list )
@@ -93,11 +93,15 @@ M: compound (see) ( word -- )
     dup prettyprint-newline r> prettyprint-elements
     prettyprint-; drop ;
 
-: generic. ( word -- ) dup methods [ method. ] each-with ;
-
-M: generic (see) ( word -- ) generic. ;
-
-M: 2generic (see) ( word -- ) generic. ;
+M: generic (see) ( word -- )
+    tab-size get dup indent [
+        one-line on
+        over "picker" word-prop prettyprint* bl
+        over "dispatcher" word-prop prettyprint* bl
+    ] with-scope
+    drop
+    \ ; word. terpri
+    dup methods [ method. ] each-with ;
 
 M: word (see) drop ;
 
index 1518dc48bfc694c06d50a9f6e8060f8054c4cd82..4fcdde8f5c4cd86be51858ab1037bc9e96480244 100644 (file)
@@ -169,3 +169,23 @@ TUPLE: shit ;
 
 M: shit complex-combination cons ;
 [ [[ << shit f >> 5 ]] ] [ << shit f >> 5 complex-combination ] unit-test
+
+[ t ] [ \ complex-combination generic? >boolean ] unit-test
+
+! TUPLE: delegating-small-generic ;
+! G: small-delegation [ over ] [ type ] ;
+! M: shit small-delegation cons ;
+! 
+! [ [[ << shit f >> 5 ]] ] [ << delegating-small-generic << shit f >> >> 5 small-delegation ] unit-test
+
+GENERIC: big-generic-test
+M: fixnum big-generic-test "fixnum" ;
+M: bignum big-generic-test "bignum" ;
+M: ratio big-generic-test "ratio" ;
+M: string big-generic-test "string" ;
+M: shit big-generic-test "shit" ;
+
+TUPLE: delegating ;
+
+[ << shit f >> "shit" ] [ << shit f >> big-generic-test ] unit-test
+[ << shit f >> "shit" ] [ << delegating << shit f >> >> big-generic-test ] unit-test
diff --git a/library/test/sequences.factor b/library/test/sequences.factor
new file mode 100644 (file)
index 0000000..bbe93f4
--- /dev/null
@@ -0,0 +1,8 @@
+IN: temporary
+USING: lists test sequences ;
+
+[ [ 1 2 3 4 ] ] [ 1 4 <range> >list ] unit-test
+[ 4 ] [ 1 4 <range> length ] unit-test
+[ [ 4 3 2 1 ] ] [ 4 1 <range> >list ] unit-test
+[ 2 ] [ 1 2 { 1 2 3 4 } <slice> length ] unit-test
+[ [ 2 3 ] ] [ 1 2 { 1 2 3 4 } <slice> >list ] unit-test
index 0358a3b79711f3133b00e8b692e5b82b19df0d05..63533dce9cec6be9d76fba3fcadaad9b518a6d78 100644 (file)
@@ -80,7 +80,7 @@ SYMBOL: failures
             "crashes" "sbuf" "threads" "parsing-word"
             "inference" "dataflow" "interpreter" "alien"
             "line-editor" "gadgets" "memory" "redefine"
-            "annotate"
+            "annotate" "sequences"
         ] %
         
         os "win32" = [