简体   繁体   English

将值从Frege传递到Java并返回

[英]Passing Values From Frege to Java and Back

Suppose I have a dumb Frege function that constructs a pair of Num s. 假设我有一个哑哑Frege函数,它构造了一对Num

newPair :: (Num α, Num β) => α -> β -> (α, β)
newPair = (,)
-- alternatively -- newPair x y = (x, y)

Attempting to call this function from Java, however, a PreludeBase.CNum<α> and a PreludeBase.CNum<β> are demanded in addition to the expected Lazy<α> and Lazy<β> . 但是,尝试从Java调用此函数,除了期望的Lazy<α>Lazy<β>之外,还需要PreludeBase.CNum<α>PreludeBase.CNum<β> Lazy<β> Likewise with Show types, where 同样,对于Show类型,

showSomething :: (Show α) => α -> String
showSomething = show
-- alternatively -- showSomething x = show x

would require a PreludeBase.CShow<α> in addition to the expected parameter. 除预期参数外,还需要PreludeBase.CShow<α>

What is the proper way to pass constrained Frege objects to and from Java? 在Java中来回传递约束的Frege对象的正确方法是什么?

Good question, since this is not explained in the wiki yet. 很好的问题,因为这在Wiki中还没有解释。

As in all cases like this, I recommend to use the 由于在这种情况下,我建议使用

:java

command in the REPL. REPL中的命令。 For example: 例如:

frege> newPair 1 2.3
frege> :java

You will then get a window that contains among all active definitions one that corresponds to this call. 然后,您将获得一个窗口,其中包含与该调用相对应的所有活动定义。 A simple text search can help find the place where newPair is called. 简单的文本搜索可以帮助找到调用newPair的位置。 This should help to resolve such issues most of the time. 这应该在大多数时间有助于解决此类问题。

In your case, the relevant part would look like: 在您的情况下,相关部分如下所示:

Console.<Integer, Double>numPair(
   PreludeBase.INum_Int.it, 
   PreludeBase.IReal_Double.it, 
   Thunk.<Integer>lazy(1), 
   Thunk.<Double>lazy(2.3))

Here is a short overwiew about how type classes and instances are named and how you can get at them. 这是有关如何命名类型类和实例以及如何获取它们的简短概述。

module x.y.Z where
    class Xable where ...

This results in a Java-interface with the fully qualified name 这将导致Java接口具有完全限定的名称

x.y.Z.CXable

And this: 和这个:

module a.b.C where
    import x.y.Z
    data MyType ... = ....
    instance Xable MyType where ...

results in some class 导致某些班级

a.b.C.IXable_MyType  /* implements CXable<TMyType> */

If your instance definition does not have constraints themselves, there will be a singleton instance that you can use. 如果您的实例定义本身没有约束,则可以使用一个单例实例。

a.b.C.IXable_MyType.it

Otherwise, you need to construct a new instance by passing all constraints as argument to the constructor. 否则,您需要通过将所有约束作为参数传递给构造函数来构造新实例。 For example, the Show instance for 例如,针对的Show实例

Maybe Int

would look something like this: 看起来像这样:

new IShow_Maybe(IShow_Int.it)

since the instance head lists a constraint for the Maybe element type: 因为实例头列出了Maybe元素类型的约束:

instance Show a => Show (Maybe a)

Note that you need to know the actual type fully, you can't make a generic type class instance. 注意,您需要完全了解实际类型,不能创建泛型类型类实例。 This is never a problem in Frege itself, as all needed instances are passed to a polymorphic function from the caller. 在Frege中,这绝不是问题,因为所有需要的实例都从调用者传递到多态函数。 However, as it stands, we don't have constraints in native functions. 但是,就目前而言,我们在本机函数中没有约束。

Should you need something like this, you can achieve the functionality in most cases by just passing the function you wanted to call as argument. 如果需要这样的功能,则在大多数情况下,只需将要调用的函数作为参数传递就可以实现该功能。

For example, this doesn't work: 例如,这不起作用:

pure native myMethod :: Show a => a -> ...

but this should: 但这应该:

pure native myMethod :: (a -> String) -> a -> ....
myMethod show (Just 47)

The example java code above also reveals that it is not always as easy as described. 上面的示例Java代码还揭示了它并不总是像描述的​​那么容易。 For example, it so happens that the Double type doesn't have a separate Num instance, but just one for Real which is a subclass of Num . 例如,恰巧Double类型没有单独的Num实例,而对于Real是一个实例,它是Num的子类。 Unfortunately, only the compiler has the knowledge of what instances are actually present for some type, and which ones are implicit, that is, provided by an instance for a sub-class. 不幸的是,只有编译器才知道某种类型实际上存在哪些实例,哪些是隐式的,即由实例为子类提供的知识。 Again, the REPL is the best way to find this out. 同样,REPL是找出这一点的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM