TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: What's the word for when your type 'compactly' represents your state?

6 点作者 sidedishes超过 3 年前
Hi HN, types-ish &#x2F; FP-ish question. I&#x27;ve asked around and not found a good answer so I wanted to ask here.<p>When I represent a system&#x27;s state, I am often searching for a type that neatly bijects to it. A dumb example is maybe, with a stoplight I might use a tagged union with 3 states (Red | Yellow | Green) over a struct with 3 booleans isRed, isYellow, isGreen, because the latter admits invalid states. A more realistic example is say, in a platformer when I see a boolean isJumping (in the air) and isFalling (in the air and also falling) when a union of (Grounded | Rising | Falling) might be more compact.<p>Likewise I might want to avoid a type having multiple values representing the same state. If I have a time series for temperature from a thermometer that&#x27;s only sometimes on, I might prefer a list&lt;optional&lt;int&gt;&gt; over a list&lt;pair&lt;bool,int&gt;&gt; where the bool is the on state, since the int doesn&#x27;t matter if when it&#x27;s off.<p>I&#x27;m not saying that the compact representation is always better but there&#x27;s certainly some utility or at least aesthetic quality. Is there a word for this? The words I&#x27;ve been using here have just been pulled out of the air.

4 条评论

mathematically超过 3 年前
In category theory this is called an initial algebra for a signature. It is the minimal structure that satisfies some specification and maps uniquely to every other structure that satisfies the same specification but it&#x27;s not clear if that&#x27;s what you&#x27;re looking for: <a href="http:&#x2F;&#x2F;tunes.org&#x2F;wiki&#x2F;initiality_20and_20finality.html" rel="nofollow">http:&#x2F;&#x2F;tunes.org&#x2F;wiki&#x2F;initiality_20and_20finality.html</a>.
shoo超过 3 年前
Compact is a fine description. If you can pin down exactly what benefit you expect a more compact representation to offer, that might suggest a word that better communicates the intent of the encoding.<p>One potential benefit is that the number of invalid states that can be represented is reduced or eliminated. If a compact choice of type prevents the representation of invalid states, then that chops away some useless part of the state space you would otherwise need to carefully guard against with validation logic and test cases. Especially useful for structures that can potentially be edited or transformed outside of your control in ways that do not preserve the invariants you have in mind (&quot;a stoplight value is always exactly one of the three colours&quot;).<p>Another potential benefit is that the number of bits needed to encode the values may be reduced. This probably doesn&#x27;t matter unless you have extreme memory limitations or are writing a game engine and trying to squeeze the most work out of memory bandwidth by avoiding wasting the cache on bytes that don&#x27;t contain information.<p>Yet another potential benefit (if no invalid states can be represented) would mean that if the value of two representations are different, then they correspond to two distinct states in your state space.<p>There are probably some settings where this kind of encoding is counterproductive. E.g. when fitting a linear regression model to predict a response from the input state, if there is no ordering between your states then it might be more natural to model the response variable as a function of n binary indicator variables instead of a single enum variable taking n integer values.
ttymck超过 3 年前
What you&#x27;re describing sounds a lot like Functional Domain Modeling[0]<p>You mention &quot;compactness&quot; and &quot;invalid states&quot;. FDM places great emphasis on ensuring your data model cannot represent illegal&#x2F;impossible states. I think this is the essence of what you are describing, and the &quot;compactness&quot; is the result.<p>[0] <a href="https:&#x2F;&#x2F;www.47deg.com&#x2F;blog&#x2F;functional-domain-modeling&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.47deg.com&#x2F;blog&#x2F;functional-domain-modeling&#x2F;</a>
gradschool超过 3 年前
Your question is related to the concept of full abstraction in programming language semantics. This page [1] is a more trustworthy explanation of it than mine. TL;DR: if you picture a set whose elements are program fragments and you construct another set whose elements are &quot;meanings&quot; of program fragments (according to you), and you cleverly arrange for a bijective (one-to-one) mapping between those sets (in other words, one that associates a unique meaning with every fragment and vice-versa), then you can say your semantic model is fully abstract. I don&#x27;t know if anyone uses the term in reference to types, but maybe you could start a trend.<p>[1] <a href="https:&#x2F;&#x2F;plato.stanford.edu&#x2F;entries&#x2F;games-abstraction&#x2F;#ProgEquiFullAbst" rel="nofollow">https:&#x2F;&#x2F;plato.stanford.edu&#x2F;entries&#x2F;games-abstraction&#x2F;#ProgEq...</a>