CSC/ECE 517 Fall 2009/wiki2 6 hl: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 196: Line 196:
==References==
==References==


1. [http://enfranchisedmind.com/blog/posts/scala-not-functional/] <br />
1. http://enfranchisedmind.com/blog/posts/scala-not-functional - Scala is not a pure Functional language <br />
2. [http://www.scala-lang.org/node/25] <br />
2. http://www.scala-lang.org/node/25 - Scala is OO and Functional <br />
3. [http://www.artima.com/weblogs/viewpost.jsp?thread=163733] <br />
3. http://www.artima.com/weblogs/viewpost.jsp?thread=163733 - History of Scala <br />
4. [http://en.wikipedia.org/wiki/Java_Virtual_Machine] <br />
4. http://en.wikipedia.org/wiki/Java_Virtual_Machine - JVM <br />
5. [http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29] <br />
5. http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29 - Imperative Scala Fibonacci <br />
6. [http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29] <br />
6. http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29 - Functional Scala Fibonacci <br />
7. [http://fandev.org/doc/docIntro/WhyFan.html#functional] <br />
7. http://fandev.org/doc/docIntro/WhyFan.html#functional - Why Fan - Functional <br />
8. [http://www.fandev.org/sidewalk/topic/193] <br />
8. http://www.fandev.org/sidewalk/topic/193 - Fan Fibonacci <br />
9. [http://github.com/tarcieri/reia/blob/master/examples/fibonacci.re] <br />
9. http://github.com/tarcieri/reia/blob/master/examples/fibonacci.re - Reia Fibonacci <br />
10. [http://caml.inria.fr/pub/ml-archives/caml-list/2002/11/64c14acb90cb14bedb2cacb73338fb15.en.html] <br />
10. http://caml.inria.fr/pub/ml-archives/caml-list/2002/11/64c14acb90cb14bedb2cacb73338fb15.en.html - OCaml's SMP Thread Support <br />
11. [http://mbishop.esoteriq.org/weblog/?p=7] <br />
11. http://mbishop.esoteriq.org/weblog/?p=7 - F# Fibonacci <br />
12. [http://www.devx.com/enterprise/Article/39223] <br />
12. http://www.devx.com/enterprise/Article/39223 - F# Object example] <br />
13. [http://en.wikipedia.org/wiki/JVM#Support_for_dynamic_languages] <br />
13. http://en.wikipedia.org/wiki/JVM#Support_for_dynamic_languages - JVM and Dynamic Languages <br />
14. [http://tirania.org/blog/archive/2009/Feb-16.html] <br />
14. http://tirania.org/blog/archive/2009/Feb-16.html - Mono and Android <br />
15. http://en.wikipedia.org/wiki/Java_Virtual_Machine - JVM Usage Numbers <br />


==Definitions==
==Definitions==
Side Effects - Methods are said to have side effects if they change the internal state of objects.
'''Side Effects''' - Methods are said to have side effects if they change the internal state of objects. <br />
Immutable - Variables who's internal state can not be changed.  Important for threaded programming if multiple threads will be accessing the variable/object.
'''Immutable''' - Variables who's internal state can not be changed.  Important for threaded programming if multiple threads will be accessing the variable/object. <br />
Mutable - Internal state can be changed.  Not considered safe in threaded programming.
'''Mutable''' - Internal state can be changed.  Not considered safe in threaded programming. <br />
SMP Threading/Threading - Symmetric Multiprocessing threads are allowed to run concurrently on multiple CPUs/CPU cores.
'''SMP Threading/Threading''' - Symmetric Multiprocessing threads are allowed to run concurrently on multiple CPUs/CPU cores. <br />
Tail Recursion - recursion where the last operation of a function is a recursive call.  Functional Programming compiles are optimized to handle tail recursion to reduce the call stack space.
'''Tail Recursion''' - recursion where the last operation of a function is a recursive call.  Functional Programming compiles are optimized to handle tail recursion to reduce the call stack space. <br />
VM - Virtual Machine.  Special programming construct that sits between the Kernel and programs.  It acts like a physical machine but protects the underlying Kernel from malicious code.
'''VM - Virtual Machine'''.  Special programming construct that sits between the Kernel and programs.  It acts like a physical machine but protects the underlying Kernel from malicious code. <br />


==External Links==
==External Links==

Latest revision as of 12:59, 14 October 2009

Hybrid Programming Languages

Hybrid Programming Languages are multi-paradigm languages that not only support basic Object Oriented features but other features of different programming paradigms. Multi-paradigm languages have been around for a while (LISP 1958, C++ 1983) but recently there has been a push to develop true Multi-paradigm languages that bridge the OO/Functional Programming gap; the push has been spurred on by the advent of the multi-core CPU. Programmers can no longer expect single threaded performance to drastically improve with each new CPU release.

The hallmark of Object Oriented Programming is the abstraction of the problem into well defined classes and objects. The Functional Programming approach is more to describe the system as mathematical/logical abstractions that limit recalculation (side effects) on a given data set.

Scala

Scala or "Scalable Language" was developed by Martin Odersky of EPFL to be a both Object Oriented and Functional language at the same time. Contrary to popular belief the "Scalable" means scalable with the programmer and their skills, ie OO programmers can move to Functional programming and Functional programmers can move to OO approaches. Scala shouldn't be considered a "pure" functional language[1], it can be thought of as an pure "OO" language since every value is an object and every function is a value[2]. It is a statically typed language that is compiled into bytecode that is run on the Java VM with comparable speeds to Java.

Language Highlights

Design Considerations

Marin Oderskay's first language at EPFL was Funnel a language based upon Functional Nets. Funnel's compiler and runtime environment were developed in Java. He later admitted that Funnel "was a beautifully simple design", but the design minimalism didn't translate into a user friendly experience[3]. After designing Funnel, Marin Oderskay focused on incorporating "pure" Object Oriented and Functional programing into a user-friendly language, Scala. Scala also improves on the Java model by achieving pseudo multiple inheritance via Modular Mixin model.

The incorporation of both Object Oriented and Functional Programming paradigms into one language is extremely powerful. Functional languages with their pattern matching schemes allow for list manipulation and recursive functions to be written succinctly. Scala recently added tail recursion to its list of supported Functional constructs. Functional programming is also improved on by allowing for to define if a data object/value is mutable (changable) or immutable (const). Immutability allows for threads to share memory space without the fear that one thread will be changing that data while another is using it.

The choice of using the JVM as the base was an extremely wise one. The JVM allows for multiple concurrency models to be used (Native Threads and/or Actor Model) and it allows for Java's existing libraries to be utilized by the programmer. Library re-use is extremely beneficial since it can take years for common libraries to grow organically in new languages. The use of the JVM also means that Scala can potentially run on any device that has the JVM installed. Sun recently announced that the JVM was on 4.5 billion devices[4].

Code Example

Fibonacci Sequence (Imperative and Functional Tail Recursive)

Imperative Example[5]:

 def fib( n: Int) = {
   // initial values
   var a = 0    // inferred type: Int
   var b = 1    // inferred type: Int
   
   // function next
   // inferred result type: Pair[Int, Int]
   
   def next( a: Int, b: Int) = Pair( b, a + b) ;    
 
   var i = 0
   while( i < n) {
     
     // get result pair members by pattern matching
     val Pair( c, d) = next( a, b) ;
     
     // assign result
     a = c
     b = d
     
     //iterate
     i = i +1
   }
   // return 'a'
   a
 }

Functional Programming Tail Recursive[6]:

 def fib( n:Int) = fib_tr( n, 1, 0) 
 def fib_tr( n: Int, b: Int, a: Int): Int = n match {
   case 0 => a 
   case _ => fib_tr( n -1, a + b, b)
 }

Fan

Fan supports Object Oriented and Functional Programming paradigms. Fan and Scala are often compared and contrasted since they both run on the Java VM. Fan isn't just tied to the JVM though it also supports the .NET CLR (Common Language Run-time) and Javascript.

Language Highlights

  • OO and Functional
  • Portable: JVM, .NET CLR and Javascript
  • Modular Mixin Model
  • REST namespace
  • Concurrent (Native Threads and Actor Model)
  • Mutable and Immutable Types

Design Considerations

Fan was designed to be a general purpose language that is extremely portable; it is supported on the JVM, .NET CLR (Common Language Run-time environment) and Javascript. Javascript compilation allows Fan programmers to be run in the browser.

The Fan philosophy is based upon design simplicity. The Fan designers updated the Java APIs into larger more elegant classes that tried to remove the overhead that accumulated over the years. Fan has an interesting take on typing since it is "both" a statically and dynamically typed language. API returns are statically typed that establish an API contract that the compiler can enforce. Outside of contract enforced areas, the programmer is free to use dynamically typed constructs.

Like Scala, Fan has incorporated Functions into the language as first-class objects[7]. This allows for the language to support Closures and functions when Java doesn't. At this time it doesn't support tail recursion.

Fan has multiple concurrency models. Objects can be declared as immutable to allows the Objects to be thread safe or the Actor Model can be utilized to pass messages between concurrent processes.

Code Example

Fibonacci in Fan[8]

  static Float fib(Float x)
  {
    if ( x < 2.0)
      return 1.0
    else
      return (fib(x - 1.0) + fib(x - 2.0))
  }

Reia

Reia is an Object Oriented and Functional Programming that incorporates Ruby/Python-like syntax to the the Erlang BEAM VM. Reia is based upon Erlang's concurrent Actor model. Objects don't share a heap which requires communication between objects to be implemented through message passing.

Language Highlights

  • Functional and OO
  • Brings Python/Ruby syntax to the BEAM VM
  • Concurrent (Actor Model)
  • Objects don't share memory
  • Variables can be assigned multiple times
  • Hooks to access/write Erlang
  • Tail Recursion

Design Considerations

Reia was designed to correct some of Erlang's perceived shortcomings by updating its syntax and adding classes/objects to the language. Class support is needed since Erlang's most complex data structure is a list of tuples. Reia also allows for variables to be assigned multiple times in one code block (a = 1; a = 2).

Reia's concurrency model matches Erlang's Actor Model; lightweight threads are not supported. All objects are assigned their own memory heap which guarantees that they are not able to cause side effects in other objects. Object communication is achieved via interprocess communication (message passing).

Reia's pattern matching is unique as well since it allows functions to be overloaded based upon the values of the variables being passed into the functions (see Ficonacci example below). Its an interesting approach since the language keeps "known" syntax but it allows for extremely expressive functions be to be developed.

Code Example

Reia's Fibonaacci[9]

module Fibonacci
  def calc(0)
    0
  end
  def calc(1)
    1
  end
  def calc(n)
    calc(n - 1) + calc(n - 2)
  end
end

F#/OCaml

F# is the officially supported Functional Programming language for .NET with Objected Oriented features. Its a variant of the popular OCaml functional/object oriented language that was developed in 1996. OCaml itself is a variant the CAML language that added Objects to the CAML language, hence the name of Objective-Caml or OCaml.

Language Highlights

  • Functional and OO
  • Improved GC
  • Comparable speeds with C or C++
  • Scripting language
  • Allows for Immutable Types
  • Concurrent (Threads, Actor Model)

Design Considerations

F# is .NET's answer to the growing resurgence of Functional Programming languages. OCaml is a "fast" functional programming language that has comparable speeds to C# and C++ with mutable and immutable objects. F# has improved OCaml by "fixing" its Garbage Collection algorithm that allows for Symmetric Multiprocessing (SMP) threading; OCaml's GC doesn't allow for SMP threading[10].

Like the other languages F# runs on an established VM, .NET's CLR environment. This allows for F# to have access to existing .NET frameworks and libraries which allows easier programmer/program migration to the language. Since F# is based upon the .NET CLR it also supports .NET's concurrency models, SMP Threads and the Actor Model.

Code Example

Fibonacci in F#[11]

 let fib n =
   let rec fib_aux (n, a, b) =
       match (n, a, b) with
         | (0, a, b) -> a
         | _ -> fib_aux (n - 1, a + b, a)
   fib_aux (n, 0, 1)

F# Objects and usage [12]

 type Shape() =
   abstract Perimeter : float
 type Circle(r, x: int, y: int) =
   inherit Shape()
   override self.Perimeter =
       Convert.ToDouble (2*r) * System.Math.PI
 type Rectangle(x1, y1, x2, y2) =
   inherit Shape()
   static member Square(x, y, a) = DerivedRectangle(x, y, x+a, y+a)
   override self.Perimeter =
       2*(x2-x1)+2*(y2-y1) |> Convert.ToDouble
 let d = seq { for i in 1 .. 20
             -> if i % 3 = 0 then
                     Circle (i, i, i) :> Shape
                elif i % 3 = 1 then
                     Rectangle (20, 20, 20+i*2, 20+i) :> Shape
                else
                     Rectangle.Square (20, 20, i) :> Shape } |> Drawing

Language Simularities

When analyzing the creation of modern Hybrid Programming Languages the following criteria should be looked at:

  • Base Paradigm
  • Concurrency
  • Portability


Base Paradigm

Scala and Fan used the OO paradigm as their base and removed Java's primitive types; Functions are values which are handled as Object methods/values. Reia and F# approach the problem from the Functional Paradigm and move towards OO as a data abstraction. With the exclusion of F#, each of the languages core behaviors can be tied back to the VMs that they are running on (F# is excluded since it is part of the .NET framework).

Scala and Fan primarily use the Java VM which has certain limitations and features. The JVM instruction set only supports statically typed objects with the exception of some allowances for dynamic classes and methods[13] (ie Fan's inner scoping can be dynamic). The Reia sits on top of the Erlang BEAM VM which doesn't support lightweight threads; which is why only the Actor model is supported for concurrency.

Concurrency

To get the best performance out of modern multicore CPUs the language must address how multiple threads, processes should be run and handled by the language/VM.

All of the discussed languages allow for concurrency to be achieved via the Actor Model. The Actor Model is a form of message passing where each process/object communicates with each other via messages and the Actors don't share state (memory addresses). Other languages (C/C++/Fortran) have similar models via Message Passing Interface (MPI) packages.

F#, Fan and Scala support concurrency via threads as well. When thread support is incorporated it is import to allow for immutable types so that side effect free programming can safely be achieved. If immutable data types are not used then thread synchronization is important to prevent one thread from affecting another threads memory space.

Portability

As the definition of the Personal Computer becomes broader a language's portability becomes more important. Reia is bound to the Erlang BEAM VM which is bound to traditional PCs/Servers. F# is compatible with the .NET CLR which opens it up to PCs/Servers and the Android platform via Mono[14]. Scala and Fan are supported via the JVM which is on 4.5 billion devices[15]. Fan also is supported in the browser since it can be compiled into Javascript bytecode.

References

1. http://enfranchisedmind.com/blog/posts/scala-not-functional - Scala is not a pure Functional language
2. http://www.scala-lang.org/node/25 - Scala is OO and Functional
3. http://www.artima.com/weblogs/viewpost.jsp?thread=163733 - History of Scala
4. http://en.wikipedia.org/wiki/Java_Virtual_Machine - JVM
5. http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29 - Imperative Scala Fibonacci
6. http://en.literateprograms.org/Fibonacci_numbers_%28Scala%29 - Functional Scala Fibonacci
7. http://fandev.org/doc/docIntro/WhyFan.html#functional - Why Fan - Functional
8. http://www.fandev.org/sidewalk/topic/193 - Fan Fibonacci
9. http://github.com/tarcieri/reia/blob/master/examples/fibonacci.re - Reia Fibonacci
10. http://caml.inria.fr/pub/ml-archives/caml-list/2002/11/64c14acb90cb14bedb2cacb73338fb15.en.html - OCaml's SMP Thread Support
11. http://mbishop.esoteriq.org/weblog/?p=7 - F# Fibonacci
12. http://www.devx.com/enterprise/Article/39223 - F# Object example]
13. http://en.wikipedia.org/wiki/JVM#Support_for_dynamic_languages - JVM and Dynamic Languages
14. http://tirania.org/blog/archive/2009/Feb-16.html - Mono and Android
15. http://en.wikipedia.org/wiki/Java_Virtual_Machine - JVM Usage Numbers

Definitions

Side Effects - Methods are said to have side effects if they change the internal state of objects.
Immutable - Variables who's internal state can not be changed. Important for threaded programming if multiple threads will be accessing the variable/object.
Mutable - Internal state can be changed. Not considered safe in threaded programming.
SMP Threading/Threading - Symmetric Multiprocessing threads are allowed to run concurrently on multiple CPUs/CPU cores.
Tail Recursion - recursion where the last operation of a function is a recursive call. Functional Programming compiles are optimized to handle tail recursion to reduce the call stack space.
VM - Virtual Machine. Special programming construct that sits between the Kernel and programs. It acts like a physical machine but protects the underlying Kernel from malicious code.

External Links

Reia Homepage - http://wiki.reia-lang.org/wiki/Reia_Programming_Language
F# Research Page - http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/about.aspx
Scala Homepage - http://www.scala-lang.org/
Fan Homepage - http://www.fandev.org/
Scala Article - http://www.infoq.com/news/Scala--combing-the-best-of-Ruby-
Reia Blog - http://www.unlimitednovelty.com/
Scala Concurrent Programming - http://blog.objectmentor.com/articles/2008/08/14/the-seductions-of-scala-part-iii-concurrent-programming
Dr. Dobb's Article on Fan - http://www.ddj.com/architect/218600690
F# Concurrent Programming Article - http://strangelights.com/blog/archive/2007/09/29/1597.aspx
F# Working with Objects - http://www.devx.com/enterprise/Article/39223
F# Inheritance - http://msdn.microsoft.com/en-us/library/dd233225%28VS.100%29.aspx