Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Scala as a Declarative Language

Wird geladen in …3

Hier ansehen

1 von 46 Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)


Ähnlich wie Scala as a Declarative Language (20)

Aktuellste (20)


Scala as a Declarative Language

  1. 1. Introduction Suresh B Velagapudi, Ph.D Chief Technical Officer Mayera Software Solutions, LLC
  2. 2. Scalastic Warning “ Prolog Technology for temporal reasoning in relational databases” In spite of Ph.D for the above research work, I love to code in and talk about scala. I warn you up front that Your present coding style is at risk.
  3. 3. Scala as a Practical Declarative Language
  4. 4. Weltanschauung Based on Peace, Practicality and Productivity Not on Power, Pride, Prejudice or Prosperity etc.
  5. 5. In A Nutshell Declarative Programming Style in Scala makes software product development from Proof-of-Concept to Deployment enjoyable.
  6. 6. Abbreviations MOD – Martin ODersky DOS – Designer Of Scala SBE – Scala By Example ASS96 – Abelson Sussman Sussman MIL78 – Milner COW – Check Out Wikipedia DOP – A Discipline Of Programming 1976
  7. 7. Ramanujan scala> for {hardyTaxi <- List.range(1000,2000) j <- List.range(1,20) k <- List.range(1,20) l <- List.range(1,20) m <- List.range(1,20) if((j*j*j)+(k*k*k) == hardyTaxi && (l*l*l)+(m*m*m) == hardyTaxi) && j!= l && k!= m && j!= m} yield (i,j,k,l,m) res2: List[(Int, Int, Int, Int, Int)] = List((1729,1,12,9,10) The smallest number expressible as the sum of two cubes in two different ways.
  8. 8. Dijkstra on non-imperative in 1985 The simplest way to characterize the difference between imperative programming languages and non-imperative ones is that in the reasoning about imperative programming you have to be aware of the 'state' of the machine as is recorded in its memory.
  9. 9. An expression is non-imperative scala> if(12*12*12 + 1*1*1 == 10*10*10 + 9*9*9) 1729 else false res5: AnyVal = 1729 scala> if(12*12*12 + 1*1*1 == 10*10*10 + 9*9*9) 1729 else 0 res6: Int = 1729
  10. 10. Declarative Programs are Executable Specifications //from SBE page 72 def isPrime(n: Int) = List.range(2, n) forall (x => n % x != 0) //from SBE page 80 for { i <- List.range(1,10) j <- List.range(1, i) if isPrime(i+j) } yield (i, j)
  11. 11. Pythagorean tuple that sums to 1000 for {i <- List.range(1,1000) j <- List.range(1,1000) k <- List.range(1,1000) if (i+j+k==1000 && i*i + j*j == k*k) } yield (i, j, k) res6: List[(Int, Int, Int)] = List((200,375,425), (375,200,425))
  12. 12. Declarative Reading is Math Definition The definition of isPrime n , an integer , is, given a range of integers 2 thru n for all X, n modulo x is not zero def fact(n: Int): Int = n*fact(n-1) def isPrime(n: Int) = List.range(2, n) forall (x => n % x != 0) The definition of factorial n , an integer , is, n times factorial n -1
  13. 13. Separation of Concerns – Dijkstra We should not head for a mathematically correct program that is so badly engineered that is beyond salvation. --Dijkstra (1976) A Discipline of Programming
  14. 14. Recursion vs iteration //recursive procedure (described in ALGOL 60 ): procedure whiledo (condition, statement); begin if condition then begin statement; whiledo (condition, statement); end end Although correct, it hurts me. DOP Preface
  15. 15. A Generation Later // SBE page 6 def While (p: => Boolean) (s: => Unit) { if (p) { s ; While(p)(s) } } Later generations will pronounce as their judgment that in the last fifteen years, recursive solutions have been placed upon too high a pedestal. DOP 178
  16. 16. Thank You Samar Singh My math comp mentor of late seventies and early eighties.
  17. 17. Tail Recursive Functions //SBE page 18 def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b,a % b) /Answer to Exercise 4.6.1 of SBE page 19 def mf (x: Int): Int = myf (x, 1); def myf (x:Int, Acc: Int): Int = if(x==1) Acc else myf(x-1, Acc * x)
  18. 18. Horner's rule def horner(L:List[Int], X : Int): Int = { def horneri(L:List[Int], X : Int,Acc: Int): Int = { if(L.isEmpty) Acc else horneri(L.tail,X, Acc*X + L.head)} horneri(L,X,0)}
  19. 19. Internal Rate of Return internal rate of return for an investment is the discount rate that makes the net present value of the investment's cash flow stream equal to zero.
  20. 20. Hilbert's Tenth Problem Determination of the solvability of a Diophantine equation. Given a Diophantine equation with any number of unknown quantities and with rational integral numerical coefficients: To devise a process according to which it can be determined by a finite number of operations whether the equation is solvable in rational integers. A Diophantine equation is a polynomial equation where the variables are integers only.
  21. 21. An Oxymoron in 1989? Programming is essentially procedural. Real Engineers program by FORTRAN Arrays, or C pointers. Philosophers and Logicians make Declarations. Ha! Ha!
  22. 22. A Syllogism All humans are mortal. Socrates is human. So Socrates is mortal. Ha! Ha!
  23. 23. Modus Ponens in PROLOG //All humans are mortal. mortal(X) :- human(X). //Socrates is human. human( socrates ). human( suresh). ?- human(X). X = socrates ?- mortal(X). X = socrates ; X = suresh.
  24. 24. Second Order Predicate ?- setof(X,mortal(X),List). List = [socrates, suresh]
  25. 25. Concatenation of Lists The concatenation of an empty list with a list L is L. The Concatenation of a list having head H and tail T, with a list L, is a list with head H and a tail that is the concatenation of T and L. concatenation([], L, L). concatenation([H|T], L, [H|CTL]) :- concatenation(T,L, CTL). ?- concatenation([socrates,suresh],[addanki],L). L = [socrates,suresh,addanki] CTL H T L
  26. 26. Concatenation in Scala def concatenation[G](xs: List[G], ys: List[G]): List[G] = xs match { case List() => ys case H :: T => H :: concatenation(T,L) } CTL H T L
  27. 27. Predicate Logic as a Programming Language – Kowalski (1974) Program is a set of axioms. Computation is a constructive proof of a goal statement from the program
  28. 28. Thanks to Trevor Legall First theory book in 1984 Foundations of Logic Programming By J.W.Lloyd
  29. 29. Floating Point Number Crunching is not the only game in computingville. Combining qualitative reasoning and quantitative Techniques to improve mechanical and electrical Product design.
  30. 30. Newton's method def sqrt(x: Double) = { def sqrtIter(guess: Double, x: Double): Double = if (isGoodEnough(guess, x)) guess else sqrtIter(improve(guess, x), x) def improve(guess: Double, x: Double) = (guess + x / guess) / 2 def isGoodEnough(guess: Double, x: Double) = abs(square(guess) - x) < 0.001 sqrtIter(1.0, x) }
  31. 31. Algorithms + Data Structures = Programs That was my fault. I included it because I liked it, and that for two reasons: 1. (z /: xs) (op) looks like an abbreviation of a left leaning tree with a `z' on the lower left end (at least to me). I.e. something like op / op x1 / z x0 That's the tree I always draw when I explain fold left. 2. (z /: xs) has the operands in the ``right'' order.
  32. 32. Algorithm = Logic + Control The logic component determines the meaning of the algorithm. The control component only affects its efficiency.
  33. 33. Algorithm Logic component Abstract Procedure Definitions Definitions of data structures represented by terms Direction e.g. (top-down or bottom-up) Strategy for execution of procedure calls (e.g. sequential or parallel Strategy for investigating alternative procedures Control component Scheme for storing data represented relationally
  34. 34. Which Factorial Program? def fact(n: Int) = 1 to n reduceLeft (_*_) def fact(n: Int):Int = If (n == 0) 1 else n * fact(n-1) def mf(x: Int): Int = myf(x, 1); def myf(x:Int, Acc: Int): Int = if(x==1) Acc Else myf(x-1, Acc * x)
  35. 35. Which Reverse? //SBE page 68 def reverse[A](xs: List[A]): List[A] = xs match case Nil => Nil case x :: xs => reverse(xs) ::: List(x)} //SBE page 75 def reverse: List[A] = ((Nil: List[A]) /: this) {(xs, x) => x :: xs} //Is Tail Recursive Reverse Possible?
  36. 36. Tail Recursive Reverse def reverse[T](l: List[T]): List[T] = { def reverseR(result: List[T], curList: List[T]): List[T] = curList match { case Nil => result case h :: tail => reverseR(h :: result, tail) } reverseR(Nil, l) }
  37. 37. Sample Conversation with ELIZA I am unhappy. >> How long have you been unhappy? Six months. Can you help me. >> What makes you think I help you? You remind me of my father and brother >>Please tell me more about father. I like teasing father. >>Does anyone else in your family like teasing father? No, only me. >> Please go on
  38. 38. ELIZA in Scala def response(stimulus : List[String]) : Unit = stimulus match { case List(&quot;i&quot;,&quot;am&quot;, x) => println(&quot;How long have you been&quot; + x+ &quot;?&quot;) case List(x, &quot;you&quot;, y, &quot;me&quot;) => println(&quot;What makes you think I &quot; +y+&quot; you?&quot;) case List(&quot;i&quot;,&quot;feel&quot;, x) => println(&quot;Do you often feel that way?&quot;) case List(&quot;i&quot;, &quot;like&quot;, x) => println(&quot;Does any one else in your family like &quot; + x +&quot; ?&quot;) case _ => println(&quot;Please Go On&quot;) }
  39. 39. Machine Learning Suppose ELIZA wants to improve. Weka switched to java from prolog. SWeka will be in Scala.
  40. 40. Future of Scala Pessimistic Scenario: Functional freaks and java addicts fight to finish to scare away all scala enthusiasts. Optimistic Scenario: Complete Domination of the entire computing scene Most likely scenario: Solid apps come up. Steady impressive growth for next decade.
  41. 41. Pollak's cpp Principle Each morning that I sit down at the keyboard and start coding Scala, I get a feeling of calm, peace, and power. I know that I’m going to have another day of taking the ideas in my head and reducing them to a running computer program that will be fast and low in defects.
  42. 42. Pollak But most importantly, Scala taught me to program and reason about programming differently. I stopped thinking in terms of allocating buffers, structs, and objects, and of changing those pieces of memory. Instead, I learned to think about most of my programs as transforming input to output. This change in thinking has lead ( sic ) to lower defect rates, more modular code, and more testable code.
  43. 43. Tony Hoare in QCON 2009 One 
... • Software 
 component Of 
it. • Software 
most Dependable 
professions. • Because 
 of research – 
 programming – and
  44. 44. I have a dream One day..... My fellow scalastics' programs will be judged not by the colorful confusion of syntax but by the content of the character sequences with declarative semantics.
  45. 45. Remember SURESH S emantics independent of execution is the aim. U niversal quantification is for stating facts and rules. R ecursion is natural and efficient with TRO Call. E xtensive use of pattern matching is desirable. S tatic typing of generics avoids awful defects. H igher order functions help declarative programming.
  46. 46. The End Thank You

Hinweis der Redaktion

  • FORTRAN is the first language. 1969 Btech IITM Did Eco and finance too. Ms and Ph.D in computer science. Intend going from 50% to 100%
  • That is my thesis title.
  • Enjoy
  • Read it after 15 years. In stone age of computing, from one stone inscription to the other takes a long time. I do not blame myself. Dijkstra is ignorant of it too,