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

 

 

 

Advertisements

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

11/16

-------- sample 1114.pl ----------
> contains(a.leaf(X)).
> contains(a, tree(b,  leaf(X), leaf(Y))).

> contains (a, X).
        a
      /   
     ...  ...

       ...
      /   
     a    ...
   /   
  ...  ...

----------------------------------

---------- bw.pl -----------------
%:- dynamic(on/2).
%on (a, b).
%on (b, c).
%on (c, table).

restart :- retractall(on(_,_)),
    retractall(move(_,_,_)),
    assert( on(a,b) ), assert( on(b,c) ), assert( on(c,table) ).

clear (table).
clear(X) :- + on (_,X).


% preconditions
% retract anything changing
% assert new changes

put_on(X, Y) :-
    X = table,
    X = Y,
    on(X,Z),
    clear(X),
    clear(Y),
    retract( on(X,Z) ),
    assert( on(X,Y) ),
    assert( move(X,Z,Y) ).    

put_on2(X,Y) :- on(X,Y).
put_on2(X,Y) :-
    + on(X,Y),
    X = table,
    X = Y,
    on(X,y),
    clear2(X),
    clear2(Y),
    on(X,Z),
    retract( on(X,Z) ),
    assert( on(X,Y) ),
    assert( move(X,Z,Y) ).

clear2(table).
clear2(X) :- x on(_,X).
clear2(x) :-
    X + table,
    on(Y,X),
    clear2(Y),
    retract( on(Y,X) ),
    assert( on(Y, table) ),
    assert( move(Y, X, table) ).




> on(a, b).
> put_on(a,table).
> listing(on), listing(move). <- should print the table



% after adding restart and commenting
> restart.



11/14

------ cutpr.pl ---------
p(X,Y) :- a(X), b(Y).
p(X,Y) :- a(X), !, b(Y).

p(X,Y) :- X = Y, c(X).

a(1).
a(2).
a(3).

b(1).
b(2).
b(3).

c(1).
c(2).
------------------------

-------- allAns.pl ------------
> bagof(X,q(X,Y,Z), Bag).
> setof(Y,q(X,Y,Z), Bag).
> findall(Y,q(X,Y,Z), Bag). = bagof(X,X^Z^q(X,Y,Z), Bag).
> setof(X,X^Z^q(X,Y,Z), Bag).
> finall(X,(qX,Y,Z), X>1), Bag).
-------------------------------

bagof(Target, Goal, Bag)
setof(Target, Goal, Bag)
findall(Target, Goal, Bag)

bagof(X, q(X,Y,Z), Bag).
Y=1, Z=4, Bag=[3,2]. <- Saying for Y=1, Z=4, the possible values of X are 3 and 2

setof returns a set (no duplicates, ordered)

findall finds all legal values for the target
findall(X, q(X,Y,Z), Bag).
Bag = [1,3,3,1,2].

we can use existential variables to generalize or search.
setof(X, Y^Z^q(X,Y,Z), Bag).
Bag = [1,2,3].
Y^Z^q(X,Y,Z) => means there exists a Y such that there exists a Z such that q(X,Y,Z) is true.
Existential variables can only be used here!


-------- write.pl -----------
test(X) :- write('I noticed you typed '), write(X), nl, !.

factorial(1, 1) :- write(1), !.
factorial(N, F) :- write(N), nl, N1 is N - 1, factorial(N1, F1), F is F1*N.
-----------------------------



Data Structures
 abc, 1.5, 'An Atom', [abc,def,ghi], a(1)
 date (2012,11,14) <- don't need to declare a data type in prolog
 oneYearLater(date(Y,M,D), date(Y1,M,D)) :- Y1 is Y+1.
 getYear (date(Y,_,_), Y).

 pattern matching OK!
 date(2012, M, D) <- matces any 2012 day.


-------- tree.pl ------------
contains(X,tree(X,_,_)).
contains(X,tree(_,R,_)) :- contains(X,R).
contains(X,tree(_,_,L)) :- contains(X,L).
contains(X,leaf(X)).
-----------------------------