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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Compile-Time Checked Truth Tables

72 点作者 chriskrycho将近 2 年前

5 条评论

tczMUFlmoNk超过 1 年前
Somewhat surprised that the author doesn&#x27;t go on to show how to reduce the truth table...<p><pre><code> let decide requiresApproval canUserApprove itemStatus = match requiresApproval, canUserApprove, itemStatus with | true, true, NotAvailable -&gt; Hidden | false, true, NotAvailable -&gt; Hidden | true, false, NotAvailable -&gt; Hidden | false, false, NotAvailable -&gt; Hidden | true, true, Available -&gt; ReadWrite | false, true, Available -&gt; Hidden | true, false, Available -&gt; ReadOnly | false, false, Available -&gt; Hidden | true, true, InUse -&gt; ReadOnly | false, true, InUse -&gt; Hidden | true, false, InUse -&gt; ReadOnly | false, false, InUse -&gt; Hidden </code></pre> ...to something like this:<p><pre><code> let decide requiresApproval canUserApprove itemStatus = match requiresApproval, canUserApprove, itemStatus with | _, _, NotAvailable -&gt; Hidden | false, _, _ -&gt; Hidden | true, true, Available -&gt; ReadWrite | true, _, _ -&gt; ReadOnly </code></pre> I would think that being able to see and effect this reduction is part of the benefit of writing out the whole table. It might not always be the right call to actually reduce it, but in this case (based on the parameter names) each of the branches seems to makes sense from a domain perspective, so why not do it?
评论 #37226209 未加载
munificent超过 1 年前
Just for kicks, here&#x27;s a Dart version:<p><pre><code> enum ItemStatus { notAvailable, available, inUse } enum FieldState { hidden, readOnly, readWrite } FieldState decide(bool requiresApproval, bool canUserApprove, ItemStatus itemStatus) =&gt; switch ((requiresApproval, canUserApprove, itemStatus)) { (true, true, ItemStatus.notAvailable) =&gt; FieldState.hidden, (false, true, ItemStatus.notAvailable) =&gt; FieldState.hidden, (true, false, ItemStatus.notAvailable) =&gt; FieldState.hidden, (false, false, ItemStatus.notAvailable) =&gt; FieldState.hidden, (true, true, ItemStatus.available ) =&gt; FieldState.readWrite, (false, true, ItemStatus.available ) =&gt; FieldState.hidden, (true, false, ItemStatus.available ) =&gt; FieldState.readOnly, (false, false, ItemStatus.available ) =&gt; FieldState.hidden, (true, true, ItemStatus.inUse ) =&gt; FieldState.readOnly, (false, true, ItemStatus.inUse ) =&gt; FieldState.hidden, (true, false, ItemStatus.inUse ) =&gt; FieldState.readOnly, (false, false, ItemStatus.inUse ) =&gt; FieldState.hidden, }; </code></pre> As with F# and Haskell, the compiler will tell you if you didn&#x27;t cover one of the combinations. In the case of Dart, we go ahead and make it a full compile-time error, not just a warning.<p>The cool thing about pattern matching over tuples with compile-time exhaustiveness checking is that you can start simplifying and collapsing rows and the compiler will let you know if you made a mistake.<p>I believe you can simplify the example here to:<p><pre><code> FieldState decide(bool requiresApproval, bool canUserApprove, ItemStatus itemStatus) =&gt; switch ((requiresApproval, canUserApprove, itemStatus)) { (_, _, ItemStatus.notAvailable) =&gt; FieldState.hidden, (false, _, ItemStatus.available ) =&gt; FieldState.hidden, (false, _, ItemStatus.inUse ) =&gt; FieldState.hidden, (true, false, ItemStatus.available ) =&gt; FieldState.readOnly, (true, _, ItemStatus.inUse ) =&gt; FieldState.readOnly, (true, true, ItemStatus.available ) =&gt; FieldState.readWrite, }; </code></pre> Doing that simplification is a useful exercise to then determine how to express the rules you&#x27;re modeling in a succinct way.
lemper超过 1 年前
good thing c# supports pattern match too. just this morning, I wrote something like this<p><pre><code> var thing = (enumA, enumB) switch { (EnumA.A,EnumB.A) =&gt; DoStuff(), _ =&gt; throw new Exception(), } </code></pre> suffice to say, c# is pretty ok, coming from ml family.
评论 #37226012 未加载
评论 #37229707 未加载
emmanueloga_超过 1 年前
I&#x27;ve built this sort of truth table in &quot;plain code&quot; and was always jealous of environments like embeddr, where things are a lot more readable and no manual alignment is required :-) [1].<p>Subtext [2] included a feature called &quot;schematic tables&quot; that is related to decision tables (demo [3], 8yr old video).<p>When I saw Subtext&#x27;s schematic tables I started to think of minimizing expressions, finding the complement, etc, to aid in writing correct boolean expressions. I think an algorithm like Quine–McCluskey [4] or espresso [5] could have applications to programming for refactoring boolean logic (normally these are used in electronics design).<p>I don&#x27;t know of any programming environment that could tell you if a boolean expression is a tautology or a contradiction... (I imagine a linter with SAT solver [6] powers could do this). In table form this would be obvious: a result column that is always true or always false. But then again you don&#x27;t want to write a truth table for EVERY boolean expression on a program. Would be nice to be able to go back and forth from table to expression, minimize the expression, check satisfiability, etc.<p>--<p>1: <a href="https:&#x2F;&#x2F;markusvoelter.medium.com&#x2F;the-evolution-of-decision-tables-80ce77bf984c" rel="nofollow noreferrer">https:&#x2F;&#x2F;markusvoelter.medium.com&#x2F;the-evolution-of-decision-t...</a><p>2: <a href="https:&#x2F;&#x2F;www.subtext-lang.org&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.subtext-lang.org&#x2F;</a><p>3: <a href="https:&#x2F;&#x2F;vimeo.com&#x2F;140738254" rel="nofollow noreferrer">https:&#x2F;&#x2F;vimeo.com&#x2F;140738254</a><p>4: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Quine%E2%80%93McCluskey_algorithm" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Quine%E2%80%93McCluskey_algori...</a><p>5: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Espresso_heuristic_logic_minimizer" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Espresso_heuristic_logic_minim...</a><p>6: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Boolean_satisfiability_problem" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Boolean_satisfiability_problem</a>
perryizgr8超过 1 年前
Just out of college and in my first job as a software engineer, I was excited to reduce the conditions in a piece of code using the K map reduction technique and then implement the resulting truth table with a switch case block. It was shot down pretty quickly by the reviewer because it was inscrutable and unverifiable to anyone without significant effort. Since then I&#x27;ve realised that such optimizations are the compiler&#x27;s job :)
评论 #37231903 未加载