简体   繁体   English

Scala和Frege(编程范式)之间的主要区别是什么?

[英]What are the main differences between Scala and Frege (in programming paradigms)?

Scala and Frege are both typed functional languages that target JVM. Scala和Frege都是针对JVM的类型化函数语言。

Frege is closer to Haskell, Scala has a more independent history. 弗雷格更接近哈斯克尔,斯卡拉有更独立的历史。

But if we don't look at syntactic differences, what are the differences in the allowed programming techniques, styles, concepts between the two? 但是如果我们不看语法差异,两者之间允许的编程技术,风格,概念有什么不同?

IMHO both are really good languages but with respect to paradigms, Scala does OO better than Frege but Frege does functional better than Scala. 恕我直言两者都是非常好的语言,但就范例而言,Scala比Frege更好OO,但Frege的功能比Scala更好。 With respect to differences, it comes down to mostly Haskell vs Scala since Frege is (or almost, see differences between Haskell and Frege here ) Haskell for the JVM. 对于分歧,把它归结为主要的Haskell VS斯卡拉由于弗雷格是(或差不多,看到Haskell和弗雷格之间的差异在这里 )为JVM哈斯克尔。

  1. Frege's type inference is global so we don't have to annotate types as often as we do in Scala (local inference). Frege的类型推断是全局的,因此我们不必像在Scala中那样频繁地注释类型(局部推理)。

  2. In Frege, modules are just namespaces for types and functions whereas Scala has better module system. 在Frege中,模块只是类型和函数的名称空间,而Scala具有更好的模块系统。 http://2013.flatmap.no/spiewak.html http://2013.flatmap.no/spiewak.html

  3. In Frege, functions are curried by default so there is no need for additional constructs for partial function application. 在Frege中,函数默认为curry,因此不需要为部分函数应用程序提供额外的构造。 Same goes for partial type constructor application. 部分类型构造函数应用程序也是如此。

  4. In Frege, there is no def vs val and everything is function. 在弗雷格,没有def vs val ,一切都是功能。 Hence functions are more first-class than Scala. 因此,函数比Scala更加一流。

  5. Frege has no sub-typing but the type system figures out the sub typing on native calls. Frege没有子类型,但类型系统会在本机调用中找出子类型。 For example, you can pass an ArrayList to a function which requires a Java List . 例如,您可以将ArrayList传递给需要Java List的函数。

    Since there is no subtyping, in Frege we cannot extend a Java class or implement an interface as of now (might be supported in future) so we need to have a Java class which would extend/implement but the method implementations would be passed from Frege as functions. 由于没有子类型,在Frege中我们现在无法扩展Java类或实现接口(将来可能会支持)所以我们需要一个可以扩展/实现的Java类,但方法实现将从Frege传递作为功​​能。

  6. From Scala, it is easy to call Java but in Frege, a Java class/method must be declared (Just the type and purity annotations) before use. 从Scala开始,很容易调用Java,但在Frege中,必须在使用之前声明Java类/方法(只是类型和纯度注释)。 For example, to use Java's LinkedList , 例如,要使用Java的LinkedList

     data LinkedList a = native java.util.LinkedList where native add :: Mutable s (LinkedList a) -> a -> ST s Bool native get :: Mutable s (LinkedList a) -> Int -> ST s (Maybe a) throws IndexOutOfBoundsException native new :: () -> STMutable s (LinkedList a) 

    Here since the functions mutate the object, they must be in ST monad. 这里由于函数改变了对象,它们必须在ST monad中。 Also note that here Frege also handles null returned from the get method since it is annotated with Maybe type. 另请注意,此处Frege还处理从get方法返回的null ,因为它使用Maybe类型进行注释。 The only way null can get through to your Frege program is through native interface since Frege doesn't have a notion of null. null可以通过本机接口进入你的Frege程序的唯一方法是因为Frege没有null的概念。

    Another example: pure native floor Math.floor :: Double -> Double 另一个例子: pure native floor Math.floor :: Double -> Double

    which states that the function is pure and hence the signature directly reflects the original Java signature without IO or ST . 表明该函数是纯函数,因此签名直接反映了没有IOST的原始Java签名。

  7. Frege has no variables as in Scala's var and the side effects are more explicit through types. Frege没有Scala的var变量,副作用在类型上更明确。 (Just no null , no var and explicit side effects make Frege more interesting, atleast for me. In a sense, Frege, just as Haskell, is a "fine imperative programming language", for the JVM!) (只是没有null ,没有var和明显的副作用使Frege更有趣,至少对我而言。从某种意义上说,Frege,就像Haskell一样,是JVM的“精确命令式编程语言”!)

  8. Being a Haskell dialect, Frege is more natural towards Functors, Applicatives, Monads and other functional "patterns" and has those in it's standard library whereas in Scala, you might need Scalaz. 作为Haskell方言,Frege对Functors,Applicatives,Monads和其他功能“模式”更自然,并且有标准库中的那些,而在Scala中,您可能需要Scalaz。

  9. Frege is lazy by default but strictness can be enabled where necessary through ! Frege默认是懒惰的,但必要时可以启用严格性! whereas Scala is strict by default but has lazy keyword for lazy evaluation. 而Scala在默认情况下是严格的,但是对于懒惰评估具有lazy关键字。

Nevertheless, being JVM languages, one language can benefit from other. 然而,作为JVM语言,一种语言可以从其他语言中受益。 I once ported an Akka example to Frege . 我曾经把一个阿卡的例子移植到弗雷格 In the end, it comes down to strictness, purity, functional, OO and type inference and how much they matter to you. 最后,它归结为严格性,纯度,功能,OO和类型推断以及它们对您有多重要。

Apart from syntactical issues, the biggest difference is in the type system and the execution model. 除语法问题外,最大的区别在于类型系统和执行模型。

As @senia already pointed out, scala is strict and not pure, which does not mean that you can't write pure functions (you can do that in C, too), just that the compiler won't enforce it. 正如@senia已经指出的那样,scala是严格的而不是纯粹的,这并不意味着你不能编写纯函数(你也可以在C中编写),只是编译器不会强制执行它。

Frege, OTOH is lazy and pure, which means that all impure effects are forced to live in the ST or IO monad. 弗雷格,OTOH是懒惰和纯粹的,这意味着所有不纯的效果都被迫生活在ST或IO monad中。 The type system is essential that of Haskell 2010, with type classes and additional higher rank function types. 类型系统对于Haskell 2010至关重要,具有类型类和更高级别的函数类型。 Type inference works program wide, the only exception are functions with higher rank types, where at least the polymorphic argument must be annotated. 类型推断在程序范围内工作,唯一的例外是具有更高级别类型的函数,其中至少必须对多态参数进行注释。 Here is an example: 这是一个例子:

both f xs ys = (f xs, f ys)

For this function, the compiler infers the type: 对于此函数,编译器推断类型:

both :: (α->β) -> α -> α -> (β, β)

Note that both xs and ys get the same type, because of the application of f . 请注意这两个xsys得到相同的类型,因为应用的f But now lets say we want use a polymorphic list function that we can use with differently typed xs and ys. 但现在假设我们想要使用多态列表函数,我们可以使用不同类型的xs和ys。 For example, we want to write: 例如,我们想写:

both reverse [1,2,3] ['a' .. 'z']

As it stands, this application would be in error, because the two lists have different element types and hence different types. 就目前而言,这个应用程序会出错,因为这两个列表具有不同的元素类型,因此具有不同的类型。 So the compiler would refuse the character list. 所以编译器会拒绝字符列表。

Fortunately, we can tell the compiler more precisly what we want with a type annotation: 幸运的是,我们可以通过类型注释更准确地告诉编译器我们想要什么:

both :: (forall e.[e] -> [e]) -> [a] -> [b] -> ([a], [b])

This tells the following: we will pass to both a function that does some list transformation but doesn't care about the list element type. 这告诉以下内容:我们将传递到both ,做一些改造名单,但不关心列表元素类型的函数。 Then we pass 2 lists with possibly different element types. 然后我们传递2个可能不同元素类型的列表。 And we get a tuple with our transformed lists back. 我们得到了一个包含我们转换后的列表的元组。 Note that the code of both needs not to be changed. 请注意, both的代码不需要更改。

Another way to achieve the same would be to write: 实现同样目的的另一种方法是写:

both (f :: forall e.[e]->[e]) xs ys = (f xs, f ys)

and the type checker infers the rest, namely that xs and ys must be lists, but can have different element types. 并且类型检查器推断其余部分,即xsys必须是列表,但可以具有不同的元素类型。

Scalas type system fully (to my knowledge) supports OO. Scalas类型系统完全(据我所知)支持OO。 While Frege supports it only partially with regard to types imported for Java, but does not support definition of own OO-like types. 虽然Frege仅部分支持为Java导入的类型,但不支持自己的OO类型的定义。

Hence, both languages support functional programming in a JVM environment, although in completly different niches. 因此,两种语言都支持JVM环境中的函数式编程,尽管它们完全不同。 A third niche is the dynamically typed one, where Clojure is king in the JVM world. 第三个利基是动态类型的,其中Clojure是JVM世界中的王者。

Maybe it's off topic, but Scala can be used to develop Android applications (*), but Frege hasn't been successfully used for that, yet.(**) IIRC, it's because interop with existing Java libraries is much easier in Scala than in Frege, among other issues. 也许这不是主题,但Scala可以用来开发Android应用程序(*),但是Frege还没有被成功地用于它。(**)IIRC,这是因为在Scala中与现有Java库的互操作要比在弗雷格,以及其他问题。

(*) Caveat emptor. (*) 买者自负。 I've only done small, example programs myself. 我自己只做了一些小的示例程序。

(**) Frege/Java mixes have been used for Android applications, but Frege-only applications are still not available, AFAIK. (**)Frege / Java混合已用于Android应用程序,但仅限Frege的应用程序仍无法使用,AFAIK。

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

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