<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Tayzen's blog]]></title><description><![CDATA[A blog about nerdy stuffs.]]></description><link>https://knorpelsenf.me</link><generator>RSS for Node</generator><lastBuildDate>Sat, 14 Nov 2026 18:33:38 GMT</lastBuildDate><atom:link href="https://knorpelsenf.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><category><![CDATA[Technology]]></category><category><![CDATA[Programming]]></category><category><![CDATA[Self-hosting]]></category><item><title><![CDATA[LLMs: Semantic Translation Machines]]></title><link>https://knorpelsenf.me/posts/semantic-translation</link><guid isPermaLink="true">https://knorpelsenf.me/posts/semantic-translation</guid><dc:creator><![CDATA[KnorpelSenf]]></dc:creator><pubDate>Sat, 26 Sep 2026 00:00:00 GMT</pubDate><content:encoded>&lt;h1&gt;LLMs: Semantic Translation Machines&lt;/h1&gt;
&lt;blockquote&gt;
&lt;p&gt;This post is based on my talk at the Waterkant Festival 2026.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The AI hype is as ubiquitous as it is annoying.
Some say that AI will beat software engineers at every task in a few months.
Some say that human programmers will always be better than AI.&lt;/p&gt;
&lt;p&gt;Obviously, both extremes are wildly wrong.
But what does it help to say &amp;quot;the truth is more complicated&amp;quot; without actually figuring out what this complicated bit is?&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Like most software engineers, I care about solving real problems in the real world.
Unsolved problems.
Hard problems.
The stuff that tickles your mind, and that requires some novel solutions and the right trade-offs.
And I think it&amp;#39;s fun to do excellent work and build something &lt;em&gt;outstanding&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Can LLMs even help me at all?&lt;/p&gt;
&lt;p&gt;I have tried to use LLMs numerous times throughout the years.
Every time I found that they produce slop and waste my time.
They either forced me to accept bad changes, or they made me fix up their changes manually, which was slower than just doing everything by hand.&lt;/p&gt;
&lt;p&gt;In early 2026, this changed.
Time to collect some evidence.&lt;/p&gt;
&lt;p&gt;Instead of just using the latest hype tool, I wanted to understand this properly.
Let&amp;#39;s take a more structured approach then:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Take a properly hard engineering problem.&lt;/li&gt;
&lt;li&gt;Get your CTO to pay for infinite tokens.&lt;/li&gt;
&lt;li&gt;Systematically try out various ways to use LLMs, and write down how it goes.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is what I did in March 2026, and here is what I found out.&lt;/p&gt;
&lt;h2&gt;A Hard Problem&lt;/h2&gt;
&lt;p&gt;At &lt;a href=&quot;https://solvares-fieldservice.com/&quot; rel=&quot;noopener noreferrer&quot;&gt;my current employer&lt;/a&gt;, we solve large instances of the Vehicle Routing Problem (&lt;a href=&quot;https://en.wikipedia.org/wiki/Vehicle_routing_problem&quot; rel=&quot;noopener noreferrer&quot;&gt;wiki&lt;/a&gt;).
One reasonably hard problem from this domain is that we have to compute a distance matrix as input to our smart algorithms.
What&amp;#39;s more, our distance matrix implementation had long suffered from heaps of legacy code and stability issues, making it a good candidate for replacement.&lt;/p&gt;
&lt;p&gt;Now, what is a distance matrix, you may ask?&lt;/p&gt;
&lt;p&gt;Let me explain.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./semantic-translation/dima.jpg&quot; alt=&quot;distance matrix trade offer&quot; title=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Essentially, we want to have a web server with a single endpoint.
As &lt;strong&gt;input&lt;/strong&gt; (request body), it accepts an array of locations as lat/lon coordinates.&lt;/p&gt;
&lt;div class=&quot;highlight highlight-source-json notranslate&quot;&gt;&lt;pre&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;&quot;coordinates&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;lat&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;54.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;lon&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10.0&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;lat&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;54.1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;lon&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10.1&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;lat&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;54.2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token property&quot;&gt;&quot;lon&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10.4&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The server then looks at the world&amp;#39;s road network and computes the best routes from each point to each other point, and returns only the distances and travel times as a large matrix.
We do not return any information about the routes themselves, except for the length and duration.&lt;/p&gt;
&lt;p&gt;Conceptually, the &lt;strong&gt;output&lt;/strong&gt; (response body) looks like this:&lt;/p&gt;
&lt;div class=&quot;highlight highlight-source-json notranslate&quot;&gt;&lt;pre&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;&quot;distances&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;18201&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;55879&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;18204&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;32444&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;61390&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;38199&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;token property&quot;&gt;&quot;times&quot;&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1319&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3121&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1343&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2279&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;3414&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2670&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note that from each point to itself, the distance is zero metres (and the time is zero seconds).
Due to one-way streets, turn restrictions, etc, going from A to B is only &lt;em&gt;approximately&lt;/em&gt; as far as from B to A.&lt;/p&gt;
&lt;p&gt;What makes this problem so hard is the extreme performance that we require.
For example, &lt;strong&gt;for 1,000 locations we have no more than 100 milliseconds&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s right: &lt;em&gt;one million routes&lt;/em&gt; must be computed and measured, and the results must be encoded and transmitted over the network and parsed by the client, and all of this must happen in &lt;em&gt;less than a tenth of a second&lt;/em&gt;.
Even if we ignore all the networking, we only have 100 nanoseconds to compute each route.&lt;/p&gt;
&lt;p&gt;That seems impossible.
I guess we can agree that this problem is reasonably hard.
Vibe-coding this cannot work.&lt;/p&gt;
&lt;h2&gt;An Impressive Solution&lt;/h2&gt;
&lt;p&gt;I don&amp;#39;t want to go into the details of the sophisticated algorithms and insane optimisations that were needed to pull this off.
After all, this post is about how LLMs helped me, not about how the system works.
But here is the data on what it took to build this service:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;3 weeks of regular full-time work&lt;/li&gt;
&lt;li&gt;by a single person (me)&lt;/li&gt;
&lt;li&gt;with around €1200 in tokens&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Around half of the time (and the tokens) was spent on the actual design and the rust implementation.
The other half was spent building testing and benchmarking tooling in order to evaluate the solution, as well as to integrate it into our existing infrastructure.&lt;/p&gt;
&lt;p&gt;Here is the p90 performance data for our &lt;code&gt;c5a.4xlarge&lt;/code&gt; instance on AWS (8 physical AMD Zen 2 cores):&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;N locations&lt;/th&gt;
&lt;th&gt;N*N matrix cells&lt;/th&gt;
&lt;th&gt;time to last byte&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;0.8 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;500&lt;/td&gt;
&lt;td&gt;250,000&lt;/td&gt;
&lt;td&gt;31 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1,000&lt;/td&gt;
&lt;td&gt;1,000,000&lt;/td&gt;
&lt;td&gt;75 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5,000&lt;/td&gt;
&lt;td&gt;25,000,000&lt;/td&gt;
&lt;td&gt;602 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;td&gt;100,000,000&lt;/td&gt;
&lt;td&gt;4,837 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;That&amp;#39;s pretty solid!
Computing 100M distances and travel times in under five seconds on a regular 8-core machine can be counted as a success.&lt;/p&gt;
&lt;p&gt;Almost everything is LLM-generated.
Out of approximately 15,000 lines of code in total, I think I wrote 4 manually and the other 14,996 or so with an LLM.&lt;/p&gt;
&lt;p&gt;Along the way, I wrote a detailed log of what I tried, what worked, and what didn&amp;#39;t.
I noted down &lt;em&gt;how&lt;/em&gt; I tried to work with LLMs, rather than saying anything about which algorithms I tried.&lt;/p&gt;
&lt;p&gt;This diary now lets us answer the key question of this entire blog post:&lt;/p&gt;
&lt;h2&gt;When do LLMs actually help, and when should you avoid them?&lt;/h2&gt;
&lt;p&gt;The answer is … that it&amp;#39;s the wrong question.
At least, there is a better question to ask:&lt;/p&gt;
&lt;h2&gt;What Is an LLM?&lt;/h2&gt;
&lt;p&gt;Knowing what an LLM is helps you characterize the kind of tasks they can handle.&lt;/p&gt;
&lt;p&gt;I mean this in an intuitive sense, not in a technical one.
Some people say that LLMs are &lt;strong&gt;next-token prediction machines&lt;/strong&gt;.
That&amp;#39;s perfectly accurate but not what I mean.
This intuition is not very enlightening in day-to-day work, and it does not tell me how to change my workflow.&lt;/p&gt;
&lt;p&gt;Instead, I believe we should understand LLMs as &lt;strong&gt;semantic translation machines&lt;/strong&gt;.
They translate an idea or a concept from one representation to another.&lt;/p&gt;
&lt;p&gt;The term &lt;em&gt;semantic translation&lt;/em&gt; needs some clarification.&lt;/p&gt;
&lt;h2&gt;Semantic Translation&lt;/h2&gt;
&lt;p&gt;By semantic translation, I mean that the same underlying concept or idea is translated from one representation to another.&lt;/p&gt;
&lt;div&gt;
&lt;svg id=&quot;semantic-translation-diagram&quot; class=&quot;semantic-diagram&quot; fill=&quot;none&quot; stroke=&quot;none&quot; stroke-linecap=&quot;square&quot; stroke-miterlimit=&quot;10&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;125 170 745 490&quot; width=&quot;745&quot;
  height=&quot;490&quot; style=&quot;display: block; width: 100%; max-width: 100%; height: auto; margin: 10px auto;&quot; role=&quot;img&quot; aria-labelledby=&quot;semantic-translation-title&quot;
  aria-describedby=&quot;semantic-translation-desc&quot;&gt;
  &lt;title id=&quot;semantic-translation-title&quot;&gt;Semantic translation&lt;/title&gt;
  &lt;desc id=&quot;semantic-translation-desc&quot;&gt;Three connected nodes form a triangle:
    English at the bottom left, English summary at the top, and source code at
    the bottom right. Solid lines connect all three nodes, and dashed lines
    extend outward from each node.&lt;/desc&gt;
  &lt;!-- Definitions --&gt;
  &lt;defs&gt;
    &lt;style&gt;
    .semantic-diagram {
      --diagram-line: #315da8;
      --diagram-dashed: #776b5a;
      --diagram-accent: #b42338;
    }
    [data-color-mode=&quot;dark&quot;] .semantic-diagram {
      --diagram-line: #8aadf4;
      --diagram-dashed: #7b8092;
      --diagram-accent: #f0808e;
    }
    .semantic-diagram .text {
      font-family: Arial, sans-serif;
      font-size: 19.5px;
      font-weight: bold;
      fill: var(--color-text);
    }
    .semantic-diagram .solid-line {
      stroke: var(--diagram-line);
      stroke-width: 2.5;
    }
    .semantic-diagram .dashed-line {
      stroke: var(--diagram-dashed);
      stroke-width: 2.5;
      stroke-dasharray: 8 6;
    }
    .semantic-diagram .dot {
      fill: var(--diagram-line);
    }
    &lt;/style&gt;
  &lt;/defs&gt;
  &lt;g&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m292.8 513.4488c0 -5.2184143 4.2304077 -9.4487915 9.448822 -9.4487915c2.5059814 0 4.9093323 0.9954834 6.6813354 2.7674866c1.7720032 1.7720032 2.7674866 4.175354 2.7674866 6.681305c0 5.218445 -4.230377 9.448853 -9.448822 9.448853c-5.2184143 0 -9.448822 -4.2304077 -9.448822 -9.448853z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m478.72 291.6888c0 -5.218445 4.230377 -9.448822 9.448822 -9.448822c2.5059814 0 4.9093323 0.9955139 6.681305 2.767517c1.7720032 1.7719727 2.767517 4.1753235 2.767517 6.681305c0 5.218445 -4.230377 9.448822 -9.448822 9.448822c-5.218445 0 -9.448822 -4.230377 -9.448822 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m690.56 465.14645c0 -5.218445 4.2304077 -9.448822 9.4487915 -9.448822c2.5060425 0 4.909363 0.9955139 6.6813354 2.7674866c1.7720337 1.7720032 2.767517 4.175354 2.767517 6.6813354c0 5.218445 -4.2304077 9.448822 -9.448853 9.448822c-5.218384 0 -9.4487915 -4.230377 -9.4487915 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;text x=&quot;282&quot; y=&quot;537&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;English&lt;/text&gt;
    &lt;text x=&quot;474&quot; y=&quot;248&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;English&lt;/text&gt;
    &lt;text x=&quot;474&quot; y=&quot;276&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;summary&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;461&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;source&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;489&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;code&lt;/text&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m326.8647 510.0l345.1353 -41.24997&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m317.66666 498.33334l156.0 -192.0&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m681.198 449.66666l-175.198 -145.66666&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m285.33334 501.0l-76.28978 -59.399994&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m311.69763 627.6881l-7.697632 -96.68811&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m710.04987 579.34406l-7.697632 -96.68811&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;M496 270 L518 210&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m709.45764 447.67413l44.73993 -84.41733&quot; fill-rule=&quot;evenodd&quot; /&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;

&lt;p&gt;This goes beyond merely translating between two human languages (something that LLMs are obviously very good at).
For example, if you have an English text, it can be translated to a matching English summary.&lt;/p&gt;
&lt;p&gt;Hypothetically, if you have an English text that describes a program in sufficient detail, such as a line-by-line description of all the operations (corresponding to a given programming language), then LLMs will be extremely good at translating this specification to the actual source code in that language.&lt;/p&gt;
&lt;p&gt;Similarly, if you have a lot of source code, an LLM can summarise it for you.&lt;/p&gt;
&lt;p&gt;What&amp;#39;s common among these examples is that &lt;strong&gt;the idea exists&lt;/strong&gt;, and the LLM &lt;strong&gt;rewrites it&lt;/strong&gt; and lets you move to a different representation of the same idea.
It does not have to come up with anything substantial on its own.&lt;sup&gt;&lt;a id=&quot;footnote-ref-1&quot; href=&quot;#footnote-1&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;But don&amp;#39;t we all know that LLMs hallucinate?!
Even for simple translations we can&amp;#39;t be sure of the output!&lt;/p&gt;
&lt;p&gt;Correct.
The process is probabilistic.&lt;/p&gt;
&lt;h2&gt;Probabilistic Semantic Translation&lt;/h2&gt;
&lt;p&gt;Essentially, when you shoot your shot at a translation, you don&amp;#39;t hit your target exactly.&lt;/p&gt;
&lt;div&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; id=&quot;semantic-translation-one&quot; class=&quot;semantic-diagram&quot; viewBox=&quot;125 170 745 490&quot; width=&quot;745&quot; height=&quot;490&quot; fill=&quot;none&quot; stroke=&quot;none&quot; stroke-linecap=&quot;square&quot; stroke-miterlimit=&quot;10&quot; style=&quot;display: block; width: 100%; max-width: 100%; height: auto; margin: 10px auto;&quot; role=&quot;img&quot; aria-labelledby=&quot;semantic-translation-one-title&quot; aria-describedby=&quot;semantic-translation-one-desc&quot;&gt;
  &lt;title id=&quot;semantic-translation-one-title&quot;&gt;Probabilistic semantic translation&lt;/title&gt;
  &lt;desc id=&quot;semantic-translation-one-desc&quot;&gt;English, English summary, and source code form a triangle. Two red dotted arrows from English pass above and below the source-code node, illustrating translation uncertainty.&lt;/desc&gt;
  &lt;g&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m292.8 513.4488c0 -5.2184143 4.2304077 -9.4487915 9.448822 -9.4487915c2.5059814 0 4.9093323 0.9954834 6.6813354 2.7674866c1.7720032 1.7720032 2.7674866 4.175354 2.7674866 6.681305c0 5.218445 -4.230377 9.448853 -9.448822 9.448853c-5.2184143 0 -9.448822 -4.2304077 -9.448822 -9.448853z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m478.72 291.6888c0 -5.218445 4.230377 -9.448822 9.448822 -9.448822c2.5059814 0 4.9093323 0.9955139 6.681305 2.767517c1.7720032 1.7719727 2.767517 4.1753235 2.767517 6.681305c0 5.218445 -4.230377 9.448822 -9.448822 9.448822c-5.218445 0 -9.448822 -4.230377 -9.448822 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m690.56 465.14645c0 -5.218445 4.2304077 -9.448822 9.4487915 -9.448822c2.5060425 0 4.909363 0.9955139 6.6813354 2.7674866c1.7720337 1.7720032 2.767517 4.175354 2.767517 6.6813354c0 5.218445 -4.2304077 9.448822 -9.448853 9.448822c-5.218384 0 -9.4487915 -4.230377 -9.4487915 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;text x=&quot;282&quot; y=&quot;537&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;English&lt;/text&gt;
    &lt;text x=&quot;474&quot; y=&quot;248&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;English&lt;/text&gt;
    &lt;text x=&quot;474&quot; y=&quot;276&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;summary&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;461&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;source&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;489&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;code&lt;/text&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m326.8647 510.0l345.1353 -41.24997&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m317.66666 498.33334l156.0 -192.0&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m681.198 449.66666l-175.198 -145.66666&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m285.33334 501.0l-76.28978 -59.399994&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m311.69763 627.6881l-7.697632 -96.68811&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m710.04987 579.34406l-7.697632 -96.68811&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;M496 270 L518 210&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m709.45764 447.67413l44.73993 -84.41733&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;defs&gt;
      &lt;marker id=&quot;translation-error-arrowhead&quot; markerUnits=&quot;userSpaceOnUse&quot; markerWidth=&quot;20&quot; markerHeight=&quot;15&quot; refX=&quot;16&quot; refY=&quot;6&quot; orient=&quot;auto&quot; viewBox=&quot;0 0 16 12&quot;&gt;
        &lt;path d=&quot;M0 0 L16 6 L0 12 Z&quot; fill=&quot;var(--diagram-accent)&quot; /&gt;
      &lt;/marker&gt;
    &lt;/defs&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;3&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;3 8&quot; d=&quot;M329.5918 518.2171 L700.9329 524.5896&quot; /&gt;
    &lt;path stroke=&quot;none&quot; marker-end=&quot;url(#translation-error-arrowhead)&quot; d=&quot;M700.9329 524.5896 L712.9311 524.7955&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;3&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;3 8&quot; d=&quot;M328.76 500.8123 L667.7802 403.7395&quot; /&gt;
    &lt;path stroke=&quot;none&quot; marker-end=&quot;url(#translation-error-arrowhead)&quot; d=&quot;M667.7802 403.7395 L679.3166 400.4362&quot; /&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;

&lt;p&gt;Instead, the LLM will give you output that is &lt;em&gt;very close&lt;/em&gt; to what you wanted.
The translation has a bit of uncertainty that introduces a slight error.&lt;/p&gt;
&lt;p&gt;There is a great deal to be said about reducing this error.
For example, going from a lot of info to very little info works well, and the other way around generally does not.
(Trying to restore the long English text from its short summary will leave you with tons of hallucinations, and false and inaccurate statements.)&lt;/p&gt;
&lt;p&gt;That being said, I will leave the discussion of reducing hallucinations to other people.
For now, it is enough to acknowledge that semantic translation is probabilistic.&lt;/p&gt;
&lt;p&gt;Another way of looking at this is that every translation incurs a debt to the truth.
You not only change the representation of the idea, you also slightly distort the idea itself.
This leaves you with a different idea, not quite identical to your original one.&lt;/p&gt;
&lt;div&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; id=&quot;semantic-translation-two&quot; class=&quot;semantic-diagram&quot; viewBox=&quot;125 170 745 490&quot; width=&quot;745&quot; height=&quot;490&quot; fill=&quot;none&quot; stroke=&quot;none&quot; stroke-linecap=&quot;square&quot; stroke-miterlimit=&quot;10&quot; style=&quot;display: block; width: 100%; max-width: 100%; height: auto; margin: 10px auto;&quot; role=&quot;img&quot; aria-labelledby=&quot;semantic-translation-two-title&quot; aria-describedby=&quot;semantic-translation-two-desc&quot;&gt;
  &lt;title id=&quot;semantic-translation-two-title&quot;&gt;Translation changes the idea&lt;/title&gt;
  &lt;desc id=&quot;semantic-translation-two-desc&quot;&gt;A red triangle is shifted from the blue triangle connecting English, English summary, and source code, illustrating how translation changes the original idea.&lt;/desc&gt;
  &lt;g&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m292.8 513.4488c0 -5.2184143 4.2304077 -9.4487915 9.448822 -9.4487915c2.5059814 0 4.9093323 0.9954834 6.6813354 2.7674866c1.7720032 1.7720032 2.7674866 4.175354 2.7674866 6.681305c0 5.218445 -4.230377 9.448853 -9.448822 9.448853c-5.2184143 0 -9.448822 -4.2304077 -9.448822 -9.448853z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m478.72 291.6888c0 -5.218445 4.230377 -9.448822 9.448822 -9.448822c2.5059814 0 4.9093323 0.9955139 6.681305 2.767517c1.7720032 1.7719727 2.767517 4.1753235 2.767517 6.681305c0 5.218445 -4.230377 9.448822 -9.448822 9.448822c-5.218445 0 -9.448822 -4.230377 -9.448822 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m690.56 465.14645c0 -5.218445 4.2304077 -9.448822 9.4487915 -9.448822c2.5060425 0 4.909363 0.9955139 6.6813354 2.7674866c1.7720337 1.7720032 2.767517 4.175354 2.767517 6.6813354c0 5.218445 -4.2304077 9.448822 -9.448853 9.448822c-5.218384 0 -9.4487915 -4.230377 -9.4487915 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;text x=&quot;282&quot; y=&quot;537&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;English&lt;/text&gt;
    &lt;text x=&quot;474&quot; y=&quot;248&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;English&lt;/text&gt;
    &lt;text x=&quot;474&quot; y=&quot;276&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;summary&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;461&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;source&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;489&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;code&lt;/text&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m326.8647 510.0l345.1353 -41.24997&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m317.66666 498.33334l156.0 -192.0&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-line)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m681.198 449.66666l-175.198 -145.66666&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m285.33334 501.0l-76.28978 -59.399994&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m311.69763 627.6881l-7.697632 -96.68811&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m710.04987 579.34406l-7.697632 -96.68811&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;M496 270 L518 210&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m709.45764 447.67413l44.73993 -84.41733&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-accent)&quot; d=&quot;m368.13333 471.7305c0 -5.218445 4.230377 -9.448822 9.448822 -9.448822c2.5059814 0 4.9093323 0.9955139 6.6813354 2.7674866c1.7719727 1.7720032 2.7674866 4.175354 2.7674866 6.6813354c0 5.218445 -4.230377 9.448822 -9.448822 9.448822c-5.218445 0 -9.448822 -4.230377 -9.448822 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;1.3333333333333333&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m368.13333 471.7305c0 -5.218445 4.230377 -9.448822 9.448822 -9.448822c2.5059814 0 4.9093323 0.9955139 6.6813354 2.7674866c1.7719727 1.7720032 2.7674866 4.175354 2.7674866 6.6813354c0 5.218445 -4.230377 9.448822 -9.448822 9.448822c-5.218445 0 -9.448822 -4.230377 -9.448822 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-accent)&quot; d=&quot;m554.05334 249.9705c0 -5.218445 4.2303467 -9.448822 9.4487915 -9.448822c2.5059814 0 4.909363 0.99549866 6.6813354 2.7674866c1.7719727 1.7720032 2.767517 4.175354 2.767517 6.6813354c0 5.2184296 -4.2304077 9.448807 -9.448853 9.448807c-5.218445 0 -9.4487915 -4.230377 -9.4487915 -9.448807z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;1.3333333333333333&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m554.05334 249.9705c0 -5.218445 4.2303467 -9.448822 9.4487915 -9.448822c2.5059814 0 4.909363 0.99549866 6.6813354 2.7674866c1.7719727 1.7720032 2.767517 4.175354 2.767517 6.6813354c0 5.2184296 -4.2304077 9.448807 -9.448853 9.448807c-5.218445 0 -9.4487915 -4.230377 -9.4487915 -9.448807z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-accent)&quot; d=&quot;m765.8933 423.42813c0 -5.218445 4.2304077 -9.448822 9.448853 -9.448822c2.5059814 0 4.9093018 0.9955139 6.6813354 2.767517c1.7719727 1.7719727 2.767456 4.1753235 2.767456 6.681305c0 5.218445 -4.2303467 9.448822 -9.4487915 9.448822c-5.218445 0 -9.448853 -4.230377 -9.448853 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;1.3333333333333333&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m765.8933 423.42813c0 -5.218445 4.2304077 -9.448822 9.448853 -9.448822c2.5059814 0 4.9093018 0.9955139 6.6813354 2.767517c1.7719727 1.7719727 2.767456 4.1753235 2.767456 6.681305c0 5.218445 -4.2303467 9.448822 -9.4487915 9.448822c-5.218445 0 -9.448853 -4.230377 -9.448853 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m402.198 468.28168l345.1353 -41.24997&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m393.0 456.61502l156.0 -192.0&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-accent)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; d=&quot;m756.5313 407.94833l-175.198 -145.66666&quot; fill-rule=&quot;evenodd&quot; /&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;

&lt;p&gt;With every hop to another representation, you add a new layer to your stack of adjacent ideas.
The more steps you take, the further you will remove yourself from the concept you started with.&lt;/p&gt;
&lt;h2&gt;Not Semantic Translation&lt;/h2&gt;
&lt;p&gt;In contrast, here are a few things that are &lt;em&gt;not&lt;/em&gt; mere translations.&lt;/p&gt;
&lt;div&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; id=&quot;semantic-translation-three&quot; class=&quot;semantic-diagram&quot; viewBox=&quot;165 195 675 410&quot; width=&quot;675&quot; height=&quot;410&quot; fill=&quot;none&quot; stroke=&quot;none&quot; stroke-linecap=&quot;square&quot; stroke-miterlimit=&quot;10&quot; style=&quot;display: block; width: 100%; max-width: 100%; height: auto; margin: 10px auto;&quot; role=&quot;img&quot; aria-labelledby=&quot;semantic-translation-three-title&quot; aria-describedby=&quot;semantic-translation-three-desc&quot;&gt;
  &lt;title id=&quot;semantic-translation-three-title&quot;&gt;Tasks beyond semantic translation&lt;/title&gt;
  &lt;desc id=&quot;semantic-translation-three-desc&quot;&gt;Three separate nodes labelled facts, requirements, and ideas each connect to a question mark with a dashed line. These tasks require more than semantic translation.&lt;/desc&gt;
  &lt;g&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m292.8 513.4488c0 -5.2184143 4.2304077 -9.4487915 9.448822 -9.4487915c2.5059814 0 4.9093323 0.9954834 6.6813354 2.7674866c1.7720032 1.7720032 2.7674866 4.175354 2.7674866 6.681305c0 5.218445 -4.230377 9.448853 -9.448822 9.448853c-5.2184143 0 -9.448822 -4.2304077 -9.448822 -9.448853z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m478.72 291.6888c0 -5.218445 4.230377 -9.448822 9.448822 -9.448822c2.5059814 0 4.9093323 0.9955139 6.681305 2.767517c1.7720032 1.7719727 2.767517 4.1753235 2.767517 6.681305c0 5.218445 -4.230377 9.448822 -9.448822 9.448822c-5.218445 0 -9.448822 -4.230377 -9.448822 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path fill=&quot;var(--diagram-line)&quot; d=&quot;m690.56 465.14645c0 -5.218445 4.2304077 -9.448822 9.4487915 -9.448822c2.5060425 0 4.909363 0.9955139 6.6813354 2.7674866c1.7720337 1.7720032 2.767517 4.175354 2.767517 6.6813354c0 5.218445 -4.2304077 9.448822 -9.448853 9.448822c-5.218384 0 -9.4487915 -4.230377 -9.4487915 -9.448822z&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;text x=&quot;282&quot; y=&quot;522&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;facts&lt;/text&gt;
    &lt;text x=&quot;468&quot; y=&quot;281&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;requirements&lt;/text&gt;
    &lt;text x=&quot;720&quot; y=&quot;470&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;24&quot; font-weight=&quot;bold&quot;&gt;ideas&lt;/text&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m645.06866 513.4487l38.131348 -34.006622&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m508.01566 284.4357l54.360077 -32.126617&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;text x=&quot;560&quot; y=&quot;262&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;45&quot; font-weight=&quot;normal&quot;&gt;?&lt;/text&gt;
    &lt;path stroke=&quot;var(--diagram-dashed)&quot; stroke-width=&quot;3.0&quot; stroke-miterlimit=&quot;8.0&quot; stroke-linecap=&quot;butt&quot; stroke-dasharray=&quot;12.0,9.0&quot; d=&quot;m318.77417 522.8975l23.97818 21.49292&quot; fill-rule=&quot;evenodd&quot; /&gt;
    &lt;text x=&quot;358&quot; y=&quot;561&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;45&quot; font-weight=&quot;normal&quot;&gt;?&lt;/text&gt;
    &lt;text x=&quot;634&quot; y=&quot;531&quot; text-anchor=&quot;end&quot; fill=&quot;var(--color-text)&quot; font-family=&quot;Arial, sans-serif&quot; font-size=&quot;45&quot; font-weight=&quot;normal&quot;&gt;?&lt;/text&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;

&lt;p&gt;This may sound obvious.
If you want to find out what your customer wants, you should not ask an LLM.
You should ask your customer.&lt;/p&gt;
&lt;p&gt;If you ask an LLM about a fact, it will apply semantic translation to that question.
The LLM effectively tells you:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Great question!
People who ask these questions also make these statements about the topic …&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;… and then proceeds to list “facts” that it may or may not reproduce from its training data.&lt;/p&gt;
&lt;p&gt;This is its way of representing the question by an answer that is as similar as possible to the question you asked.
However, the facts needed for that answer were not part of the question, so they cannot be part of the translation and have to be made up.&lt;sup&gt;&lt;a id=&quot;footnote-ref-2&quot; href=&quot;#footnote-2&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Facts, requirements, or novel ideas&lt;sup&gt;&lt;a id=&quot;footnote-ref-3&quot; href=&quot;#footnote-3&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;3&lt;/a&gt;&lt;/sup&gt; cannot be LLM-generated.
They can only be LLM-translated.&lt;sup&gt;&lt;a id=&quot;footnote-ref-4&quot; href=&quot;#footnote-4&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;
(This is especially true for things that did not appear often in the training data.)&lt;/p&gt;
&lt;h2&gt;LLMs for Software Engineering&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s get a little more hands-on.
We now have a good intuition for LLMs as semantic translation machines.
But how exactly does this help programmers?&lt;/p&gt;
&lt;p&gt;The thing is, semantic translation can happen in several steps, and combine several data sources.
For example, a kind of prompt that works very well might contain&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;a source file name&lt;/li&gt;
&lt;li&gt;a problem description&lt;/li&gt;
&lt;li&gt;a brief sketch of a refactoring plan&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;and then the LLM can perform the following steps of semantic translation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;source file name → a &lt;code&gt;Read&lt;/code&gt; tool call&lt;/li&gt;
&lt;li&gt;the problem description + refactoring plan → a set of refactoring steps&lt;/li&gt;
&lt;li&gt;source code from (1) + refactoring steps → list of &lt;code&gt;Write&lt;/code&gt; tool calls&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If step (2) is non-trivial, or if the refactoring plan in your prompt does not have enough details, an LLM can fix that for you.
Take your initial prompt and let the LLM translate it to a few &lt;code&gt;Read&lt;/code&gt; tool calls or web searches that give it enough context to write a better prompt (usually called plan mode).&lt;/p&gt;
&lt;p&gt;LLMs automate the grunt work.&lt;/p&gt;
&lt;p&gt;You understand the problem, and you come up with the solution.
The LLM helps you get there faster.&lt;sup&gt;&lt;a id=&quot;footnote-ref-5&quot; href=&quot;#footnote-5&quot; data-footnote-ref aria-describedby=&quot;footnote-label&quot;&gt;5&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;In my case, doing a lot of performance work requires a ton of tasks that LLMs automate easily.
Instrumenting code, running benchmarks, generating flamegraphs, sifting through endless amounts of performance metric data, and thereby finding bottlenecks are perfect examples of semantic translation.
Those are &lt;em&gt;trivial&lt;/em&gt; tasks for LLMs.
Once you know what the exact bottleneck is, it&amp;#39;s usually straightforward to fix it and repeat the process.&lt;/p&gt;
&lt;p&gt;That&amp;#39;s how LLMs help.&lt;/p&gt;
&lt;h2&gt;Addendum: Pattern Recognition and Recombination Machines&lt;/h2&gt;
&lt;p&gt;A different intuition for LLMs is called &lt;em&gt;pattern recognition and recombination&lt;/em&gt; machines (thanks to Marc Heimann for telling me about it).
The idea is that LLMs do not translate, but that they detect patterns that they can replicate and recombine.
That&amp;#39;s arguably a more accurate description when you factor in the underlying technology, but I would not say that it is necessarily more intuitive.
If somebody interrupted me during programming and gave me a machine to recombine textual patterns, I would not know how to deal with it.&lt;/p&gt;
&lt;p&gt;I intentionally chose a very liberal interpretation of the word &lt;em&gt;translation&lt;/em&gt;.
It includes things like translating &lt;code&gt;2 4 6 8&lt;/code&gt; and &lt;code&gt;generate 3 more numbers!&lt;/code&gt; to &lt;code&gt;2 4 6 8 10 12 14&lt;/code&gt;.
Admittedly, this is clearly more of a pattern recognition task.
My stance, however, is that &lt;code&gt;2 4 6 8&lt;/code&gt; and &lt;code&gt;2 4 6 8 10 12 14&lt;/code&gt; both are different representations of the same concept (positive even numbers), and continuing the sequence is just picking a different representation of that concept.&lt;/p&gt;
&lt;section class=&quot;footnotes&quot; data-footnotes&gt;
&lt;h2 id=&quot;footnote-label&quot; class=&quot;sr-only&quot;&gt;Footnotes&lt;/h2&gt;
&lt;ol&gt;
&lt;li id=&quot;footnote-1&quot;&gt;
&lt;p&gt;The idea does not necessarily have to be part of the prompt. It can also be the result of a tool call. In both cases, it resides in the context of the session. &lt;a href=&quot;#footnote-ref-1&quot; data-footnote-backref aria-label=&quot;Back to reference 1&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;footnote-2&quot;&gt;
&lt;p&gt;This is why you can essentially get the LLM to argue any position simply by phrasing the question a bit differently. &lt;a href=&quot;#footnote-ref-2&quot; data-footnote-backref aria-label=&quot;Back to reference 2&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;footnote-3&quot;&gt;
&lt;p&gt;Note that you can very well use LLMs for &lt;em&gt;brainstorming&lt;/em&gt; ideas. Their ability to put things differently is great for changing your perspective on a problem, and thus getting creative. But either way, the ideas are generated by your brain, not the LLM. &lt;a href=&quot;#footnote-ref-3&quot; data-footnote-backref aria-label=&quot;Back to reference 3&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;footnote-4&quot;&gt;
&lt;p&gt;Sometimes, you can take these tasks and turn them into semantic translation problems. For example, if your LLM has access to Google and Wikipedia, it can effectively translate your request for facts to a tool call to search the web, and then rephrase the info it found. This means that it&amp;#39;s worth looking for ways to convert your tasks into those that LLMs can do well. &lt;a href=&quot;#footnote-ref-4&quot; data-footnote-backref aria-label=&quot;Back to reference 4&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&quot;footnote-5&quot;&gt;
&lt;p&gt;Another analogy I came up with is that LLMs are seven-league boots. They are amazing if you know where you want to go. But if you run in the wrong direction half the time, you end up exactly where you started. &lt;a href=&quot;#footnote-ref-5&quot; data-footnote-backref aria-label=&quot;Back to reference 5&quot;&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</content:encoded></item><item><title><![CDATA[Putting Advanced TypeScript Types Into Practice]]></title><link>https://knorpelsenf.me/posts/tsglobal24</link><guid isPermaLink="true">https://knorpelsenf.me/posts/tsglobal24</guid><dc:creator><![CDATA[KnorpelSenf]]></dc:creator><pubDate>Mon, 10 Jun 2024 00:00:00 GMT</pubDate><content:encoded>&lt;h1&gt;Putting Advanced TypeScript Types Into Practice&lt;/h1&gt;
&lt;p&gt;I was a speaker at &lt;a href=&quot;https://events.geekle.us/typescript24/&quot; rel=&quot;noopener noreferrer&quot;&gt;TypeScript Global Summit 2024&lt;/a&gt;.
My talk is about building complex TypeScript types in a way that they can be applied to practical problems.
This can be illustrated with grammY, my first large open-source project.
You can watch the recording &lt;a href=&quot;https://youtu.be/ZvT_xexjnMk&quot; rel=&quot;noopener noreferrer&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/ZvT_xexjnMk&quot; rel=&quot;noopener noreferrer&quot;&gt;&lt;img src=&quot;./tsglobal/thumb.png&quot; alt=&quot;talk&quot; title=&quot;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit 2024-07-18:&lt;/strong&gt; I have added subtitles to the video.
I still recommend watching the video, but if that is not your thing, you can read the following transcript.
Be sure to compare it to the &lt;a href=&quot;https://github.com/KnorpelSenf/tsglobal24&quot; rel=&quot;noopener noreferrer&quot;&gt;snapshots of the source code&lt;/a&gt; while you&amp;#39;re reading.&lt;/p&gt;
&lt;p&gt;Here is the transcript:&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Hi! My name is Steffen. As many of you guys, I very much enjoy building very complex types with TypeScript. The problem is that the more complex these types get, the more useless they get, too. So I was very excited when I found a use case where I could build very fancy and complicated types and have them actually be practical and relevant. So I would like to share this with you today. The idea of this talk is basically that we are going to do a lot of live coding and then we are going to build up this toolbox of useful tips and tricks that you can apply to your own project if you want to do fancy types and have them be useful.&lt;/p&gt;
&lt;p&gt;Before we can actually build types, we have to write some code. This code has to solve a problem, so let&amp;#39;s look at some problem. The most common problem (for backend at least) is probably building a web server. If you think about a web server, all it really does is handle HTTP requests. These HTTP requests have a certain structure applied to them already. So you have like a method like GET or POST and stuff, and then you have a path, and query parameters and hopefully also some headers. So this is pretty common, I guess.&lt;/p&gt;
&lt;p&gt;What I&amp;#39;ve been doing in the past few years is to spend a lot of time with Telegram chat bots. A Telegram chat bot is also just a server that handles HTTP requests, but these requests don&amp;#39;t really have much structure. They don&amp;#39;t have a method. It&amp;#39;s always POST so you can&amp;#39;t get any info from that. And it&amp;#39;s always the same path. There&amp;#39;s no headers or anything, so everything you get really is contained in the JSON body of the request.&lt;/p&gt;
&lt;p&gt;If you want to build a web framework for web servers, then typically, what people do is have this syntax that supports like POST and like the paths and all these things. But when you build a bot, that does not really work because that&amp;#39;s not a useful abstraction so when I built my framework grammY which helps build Telegram chat bots, we used this syntax instead which basically lets you register handlers for certain types of events, called updates, that your bot receives from Telegram. This is where the type magic happens, too, so like this part of the handler is what I want to focus on today.&lt;/p&gt;
&lt;p&gt;And so we have to take a little deeper look into what these updates actually are. The most common update is probably this. You just get like a message property and it has some text in it. So this just tells us “okay, there&amp;#39;s like a new message from some user.” If the user edits this message, then you&amp;#39;re going to get like an edited_message update and you can see “okay, this is the new text now.” Similarly, if the user sends a photo, you get like this photo message. So this is pretty clear, I guess. In grammY, you have like hundreds and hundreds of these event types that you can filter for. They&amp;#39;re called filter queries and in your editor, it will look like this. So you can type “okay, give me text messages” and it&amp;#39;s going to suggest you all the types of messages that you can listen for.&lt;/p&gt;
&lt;p&gt;Alright, so now that we know roughly what we want to build, let&amp;#39;s take a look at how we can do this in TypeScript. The first thing is to model the JSON objects that we get from the Telegram API which is easily done with a union type of message or edited_message and then we can get like message objects. And so what we want to end up with in the end is something like this. And now we can do something like bot.on(&amp;#39;message&amp;#39;), and receive a context object here and then we wanna do like, whatever, something like this, right.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s create this bot class to make that code actually work. It will take a context object as a parameter. Now we can write our normal functions here to handle updates. I&amp;#39;m going to skip the implementation of these functions because we just want to talk about types for now and not the runtime code. Anyone can do that.&lt;/p&gt;
&lt;p&gt;Okay, so, event types. You could think of them like event types in event emitters, but I&amp;#39;m going to stick with the grammY terminology of filter queries because in practice, they really aren&amp;#39;t event types. But that code would like something like this.&lt;/p&gt;
&lt;p&gt;Okay, so this code works now, except that TypeScript does not know that when we want to handle message updates, that the message property is actually present on the context object. So at this point, even though we handle message updates only, TypeScript still thinks that the update could be a message or an edited_message. So, that is sort of annoying. But TypeScript lets us fix this by introducing a type parameter. So we can just put the type parameter here and say “okay, now we want to use it for the filter query type.” So what this buys us is that we can get access to Q here. And instead of just doing a handler for the context object, we can now actually filter down the context object based on the query that we receive.&lt;/p&gt;
&lt;p&gt;Only thing that&amp;#39;s left to do is define the type Filter. It receives a context object and then some filter query. Now, all we have to do really is to say okay, if you filter down a context, then it&amp;#39;s still the context, but also, we know that the update property has, like, more things attached to it than TypeScript knows. In particular, in this case, what we know about the update if, like, Q for example is &amp;quot;message&amp;quot;, is that we can extract this union member from the update. So, TypeScript has this helper type, it&amp;#39;s called Extract, and we just put the update in there and what we have to do is, we have to extract all those union members that have a message key, so in this case, I will build this record here. And what this basically, like, this record, what this will end up being is something like this (at least, if you use a message handler). So you can see, in bot.on, it will infer correctly that Q is &amp;quot;message&amp;quot; and it will pass it to the handler, and so &amp;quot;message&amp;quot; will be here, and &amp;quot;message&amp;quot; will be there, and then it will extract the right union member of the update. Now you can see, okay, message is actually present, so we can handle it. That&amp;#39;s nice, but what doesn&amp;#39;t work yet is auto-complete.&lt;/p&gt;
&lt;p&gt;If we try to auto-complete here, it doesn&amp;#39;t know which filter queries are possible because all we said is that it can be any string, so TypeScript has no information about this. What we need to do for this is to actually define a new type, we will call it FilterQuery, and we can just say, okay, so the naïve implementation of FilterQuery would be to say okay, this is just keyof Update. Actually, it&amp;#39;s string &amp;amp; keyof Update because ... TypeScript things. But we will see that this type is never. It is never because keyof a union just means “give me all the keys that exist in every single union member.” But there are no keys that exist in every single union member. Like, this only has the key message and this only has the key edited_message. There&amp;#39;s no overlap. Hence, the result is never. There is a way to work around this, even though the fact that this doesn&amp;#39;t work out of the box sort of is a red flag already that our modelling maybe is not that great. But we can define this helper type KeyOfUnion and then ... I&amp;#39;m not going to explain how this thing works, we&amp;#39;re going to throw it out soon anyway, but the main point you should take away from this is just that you can work around it, but it&amp;#39;s nasty. So ... we can do extends unknown and then do string &amp;amp; keyof Update and never and now you can see that if we use this type instead, KeyOfUnion&lt;Update&gt;, that things will ... not work actu–? Oh. Oh, this is just wrong. Right. No– oh. Oh. Oh, okay. Sorry. Now it works! Awesome. Live coding. What could go wrong, right? Okay, so now we have this type FilterQuery and when we check this, we do actually get the correct suggestions. So, that&amp;#39;s nice.&lt;/p&gt;
&lt;p&gt;Now, what we&amp;#39;ve seen is that here, we still did not put FilterQuery. We just permit string. And you could get the idea, okay, let&amp;#39;s make the type stricter and put FilterQuery here, too, because we know that our Filter only should work for filter queries. Because stricter types are better, right? I would argue: no, we should leave this string because at the point where we call bot.on, it already checks that this is a FilterQuery, so at this point it already knows that it&amp;#39;s a FilterQuery. Passing this on here still makes it a FilterQuery, so checking it again against filter queries comes at a performance cost. TypeScript has to check the same thing again—something that we already know. When I implemented it with a tighter constraint in the beginning, it actually took 60 seconds for TypeScript to open up auto-complete in grammY. And you really don&amp;#39;t want to wait 60 seconds. So widening this union type of string literals from FilterQuery to just string actually boosted the performance quite a bit.&lt;/p&gt;
&lt;p&gt;That is already the first point that I would you to take away from this talk. It&amp;#39;s that if you can widen types to avoid unions, then do it. Avoid unions. Unions are expensive and if you don&amp;#39;t have to compute them, just don&amp;#39;t.&lt;/p&gt;
&lt;p&gt;Alright, so that&amp;#39;s nice. Let&amp;#39;s add another feature. Let&amp;#39;s say I want to handle not just one type of event, but let&amp;#39;s say, okay, I want to handle either a message or an edited message. This is very simple. We just have to say, okay, instead of just permitting a filter query, we also permit an array of filter queries. Simple as that. And now, in the function body, we can say, okay, what if ctx.update.message is present, then we can do one thing, and otherwise, we can do something else, whatever. And now, you will see this won&amp;#39;t work because ctx.update is never. And, why is that?&lt;/p&gt;
&lt;p&gt;Well, bot.on infers Q as &amp;quot;message&amp;quot; | &amp;quot;edited_message&amp;quot;. So it basically builds up a union type of all the elements in the array. So that means that there&amp;#39;s gonna be a union type here of &amp;quot;message&amp;quot; | &amp;quot;edited_message&amp;quot;. So we will pass &amp;quot;message&amp;quot; | &amp;quot;edited_message&amp;quot; to Filter. So we will get &amp;quot;message&amp;quot; | &amp;quot;edited_message&amp;quot; here. So it will build a record of &amp;quot;message&amp;quot; | &amp;quot;edited_message&amp;quot; which will look like this. The problem with this is that Extract will check all the different types, the union members of Update, against this object, and none of the members of this union type actually have both keys present. So what we want to have instead is something like this, right? That&amp;#39;s actually what we meant. But TypeScript doesn&amp;#39;t do this. And now, the only way to fix this is to use something called a distributed union type.&lt;/p&gt;
&lt;p&gt;A distributed union type is not really something that is like extra syntax. It&amp;#39;s more like a different way how TypeScript evaluates conditional types. So let&amp;#39;s take a look at what this means. If you have like a regular type like some record of whatever, and you pass a union of string literals such as &amp;quot;a&amp;quot; | &amp;quot;b&amp;quot;, then TypeScript will basically pass on this union to the record and you will get a record of a union type. So a record of two keys. So &amp;quot;a&amp;quot;: string or &amp;quot;b&amp;quot;: string. That&amp;#39;s the normal TypeScript thing, and no distribution going on there.&lt;/p&gt;
&lt;p&gt;So, distributed union types, they look a bit different. They have this conditional type where you just extend unknown, and everything extends unknown, so it looks kind of useless at first sight. But if you pass a union to this distributed union type now, what happens is actually that TypeScript will compute the record for each member of the union individually. So you will instead get a union of records, rather than a record of a union. So this union of records will then translate to a union of two different objects, one with &amp;quot;a&amp;quot; to string and one with &amp;quot;b&amp;quot; to string. So in a way, this T extends unknown condition distributes the unions of the type parameter to two different unions. That&amp;#39;s why they&amp;#39;re called distributed union types. This is something that isn&amp;#39;t apparent in the syntax, but it&amp;#39;s very fundamental to how union types work. And so, this is the second learning today. You have to think about union distribution. It&amp;#39;s very useful to have this feature, and at the same time it can be really painful if you don&amp;#39;t know about it. So, now we do know, so that&amp;#39;s nice.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s continue. For example, we&amp;#39;ve seen here that the distributed union type works becau– well, okay, no, we have to implement it first. Okay. We can do this implementation by saying, okay, we&amp;#39;ve learned that we can just do Q extends unknown, and then do one condition here and one condition there, and now, ctx.update is no longer never, but it&amp;#39;s actually a union type of both possibilities. Turns out, it still doesn&amp;#39;t really work, but this is not so much about union types. It&amp;#39;s just because if ctx.update has edited_message, then message is not present, so we can&amp;#39;t actually access .message. So we have to do this ugly workaround, which is another, a second ugly workaround which is another red flag that maybe our modelling is not a great as it could be. But at this point it works. Now, we can just access ctx.update.message and it just turns out to work.&lt;/p&gt;
&lt;p&gt;We did that for a while in grammY, but the problem with this is that it took like 7 GB of memory to compile the library and compilation would take 45 minutes. And you really don&amp;#39;t have that much time to wait 45 minutes every single time you want to compile. And so not only do we have this ugly workaround and the other ugly workaround up here, we also have very slow compilation times. And so, it really became a big problem when we saw errors like this appear. And TypeScript already tells us that there are no quick fixes available. So we have to completely reconsider how to model this API.&lt;/p&gt;
&lt;p&gt;Because union types are bad. Not generally, but like, at scale. Because what happens in this Extract? If you use Extract, and you have this message: object mapping, it will check assignability to the first member of the update union, and then it will check assignability to the second update union, and then it will check assignability to all the other parts of Update that I did not mention in this talk. And even worse, if we not listen for one update but like for &amp;quot;message&amp;quot; | &amp;quot;edited_message&amp;quot;, we get like this union of records and we have to check assignability between both things to message. And then we have to check assignability of both things to edited_message and so on and so forth.&lt;/p&gt;
&lt;p&gt;So we build up this huge quadratic table of assignability checks and, you know, if there are like more than a thousand filter queries in grammY and you can use several, like, maybe a dozen or so in the event handling, and there are, I don&amp;#39;t know, hundreds of types of messages, it makes a lot of sense why TypeScript gets so slow. It&amp;#39;s just an immense amount of computation that TypeScript has to perfom.&lt;/p&gt;
&lt;p&gt;So let&amp;#39;s just throw out unions entirely. And instead of using a union here, we&amp;#39;re just going to inline everything and say, okay, we just make it a good old interface with optional keys. And here as well. And make them optional again. Nice.&lt;/p&gt;
&lt;p&gt;Now, obviously, well, okay, the first thing we notice is that we no longer need the KeyOfUnion helper type. We can literally just say, okay, we wanna have a regular keyof Update and everything works as it should. And the next thing is that instead of having to do some weird extract logic, we can just say, okay, let&amp;#39;s throw this away entirely, and say okay, we have still a context object and we still know something else about the update. And all we really want to assert is that there&amp;#39;s a record of some FilterQuery type and it maps to object and this is known to be present. So, this is already everything you need to work with our new abstraction.&lt;/p&gt;
&lt;p&gt;And it will work here and there and it also works down here and we can even through away our weird workaround around the types and instead use regular idiomatic JavaScript and it all works out.&lt;/p&gt;
&lt;p&gt;So the downside of not using a union type is that if you look at ctx.update, it will still suggest you to use edited_message, even though we know we&amp;#39;re in a message handler. But it will know that edited_message is potentially absent. So that means that we can&amp;#39;t really get any runtime errors because whenever we would use this, it would force us to check for presence. But technically, the types are a little less accurate than before. And my point is that that&amp;#39;s fine.&lt;/p&gt;
&lt;p&gt;We should balance type performance and type accuracy. You don&amp;#39;t have to go crazy on accuracy if it impacts performance that negatively. So it&amp;#39;s not an end in itself to make your types as accurate as possible. You just have to make them usable and practical and there&amp;#39;s still very fancy stuff you can do without hurting performance too much and hurting usability too much.&lt;/p&gt;
&lt;p&gt;Alright. Let&amp;#39;s add yet another feature. We can see that we always have to access ctx.update and that is rather annoying. Instead, I would just like to do ctx.message. That&amp;#39;s pretty simple to implement. We just go to the Context type, define a getter, and say, okay, ctx.message just always returns this.update.message. Same thing for editedMessage. Easy.&lt;/p&gt;
&lt;p&gt;Notice how in the modelling of our JSON updates, we had to use like the snake case naming, but now that we are in JavaScript land, we can use idiomatic JavaScript names of camel casing. We can immediately see that even though we&amp;#39;re still in a message handler, TypeScript does not understand our implementation. It still thinks that ctx.message is potentially absent, even though it really can&amp;#39;t be.&lt;/p&gt;
&lt;p&gt;So we have to explicitly tell TypeScript, okay, we can intersect another record here, and be like, okay, not only is the update type (the property) narrowed down, but also the getter itself. This works, so that&amp;#39;s cool, but you notice that when you look at edited_message updates, then it will actually suggest ctx.edited_message and think that this is present. That is obviously wrong. The problem is that in our record, we just passed on the FilterQuery type directly to Record, so now it asserts this underscore naming, even though our getter really has the camel case naming and so this is just all wrong.&lt;/p&gt;
&lt;p&gt;And you could give up and say, okay, this is impossible to type, but we are in TypeScript land and we can do anything, so let&amp;#39;s just define a type that translates our snake to camel case. That is rather simple actually. Surprisingly simple. It just takes any string and checks if this string has an underscore in it. It it does, then we infer the left side of the string and then we also infer the right side of the string. If it does not have this underscore, we just return the string itself, but if it does, then we can construct a new string and say, okay, we take the left side again, and no underscore, and then we put the capitalised version of the right side here. That&amp;#39;s it.&lt;/p&gt;
&lt;p&gt;Now we can test this and say, okay, let&amp;#39;s say type X, whatever, is SnakeToCamelCase of &amp;#39;foo&amp;#39;. Very simple. Nice, that works correctly. Let&amp;#39;s say we want to use &amp;#39;foo_bar&amp;#39;. Nice, that also works correctly. Let&amp;#39;s try &amp;#39;foo_bar_baz&amp;#39;. And here you can see that it only actually substitutes the first underscore and not the rest, so we&amp;#39;ve seen that it does not recursively apply this logic to itself, so all we have to do really is to say, okay, we just add another SnakeToCamelCase definition inside here and now things work.&lt;/p&gt;
&lt;p&gt;What you&amp;#39;ve seen just now is that I defined three completely useless helper types with very concrete values that help me make sense of this rather weird-looking magic. It&amp;#39;s not a lot of code, but it is, like, you have to think about it for a second to understand it. And that is a very effective strategy to understand how types work and to develop them, too. So that&amp;#39;s the fourth learning for today, is like, inspecting intermediate types. That is, like, so useful. Instead of, like, looking at a huge complex type and being overwhelmed, actually just strip it down, try to put very concrete values into it and test with type inference and hovering how these things work. That is a very effective strategy to make sense of all this magic that you write.&lt;/p&gt;
&lt;p&gt;So that&amp;#39;s what we used in grammY and now we can actually see that our type works, too. We can apply SnakeToCamelCase here around our record (the keys) and it will tell us, okay, edited_message is absent. This doesn&amp;#39;t exist. And it even says, hey, “Did you mean editedMessage?” We did. It should be this.&lt;/p&gt;
&lt;p&gt;This has worked for quite a while actually very well. The logic in grammY is a lot more complicated but basically, it&amp;#39;s this. In the past three years, we&amp;#39;ve added more and more filter queries to grammY and we&amp;#39;ve crossed—just last month actually!—the threshold of more than a thousand of them. We just recently ran into an issue where we&amp;#39;d get like another case of union types that are too complex to represent and just stuff that is very hard to understand.&lt;/p&gt;
&lt;p&gt;You can&amp;#39;t always make sense of these things. If your library develops in complexity and everything grows and becomes more feature-rich, then the most effective strategy to find out why things break and how they work is to try it and see. This practical, iterative approach—just, you know, dive it and analyse why things work the way they work—that is basically the best advice I can give to anyone who wants to build complex types.&lt;/p&gt;
&lt;p&gt;All of the above four things are things that I only found out because I tested and tried. Even though types sometimes can feel like some academic wizardry or, I don&amp;#39;t know, you have to be some genius to write them, that is not actually the case. They are quite similar to how runtime code is written or like, I don&amp;#39;t know, how you do performance benchmarking. You have to just test it and measure it and see and try and narrow down the problems.&lt;/p&gt;
&lt;p&gt;If you only take one thing away from my talk, then it should be that you should balance type performance and type accuracy. I know it&amp;#39;s a lot of fun to go crazy on accuracy, but it comes at a cost. It comes at the cost of performance, it comes at the cost of understandability, and it&amp;#39;s important to have this in mind that accuracy is not everything, even though it can be a lot of fun.&lt;/p&gt;
&lt;p&gt;With that, I thank you very much for your attention. This is how you can reach me if you have further questions. I also put snapshots of what I just coded on GitHub so you can have a look at the repository. Otherwise, I will hand it back to Sia.&lt;/p&gt;
</content:encoded></item></channel></rss>