Haskell Internals
Upcoming SlideShare
Loading in...5
×
 

Haskell Internals

on

  • 2,562 views

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

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

Statistics

Views

Total Views
2,562
Views on SlideShare
2,419
Embed Views
143

Actions

Likes
0
Downloads
39
Comments
0

2 Embeds 143

http://funnel.fossmeet.in 139
http://www.slideshare.net 4

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Haskell Internals Haskell Internals Presentation Transcript

  • Haskell Internals Ramkumar Ramachandra FOSS.IN/2009 01 December 2009 Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 1 / 13
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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