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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Forall.js: Idris-like static types and invariant checks for JavaScript

63 点作者 LightMachine大约 8 年前

9 条评论

chriswarbo大约 8 年前
I would avoid using the terms &quot;type&quot; and &quot;check&quot; together for this, since &quot;type checking&quot; has a well established meaning (referred to as the &quot;classical&quot;&#x2F;&quot;logical&quot; approach in this README).<p>Random testing like this, as WaxProlix says, is reminiscent of QuickCheck; in that case you might replace &quot;type&quot; with &quot;property&quot;, since the phrase &quot;property checking&quot; is associated with this approach.<p>Alternatively, stick with &quot;type&quot; (or maybe &quot;contract&quot;? These terms are all very overloaded!), but use &quot;testing&quot; instead of &quot;checking&quot;.<p>Note that this approach is should be possible in Idris too, since we have equality types. For example, assuming we have some QuickCheck-style functions available, I could say:<p><pre><code> someProp : Foo -&gt; Bar -&gt; Bool someProp x y = whateverExpressionWeLike somePropCheck : quickCheck someProp = True somePropCheck = refl </code></pre> This will only type-check (in the traditional sense) if `quickCheck someProp` evaluates to `True`; otherwise `somePropCheck` will be ill-typed. `someProp` and `somePropCheck` have no impact at runtime, since they&#x27;re never called from anywhere (they could be encapsulated privately in a module too).<p>`quickCheck` can be a normal function; it&#x27;s simple enough to generate &quot;random&quot; values in a pure way (e.g. Mersenne Twister with some arbitrary seed), although that would lose one of the features of property checking: each time it runs, we explore more of the possible inputs, and hence gain more confidence. Since runtime guarantees don&#x27;t depend on the particular values generated by `quickCheck`, it&#x27;s still &quot;morally pure&quot; even if implemented in an impure way; hence we could use a mechanism similar to &quot;type providers&quot; to impurely generate random values, and hence regain that feature.
评论 #14170760 未加载
phpnode大约 8 年前
There&#x27;s a fair bit of work in this area that leverages Flow type syntax, e.g:<p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;tcomb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;tcomb</a><p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts</a> (for typescript)<p>- <a href="https:&#x2F;&#x2F;github.com&#x2F;codemix&#x2F;flow-runtime" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;codemix&#x2F;flow-runtime</a> (i wrote this)<p>And another alternative is to use Design by Contract, like Eiffel:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;codemix&#x2F;babel-plugin-contracts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;codemix&#x2F;babel-plugin-contracts</a> (also me)
评论 #14170732 未加载
jart大约 8 年前
Closure Compiler already does this. Most JavaScript at Google is written with static types in JSDoc. Closure Compiler also has a DSL (sort of like template metaprogramming) where complicated generic type transitions can be defined with functional expressions.<p>Examples: <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;d1c1673d1d891616144f1f5d0d9642dd1e232612&#x2F;externs&#x2F;es6.js#L1110" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;d1c1673d1d89...</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;55cf43ee31e80d89d7087af65b5542aa63987874&#x2F;contrib&#x2F;externs&#x2F;angular-1.3-q_templated.js#L98" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;55cf43ee31e8...</a><p>Documentation: <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;e7954defc514920469c79349e4742fca4c82fd58&#x2F;src&#x2F;com&#x2F;google&#x2F;javascript&#x2F;jscomp&#x2F;parsing&#x2F;TypeTransformationParser.java#L53" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;e7954defc514...</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;b8d8d1d6c6d245a3df57feedc511f2e849b01932&#x2F;test&#x2F;com&#x2F;google&#x2F;javascript&#x2F;jscomp&#x2F;parsing&#x2F;JsDocInfoParserTest.java#L3178" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;b8d8d1d6c6d2...</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;8829c165b30fdb1937ce7f4285d23ee38ebe3535&#x2F;test&#x2F;com&#x2F;google&#x2F;javascript&#x2F;jscomp&#x2F;TypeTransformationTest.java" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;closure-compiler&#x2F;blob&#x2F;8829c165b30f...</a><p>Welcome to what happens when a company hires PhDs to write web pages.
indolering大约 8 年前
&gt; the catch is that it isn&#x27;t doing classical (logical) type-checking, but, instead, it &quot;type-checks&quot; by inspecting with random samples.<p>So it&#x27;s almost nothing like Idris, which is geared towards formal verification.<p>All of these unsound type systems make development and refactoring easier, but you don&#x27;t really know what type checks are being done at a lower level.<p>TS* has (had) an untrusted type for input from potentially hostile adversaries. I would have preferred to do high-assurance client side code development in that, but it&#x27;s no longer maintained.
setra大约 8 年前
This is really not like the Idris type system at all. Its not even really like the Haskell type system. This is basically doing the same thing as the Haskell quickcheck library but running it at compile time. AKA just testing a function with a few values.
评论 #14170698 未加载
WaxProlix大约 8 年前
Not trying to be a downer on the creator, because it&#x27;s very cool, but I think the best thing that can come from libraries like this is adoption into Typescript. Typing is really better suited to being a part of the language and not a facet of libraries (or <i>is</i> it - an interesting thought there, the ability to import type systems).
评论 #14171131 未加载
评论 #14170840 未加载
didibus大约 8 年前
I&#x27;m confused. Does this actually statically checks, or is this testing?<p>Like, does it tag everything with type info and walk the code statically without executing it and checks that all connections respect their types?<p>It seems to me it does that for non dependent types, and then it does generative testing for the dependent types? And can also add runtime contracts for them?
be5invis大约 8 年前
So where is the famous Idris elaborator? Where are the universes? Where’s inductive types? Where are they?
sunesimonsen大约 8 年前
An alternative that is much easier to adopt, is to use unexpected-check <a href="http:&#x2F;&#x2F;unexpected.js.org&#x2F;unexpected-check&#x2F;" rel="nofollow">http:&#x2F;&#x2F;unexpected.js.org&#x2F;unexpected-check&#x2F;</a>