11/30

Comparative Programming Languages

Haskell

Prolog

Language structures, characteristics

 

Paradigms:

  • functional
  • object-oriented
  • logic
  • imperative
  • declarative

Compiled vs. Interpreted:

  • Compiler
  • Interpreter

Typing

  • Static
  • Dynamic
  • duck typing
  • strict typing
  • type inference (haskell)
  • type checking

Binding

  • Static
  • Dynamic

Memory Management

  • garbage collection
  • manual memory management

Concurrency:

  • Parallelism
  • threads
  • sharing memory
  • processes

Portability

Efficiency (Programmer, Computation)

Closures

  • First-class functions
  • First class citizen

Mutability (haskell is immutable)

Domain Specific Languages

Mixed Language Programming

 

Haskell

  • Archetypal Functional Programming Language
  • Recursion => tail recursion
  • Pattern matching
  • lists, list comprehension
  • let and where
  • Types, user defined types
  • partial functions
  • Currying
    • works on one argument at a time
    • every function takes one argument (return other functions to deal with other arguments)
  • curry, uncurry
  • lazy evaluation
  • monads
    • IO
    • Random #
    • functional purity

 

f x = 2 * (x + x)

(*) 2 $ x + x

 

foldl, foldl1 => returns a value

lambda notation

map => returns list

zipWidth

 

 

 

11/28

Context-free grammar

S – ab

S – A

A – a

A – b

 

CAPS: non-terminal (can’t stop till all gone)

lowercase: terminal symbols

 

S => ab, a, b

  1. – ab
  2. – A
    – a
  3. – A

    – b

 

parse.pl

> parse([s(3)], S).

> parse([a(3)], S).

> parse([b(3)], S).

> parse([c(3)], S).

 

parse([], []).

parse([H|T],[H|T1]) :-

integer(H),

parse( T, T1 ).

parse([H|T], Result) :-

rep( H, H1 ),

append( H1, T, Newlist), //Concatenation

parse( Newlist, Result).

 

 

List-doubler

listDbl( [], [] ).

listDbl( [H|T], [H1|T1] ):-

H1 is 2*H,

listDbl(T, T1).

 

> listDbl([], S).

> listDbl([1], S).

> listDbl([1, -3, 4, 5], S).

 

 

H99

 

11/26

Specific Domain Language Examples:

  • Awk:
    • process text files
    • unix/linux command line tool
    • used in conjunction with others
      • e.g. grep
  • VHDL:
    • VHSIC hardware description language
    • design and simulate hardware/circuits
    • can test virtually before building
  • HTML:
    • mark-up for web pages
  • Maple/Mathematica/Matlab:
    • mathematical computation
    • include visualization e.g. plotting
    • often used via interpreter
  • SQL
    • relational database management
    • several dialects
  • R / S
    • statistical computing languages
    • often via interpreter
    • R includes graphics and visualization
  • latex:
    • markup language for generating text documents
    • not WYSIWYG => little more work to learn
    • high quality publication
    • easy to change layout style

Using DSLs

  • use them on their own
  • or, with other languages
    • many have bindings to other programming languages
  • ODBC(windows)-JDBC(java)
    • bridge using SQL inside Java
  • Advantage: use a DSL where it is strong
  • Disadvantage:
    • code can got very complicated
    • can be hard to understand
    • easy to introduce security holes
      • pass strings poorly can lead to an injection attack
  • can use them in conjunction with other languages and/or applications
    • latex => temp
    • PDFLatex => pdf
    • PDF Viewers to view
  • Fragile
    • might not be portable between environments
    • each application could change separately
  • blurry line between DSLs and libraries
    • inference engine (library) vs Prolog
    • sometimes you have the choice: both are available.
  • Libraries are often easier to use
    • flexibility & power of a general purpose language
    • specific functionality of library
    • easier to set up projects and compile
  • At some point, a separate DSL makes more sense (e.g. SQL).

Mix Language Programming

  • some languages are great for developments
  • some languages are very fast in terms of processing
  • working in one language, but you want to use a library available in another language
  • interprocess communication
    • sockets, network calls, message passing
    • this can be slow and messy
  • compilers can sometimes compile different languages to object code then link them together
  • multi-threading <- requires that all can run in the same execution environment/VM
    • SWIG: simplified wrapper and interface generator
    • allows access to C/C++ libraries from Python, Java, Ruby, Perl, etc.
  1. Write everything in dev-friendly language such as Java or Python (only worry about big O)
  2. If slow => use a profiler -> will tell which code is slow
  3. Optimize that part of slow code
  4. Rewrite that part in C and call it using SWIG
  5. test it
  6. If the slow functions are pure function => try parallelize it.
  7. Look at clustering tools if necessary

11/23

Domain Specific Language

  • dedicated to a problem domain, a representation technique or a solution technique

 

Advantage:

  • more efficient for specific problems
  • very clear
    • code that documents itself
    • the code is written at the level/focus/vocabulary of the domain
  • easier to use, easier to train
  • control of how it’s used
    • ensures safety
  • involve domain experts more
    • less intimidating
    • validate
    • modify

 

Disadvantages:

  • Investment in building, teaching and maintaining the language!!!
  • Documentation!!!
  • balance between general use and specialization is difficult
  • making it useful
  • vender lock-in/lock-out
  • re-usability
  • redundancy
  • potential loss of processing efficiency!!!
    • memory
  • flexibility
  • transferability

11/21

———————

bw.pl

> restart.

> put_on2(c,a).

> listing(on/2), list(move/3).

> put_on3(c, a, [on(a,b),on(b,c), on(c,table)],S).

> start(X), put_on3(c,a,X,S).

———————

Logic Programming

  • Not really a general purpose technique
    • difficult to do the things easier in other programming paradigms
  • Some things are easier:
    • implications
    • backtracking is built in
    • pattern matching -> fining possible solutions
  • Things there are hard:
    • strings manipulations
    • not much support for graphics, etc.
    • Math (other than the most simple stuff)
  • In general, think of logic programming as an option for when it is appropriate
  • Bridge between Prolog and another languages
  • Non-Prolog tools
    • inference engines
      • use implications to prove facts
      • very similar in effect to prolog
    • available for other kinds of logic => Bayesian Logic
  • Mix

Domain Specific Languages

We’ve been looking at general purpose programming languages

  • designed to be used for many problems
    • for some, almost any conceivable programming problem
  • implies Turing completeness
    • equivalent to a Turing machine
    • in theory, can solve any problems that another Turing -complicate language can

Domain specific languages are designed to solve one kind of problem very well.

  • not necessarily programming language in the traditional sense
  • output can be a technical design, graphical objects, data, etc.
  • Can be very useful when you have a problem that fits