{"id":149863,"date":"1992-01-01T00:00:00","date_gmt":"1992-01-01T00:00:00","guid":{"rendered":"https:\/\/www.microsoft.com\/en-us\/research\/msr-research-item\/implementing-functional-languages-a-tutorial\/"},"modified":"2018-10-16T20:34:10","modified_gmt":"2018-10-17T03:34:10","slug":"implementing-functional-languages-a-tutorial","status":"publish","type":"msr-research-item","link":"https:\/\/www.microsoft.com\/en-us\/research\/publication\/implementing-functional-languages-a-tutorial\/","title":{"rendered":"Implementing functional languages: a tutorial"},"content":{"rendered":"<p>This book gives a practical approach to understanding implementations of non-strict functional languages using lazy graph reduction. The book is intended to be a source of practical labwork material, to help make functional-language implementations `come alive&#8217;, by helping the reader to develop, modify and experiment with some non-trivial compilers.<\/p>\n<p>The unusual aspect of the book is that it is meant to be <em>executed<\/em> as well as <em>read<\/em>. Rather than merely presenting an abstract description of each implementation technique, we present the code for a complete working prototype of each major method, and then work through a series of improvements to it. All of the code is available in machine-readable form.<\/p>\n<h2>Overview of the book<\/h2>\n<p>The principal content of the book is a series of implementations of a small functional language called the <em>Core language<\/em>. The Core language is designed to be as small as possible, so that it is easy to implement, but still rich enough to allow modern non-strict functional languages to be translated into it without losing efficiency. It is described in detail in Chapter 1, in which we also develop a parser and pretty-printer for the Core language.<\/p>\n<p>Appendix B contains a selection of Core-language programs for use as test programs thoughout the book.<\/p>\n<p>The main body of the book consists of four distinct implementations of the Core language.<\/p>\n<ul>\n<li>Chapter 2 describes the most direct implementation, based on <em>template instantiation<\/em>.<\/li>\n<li>Chapter 3 introduces the <em>G-machine<\/em>, and shows how to compile the program to sequences of instructions (G-code) which can be further translated to machine code.<\/li>\n<li>Chapter 4 repeats the same exercise for a different abstract machine, the <em>Three Instruction Machine<\/em> (TIM), whose evaluation model is very different from that of the G-machine. The TIM was developed more recently than the G-machine, so there is much less other literature about it. Chapter 4 therefore contains a rather more detailed development of the TIM&#8217;s evaluation model than that given for the G-machine.<\/li>\n<li>Finally, Chapter 5 adds a new dimension by showing how to compile functional programs for a <em>parallel G-machine<\/em>.<\/li>\n<\/ul>\n<p>For each of these implementations we discuss two main parts, the <em>compiler<\/em> and the <em>machine interpreter<\/em>. The compiler takes a Core-language program and translates it into a form suitable for execution by the machine interpreter.<\/p>\n<p>The machine interpreter simulates the execution of the compiled program. In each case the interpreter is modelled as a <em>state transition system<\/em> so that there is a very clear connection between the machine interpreter and a `real&#8217; implementation.<\/p>\n<p>One important way in which the Core language is restrictive is in its lack of local function definitions. There is a well-known transformation, called <em>lambda lifting<\/em>, which turns local function definitions into global ones, thus enabling local function definitions to be written freely and transformed out later. In Chapter 6 we develop a suitable lambda lifter. This chapter is more than just a re-presentation of standard material. <em>Full laziness<\/em> is a property of functional programs which had previously been seen as inseparable from lambda lifting. In Chapter 6 we show that they are in fact quite distinct, and show how to implement full laziness in a separate pass from lambda lifting.<\/p>\n<h2>Typographical errors<\/h2>\n<p>Here are some typographical errors, spotted by various readers. Page numbers refer to the online versions (which differ from the printed version).<\/p>\n<ul>\n<li>Page 128, second to last paragraph of section 3.7, first sentence. &#8220;addis&#8221; should be &#8220;is a&#8221;.<\/li>\n<li>Page 134, the code for i&#8217;. The &#8220;n&#8221; under the big brace symbol should say &#8220;n pairs&#8221;.<\/li>\n<li>Page 135. Equation 3.36 should have &#8220;NConstr&#8221;, not &#8220;Constr&#8221;.<\/li>\n<li>Page 230, second paragraph. &#8220;There one final gloss&#8230;&#8221; should be &#8220;There is one final gloss&#8230;&#8221;.<\/li>\n<li>Page 278, the definition of nfib. Replace &#8220;n==0&#8221; by &#8220;n<=1&#8221;.<\/li>\n<\/ul>\n<p>Now, alas, out of print. However the full text of the book is available above. You can order a nicely-bound copy from <a class=\"msr-external-link glyph-append glyph-append-open-in-new-tab glyph-append-xsmall\" rel=\"noopener noreferrer\" target=\"_blank\" href=\"http:\/\/www.cafepress.com\/haskell_books\">http:\/\/www.cafepress.com\/haskell_books<span class=\"sr-only\"> (opens in new tab)<\/span><\/a>. Thanks to John Meacham for setting this up.<\/p>\n<p>There is also a <a href=\"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/1992\/01\/tutor.pdf\">tutor&#8217;s guide <\/a>available.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This book gives a practical approach to understanding implementations of non-strict functional languages using lazy graph reduction. The book is intended to be a source of practical labwork material, to help make functional-language implementations `come alive&#8217;, by helping the reader to develop, modify and experiment with some non-trivial compilers. The unusual aspect of the book [&hellip;]<\/p>\n","protected":false},"featured_media":0,"template":"","meta":{"msr-url-field":"","msr-podcast-episode":"","msrModifiedDate":"","msrModifiedDateEnabled":false,"ep_exclude_from_search":false,"_classifai_error":"","msr-author-ordering":null,"msr_publishername":"Prentice Hall","msr_publisher_other":"","msr_booktitle":"Implementing functional languages: a tutorial","msr_chapter":"","msr_edition":"","msr_editors":"","msr_how_published":"","msr_isbn":"","msr_issue":"","msr_journal":"","msr_number":"","msr_organization":"","msr_pages_string":"","msr_page_range_start":"","msr_page_range_end":"","msr_series":"","msr_volume":"","msr_copyright":"","msr_conference_name":"","msr_doi":"","msr_arxiv_id":"","msr_s2_paper_id":"","msr_mag_id":"","msr_pubmed_id":"","msr_other_authors":"SL Peyton Jones, DR Lester","msr_other_contributors":"","msr_speaker":"","msr_award":"","msr_affiliation":"","msr_institution":"","msr_host":"","msr_version":"","msr_duration":"","msr_original_fields_of_study":"","msr_release_tracker_id":"","msr_s2_match_type":"","msr_citation_count_updated":"","msr_published_date":"1992-01-01","msr_highlight_text":"","msr_notes":"","msr_longbiography":"","msr_publicationurl":"http:\/\/www.cafepress.com\/haskell_books","msr_external_url":"","msr_secondary_video_url":"","msr_conference_url":"","msr_journal_url":"","msr_s2_pdf_url":"","msr_year":1992,"msr_citation_count":0,"msr_influential_citations":0,"msr_reference_count":0,"msr_s2_match_confidence":0,"msr_microsoftintellectualproperty":true,"msr_s2_open_access":false,"msr_s2_author_ids":[],"msr_pub_ids":[],"msr_hide_image_in_river":0,"footnotes":""},"msr-research-highlight":[],"research-area":[13560],"msr-publication-type":[193719],"msr-publisher":[],"msr-focus-area":[],"msr-locale":[268875],"msr-post-option":[],"msr-field-of-study":[],"msr-conference":[],"msr-journal":[],"msr-impact-theme":[],"msr-pillar":[],"class_list":["post-149863","msr-research-item","type-msr-research-item","status-publish","hentry","msr-research-area-programming-languages-software-engineering","msr-locale-en_us"],"msr_publishername":"Prentice Hall","msr_edition":"","msr_affiliation":"","msr_published_date":"1992-01-01","msr_host":"","msr_duration":"","msr_version":"","msr_speaker":"","msr_other_contributors":"","msr_booktitle":"Implementing functional languages: a tutorial","msr_pages_string":"","msr_chapter":"","msr_isbn":"","msr_journal":"","msr_volume":"","msr_number":"","msr_editors":"","msr_series":"","msr_issue":"","msr_organization":"","msr_how_published":"","msr_notes":"","msr_highlight_text":"","msr_release_tracker_id":"","msr_original_fields_of_study":"","msr_download_urls":"","msr_external_url":"","msr_secondary_video_url":"","msr_longbiography":"","msr_microsoftintellectualproperty":1,"msr_main_download":"258306","msr_publicationurl":"http:\/\/www.cafepress.com\/haskell_books","msr_doi":"","msr_publication_uploader":[{"type":"file","title":"student","viewUrl":"https:\/\/www.microsoft.com\/en-us\/research\/wp-content\/uploads\/1992\/01\/student.pdf","id":258306,"label_id":0},{"type":"url","title":"http:\/\/www.cafepress.com\/haskell_books","viewUrl":false,"id":false,"label_id":0}],"msr_related_uploader":"","msr_citation_count":0,"msr_citation_count_updated":"","msr_s2_paper_id":"","msr_influential_citations":0,"msr_reference_count":0,"msr_arxiv_id":"","msr_s2_author_ids":[],"msr_s2_open_access":false,"msr_s2_pdf_url":null,"msr_attachments":[{"id":0,"url":"http:\/\/www.cafepress.com\/haskell_books"}],"msr-author-ordering":[{"type":"text","value":"SL Peyton Jones","user_id":0,"rest_url":false},{"type":"text","value":"DR Lester","user_id":0,"rest_url":false},{"type":"user_nicename","value":"simonpj","user_id":33665,"rest_url":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/microsoft-research\/v1\/researchers?person=simonpj"}],"msr_impact_theme":[],"msr_research_lab":[],"msr_event":[],"msr_group":[],"msr_project":[],"publication":[],"video":[],"msr-tool":[],"msr_publication_type":"book","related_content":[],"_links":{"self":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-research-item\/149863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-research-item"}],"about":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/types\/msr-research-item"}],"version-history":[{"count":3,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-research-item\/149863\/revisions"}],"predecessor-version":[{"id":528708,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-research-item\/149863\/revisions\/528708"}],"wp:attachment":[{"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/media?parent=149863"}],"wp:term":[{"taxonomy":"msr-research-highlight","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-research-highlight?post=149863"},{"taxonomy":"msr-research-area","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/research-area?post=149863"},{"taxonomy":"msr-publication-type","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-publication-type?post=149863"},{"taxonomy":"msr-publisher","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-publisher?post=149863"},{"taxonomy":"msr-focus-area","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-focus-area?post=149863"},{"taxonomy":"msr-locale","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-locale?post=149863"},{"taxonomy":"msr-post-option","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-post-option?post=149863"},{"taxonomy":"msr-field-of-study","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-field-of-study?post=149863"},{"taxonomy":"msr-conference","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-conference?post=149863"},{"taxonomy":"msr-journal","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-journal?post=149863"},{"taxonomy":"msr-impact-theme","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-impact-theme?post=149863"},{"taxonomy":"msr-pillar","embeddable":true,"href":"https:\/\/www.microsoft.com\/en-us\/research\/wp-json\/wp\/v2\/msr-pillar?post=149863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}