]> gitweb.factorcode.org Git - factor.git/commitdiff
separate stack effect typing from hints. put it in a "typed" vocab, and have a TYPED...
authorJoe Groff <arcata@gmail.com>
Wed, 2 Sep 2009 16:45:30 +0000 (11:45 -0500)
committerJoe Groff <arcata@gmail.com>
Wed, 2 Sep 2009 16:45:30 +0000 (11:45 -0500)
basis/hints/hints.factor
extra/typed/typed.factor [new file with mode: 0644]

index 07c80917f140fa312482a9a18093dfef99463c8b..73142cf7473d5deac09049b5f650278e87527846 100644 (file)
@@ -60,7 +60,7 @@ t specialize-method? set-global
         "method-generic" word-prop standard-generic?
     ] [ drop f ] if ;
 
-: (specialized-def) ( word -- quot )
+: specialized-def ( word -- quot )
     [ def>> ] keep
     dup generic? [ drop ] [
         [ dup standard-method? [ specialize-method ] [ drop ] if ]
@@ -68,32 +68,6 @@ t specialize-method? set-global
         bi
     ] if ;
 
-ERROR: type-mismatch-error word expected-types ;
-
-: typed-stack-effect? ( effect -- ? )
-    [ object = ] all? not ;
-
-: type-mismatch-quot ( word types -- quot )
-    [ type-mismatch-error ] 2curry ;
-
-: make-coercer ( types -- quot )
-    [ "coercer" word-prop [ ] or ]
-    [ swap \ dip [ ] 2sequence prepend ]
-    map-reduce ;
-
-: typed-inputs ( quot word -- quot' )
-    dup stack-effect effect-in-types {
-        [ 2nip make-coercer ]
-        [ 2nip make-specializer ]
-        [ nip swap '[ _ declare @ ] ]
-        [ [ drop ] 2dip type-mismatch-quot ]
-    } 3cleave '[ @ @ _ _ if ] ;
-
-: specialized-def ( word -- quot )
-    [ (specialized-def) ] keep
-    dup stack-effect effect-in-types typed-stack-effect?
-    [ typed-inputs ] [ drop ] if ;
-
 : specialized-length ( specializer -- n )
     dup [ array? ] all? [ first ] when length ;
 
diff --git a/extra/typed/typed.factor b/extra/typed/typed.factor
new file mode 100644 (file)
index 0000000..624d3e1
--- /dev/null
@@ -0,0 +1,56 @@
+USING: accessors combinators definitions effects fry hints
+kernel kernel.private parser sequences words ;
+IN: typed
+
+ERROR: type-mismatch-error word expected-types ;
+ERROR: input-mismatch-error < type-mismatch-error ;
+ERROR: output-mismatch-error < type-mismatch-error ;
+
+! typed inputs
+
+: typed-stack-effect? ( effect -- ? )
+    [ object = ] all? not ;
+
+: input-mismatch-quot ( word types -- quot )
+    [ input-mismatch-error ] 2curry ;
+
+: make-coercer ( types -- quot )
+    [ "coercer" word-prop [ ] or ]
+    [ swap \ dip [ ] 2sequence prepend ]
+    map-reduce ;
+
+: typed-inputs ( quot word types -- quot' )
+    {
+        [ 2nip make-coercer ]
+        [ 2nip make-specializer ]
+        [ nip swap '[ _ declare @ ] ]
+        [ [ drop ] 2dip input-mismatch-quot ]
+    } 3cleave '[ @ @ _ _ if ] ;
+
+! typed outputs
+
+: output-mismatch-quot ( word types -- quot )
+    [ output-mismatch-error ] 2curry ;
+
+: typed-outputs ( quot word types -- quot' )
+    2drop ;
+
+! defining typed words
+
+PREDICATE: typed < word "typed" word-prop ;
+
+: typed-def ( word def effect -- quot )
+    [ swap ] dip
+    [ effect-in-types dup typed-stack-effect? [ typed-inputs ] [ 2drop ] if ] 
+    [ effect-out-types dup typed-stack-effect? [ typed-outputs ] [ 2drop ] if ] 2bi ;
+
+: define-typed ( word def effect -- )
+    [ [ 2drop ] [ typed-def ] [ 2nip ] 3tri define-declared ]
+    [ drop "typed" set-word-prop ] 3bi ;
+
+SYNTAX: TYPED:
+    (:) define-typed ;
+
+M: typed definer drop \ TYPED: \ ; ;
+M: typed definition "typed" word-prop ;
+