榆木-检查值的类型 [英] Elm - Check the Type of a value

查看:68
本文介绍了榆木-检查值的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在检查Elm中变量类型的函数?例如(repl):

Does a function exists that checks the Type of a variable in an Elm? For example (repl):

numberTwo = 2
.....
returnType numberTwo
"number" : String

这样做的动机是您正在使用 Signal.map [n] 时,通常会出现以下情况:并非要应用的函数的所有参数都是信号-然后通常必须提升使用 Signal.constant 发送信号-如果我可以检查此类参数的类型,则可以创建一个函数 Signal.allSigMap [n] 会自动将此类参数转换为Signals。

The motivation for this is that when you are using Signal.map[n] the situation usually arises that not all of the arguments to the function to be applied are Signals - they then usually have to be 'promoted' to Signals using Signal.constant - if I could check the type of such arguments, I could create a function Signal.allSigMap[n] that would turn such arguments into Signals automatically.

So

Signal.map2 grandFatherClock clockSignalElement (Signal.constant cabinetElement)

成为

Signal.allSigMap2 grandFatherClock clockSignalElement cabinetElement

虽然可能是不好的做法。请让我知道。

Could be bad practice though. Please let me know.

推荐答案

我将首先回应您使用 returnType 作为提升 Signal 必要类型的一种方式。这将导致 returnType 或其他函数实际上返回类型而不是 String ,因为没有其他函数使类型检查器满意的一种方法。

I'll respond first to your intention to use returnType as a way of promoting types as necessary to Signal. That would entail that returnType or some other function along the way actually return a type rather than a String since there's no other way to make the type checker happy.

这种功能在当今Elm中并不存在,也不可能存在。您需要的是可以检查值编译时间的类型,然后在该类型上运行函数的东西。

Such a function does not exist and cannot exist within Elm as it stands today. What you're asking for is something that can inspect the type of a value compile time and then run a function on that type.

要了解为什么它与Elm中当前存在的任何事物完全不同,我们假设存在这样的功能。

To see why this is radically different from anything that currently exists in Elm, let's assume such a function exists.

returnType : a -> ?

我们立即面临第一个问题,即 returnType 。让我们动手做这件事,说我们有一个用于所有类型的类型,称为 Type (有其自身的逻辑问题,我们将放在一边)。

We're immediately confronted with the first question of what exactly is the type of returnType. Let's handwave this and say we have a type for all types called Type (which has its own set of logical problems we'll leave aside).

returnType : a -> Type

我们实际上如何使用此功能?

How do we actually use this function? Presumably it'll be able to go in the type signature since it's returning a type.

 x : returnType 5

现在 是与Elm中其他所有东西完全不同的类型签名。有一个数字文字(和一个函数)!突然之间,您可以开始写这样的东西。

Now that is a type signature completely different from everything else in Elm. There's a numeric literal (and a function)! All of a sudden you can start to write things like this.

y = 5

x : returnType y
x = 6

这远远超出了Elm的字体系统所能做的。这种(令人兴奋且功能强大的)类型级别和值级别混合被称为依赖类型输入没有主流的完全依赖类型的语言;最接近主流的可能是Coq,Agda,Idris和ATS,它们都相当模糊。

That is way beyond what Elm's type system can do. That sort of (exciting and powerful) type level and value level mixing is known as dependent typing and no mainstream fully dependently typed languages exist; the closest things to mainstream are probably Coq, Agda, Idris, and ATS which are all fairly obscure.

至于字面上所说的函数 returnType:a-> String 可打印出代表值类型的字符串,在Elm中也是不可能的,尽管出于其他原因。这样的功能(它的应用程序在运行时发生)必须能够重构有关运行时值的类型信息,但是Elm的运行时值只是Javascript值;他们被剥夺了榆木类型。您将不得不从Javascript值重构原始Elm类型(并非总是可能的,因为不同的类型最终可能会变成相同的Javascript值),或者必须具有特殊的编译器支持。

As for the question as literally stated of having a function returnType : a -> String that prints out a string representing the type of a value, that's also not possible in Elm, although for other reasons. Such a function (which is something whose application occurs at runtime) must be able to reconstruct type information about a runtime value, but runtime values of Elm are just Javascript values; they've been stripped of their Elm types. You'll have to either reconstruct the original Elm type from the Javascript value (not always possible as different types can end up as the same Javascript value) or have special compiler support.

对于Elm REPL,则选择后者。整个REPL用Haskell编写,并利用了Elm类型在Haskell运行时中的实现方式。

In the case of the Elm REPL, the latter is chosen. The whole REPL is written in Haskell and takes of advantage of how Elm types are implemented in the Haskell runtime.

这篇关于榆木-检查值的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆