2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! Copyright (C) 2009 Daniel Ehrenberg
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								! See http://factorcode.org/license.txt for BSD license.
							 | 
						
					
						
							
								
									
										
										
										
											2016-03-31 02:29:48 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								USING: accessors arrays assocs combinators.short-circuit fry
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								hash-sets kernel locals math regexp.classes
							 | 
						
					
						
							
								
									
										
										
										
											2016-03-31 02:29:48 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								regexp.transition-tables sequences sets sorting ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								IN: regexp.minimize
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 17:48:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: table>state-numbers ( table -- assoc )
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-16 13:53:36 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    transitions>> keys H{ } zip-index-as ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 17:48:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: number-states ( table -- newtable )
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-05 17:34:04 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    dup table>state-numbers transitions-at ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 17:48:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-08 23:34:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: has-conditions? ( assoc -- ? )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    values [ condition? ] any? ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-04 14:22:22 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 17:48:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: initially-same? ( s1 s2 transition-table -- ? )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ drop <= ]
							 | 
						
					
						
							
								
									
										
										
										
											2010-03-02 18:05:37 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        [ final-states>> '[ _ in? ] bi@ = ]
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        [ transitions>> '[ _ at keys ] bi@ set= ]
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 17:48:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    } 3&& ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								:: initialize-partitions ( transition-table -- partitions )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ! Partition table is sorted-array => ?
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    transition-table transitions>> keys natural-sort :> states
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    states length 2/ sq <hash-set> :> out
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    states [| s1 i1 |
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        states [| s2 |
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 17:48:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            s1 s2 transition-table initially-same?
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            [ s1 s2 2array out adjoin ] when
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        ] i1 each-from
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ] each-index out ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: same-partition? ( s1 s2 partitions -- ? )
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    { [ [ sort-pair 2array ] dip in? ] [ drop = ] } 3|| ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: stay-same? ( s1 s2 transition partitions -- ? )
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ '[ _ transitions>> at ] bi@ ] dip
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    '[ [ at ] dip _ same-partition? ] with assoc-all? ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								:: partition-more ( partitions transition-table -- partitions changed? )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    partitions cardinality :> size
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    partitions members [
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        dup first2 transition-table partitions stay-same?
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        [ drop ] [ partitions delete ] if
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ] each partitions dup cardinality size = not ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: partition>classes ( partitions -- synonyms ) ! old-state => new-state
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    members natural-sort <reversed> [ swap ] H{ } assoc-map-as ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-04 01:36:03 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-08 23:34:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: (state-classes) ( transition-table -- partition )
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 13:05:38 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ initialize-partitions ] keep '[ _ partition-more ] loop ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-08 23:34:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: assoc>set ( assoc -- keys-set )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ drop dup ] assoc-map ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: state-classes ( transition-table -- synonyms )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    clone [ [ nip has-conditions? ] assoc-partition ] change-transitions
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ assoc>set ] [ (state-classes) partition>classes ] bi* assoc-union ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-07 17:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: canonical-state? ( state transitions state-classes -- ? )
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-08 23:34:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    '[ dup _ at =  ] swap '[ _ at has-conditions? ] bi or ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: delete-duplicates ( transitions state-classes -- new-transitions )
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-07 17:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    dupd '[ drop _ _ canonical-state? ] assoc-filter ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: combine-states ( table -- smaller-table )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    dup state-classes
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-05 18:44:29 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ transitions-at ] keep
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    '[ _ delete-duplicates ] change-transitions ;
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-07 17:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								: combine-state-transitions ( hash -- hash )
							 | 
						
					
						
							
								
									
										
										
										
											2009-11-05 18:03:24 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    [ H{ } clone ] dip over '[
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-07 17:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        _ [ 2array <or-class> ] change-at
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ] assoc-each [ swap ] assoc-map ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: combine-transitions ( table -- table )
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    [ [ combine-state-transitions ] assoc-map ] change-transitions ;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-02-19 01:11:45 -05:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								: minimize ( table -- minimal-table )
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-07 17:31:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    clone
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    number-states
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-08 23:34:11 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    combine-states
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-11 23:04:47 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    combine-transitions
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    expand-ors ;
							 |