MinCaml/TypeInferrer.pm

filter: Statementstmt 4.8% 9 / 185 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 111 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 27.2% 3 / 11 Podpod n/a totaltotal 2.5% 12 / 467 CC 105 SCAR 66.2
CC
105
Cov
2%
unify
1122.0
infer_rec
992.0
deref_node
650.0
CRAP 752.0
« MinCaml/Evaluator.pm ↑ Summary MinCaml/Lexer.pm »
1 package Language::MinCaml::TypeInferrer;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Language::MinCaml::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
7 0 my $class = shift;
8 0 return bless {}, $class;
9 }
10  
11 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
12 0 croak "typing error!";
13 }
14  
15 0 ▶sub deref_type {
Subroutine deref_type: not called CC=8 SCAR=42.8
16 0 my($self, $type) = @_;
17 0 my $kind = $type->kind;
18  
19 0 ▶ if ($kind eq 'Fun') {
Branch: 4 branches
BranchTrueFalseTotal
if ($kind eq 'Fun') { } 0 0 0
elsif ($kind eq 'Tuple') { } 0 0 0
elsif ($kind eq 'Array') { } 0 0 0
elsif ($kind eq 'Var') { } 0 0 0
20 0 my @new_args = ();
21 0 for my $arg (@{$type->children->[0]}) {
22 0 push(@new_args, $self->deref_type($arg));
23 }
24 0 $type->children->[0] = \@new_args;
25 0 $type->children->[1] = $self->deref_type($type->children->[1]);
26 }
27 elsif ($kind eq 'Tuple') {
28 0 my @new_elems = ();
29 0 for my $elem (@{$type->children->[0]}) {
30 0 push(@new_elems, $self->deref_type($elem));
31 }
32 0 $type->children->[0] = \@new_elems;
33 }
34 elsif ($kind eq 'Array') {
35 0 $type->children->[0] = $self->deref_type($type->children->[0]);
36 }
37 elsif ($kind eq 'Var') {
38 0 ▶ if ($type->children->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->children->[0]) { } 0 0 0
39 0 $type->children->[0] = $self->deref_type($type->children->[0]);
40 0 return $type->children->[0];
41 }
42 else {
43 0 croak "This must not happen.";
44 }
45 }
46  
47 0 return $type;
48 }
49  
50 0 ▶sub deref_ident_type {
Subroutine deref_ident_type: not called CC=1 SCAR=6.9
51 0 my($self, $ident) = @_;
52 0 return [$ident->[0], $self->deref_type($ident->[1])];
53 }
54  
55 0 ▶sub deref_node {
Subroutine deref_node: not called CC=25 SCAR=64.8
56 0 my($self, $node) = @_;
57 0 my $kind = $node->kind;
58  
59 0 ▶ if ($kind eq 'Not' || $kind eq 'Neg' || $kind eq 'FNeg') {
Branch: 8 branches
BranchTrueFalseTotal
if ($kind eq 'Not' or $kind eq 'Neg' or $kind eq 'FNeg') { } 0 0 0
elsif ($kind eq 'Add' or $kind eq 'Sub' or $kind eq 'Eq' or $kind eq 'LE' or $kind eq 'FAdd' or $kind eq 'FSub' or $kind eq 'FMul' or $kind eq 'FDiv' or $kind eq 'Array' or $kind eq 'Get') { } 0 0 0
elsif ($kind eq 'If' or $kind eq 'Put') { } 0 0 0
elsif ($kind eq 'Let') { } 0 0 0
elsif ($kind eq 'LetRec') { } 0 0 0
elsif ($kind eq 'App') { } 0 0 0
elsif ($kind eq 'Tuple') { } 0 0 0
elsif ($kind eq 'LetTuple') { } 0 0 0
Condition0%0/6
$kind eq "Not" or $kind eq "Neg" or $kind eq "FNeg"
l!l&&r!l&&!r
000
$kind eq "Not" or $kind eq "Neg"
l!l&&r!l&&!r
000
MC/DC0%0/3
$kind eq "Not" or $kind eq "Neg" or $kind eq "FNeg"
$kind eq "Not" $kind eq "Neg" $kind eq "FNeg"
Truth table0%0/4
A or B or C
A= $kind eq "Not"B= $kind eq "Neg"C= $kind eq "FNeg"
ABCresult
1XX1
01X1
0011
0000
60 0 $node->children->[0] = $self->deref_node($node->children->[0]);
61 }
62 ▶ elsif ($kind eq 'Add' || $kind eq 'Sub' || $kind eq 'Eq'
Condition0%0/27
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd" or $kind eq "FSub" or $kind eq "FMul" or $kind eq "FDiv" or $kind eq "Array" or $kind eq "Get"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd" or $kind eq "FSub" or $kind eq "FMul" or $kind eq "FDiv" or $kind eq "Array"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd" or $kind eq "FSub" or $kind eq "FMul" or $kind eq "FDiv"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd" or $kind eq "FSub" or $kind eq "FMul"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd" or $kind eq "FSub"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq"
l!l&&r!l&&!r
000
$kind eq "Add" or $kind eq "Sub"
l!l&&r!l&&!r
000
MC/DC0%0/10
$kind eq "Add" or $kind eq "Sub" or $kind eq "Eq" or $kind eq "LE" or $kind eq "FAdd" or $kind eq "FSub" or $kind eq "FMul" or $kind eq "FDiv" or $kind eq "Array" or $kind eq "Get"
$kind eq "Add" $kind eq "Sub" $kind eq "Eq" $kind eq "LE" $kind eq "FAdd" $kind eq "FSub" $kind eq "FMul" $kind eq "FDiv" $kind eq "Array" $kind eq "Get"
Truth table0%0/11
A or B or C or D or E or F or G or H or I or J
A= $kind eq "Add"B= $kind eq "Sub"C= $kind eq "Eq"D= $kind eq "LE"E= $kind eq "FAdd"F= $kind eq "FSub"G= $kind eq "FMul"H= $kind eq "FDiv"I= $kind eq "Array"J= $kind eq "Get"
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
63 || $kind eq 'LE' || $kind eq 'FAdd' || $kind eq 'FSub'
64 || $kind eq 'FMul' || $kind eq 'FDiv' || $kind eq 'Array'
65 || $kind eq 'Get') {
66 0 $node->children->[0] = $self->deref_node($node->children->[0]);
67 0 $node->children->[1] = $self->deref_node($node->children->[1]);
68 }
69 ▶ elsif ($kind eq 'If' || $kind eq 'Put') {
Condition0%0/3
$kind eq "If" or $kind eq "Put"
l!l&&r!l&&!r
000
MC/DC0%0/2
$kind eq "If" or $kind eq "Put"
$kind eq "If" $kind eq "Put"
Truth table0%0/3
A or B
A= $kind eq "If"B= $kind eq "Put"
ABresult
1X1
011
000
70 0 $node->children->[0] = $self->deref_node($node->children->[0]);
71 0 $node->children->[1] = $self->deref_node($node->children->[1]);
72 0 $node->children->[2] = $self->deref_node($node->children->[2]);
73 }
74 elsif ($kind eq 'Let') {
75 0 $node->children->[0] = $self->deref_ident_type($node->children->[0]);
76 0 $node->children->[1] = $self->deref_node($node->children->[1]);
77 0 $node->children->[2] = $self->deref_node($node->children->[2]);
78 }
79 elsif ($kind eq 'LetRec') {
80 0 $node->children->[0]->{ident} = $self->deref_ident_type($node->children->[0]->{ident});
81 0 my @new_let_args = ();
82 0 for my $let_arg (@{$node->children->[0]->{args}}) {
83 0 push(@new_let_args, $self->deref_ident_type($let_arg));
84 }
85 0 $node->children->[0]->{args} = \@new_let_args;
86 0 $node->children->[0]->{body} = $self->deref_node($node->children->[0]->{body});
87 0 $node->children->[1] = $self->deref_node($node->children->[1]);
88 }
89 elsif ($kind eq 'App') {
90 0 $node->children->[0] = $self->deref_node($node->children->[0]);
91 0 my @new_app_args = ();
92 0 for my $app_arg (@{$node->children->[1]}) {
93 0 push(@new_app_args, $self->deref_node($app_arg));
94 }
95 0 $node->children->[1] = \@new_app_args;
96 }
97 elsif ($kind eq 'Tuple') {
98 0 my @new_elems = ();
99 0 for my $elem (@{$node->children->[0]}) {
100 0 push(@new_elems, $self->deref_node($elem));
101 }
102 0 $node->children->[0] = \@new_elems;
103 }
104 elsif ($kind eq 'LetTuple') {
105 0 my @new_elem_idents = ();
106 0 for my $elem_ident (@{$node->children->[0]}) {
107 0 push(@new_elem_idents, $self->deref_ident_type($elem_ident));
108 }
109 0 $node->children->[0] = \@new_elem_idents;
110 0 $node->children->[1] = $self->deref_node($node->children->[1]);
111 0 $node->children->[2] = $self->deref_node($node->children->[2]);
112 }
113  
114 0 return $node;
115 }
116  
117 0 ▶sub occur {
Subroutine occur: not called CC=12 SCAR=50.5
118 0 my($self, $left_type, $right_type) = @_;
119  
120 0 ▶ if ($right_type->kind eq 'Fun') {
Branch: 5 branches
BranchTrueFalseTotal
if ($right_type->kind eq 'Fun') { } 0 0 0
elsif ($right_type->kind eq 'Tuple') { } 0 0 0
elsif ($right_type->kind eq 'Array') { } 0 0 0
elsif ($right_type->kind eq 'Var' and $left_type == $right_type) { } 0 0 0
elsif ($right_type->kind eq 'Var' and $right_type->children->[0]) { } 0 0 0
121 0 for my $arg_type (@{$right_type->children->[0]}) {
122 0 ▶ return 1 if $self->occur($left_type, $arg_type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->occur($left_type, $arg_type) 0 0 0
123 }
124  
125 0 return $self->occur($left_type, $right_type->children->[1]);
126 }
127 elsif ($right_type->kind eq 'Tuple') {
128 0 for my $elem_type (@{$right_type->children->[0]}) {
129 0 ▶ return 1 if $self->occur($left_type, $elem_type);
Branch: 1 branch
BranchTrueFalseTotal
if $self->occur($left_type, $elem_type) 0 0 0
130 }
131  
132 0 return 0;
133 }
134 elsif ($right_type->kind eq 'Array') {
135 0 return $self->occur($left_type, $right_type->children->[0]);
136 }
137 ▶ elsif ($right_type->kind eq 'Var' && $left_type == $right_type) {
Condition0%0/3
$right_type->kind eq "Var" and $left_type == $right_type
!ll&&!rl&&r
000
MC/DC0%0/2
$right_type->kind eq "Var" and $left_type == $right_type
$right_type->kind eq "Var" $left_type == $right_type
Truth table0%0/3
A and B
A= $right_type->kind eq "Var"B= $left_type == $right_type
ABresult
0X0
100
111
138 0 return 1;
139 }
140 ▶ elsif ($right_type->kind eq 'Var' && $right_type->children->[0]) {
Condition0%0/3
$right_type->kind eq "Var" and $right_type->children->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$right_type->kind eq "Var" and $right_type->children->[0]
$right_type->kind eq "Var" $right_type->children->[0]
Truth table0%0/3
A and B
A= $right_type->kind eq "Var"B= $right_type->children->[0]
ABresult
0X0
100
111
141 0 return $self->occur($left_type, $right_type->children->[0]);
142 }
143 else {
144 0 return 0;
145 }
146 }
147  
148 0 ▶sub unify {
Subroutine unify: not called CC=33 SCAR=70.2
149 0 my($self, $left_type, $right_type) = @_;
150  
151 0 ▶ return if $left_type == $right_type;
Branch: 1 branch
BranchTrueFalseTotal
if $left_type == $right_type 0 0 0
152  
153 0 ▶ if (($left_type->kind eq 'Unit' && $right_type->kind eq 'Unit')
Branch: 9 branches
BranchTrueFalseTotal
if ($left_type->kind eq 'Unit' and $right_type->kind eq 'Unit' or $left_type->kind eq 'Bool' and $right_type->kind eq 'Bool' or $left_type->kind eq 'Int' and $right_type->kind eq 'Int' or $left_type->kind eq 'Float' and $right_type->kind eq 'Float') { } 0 0 0
elsif ($left_type->kind eq 'Fun' and $right_type->kind eq 'Fun') { } 0 0 0
elsif ($left_type->kind eq 'Tuple' and $right_type->kind eq 'Tuple') { } 0 0 0
elsif ($left_type->kind eq 'Array' and $right_type->kind eq 'Array') { } 0 0 0
elsif ($left_type->kind eq 'Var' and $right_type->kind eq 'Var' and $left_type->children->[0] and $right_type->children->[0] and $left_type->children->[0]->kind eq $right_type->children->[0]->kind) { } 0 0 0
elsif ($left_type->kind eq 'Var' and $left_type->children->[0]) { } 0 0 0
elsif ($right_type->kind eq 'Var' and $right_type->children->[0]) { } 0 0 0
elsif ($left_type->kind eq 'Var' and not $self->occur($left_type, $right_type)) { } 0 0 0
elsif ($right_type->kind eq 'Var' and not $self->occur($right_type, $left_type)) { } 0 0 0
Condition0%0/21
$left_type->kind eq "Unit" and $right_type->kind eq "Unit" or $left_type->kind eq "Bool" and $right_type->kind eq "Bool" or $left_type->kind eq "Int" and $right_type->kind eq "Int" or $left_type->kind eq "Float" and $right_type->kind eq "Float"
l!l&&r!l&&!r
000
$left_type->kind eq "Unit" and $right_type->kind eq "Unit" or $left_type->kind eq "Bool" and $right_type->kind eq "Bool" or $left_type->kind eq "Int" and $right_type->kind eq "Int"
l!l&&r!l&&!r
000
$left_type->kind eq "Unit" and $right_type->kind eq "Unit" or $left_type->kind eq "Bool" and $right_type->kind eq "Bool"
l!l&&r!l&&!r
000
$left_type->kind eq "Unit" and $right_type->kind eq "Unit"
!ll&&!rl&&r
000
$left_type->kind eq "Bool" and $right_type->kind eq "Bool"
!ll&&!rl&&r
000
$left_type->kind eq "Int" and $right_type->kind eq "Int"
!ll&&!rl&&r
000
$left_type->kind eq "Float" and $right_type->kind eq "Float"
!ll&&!rl&&r
000
MC/DC0%0/8
$left_type->kind eq "Unit" and $right_type->kind eq "Unit" or $left_type->kind eq "Bool" and $right_type->kind eq "Bool" or $left_type->kind eq "Int" and $right_type->kind eq "Int" or $left_type->kind eq "Float" and $right_type->kind eq "Float"
$left_type->kind eq "Unit" $right_type->kind eq "Unit" $left_type->kind eq "Bool" $right_type->kind eq "Bool" $left_type->kind eq "Int" $right_type->kind eq "Int" $left_type->kind eq "Float" $right_type->kind eq "Float"
Truth table0%0/31
A and B or C and D or E and F or G and H
A= $left_type->kind eq "Unit"B= $right_type->kind eq "Unit"C= $left_type->kind eq "Bool"D= $right_type->kind eq "Bool"E= $left_type->kind eq "Int"F= $right_type->kind eq "Int"G= $left_type->kind eq "Float"H= $right_type->kind eq "Float"
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
154 || ($left_type->kind eq 'Bool' && $right_type->kind eq 'Bool')
155 || ($left_type->kind eq 'Int' && $right_type->kind eq 'Int')
156 || ($left_type->kind eq 'Float' && $right_type->kind eq 'Float')) {
157 }
158 ▶ elsif ($left_type->kind eq 'Fun' && $right_type->kind eq 'Fun') {
Condition0%0/3
$left_type->kind eq "Fun" and $right_type->kind eq "Fun"
!ll&&!rl&&r
000
MC/DC0%0/2
$left_type->kind eq "Fun" and $right_type->kind eq "Fun"
$left_type->kind eq "Fun" $right_type->kind eq "Fun"
Truth table0%0/3
A and B
A= $left_type->kind eq "Fun"B= $right_type->kind eq "Fun"
ABresult
0X0
100
111
159 0 ▶ $self->error unless @{$left_type->children->[0]} == @{$right_type->children->[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$left_type->children->[0];} == @{$right_type->children->[0];} 0 0 0
160  
161 0 for my $index (0..$#{$left_type->children->[0]}) {
162 0 $self->unify($left_type->children->[0]->[$index],
163 $right_type->children->[0]->[$index]);
164 }
165  
166 0 $self->unify($left_type->children->[1], $right_type->children->[1]);
167 }
168 ▶ elsif ($left_type->kind eq 'Tuple' && $right_type->kind eq 'Tuple') {
Condition0%0/3
$left_type->kind eq "Tuple" and $right_type->kind eq "Tuple"
!ll&&!rl&&r
000
MC/DC0%0/2
$left_type->kind eq "Tuple" and $right_type->kind eq "Tuple"
$left_type->kind eq "Tuple" $right_type->kind eq "Tuple"
Truth table0%0/3
A and B
A= $left_type->kind eq "Tuple"B= $right_type->kind eq "Tuple"
ABresult
0X0
100
111
169 0 ▶ $self->error unless @{$left_type->children->[0]} == @{$right_type->children->[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$left_type->children->[0];} == @{$right_type->children->[0];} 0 0 0
170  
171 0 for my $index (0..$#{$left_type->children->[0]}) {
172 0 $self->unify($left_type->children->[0]->[$index],
173 $right_type->children->[0]->[$index]);
174 }
175 }
176 ▶ elsif ($left_type->kind eq 'Array' && $right_type->kind eq 'Array') {
Condition0%0/3
$left_type->kind eq "Array" and $right_type->kind eq "Array"
!ll&&!rl&&r
000
MC/DC0%0/2
$left_type->kind eq "Array" and $right_type->kind eq "Array"
$left_type->kind eq "Array" $right_type->kind eq "Array"
Truth table0%0/3
A and B
A= $left_type->kind eq "Array"B= $right_type->kind eq "Array"
ABresult
0X0
100
111
177 0 $self->unify($left_type->children->[0], $right_type->children->[0]);
178 }
179 ▶ elsif ($left_type->kind eq 'Var' && $right_type->kind eq 'Var'
Condition0%0/12
$left_type->kind eq "Var" and $right_type->kind eq "Var" and $left_type->children->[0] and $right_type->children->[0] and $left_type->children->[0]->kind eq $right_type->children->[0]->kind
!ll&&!rl&&r
000
$left_type->kind eq "Var" and $right_type->kind eq "Var" and $left_type->children->[0] and $right_type->children->[0]
!ll&&!rl&&r
000
$left_type->kind eq "Var" and $right_type->kind eq "Var" and $left_type->children->[0]
!ll&&!rl&&r
000
$left_type->kind eq "Var" and $right_type->kind eq "Var"
!ll&&!rl&&r
000
MC/DC0%0/5
$left_type->kind eq "Var" and $right_type->kind eq "Var" and $left_type->children->[0] and $right_type->children->[0] and $left_type->children->[0]->kind eq $right_type->children->[0]->kind
$left_type->kind eq "Var" $right_type->kind eq "Var" $left_type->children->[0] $right_type->children->[0] $left_type->children->[0]->kind eq $right_type->children->[0]->kind
Truth table0%0/6
A and B and C and D and E
A= $left_type->kind eq "Var"B= $right_type->kind eq "Var"C= $left_type->children->[0]D= $right_type->children->[0]E= $left_type->children->[0]->kind eq $right_type->children->[0]->kind
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
180 && $left_type->children->[0] && $right_type->children->[0]
181 && $left_type->children->[0]->kind eq $right_type->children->[0]->kind) {
182 }
183 ▶ elsif ($left_type->kind eq 'Var' && $left_type->children->[0]) {
Condition0%0/3
$left_type->kind eq "Var" and $left_type->children->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$left_type->kind eq "Var" and $left_type->children->[0]
$left_type->kind eq "Var" $left_type->children->[0]
Truth table0%0/3
A and B
A= $left_type->kind eq "Var"B= $left_type->children->[0]
ABresult
0X0
100
111
184 0 $self->unify($left_type->children->[0], $right_type);
185 }
186 ▶ elsif ($right_type->kind eq 'Var' && $right_type->children->[0]) {
Condition0%0/3
$right_type->kind eq "Var" and $right_type->children->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$right_type->kind eq "Var" and $right_type->children->[0]
$right_type->kind eq "Var" $right_type->children->[0]
Truth table0%0/3
A and B
A= $right_type->kind eq "Var"B= $right_type->children->[0]
ABresult
0X0
100
111
187 0 $self->unify($left_type, $right_type->children->[0]);
188 }
189 ▶ elsif ($left_type->kind eq 'Var' && !$self->occur($left_type, $right_type)) {
Condition0%0/3
$left_type->kind eq "Var" and not $self->occur($left_type, $right_type)
!ll&&!rl&&r
000
MC/DC0%0/2
$left_type->kind eq "Var" and not $self->occur($left_type, $right_type)
$left_type->kind eq "Var" not $self->occur($left_type, $right_type)
Truth table0%0/3
A and not(B)
A= $left_type->kind eq "Var"B= not $self->occur($left_type, $right_type)
ABresult
0X0
100
111
190 0 $left_type->children->[0] = $right_type;
191 }
192 ▶ elsif ($right_type->kind eq 'Var' && !$self->occur($right_type, $left_type)) {
Condition0%0/3
$right_type->kind eq "Var" and not $self->occur($right_type, $left_type)
!ll&&!rl&&r
000
MC/DC0%0/2
$right_type->kind eq "Var" and not $self->occur($right_type, $left_type)
$right_type->kind eq "Var" not $self->occur($right_type, $left_type)
Truth table0%0/3
A and not(B)
A= $right_type->kind eq "Var"B= not $self->occur($right_type, $left_type)
ABresult
0X0
100
111
193 0 $right_type->children->[0] = $left_type;
194 }
195 else {
196 0 $self->error;
197 }
198  
199 0 return;
200 }
201  
202 0 ▶sub infer_rec {
Subroutine infer_rec: not called CC=31 SCAR=69.0
203 0 my($self, $node, %env) = @_;
204 0 my $kind = $node->kind;
205  
206 0 ▶ if ($kind eq 'Unit') {
Branch: 20 branches
BranchTrueFalseTotal
if ($kind eq 'Unit') { } 0 0 0
elsif ($kind eq 'Bool') { } 0 0 0
elsif ($kind eq 'Int') { } 0 0 0
elsif ($kind eq 'Float') { } 0 0 0
elsif ($kind eq 'Not') { } 0 0 0
elsif ($kind eq 'Neg') { } 0 0 0
elsif ($kind eq 'Add' or $kind eq 'Sub') { } 0 0 0
elsif ($kind eq 'FNeg') { } 0 0 0
elsif ($kind eq 'FAdd' or $node->kind eq 'FSub' or $kind eq 'FMul' or $kind eq 'FDiv') { } 0 0 0
elsif ($kind eq 'Eq' or $kind eq 'LE') { } 0 0 0
elsif ($kind eq 'If') { } 0 0 0
elsif ($kind eq 'Let') { } 0 0 0
elsif ($kind eq 'Var') { } 0 0 0
elsif ($kind eq 'LetRec') { } 0 0 0
elsif ($kind eq 'App') { } 0 0 0
elsif ($kind eq 'Tuple') { } 0 0 0
elsif ($kind eq 'LetTuple') { } 0 0 0
elsif ($kind eq 'Array') { } 0 0 0
elsif ($kind eq 'Get') { } 0 0 0
elsif ($kind eq 'Put') { } 0 0 0
207 0 return Type_Unit();
208 }
209 elsif ($kind eq 'Bool') {
210 0 return Type_Bool();
211 }
212 elsif ($kind eq 'Int') {
213 0 return Type_Int();
214 }
215 elsif ($kind eq 'Float') {
216 0 return Type_Float();
217 }
218 elsif ($kind eq 'Not') {
219 0 $self->unify(Type_Bool, $self->infer_rec($node->children->[0], %env));
220  
221 0 return Type_Bool();
222 }
223 elsif ($kind eq 'Neg') {
224 0 $self->unify(Type_Int, $self->infer_rec($node->children->[0], %env));
225  
226 0 return Type_Int();
227 }
228 ▶ elsif ($kind eq 'Add' || $kind eq 'Sub') {
Condition0%0/3
$kind eq "Add" or $kind eq "Sub"
l!l&&r!l&&!r
000
MC/DC0%0/2
$kind eq "Add" or $kind eq "Sub"
$kind eq "Add" $kind eq "Sub"
Truth table0%0/3
A or B
A= $kind eq "Add"B= $kind eq "Sub"
ABresult
1X1
011
000
229 0 $self->unify(Type_Int, $self->infer_rec($node->children->[0], %env));
230 0 $self->unify(Type_Int, $self->infer_rec($node->children->[1], %env));
231  
232 0 return Type_Int();
233 }
234 elsif ($kind eq 'FNeg') {
235 0 $self->unify(Type_Float, $self->infer_rec($node->children->[0], %env));
236  
237 0 return Type_Float();
238 }
239 ▶ elsif ($kind eq 'FAdd' || $node->kind eq 'FSub'
Condition0%0/9
$kind eq "FAdd" or $node->kind eq "FSub" or $kind eq "FMul" or $kind eq "FDiv"
l!l&&r!l&&!r
000
$kind eq "FAdd" or $node->kind eq "FSub" or $kind eq "FMul"
l!l&&r!l&&!r
000
$kind eq "FAdd" or $node->kind eq "FSub"
l!l&&r!l&&!r
000
MC/DC0%0/4
$kind eq "FAdd" or $node->kind eq "FSub" or $kind eq "FMul" or $kind eq "FDiv"
$kind eq "FAdd" $node->kind eq "FSub" $kind eq "FMul" $kind eq "FDiv"
Truth table0%0/5
A or B or C or D
A= $kind eq "FAdd"B= $node->kind eq "FSub"C= $kind eq "FMul"D= $kind eq "FDiv"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
240 || $kind eq 'FMul' || $kind eq 'FDiv') {
241 0 $self->unify(Type_Float, $self->infer_rec($node->children->[0], %env));
242 0 $self->unify(Type_Float, $self->infer_rec($node->children->[1], %env));
243  
244 0 return Type_Float();
245 }
246 ▶ elsif ($kind eq 'Eq' || $kind eq 'LE') {
Condition0%0/3
$kind eq "Eq" or $kind eq "LE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$kind eq "Eq" or $kind eq "LE"
$kind eq "Eq" $kind eq "LE"
Truth table0%0/3
A or B
A= $kind eq "Eq"B= $kind eq "LE"
ABresult
1X1
011
000
247 0 $self->unify($self->infer_rec($node->children->[0], %env),
248 $self->infer_rec($node->children->[1], %env));
249  
250 0 return Type_Bool();
251 }
252 elsif ($kind eq 'If') {
253 0 $self->unify($self->infer_rec($node->children->[0], %env),
254 Type_Bool());
255 0 my $stat_type = $self->infer_rec($node->children->[1], %env);
256 0 $self->unify($stat_type, $self->infer_rec($node->children->[2], %env));
257  
258 0 return $stat_type;
259 }
260 elsif ($kind eq 'Let') {
261 0 $self->unify($node->children->[0]->[1],
262 $self->infer_rec($node->children->[1], %env));
263 0 $env{$node->children->[0]->[0]} = $node->children->[0]->[1];
264  
265 0 return $self->infer_rec($node->children->[2], %env);
266 }
267 elsif ($kind eq 'Var') {
268 0 my $ident_name = $node->children->[0];
269  
270 0 ▶ if (exists $env{$ident_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env{$ident_name}) { } 0 0 0
271 0 return $env{$ident_name};
272 }
273 else {
274 0 $self->error;
275 }
276 }
277 elsif ($kind eq 'LetRec') {
278 0 my $ident = $node->children->[0]->{ident};
279 0 my $let_args = $node->children->[0]->{args};
280 0 my $body = $node->children->[0]->{body};
281 0 $env{$ident->[0]} = $ident->[1];
282 0 my @arg_types = ();
283 0 my %tmp_env = %env;
284  
285 0 for my $arg (@$let_args) {
286 0 push(@arg_types, $arg->[1]);
287 0 $tmp_env{$arg->[0]} = $arg->[1];
288 }
289  
290 0 $self->unify($ident->[1],
291 Type_Fun(\@arg_types, $self->infer_rec($body, %tmp_env)));
292  
293 0 return $self->infer_rec($node->children->[1], %env);
294 }
295 elsif ($kind eq 'App') {
296 0 my $app_ident_type = $self->infer_rec($node->children->[0], %env);
297 0 my $app_args = $node->children->[1];
298 0 my @arg_types = ();
299  
300 0 for my $arg (@$app_args) {
301 0 push(@arg_types, $self->infer_rec($arg, %env));
302 }
303  
304 0 my $tmp_type = Type_Var();
305 0 $self->unify($app_ident_type, Type_Fun(\@arg_types, $tmp_type));
306  
307 0 return $tmp_type;
308 }
309 elsif ($kind eq 'Tuple') {
310 0 my @elem_types = ();
311  
312 0 for my $elem (@{$node->children->[0]}) {
313 0 push(@elem_types, $self->infer_rec($elem, %env));
314 }
315  
316 0 return Type_Tuple(\@elem_types);
317 }
318 elsif ($kind eq 'LetTuple') {
319 0 my @elem_types = ();
320 0 my %tmp_env = %env;
321  
322 0 for my $elem_ident (@{$node->children->[0]}) {
323 0 push(@elem_types, $elem_ident->[1]);
324 0 $tmp_env{$elem_ident->[0]} = $elem_ident->[1];
325 }
326 0 $self->unify(Type_Tuple(\@elem_types),
327 $self->infer_rec($node->children->[1], %env));
328  
329 0 return $self->infer_rec($node->children->[2], %tmp_env);
330 }
331 elsif ($kind eq 'Array') {
332 0 $self->unify($self->infer_rec($node->children->[0], %env), Type_Int());
333  
334 0 return Type_Array($self->infer_rec($node->children->[1], %env));
335 }
336 elsif ($kind eq 'Get') {
337 0 my $tmp_type = Type_Var();
338  
339 0 $self->unify(Type_Array($tmp_type),
340 $self->infer_rec($node->children->[0], %env));
341 0 $self->unify(Type_Int(),
342 $self->infer_rec($node->children->[1], %env));
343  
344 0 return $tmp_type;
345 }
346 elsif ($kind eq 'Put') {
347 0 my $tmp_type = $self->infer_rec($node->children->[2], %env);
348  
349 0 $self->unify(Type_Array($tmp_type),
350 $self->infer_rec($node->children->[0], %env));
351 0 $self->unify(Type_Int(),
352 $self->infer_rec($node->children->[1], %env));
353  
354 0 return Type_Unit();
355 }
356 else {
357 0 croak "This must not happen.";
358 }
359 }
360  
361 sub infer {
362 my($self, $root_node, %type_env) = @_;
363 my $top_level_type = $self->infer_rec($root_node, %type_env);
364  
365 $self->unify($top_level_type, Type_Unit());
366 $self->deref_node($root_node);
367  
368 return;
369 }
370  
371 1;
« MinCaml/Evaluator.pm ↑ Summary MinCaml/Lexer.pm »