Top Banner
Haskell Internals Ramkumar Ramachandra FOSS.IN/2009 01 December 2009 Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 1 / 13
14

Haskell Internals

May 11, 2015

Download

Technology

Illustrates how various Haskell programs are converted to the Core language using graph reduction
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Haskell Internals

Haskell Internals

Ramkumar Ramachandra

FOSS.IN/2009

01 December 2009

Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 1 / 13

Page 2: Haskell Internals

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

Page 3: Haskell Internals

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

Page 4: Haskell Internals

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

Page 5: Haskell Internals

Part I: Thinking in Haskell

Re-think the problem in terms of folds

foldr :: (a -> b -> b) -> b -> [a] -> b

1 euler5 :: (Integral a) => a2 euler5 = foldr lcm 1 [1..20]3 where gcd a 0 = a4 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

Page 6: Haskell Internals

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,1015: 1,3,5,1521: 1,3,7,2128: 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

Page 7: Haskell Internals

Part I: Thinking in Haskell

Solve it in Haskell

filter :: (a -> Bool) -> [a] -> [a] map :: (a -> b) -> [a] -> [b]

1 euler12 :: (Integral a) => a2 euler12 = head $ filter ((> 500) . n_divisors) triangleSeries3 where triangleSeries = [div (n * (n + 1)) 2 | n <- [1..]]4 n_divisors n = product . map ((+1) . length) . primeGroups $ n5 primeGroups = group . (primeFactors n) . filterPrimes6 filterPrimes n = filter (\x -> n `mod ` x == 0) primes

Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 6 / 13

Page 8: Haskell Internals

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

Page 9: Haskell Internals

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

Page 10: Haskell Internals

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

Page 11: Haskell Internals

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

Page 12: Haskell Internals

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

Page 13: Haskell Internals

Conclusion

References

[1] Augustsson, L., and Johnsson, T. Parallel graph reduction with the (v , g)-machine. InFPCA ’89: Proceedings of the fourth international conference on Functional programminglanguages 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 ProgrammingLanguages. 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 BachelorThesis.

Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 12 / 13

Page 14: Haskell Internals

Conclusion

Contact information

Ramkumar [email protected]://artagnon.com

Indian Institute of Technology, KharagpurPresentation source available on http://github.com/artagnon/foss.in

Ramkumar Ramachandra (FOSS.IN/2009) Haskell Internals 01 December 2009 13 / 13