Skip to content

Commit

Permalink
Fixed Jira bug I7-2241
Browse files Browse the repository at this point in the history
  • Loading branch information
ganelson committed Nov 17, 2024
1 parent a01526f commit bb3fe3a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
17 changes: 14 additions & 3 deletions docs/kinds-module/2-knd.html
Original file line number Diff line number Diff line change
Expand Up @@ -501,20 +501,31 @@ <h1><a href="../index.html">
subkind of <span class="extract"><span class="extract-syntax">W</span></span> is replaced by <span class="extract"><span class="extract-syntax">W</span></span>.
</p>

<p class="commentary">We do need to be careful over contravariance: the kind "object based rulebook
producing a number" is stronger than "thing based rulebook producing a number",
not weaker, because the K term for "K based rulebook producing L" is contravariant.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="reserved-syntax">kind</span><span class="plain-syntax"> *</span><span class="function-syntax">Kinds::weaken</span><span class="plain-syntax">(</span><span class="reserved-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="reserved-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">W</span><span class="plain-syntax">) {</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><a href="2-knd.html#SP13" class="function-link"><span class="function-syntax">Kinds::is_proper_constructor</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">)) {</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">X</span><span class="plain-syntax"> = </span><span class="identifier-syntax">NULL</span><span class="plain-syntax">, *</span><span class="identifier-syntax">Y</span><span class="plain-syntax"> = </span><span class="identifier-syntax">NULL</span><span class="plain-syntax">;</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">a</span><span class="plain-syntax"> = </span><a href="2-knd.html#SP13" class="function-link"><span class="function-syntax">Kinds::arity_of_constructor</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">a</span><span class="plain-syntax"> == </span><span class="constant-syntax">1</span><span class="plain-syntax">) {</span>
<span class="plain-syntax"> </span><span class="identifier-syntax">X</span><span class="plain-syntax"> = </span><a href="2-knd.html#SP14" class="function-link"><span class="function-syntax">Kinds::unary_construction_material</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><a href="2-knd.html#SP6_3" class="function-link"><span class="function-syntax">Kinds::unary_con</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><a href="2-knd.html#SP18" class="function-link"><span class="function-syntax">Kinds::weaken</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">X</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">));</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">WX</span><span class="plain-syntax"> = </span><span class="identifier-syntax">X</span><span class="plain-syntax">;</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><a href="4-kc2.html#SP17" class="function-link"><span class="function-syntax">KindConstructors::variance</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><span class="constant-syntax">0</span><span class="plain-syntax">) == </span><span class="constant-syntax">COVARIANT</span><span class="plain-syntax">) </span><span class="identifier-syntax">WX</span><span class="plain-syntax"> = </span><a href="2-knd.html#SP18" class="function-link"><span class="function-syntax">Kinds::weaken</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">X</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><a href="2-knd.html#SP6_3" class="function-link"><span class="function-syntax">Kinds::unary_con</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><span class="identifier-syntax">WX</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax"> </span><a href="2-knd.html#SP15" class="function-link"><span class="function-syntax">Kinds::binary_construction_material</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, &amp;</span><span class="identifier-syntax">X</span><span class="plain-syntax">, &amp;</span><span class="identifier-syntax">Y</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><a href="2-knd.html#SP6_3" class="function-link"><span class="function-syntax">Kinds::binary_con</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><a href="2-knd.html#SP18" class="function-link"><span class="function-syntax">Kinds::weaken</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">X</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">), </span><a href="2-knd.html#SP18" class="function-link"><span class="function-syntax">Kinds::weaken</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">Y</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">));</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">WX</span><span class="plain-syntax"> = </span><span class="identifier-syntax">X</span><span class="plain-syntax">, *</span><span class="identifier-syntax">WY</span><span class="plain-syntax"> = </span><span class="identifier-syntax">Y</span><span class="plain-syntax">;</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><a href="4-kc2.html#SP17" class="function-link"><span class="function-syntax">KindConstructors::variance</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><span class="constant-syntax">0</span><span class="plain-syntax">) == </span><span class="constant-syntax">COVARIANT</span><span class="plain-syntax">) </span><span class="identifier-syntax">WX</span><span class="plain-syntax"> = </span><a href="2-knd.html#SP18" class="function-link"><span class="function-syntax">Kinds::weaken</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">X</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><a href="4-kc2.html#SP17" class="function-link"><span class="function-syntax">KindConstructors::variance</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><span class="constant-syntax">1</span><span class="plain-syntax">) == </span><span class="constant-syntax">COVARIANT</span><span class="plain-syntax">) </span><span class="identifier-syntax">WY</span><span class="plain-syntax"> = </span><a href="2-knd.html#SP18" class="function-link"><span class="function-syntax">Kinds::weaken</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">Y</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><a href="2-knd.html#SP6_3" class="function-link"><span class="function-syntax">Kinds::binary_con</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">construct</span><span class="plain-syntax">, </span><span class="identifier-syntax">WX</span><span class="plain-syntax">, </span><span class="identifier-syntax">WY</span><span class="plain-syntax">);</span>
<span class="plain-syntax"> }</span>
<span class="plain-syntax"> } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">K</span><span class="plain-syntax">) &amp;&amp; (</span><a href="2-knd.html#SP25" class="function-link"><span class="function-syntax">Kinds::conforms_to</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">)) &amp;&amp; (</span><a href="2-knd.html#SP24" class="function-link"><span class="function-syntax">Kinds::eq</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="identifier-syntax">K_nil</span><span class="plain-syntax">) == </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">) &amp;&amp; (</span><a href="2-knd.html#SP24" class="function-link"><span class="function-syntax">Kinds::eq</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="identifier-syntax">K_void</span><span class="plain-syntax">) == </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">)) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">W</span><span class="plain-syntax">;</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">K</span><span class="plain-syntax">) &amp;&amp; (</span><a href="2-knd.html#SP25" class="function-link"><span class="function-syntax">Kinds::conforms_to</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="identifier-syntax">W</span><span class="plain-syntax">)) &amp;&amp; (</span><a href="2-knd.html#SP24" class="function-link"><span class="function-syntax">Kinds::eq</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="identifier-syntax">K_nil</span><span class="plain-syntax">) == </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">) &amp;&amp;</span>
<span class="plain-syntax"> (</span><a href="2-knd.html#SP24" class="function-link"><span class="function-syntax">Kinds::eq</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">K</span><span class="plain-syntax">, </span><span class="identifier-syntax">K_void</span><span class="plain-syntax">) == </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">)) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">W</span><span class="plain-syntax">;</span>
<span class="plain-syntax"> }</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">K</span><span class="plain-syntax">;</span>
<span class="plain-syntax">}</span>
Expand Down
2 changes: 1 addition & 1 deletion docs/kinds-module/4-kc2.html
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ <h1><a href="../index.html">
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">con</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">tupling</span><span class="plain-syntax">[</span><span class="identifier-syntax">b</span><span class="plain-syntax">];</span>
<span class="plain-syntax">}</span>

<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="function-syntax">KindConstructors::variance</span><button class="popup" onclick="togglePopup('usagePopup13')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup13">Usage of <span class="code-font"><span class="function-syntax">KindConstructors::variance</span></span>:<br/>Kinds - <a href="2-knd.html#SP17">&#167;17</a><br/>The Lattice of Kinds - <a href="2-tlok.html#SP13_4">&#167;13.4</a></span></button><span class="plain-syntax">(</span><span class="reserved-syntax">kind_constructor</span><span class="plain-syntax"> *</span><span class="identifier-syntax">con</span><span class="plain-syntax">, </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">b</span><span class="plain-syntax">) {</span>
<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="function-syntax">KindConstructors::variance</span><button class="popup" onclick="togglePopup('usagePopup13')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup13">Usage of <span class="code-font"><span class="function-syntax">KindConstructors::variance</span></span>:<br/>Kinds - <a href="2-knd.html#SP17">&#167;17</a>, <a href="2-knd.html#SP18">&#167;18</a><br/>The Lattice of Kinds - <a href="2-tlok.html#SP13_4">&#167;13.4</a></span></button><span class="plain-syntax">(</span><span class="reserved-syntax">kind_constructor</span><span class="plain-syntax"> *</span><span class="identifier-syntax">con</span><span class="plain-syntax">, </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">b</span><span class="plain-syntax">) {</span>
<span class="plain-syntax"> </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">con</span><span class="plain-syntax">-&gt;</span><span class="element-syntax">variance</span><span class="plain-syntax">[</span><span class="identifier-syntax">b</span><span class="plain-syntax">];</span>
<span class="plain-syntax">}</span>

Expand Down
27 changes: 14 additions & 13 deletions inform7/Figures/timings-diagnostics.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
100.0% in inform7 run
66.4% in compilation to Inter
43.9% in //Sequence::undertake_queued_tasks//
4.1% in //MajorNodes::pre_pass//
3.4% in //MajorNodes::pass_1//
65.7% in compilation to Inter
43.3% in //Sequence::undertake_queued_tasks//
4.4% in //MajorNodes::pre_pass//
3.3% in //MajorNodes::pass_1//
2.0% in //RTKindConstructors::compile//
1.7% in //ImperativeDefinitions::assess_all//
1.6% in //ImperativeDefinitions::assess_all//
1.3% in //RTPhrasebook::compile_entries//
1.0% in //Sequence::lint_inter//
0.6% in //ImperativeDefinitions::compile_first_block//
Expand All @@ -15,16 +15,17 @@
0.3% in //Sequence::undertake_queued_tasks//
0.3% in //Sequence::undertake_queued_tasks//
0.3% in //Task::make_built_in_kind_constructors//
4.9% not specifically accounted for
26.2% in running Inter pipeline
8.6% in step 14/15: generate inform6 -> auto.inf
6.9% in step 5/15: load-binary-kits
5.5% in step 6/15: make-synoptic-module
4.7% not specifically accounted for
27.1% in running Inter pipeline
8.8% in step 14/15: generate inform6 -> auto.inf
6.7% in step 5/15: load-binary-kits
5.7% in step 6/15: make-synoptic-module
2.0% in step 9/15: make-identifiers-unique
0.3% in step 11/15: eliminate-redundant-labels
0.3% in step 12/15: eliminate-redundant-operations
0.3% in step 4/15: compile-splats
0.3% in step 7/15: shorten-wiring
0.3% in step 8/15: detect-indirect-calls
1.7% not specifically accounted for
6.2% in supervisor
1.1% not specifically accounted for
2.1% not specifically accounted for
6.4% in supervisor
0.7% not specifically accounted for
16 changes: 13 additions & 3 deletions services/kinds-module/Chapter 2/Kinds.w
Original file line number Diff line number Diff line change
Expand Up @@ -380,20 +380,30 @@ kind *Kinds::substitute_inner(kind *K, kind **meanings, int *changed, int contra
This operation corresponds to rounding kinds up to |W|: that is, any
subkind of |W| is replaced by |W|.

We do need to be careful over contravariance: the kind "object based rulebook
producing a number" is stronger than "thing based rulebook producing a number",
not weaker, because the K term for "K based rulebook producing L" is contravariant.

=
kind *Kinds::weaken(kind *K, kind *W) {
if (Kinds::is_proper_constructor(K)) {
kind *X = NULL, *Y = NULL;
int a = Kinds::arity_of_constructor(K);
if (a == 1) {
X = Kinds::unary_construction_material(K);
return Kinds::unary_con(K->construct, Kinds::weaken(X, W));
kind *WX = X;
if (KindConstructors::variance(K->construct, 0) == COVARIANT) WX = Kinds::weaken(X, W);
return Kinds::unary_con(K->construct, WX);
} else {
Kinds::binary_construction_material(K, &X, &Y);
return Kinds::binary_con(K->construct, Kinds::weaken(X, W), Kinds::weaken(Y, W));
kind *WX = X, *WY = Y;
if (KindConstructors::variance(K->construct, 0) == COVARIANT) WX = Kinds::weaken(X, W);
if (KindConstructors::variance(K->construct, 1) == COVARIANT) WY = Kinds::weaken(Y, W);
return Kinds::binary_con(K->construct, WX, WY);
}
} else {
if ((K) && (Kinds::conforms_to(K, W)) && (Kinds::eq(K, K_nil) == FALSE) && (Kinds::eq(K, K_void) == FALSE)) return W;
if ((K) && (Kinds::conforms_to(K, W)) && (Kinds::eq(K, K_nil) == FALSE) &&
(Kinds::eq(K, K_void) == FALSE)) return W;
}
return K;
}
Expand Down

0 comments on commit bb3fe3a

Please sign in to comment.