Your SlideShare is downloading. ×
0
Haskell Internals

                                      Ramkumar Ramachandra

                                           ...
A Gentle Introduction to Haskell




1     A Gentle Introduction to Haskell




2     Part I: Thinking in Haskell




3   ...
A Gentle Introduction to Haskell


Why Haskell? What’s in it for you?




                                          Theore...
Part I: Thinking in Haskell


Solve a simple problem imperatively




PE 5: What is the smallest number divisible by each ...
Part I: Thinking in Haskell


Re-think the problem in terms of folds




                                      foldr :: ( ...
Part I: Thinking in Haskell


Pick a more challenging problem



What is the first triangle number to have over 500 divisor...
Part I: Thinking in Haskell


Solve it in Haskell




    filter :: ( a -> Bool ) -> [ a ] -> [ a ]                       ...
Part II: A peek into GHC


Behind the scenes




                                  Glasgow Haskell Compiler
              ...
Part II: A peek into GHC


What the core language looks like




                                  Local defintions
       ...
Part II: A peek into GHC


Apply graph reduction to the core language


square x = x * x ;
main = square ( square 3)




R...
Part II: A peek into GHC


Why bother with laziness




                                      euler14 :: Integer
         ...
Part II: A peek into GHC


A deeper look into the compiler




                                        G-Machine compiler
...
Conclusion


References




[1] Augustsson, L., and Johnsson, T. Parallel graph reduction with the (v , g)-machine. In
   ...
Conclusion


Contact information




Ramkumar Ramachandra
artagnon@gmail.com
http://artagnon.com
Indian Institute of Techn...
Upcoming SlideShare
Loading in...5
×

Haskell Internals

2,020

Published on

Illustrates how various Haskell programs are converted to the Core language using graph reduction

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,020
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
42
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Haskell Internals"

  1. 1. Haskell Internals Ramkumar Ramachandra FOSS.IN/2009 01 December 2009 Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 1 / 13
  2. 2. A Gentle Introduction to Haskell 1 A Gentle Introduction to Haskell 2 Part I: Thinking in Haskell 3 Part II: A peek into GHC 4 Conclusion Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 2 / 13
  3. 3. A Gentle Introduction to Haskell Why Haskell? What’s in it for you? Theoretical interest Ideas to apply in other places Real-world applications Concurrency: STM Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 2 / 13
  4. 4. Part I: Thinking in Haskell Solve a simple problem imperatively PE 5: What is the smallest number divisible by each of the numbers 1 to 20? 1 lcm_store = 1; 2 for ( i = 1; i <= 20; i ++) { 3 lcm_store = lcm ( lcm_store , i ) ; 4 } Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 3 / 13
  5. 5. Part I: Thinking in Haskell Re-think the problem in terms of folds foldr :: ( a -> b -> b ) -> b -> [ a ] -> b 1 euler5 :: ( Integral a ) = > a 2 euler5 = foldr lcm 1 [1..20] 3 where gcd a 0 = a 4 gcd a b = gcd b ( a ` mod ` b ) 5 lcm a b = ( a * b ) ` div ` gcd a b Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 4 / 13
  6. 6. Part I: Thinking in Haskell Pick a more challenging problem What is the first triangle number to have over 500 divisors? 10: 1 ,2 ,5 ,10 15: 1 ,3 ,5 ,15 21: 1 ,3 ,7 ,21 28: 1 ,2 ,4 ,7 ,14 ,28 28 = 2^2 + 7^1 (2+1) * (1+1) = 6 divisors Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 5 / 13
  7. 7. Part I: Thinking in Haskell Solve it in Haskell filter :: ( a -> Bool ) -> [ a ] -> [ a ] map :: ( a -> b ) -> [ a ] -> [ b ] 1 euler12 :: ( Integral a ) = > a 2 euler12 = head $ filter (( > 500) . n_divisors ) tri angleSe ries 3 where triangleSeries = [ div ( n * ( n + 1) ) 2 | n <- [1..]] 4 n_divisors n = product . map ((+1) . length ) . primeGroups $ n 5 primeGroups = group . ( primeFactors n ) . filterPrimes 6 filterPrimes n = filter ( x -> n ` mod ` x == 0) primes Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 6 / 13
  8. 8. Part II: A peek into GHC Behind the scenes Glasgow Haskell Compiler Parse everything into Core Language Use graph reduction Apply optimizations Compile Core Language into native code via GCC Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 7 / 13
  9. 9. Part II: A peek into GHC What the core language looks like Local defintions Lexical closures provided by let / letrec case for pattern matching Local function definitions (lambda abstractions) Structured data types provided by Pack Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 8 / 13
  10. 10. Part II: A peek into GHC Apply graph reduction to the core language square x = x * x ; main = square ( square 3) Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 9 / 13
  11. 11. Part II: A peek into GHC Why bother with laziness euler14 :: Integer -- Stack overflow ! euler14 = foldl1 ( pick_larger chain_length ) l -- [2 , 3 .. 999999] where chain_length = length . collatz_chain euler14 = foldl1 ( pick_larger snd ) collatzip -- [(2 ,2) ,(3 ,8) ,(4 ,3) ,(5 ,6) ,(6 ,9) ,(7 ,17) ] where collatzip = zip l chain_length Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 10 / 13
  12. 12. Part II: A peek into GHC A deeper look into the compiler G-Machine compiler TIM compiler Parallel G-machine compiler Lambda lifter Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 11 / 13
  13. 13. Conclusion References [1] Augustsson, L., and Johnsson, T. Parallel graph reduction with the (v , g)-machine. In FPCA ’89: Proceedings of the fourth international conference on Functional programming languages and computer architecture (New York, NY, USA, 1989), ACM, pp. 202–213. [2] Johnsson, T. Efficient compilation of lazy evaluation. SIGPLAN Not. 39, 4 (2004), 125–138. [3] Jones, S. L. P., and Lester, D. R. The Implementation of Functional Programming Languages. Prentice Hall, 1987. [4] Jones, S. L. P., and Lester, D. R. Impelementing Functional Languages: A Tutorial. Prentice Hall, 1992. [5] Terei, D. A. Low level virtual machine for glasgow haskell compiler, 2009. A Bachelor Thesis. Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 12 / 13
  14. 14. Conclusion Contact information Ramkumar Ramachandra artagnon@gmail.com http://artagnon.com Indian Institute of Technology, Kharagpur Presentation source available on http://github.com/artagnon/foss.in Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 13 / 13
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×