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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 120: Line 120:
   type Shape() =
   type Shape() =
     abstract Perimeter : float
     abstract Perimeter : float
   type Circle(r, x: int, y: int) =
   type Circle(r, x: int, y: int) =
     inherit Shape()
     inherit Shape()
     override self.Perimeter =
     override self.Perimeter =
         Convert.ToDouble (2*r) * System.Math.PI
         Convert.ToDouble (2*r) * System.Math.PI
   type Rectangle(x1, y1, x2, y2) =
   type Rectangle(x1, y1, x2, y2) =
     inherit Shape()
     inherit Shape()
Line 131: Line 129:
     override self.Perimeter =
     override self.Perimeter =
         2*(x2-x1)+2*(y2-y1) |> Convert.ToDouble
         2*(x2-x1)+2*(y2-y1) |> Convert.ToDouble
   let d = seq { for i in 1 .. 20
   let d = seq { for i in 1 .. 20
               -> if i % 3 = 0 then
               -> if i % 3 = 0 then

Revision as of 07:36, 9 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 [LINK TO SCALA BLOG], it can be thought of as an pure "OO" language since every value is an object and every function is a value [LINK TO SCALA INTRO PAGE]. It is a statically typed language that is compiled into bytecode that is compatible with the Java VM with comparable speeds to Java.

Language Highlights

  • OO and Functional
  • JVM
  • Concurrent (Native Threads and Actor Model)
  • Compatible with existing Java Libraries
  • Statically Typed
  • Mixin Model
  • Mutable and Immutable types
  • Tuple return support

Code Example

Fibonacci Sequence (Imperative and Functional Tail Recursive)

Imperative Example[LINK]:

 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[LINK]:

 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 an 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. Fan is a "loosely" typed language : method/function responses are statically typed but inner scoped variables are allowed to be dynamically typed. It's an interesting approach since a method's type contract should always be adhered to but within a method forced typing can reduce programming efficiency.

Language Highlights

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

Code Example

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. Unlike Erlang the Reia compiler allows for variables to be assigned multiple times with in one code block (a = 1; a = 2;).

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

Code Example

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, hence the name of Objective-Caml or OCaml. F# has extended OCaml by adding .NET support with improvements to the Garbage Collector (GC) that allows for Symmetric Multiprocessing (SMP) threading. OCaml's GC doesn't allow for SMP threading [LINK to OCAML GC]

Language Highlights

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

Code Example

Fib[1]

 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 [2]

 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 Creation and Simularities

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

  • Base Paradigm - Language Genesis
  • Expressiveness - How verbose must the programmer be
  • Extendability - How hard is it to add components
  • Concurrency - How hard is it to get performance
  • Portability - Where can I run the program


Base Paradigm

When the Hybrid Languages are created typically one paradigm is the base and the other paradigms are folded in. 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.

Expressiveness

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. Reia doesn't support threads since the BEAM VM doesn't. 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.

Extendability

All of the above languages have Functional Programming components that allow for

Portability