| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | ! Copyright (C) 2005, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 21:07:00 -04:00
										 |  |  | USING: arrays generic hashtables kernel kernel.private math | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | namespaces sequences words quotations layouts combinators | 
					
						
							| 
									
										
										
										
											2008-04-03 22:19:20 -04:00
										 |  |  | sequences.private classes classes.builtin classes.algebra | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | definitions math.order math.private assocs ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: generic.math | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: math-class < class | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     dup null bootstrap-word eq? [ | 
					
						
							|  |  |  |         drop f
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-05-02 03:51:38 -04:00
										 |  |  |         number bootstrap-word class<= | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : bootstrap-words ( classes -- classes' )
 | 
					
						
							|  |  |  |     [ bootstrap-word ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 02:27:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | : math-precedence ( class -- pair )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-04-30 01:38:55 -04:00
										 |  |  |         { fixnum integer rational real number object } bootstrap-words | 
					
						
							|  |  |  |         swap [ swap class<= ] curry find drop -1 or
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2009-04-30 01:38:55 -04:00
										 |  |  |         { fixnum bignum ratio float complex object } bootstrap-words | 
					
						
							|  |  |  |         swap [ class<= ] curry find drop -1 or
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |     ] bi 2array ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 02:27:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : (math-upgrade) ( max class -- quot )
 | 
					
						
							| 
									
										
										
										
											2007-09-27 04:50:24 -04:00
										 |  |  |     dupd = [ drop [ ] ] [ "coercer" word-prop [ ] or ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : math-class-max ( class1 class2 -- class )
 | 
					
						
							|  |  |  |     [ [ math-precedence ] bi@ after? ] most ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : math-upgrade ( class1 class2 -- quot )
 | 
					
						
							|  |  |  |     [ math-class-max ] 2keep
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-11-23 03:44:56 -05:00
										 |  |  |         (math-upgrade) | 
					
						
							|  |  |  |         dup empty? [ [ dip ] curry [ ] like ] unless
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |     ] [ (math-upgrade) ] | 
					
						
							|  |  |  |     bi-curry* bi append ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-20 16:00:49 -04:00
										 |  |  | ERROR: no-math-method left right generic ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | : default-math-method ( generic -- quot )
 | 
					
						
							|  |  |  |     [ no-math-method ] curry [ ] like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-21 18:42:20 -04:00
										 |  |  | : (math-method) ( generic class -- quot )
 | 
					
						
							| 
									
										
										
										
											2008-02-03 15:19:07 -05:00
										 |  |  |     over method | 
					
						
							| 
									
										
										
										
											2008-03-16 03:43:00 -04:00
										 |  |  |     [ 1quotation ] | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  |     [ default-math-method ] ?if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : object-method ( generic -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-09-21 18:42:20 -04:00
										 |  |  |     object bootstrap-word (math-method) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : math-method ( word class1 class2 -- quot )
 | 
					
						
							|  |  |  |     2dup and [ | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |         [ 2array [ declare ] curry nip ] | 
					
						
							|  |  |  |         [ math-upgrade nip ] | 
					
						
							| 
									
										
										
										
											2009-09-21 18:42:20 -04:00
										 |  |  |         [ math-class-max over nearest-class (math-method) ] | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |         3tri 3append
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         2drop object-method | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2008-11-13 04:51:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  | SYMBOL: generic-word | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-09 02:38:10 -05:00
										 |  |  | : make-math-method-table ( classes quot: ( ... class -- ... quot ) -- alist )
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |     [ bootstrap-words ] dip
 | 
					
						
							|  |  |  |     [ [ drop ] [ call ] 2bi ] curry { } map>assoc ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : math-alist>quot ( alist -- quot )
 | 
					
						
							|  |  |  |     [ generic-word get object-method ] dip alist>quot ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tag-dispatch-entry ( tag picker -- quot )
 | 
					
						
							|  |  |  |     [ "type" word-prop 1quotation [ tag ] [ eq? ] surround ] dip prepend ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tag-dispatch ( picker alist -- alist' )
 | 
					
						
							|  |  |  |     swap [ [ tag-dispatch-entry ] curry dip ] curry assoc-map math-alist>quot ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tuple-dispatch-entry ( class picker -- quot )
 | 
					
						
							|  |  |  |     [ 1quotation [ { tuple } declare class ] [ eq? ] surround ] dip prepend ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tuple-dispatch ( picker alist -- alist' )
 | 
					
						
							|  |  |  |     swap [ [ tuple-dispatch-entry ] curry dip ] curry assoc-map math-alist>quot ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-09 02:38:10 -05:00
										 |  |  | : math-dispatch-step ( picker quot: ( ... class -- ... quot ) -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |     [ [ { bignum float fixnum } ] dip make-math-method-table ] | 
					
						
							|  |  |  |     [ [ { ratio complex } ] dip make-math-method-table tuple-dispatch ] 2bi
 | 
					
						
							|  |  |  |     tuple swap 2array prefix tag-dispatch ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 19:00:26 -04:00
										 |  |  | SINGLETON: math-combination | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 17:20:07 -05:00
										 |  |  | M: math-combination make-default-method | 
					
						
							|  |  |  |     drop default-math-method ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | M: math-combination perform-combination | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:35 -04:00
										 |  |  |     drop dup generic-word [ | 
					
						
							|  |  |  |         dup
 | 
					
						
							|  |  |  |         [ fixnum bootstrap-word dup math-method ] | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ over ] [ | 
					
						
							|  |  |  |                 dup math-class? [ | 
					
						
							|  |  |  |                     [ dup ] [ math-method ] with with math-dispatch-step | 
					
						
							|  |  |  |                 ] [ | 
					
						
							|  |  |  |                     drop object-method | 
					
						
							|  |  |  |                 ] if
 | 
					
						
							|  |  |  |             ] with math-dispatch-step | 
					
						
							|  |  |  |         ] bi
 | 
					
						
							|  |  |  |         [ if ] 2curry [ 2dup both-fixnums? ] prepend
 | 
					
						
							|  |  |  |         define | 
					
						
							|  |  |  |     ] with-variable ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-26 19:23:19 -04:00
										 |  |  | PREDICATE: math-generic < generic ( word -- ? )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     "combination" word-prop math-combination? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: math-generic definer drop \ MATH: f ;
 |