2008-07-20 05:24:37 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: arrays assocs math math.intervals kernel accessors
|
|
|
|
sequences namespaces disjoint-sets classes classes.algebra
|
2008-07-22 05:45:03 -04:00
|
|
|
combinators words compiler.tree compiler.tree.propagation.info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: compiler.tree.propagation.constraints
|
|
|
|
|
|
|
|
! A constraint is a statement about a value.
|
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Maps constraints to constraints ("A implies B")
|
2008-07-20 05:24:37 -04:00
|
|
|
SYMBOL: constraints
|
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
GENERIC: assume ( constraint -- )
|
|
|
|
GENERIC: satisfied? ( constraint -- ? )
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Boolean constraints
|
|
|
|
TUPLE: true-constraint value ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: <true-constraint> ( value -- constriant )
|
|
|
|
resolve-copy true-constraint boa ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: true-constraint assume
|
|
|
|
[ constraints get at [ assume ] when* ]
|
|
|
|
[ \ f class-not <class-info> swap value>> refine-value-info ]
|
|
|
|
bi ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: true-constraint satisfied?
|
|
|
|
value>> value-info class>> \ f class-not class<= ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
TUPLE: false-constraint value ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: <false-constraint> ( value -- constriant )
|
|
|
|
resolve-copy false-constraint boa ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: false-constraint assume
|
|
|
|
[ constraints get at [ assume ] when* ]
|
|
|
|
[ \ f <class-info> swap value>> refine-value-info ]
|
|
|
|
bi ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: false-constraint satisfied?
|
2008-07-23 01:17:08 -04:00
|
|
|
value>> value-info class>> \ f class<= ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Class constraints
|
|
|
|
TUPLE: class-constraint value class ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: <class-constraint> ( value class -- constraint )
|
|
|
|
[ resolve-copy ] dip class-constraint boa ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: class-constraint assume
|
|
|
|
[ class>> <class-info> ] [ value>> ] bi refine-value-info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Interval constraints
|
|
|
|
TUPLE: interval-constraint value interval ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: <interval-constraint> ( value interval -- constraint )
|
|
|
|
[ resolve-copy ] dip interval-constraint boa ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: interval-constraint assume
|
|
|
|
[ interval>> <interval-info> ] [ value>> ] bi refine-value-info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Literal constraints
|
|
|
|
TUPLE: literal-constraint value literal ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: <literal-constraint> ( value literal -- constraint )
|
|
|
|
[ resolve-copy ] dip literal-constraint boa ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: literal-constraint assume
|
|
|
|
[ literal>> <literal-info> ] [ value>> ] bi refine-value-info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Implication constraints
|
|
|
|
TUPLE: implication p q ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
C: <implication> implication
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: implication assume
|
|
|
|
[ q>> ] [ p>> ] bi
|
|
|
|
[ constraints get set-at ]
|
|
|
|
[ satisfied? [ assume ] [ drop ] if ] 2bi ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Conjunction constraints
|
|
|
|
TUPLE: conjunction p q ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
C: <conjunction> conjunction
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
M: conjunction assume [ p>> assume ] [ q>> assume ] bi ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! No-op
|
|
|
|
M: f assume drop ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
! Utilities
|
|
|
|
: if-true ( constraint boolean-value -- constraint' )
|
|
|
|
<true-constraint> swap <implication> ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: if-false ( constraint boolean-value -- constraint' )
|
|
|
|
<false-constraint> swap <implication> ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-22 05:45:03 -04:00
|
|
|
: <conditional> ( true-constr false-constr boolean-value -- constraint )
|
|
|
|
tuck [ if-true ] [ if-false ] 2bi* <conjunction> ;
|