| 1 | # Implements the unification algorithm for type inference described | ||
| 2 | # in the Dragon's book by Aho, Sethi and Ullman, chapter 6. Section 6.7. page 376 | ||
| 3 | package Parse::Eyapp::Unify; | ||
| 4 | use Data::Dumper; | ||
| 5 | use Parse::Eyapp::Node; | ||
| 6 | use base qw (Exporter); | ||
| 7 | our @EXPORT = qw(unify representative strunifiedtree hnewunifiedtree newunifiedtree); | ||
| 8 | |||
| 9 | my $count = 0; | ||
| 10 | my $set = 'representative'; | ||
| 11 | my $isvar = sub { }; | ||
| 12 | my $samebasic = sub { }; | ||
| 13 | |||
| 14 | # Not OOP | ||
| 15 | sub set { | ||
| 16 | my $class = shift if @_ %2; | ||
| 17 | $class = __PACKAGE__ unless defined($class); | ||
| 18 | |||
| 19 | my %handler = @_; | ||
| 20 | |||
| 21 | $set = 'representative'; | ||
| 22 | $set = $handler{key} if exists($handler{key}); | ||
| 23 | $isvar = $handler{isvar} if exists($handler{isvar}); | ||
| 24 | $samebasic = $handler{samebasic} if exists($handler{samebasic}); | ||
| 25 | $count = 0; | ||
| 26 | |||
| 27 | bless { key => $set, isvar => $isvar, samebasic => $samebasic, count => $count }, $class; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub mergevar { | ||
| 31 | my ($s, $t) = @_; | ||
| 32 | |||
| 33 | if ($isvar->($s)) { | ||
| 34 | $s->{$set} = representative($t); | ||
| 35 | # print "Merged ".representative($s)->str." and ".representative($t)->str."\n"; | ||
| 36 | return 1; | ||
| 37 | } | ||
| 38 | if ($isvar->($t)) { | ||
| 39 | $t->{$set} = representative($s); | ||
| 40 | # print "Merged ".representative($s)->str." and ".representative($t)->str."\n"; | ||
| 41 | return 1; | ||
| 42 | } | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub representative { | ||
| 47 | my $t = shift; | ||
| 48 | |||
| 49 | if (@_) { | ||
| 50 | $t->{$set} = shift; | ||
| 51 | return $t; | ||
| 52 | } | ||
| 53 | $t = $t->{$set} while defined($t->{set}) && ($t != $t->{$set}); | ||
| 54 | die "Representative ($set) not defined!".Dumper($t) unless defined($t->{set}); | ||
| 55 | return $t; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub unify { | ||
| 59 | my ($m, $n) = @_; | ||
| 60 | |||
| 61 | my $s = representative($m); | ||
| 62 | my $t = representative($n); | ||
| 63 | |||
| 64 | return 1 if ($s == $t); | ||
| 65 | |||
| 66 | return 1 if $samebasic->($s, $t); | ||
| 67 | |||
| 68 | # print "Unifying ".representative($s)->str." and ".representative($t)->str."\n"; | ||
| 69 | return 1 if (mergevar($s, $t)); | ||
| 70 | |||
| 71 | if (ref($s) eq ref($t)) { | ||
| 72 | $s->{$set} = representative($t); | ||
| 73 | my $i = 0; | ||
| 74 | for ($s->children) { | ||
| 75 | my $tc = $t->child($i++); | ||
| 76 | return 0 unless unify($_, $tc); | ||
| 77 | } | ||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub strunifiedtree { | ||
| 85 | local $Parse::Eyapp::Node::CLASS_HANDLER = sub { ref(representative($_[0])) }; | ||
| 86 | $_[0]->str; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub hnewunifiedtree { | ||
| 90 | local $Parse::Eyapp::Node::INDENT = 0; | ||
| 91 | local $Parse::Eyapp::Node::STRSEP = ","; | ||
| 92 | my $td = strunifiedtree($_[0]); | ||
| 93 | Parse::Eyapp::Node->hnew($td); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub newunifiedtree { | ||
| 97 | local $Parse::Eyapp::Node::INDENT = 0; | ||
| 98 | local $Parse::Eyapp::Node::STRSEP = ","; | ||
| 99 | my $td = strunifiedtree($_[0]); | ||
| 100 | Parse::Eyapp::Node->new($td); | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; |