Plan for Python Lecture 2 Review List Comprehensions List

Total Page:16

File Type:pdf, Size:1020Kb

Plan for Python Lecture 2 Review List Comprehensions List Plan For Python Lecture 2 § Review § List Comprehensions Review § Iterators, Generators § Imports >>>import this § Functions The Zen of Python, by Tim Peters § *args, **kwargs, first class functions § Classes § inheritance § “magic” methods (objects behave like built-in types) § Profiling § timeit § cProfile § Idioms CIS 391 - Fal l 2015 Intro to AI 1 List Comprehensions List Comprehension extra for [<statement> for <item> in <iterable> if <c ondi tio n>] #Translation [x for x in lst1 if x > 2 for y in lst2 for z in lst3 lst = [] if x + y + z < 8] for <item> in <iter abl e>: if <condition>: res = [] # translation lst. app end(<statement>) for x in lst1: if x > 2: >>> li = [(‘a’, 1), (‘b’, 2), (‘c’, 7)] for y in lst2: >>> [ n * 3 for (x, n) in li if x == ‘b’ or x = = ‘ c’] for z in lst3: [6, 2 1] if x + y + z > 8: #Translation res.append(x) lst = [] for (x,n) in li: if x == ‘b’ or x == ‘c’ : lst.append(n *3) CIS 391 - Fal l 2015 Intro to AI 3 CIS 391 - Fal l 2015 Intro to AI 4 Iterators use memory efficiently Generators (are iterators) § Iterators are objects with a next() method: § Function def reverse(data): § To be iterable: __iter__() for i in range(len(data)-1, -1, -1): § To be iterators: next() yield data[i] Generator Expression >>> k = [1,2,3] § (data[i] for index in range(le n(da ta) -1, -1, -1)) >>> i = iter(k) # co uld als o u se k .__ iter __( ) >>> i.next() >>> xvec = [10, 20, 30] 1 >>> yvec = [7, 5, 3] >>> i.next() >>> sum(x *y f or x,y in zip(xve c, yvec)) # dot pr odu ct 2 260 >>> i.next() 3 § Lazy Evaluation (on demand, values generated) >>> i.next() StopIteration CIS 391 - Fal l 2015 Intro to AI 5 CIS 391 - Fal l 2015 Intro to AI 6 1 Import Modules and Files >>> import math >>> math.sqrt(9) 3.0 # NOT: >>> from math import * >>> sqrt(9) # unclear where function defined Functions #hw1.py def concatenate(seqs ): return [seq for seq in seqs] # Th is i s w rong (Methods later) # run python interactive interpreter (REPL) in directory with hw1.py >>> import hw1 >>> assert hw1.concatenate([[1, 2], [3, 4]]) == [ 1, 2, 3 , 4] #AssertionError >>> reload(hw1) #after fixing hw1 CIS 391 - Fal l 2015 Intro to AI 7 Defining Functions Function overloading? No. Function definition begins with def Function name and its arguments. § There is no function overloading in Python. def get_final_answer(filename): § Unlike Java, a Python function is specified by its name alone """Documentation String""" Two different functions can’t have the same name, even if they line1 § have different numbers, order, or names of arguments. line2 Colon return total_counter ... § But operator overloading – overloading +, ==, -, etc. – is possible using special methods on various classes (see later slides) First line with less ‘return’ indicates the indentation is considered to be value to be sent back to the caller. outside of the function definition. No declaration of typesof arguments or result CIS 391 - Fal l 2015 Intro to AI 9 CIS 391 - Fal l 2015 Intro to AI 10 Default Values for Arguments Keyword Arguments § You can provide default values for a function’s arguments § Functions can be called with arguments out of order § These arguments are optional when the function is called § These arguments are specified in the call >>> def myfu n(b , c= 3, d=“h ell o”): § Keyword arguments can be used for a final subset of ret urn b + c the arguments. >>> myfun(5, 3,”bob”) >>> def myfu n (a, b, c): 8 re turn a-b >>> myfun(5, 3) >>> myfun(2, 1, 43) 8 1 >>> myfun(5) >>> myfun(c= 43, b=1 , a =2) 8 1 >>> myfun(2, c=43, b=1) 1 >>> myfun(a= 2, b=3, 5) SyntaxError: non-ke ywo rd arg afte r k eywo rd arg CIS 391 - Fal l 2015 Intro to AI 11 CIS 391 - Fal l 2015 Intro to AI 12 2 *args **kwargs § Suppose you want to accept a variable number of § Suppose you want to accept a variable number of non-keyword arguments to your function. keyword arguments to your function. def print_keyword_args(**kw arg s): def print_everything(*args): # kwargs is a di ct of the keyword args pa sse d to th e fn # args is a t upl e o f ar gum ents pa ssed to the fn for key, value in kwargs.iteritems(): #. item s() is list for count, thing in enumerate(arg s): print "% s = % s" % ( key , va lue ) print '{ 0}. { 1} '.fo rma t(co unt , th ing ) >>> lst = ['a ', ’ b', 'c ’] >>> kwargs = { 'first_name': 'B obby ', ' las t_n ame': 'Smith'} >>> print_everything(*lst) >>> print_keyword_args(**kw arg s) 0. a first_name = J oh n 1. b last_name = Doe 2. c >>> print_keyword_args(firs t_n ame="John”, la st_n ame ="Do e") >>> print_everything('a ', ’ b', 'c' ) # Combining ideas foo(arg1, *ar gs, **kwargs) # o ne m and ator y a rgum ent CIS 391 - Fal l 2015 Intro to AI 13 CIS 391 - Fal l 2015 Intro to AI 14 Scope Default Arguments & Memoization § Function sees the most current value of variables § Default parameter values are evaluated only when the def statement they belong to is executed. >>> i = 10 § The function uses the same default object each call def add(x): return x + i def fib(n, fibs={}): if n in fibs: >>> add(5) return fibs[n] 15 if n <= 1: >>> i = 20 fibs[n] = n >>> add(5) else: 25 fibs[n] = fi b(n -1) + f ib(n -2) return fi bs[ n] CIS 391 - Fal l 2015 Intro to AI 15 CIS 391 - Fal l 2015 Intro to AI 16 First Class Functions Higher Order Functions: Map, Filter § Functions are “first-class citizens” >>> [int(i) for i in ['1', '2']] § Pass functions as arguments to other functions, [1, 2] >>> map(int, ['1', '2']) #equivalent to above § returning functions as the values from other functions, [1, 2] § Assign functions to variables or store them in data structures § Higher order functions: take functions as input def is_even(x): return x % 2 == 0 def compose(f, g, x): >>> [i for i in [1, 2, 3, 4, 5] i f is_even (i)] return f( g (x)) [2, 4] >>> compose(str, sum, [1,2,3]) >>> filter(is_even , [1, 2, 3, 4, 5]) [2, 4] #equivalent to above '6' >>> t1 = (0, 10) >>> t2 = (100, 2) >>> min([t1, t2], key= lambda x: x[1]) (100, 2) CIS 391 - Fal l 2015 Intro to AI 17 CIS 391 - Fal l 2015 Intro to AI 18 3 Sorted list of n-grams from operator import it emge tte r def calc_ngram(inputstring, nl en): ngram_list = [inpu tst ring [x: x+nl en] for x i n \ xrange(le n(in put stri ng)-nle n+1 )] ngram_freq = {} # dict fo r s tori ng r esu lts for n in ng ram _lis t: # collect the distinct n-gra ms a nd Classes and Inheritance count if n in ngram_freq: ngram_freq[n] + = 1 else: ngram_freq[n] = 1 # h uma n co unt ing n um bers st art a t 1 # set reverse = False to change order of sort (ascending/descending) return sorted(ngra m_f req. ite rite ms(), \ key=itemgetter(1 ), r eve rse =Tru e) http://times.jayliew.com/2010/05/20/a-simple-n-gram-calculator-pyn gram/ CIS 391 - Fal l 2015 Intro to AI 19 Called when an Creating a class object is Subclasses instantiated Class Student: § A class can extend the definition of another class univ = ”upenn” # class attribute Every method begins with the § Allows use (or extension ) of methods and attributes already defined in the previous one. variable self def __init__(self, name, dept): § New class: subclass. Original: parent, ancestor or superclass self.student_name = name self.student_dept = dept § To define a subclass, put the name of the def print_details(self): Another member superclass in parentheses after the subclass’s print "Name: " + self.student_name method name on the first line of the definition. print "Dept: " + self.student_dept cl ass ai_ stu dent (st uden t): Creating an instance, student1 = Student(”john", "cis") note no self § Python has no ‘extends’ keyword like Java. student1.print_details() Student.print_details(student1) Calling methods § Multiple inheritance is supported. Student.univ of an object CIS 391 - Fal l 2015 Intro to AI 21 CIS 391 - Fal l 2015 Intro to AI 22 Redefining Methods __init__constructors in subclasses: § Very similar to over-riding methods in Java § UNLIKE Java: To execute the ancestor’s __init__ § To redefine a method of the parent class, include a new method the ancestor’s __init__ must be called explicitly definition using the same name in the subclass. (if the descendants __init__ is specified) § The old code won’t get executed. § To execute the method in the parent class in addition to § The first line of the __init__ method of a subclass will new code for some method, explicitly call the parent’s often be: version of the method. parentClass.__init__(x, y) super(se lf._ _cl ass__, self).__ini t__ (x, y ) # equ iva lent parentClass.methodName(self, a , b , c ) § The only time you ever explicitly pass self as an argument is when calling a method of an ancestor. So: myOwnClass.methodName(a,b,c) CIS 391 - Fal l 2015 Intro to AI 23 CIS 391 - Fal l 2015 Intro to AI 24 4 Multiple Inheritance class A(object): def foo(self): print 'Foo!' class B(object): def foo(self): Special Built-In print 'Foo?' Methods and Attributes def bar(sel f): print 'Bar!' class C(A, B): def foobar(self): super(C, self).foo() # Foo! super(C, self).bar() # Bar! CIS 391 - Fal l 2015 Intro to AI 25 Magic Methods and Duck Typing Other “Magic” Methods § Magic Methods allow user-defined classes to behave § Used to implement operator overloading like built in types § Most operators trigger a special method, dependent on class § Duck typing establishes suitability of an object by determining presence of methods __init__: The constructor for the class.
Recommended publications
  • Comprehending Adts*
    Comprehending ADTs* Martin Erwig School of EECS Oregon State University [email protected] Abstract We show how to generalize list comprehensions to work on abstract data types. First, we make comprehension notation automatically available for any data type that is specified as a constructor/destructor-pair (bialgebra). Second, we extend comprehensions to enable the use of dif- ferent types in one comprehension and to allow to map between different types. Third, we refine the translation of comprehensions to give a reasonable behavior even for types that do not obey the monad laws, which significantly extends the scope of comprehensions. Keywords: Functional Programming, Comprehension Syntax, Abstract Data Type, Generalized Fold, Monad 1 Introduction Set comprehensions are a well-known and appreciated notation from mathematics: an expression such as fE(x) j x 2 S; P(x)g denotes the set of values given by repeatedly evaluating the expression E for all x taken from the set S for which P(x) yields true. This notation has found its way into functional languages as list comprehensions [3, 17, 18]. Assuming that s denotes a list of numbers, the following Haskell expression computes squares of the odd numbers in s. [ x ∗ x j x s; odd x ] The main difference to the mathematical set comprehensions is the use of lists in the generator and as a result. In general, a comprehension consists of an expression defining the result values and of a list of qualifiers, which are either generators for supplying variable bindings or filters restricting these. Phil Wadler has shown that the comprehension syntax can be used not only for lists and sets, but more generally for arbitrary monads [19].
    [Show full text]
  • Coffeescript Accelerated Javascript Development.Pdf
    Download from Wow! eBook <www.wowebook.com> What readers are saying about CoffeeScript: Accelerated JavaScript Development It’s hard to imagine a new web application today that doesn’t make heavy use of JavaScript, but if you’re used to something like Ruby, it feels like a significant step down to deal with JavaScript, more of a chore than a joy. Enter CoffeeScript: a pre-compiler that removes all the unnecessary verbosity of JavaScript and simply makes it a pleasure to write and read. Go, go, Coffee! This book is a great introduction to the world of CoffeeScript. ➤ David Heinemeier Hansson Creator, Rails Just like CoffeeScript itself, Trevor gets straight to the point and shows you the benefits of CoffeeScript and how to write concise, clear CoffeeScript code. ➤ Scott Leberknight Chief Architect, Near Infinity Though CoffeeScript is a new language, you can already find it almost everywhere. This book will show you just how powerful and fun CoffeeScript can be. ➤ Stan Angeloff Managing Director, PSP WebTech Bulgaria Download from Wow! eBook <www.wowebook.com> This book helps readers become better JavaScripters in the process of learning CoffeeScript. What’s more, it’s a blast to read, especially if you are new to Coffee- Script and ready to learn. ➤ Brendan Eich Creator, JavaScript CoffeeScript may turn out to be one of the great innovations in web application development; since I first discovered it, I’ve never had to write a line of pure JavaScript. I hope the readers of this wonderful book will be able to say the same. ➤ Dr. Nic Williams CEO/Founder, Mocra CoffeeScript: Accelerated JavaScript Development is an excellent guide to Coffee- Script from one of the community’s most esteemed members.
    [Show full text]
  • The Language Features and Architecture of B-Prolog 3
    Theory and Practice of Logic Programming 1 The Language Features and Architecture of B-Prolog Neng-Fa Zhou Department of Computer and Information Science CUNY Brooklyn College & Graduate Center [email protected] submitted 5th October 2009; revised 1th March 2010; accepted 21st February 2011 Abstract B-Prolog is a high-performance implementation of the standard Prolog language with sev- eral extensions including matching clauses, action rules for event handling, finite-domain constraint solving, arrays and hash tables, declarative loop constructs, and tabling. The B-Prolog system is based on the TOAM architecture which differs from the WAM mainly in that (1) arguments are passed old-fashionedly through the stack, (2) only one frame is used for each predicate call, and (3) instructions are provided for encoding matching trees. The most recent architecture, called TOAM Jr., departs further from the WAM in that it employs no registers for arguments or temporary variables, and provides variable-size instructions for encoding predicate calls. This paper gives an overview of the language features and a detailed description of the TOAM Jr. architecture, including architectural support for action rules and tabling. KEYWORDS: Prolog, logic programming system 1 Introduction Prior to the first release of B-Prolog in 1994, several prototypes had been de- arXiv:1103.0812v1 [cs.PL] 4 Mar 2011 veloped that incorporated results from various experiments. The very first proto- type was based on the Warren Abstract Machine (WAM) (Warren 1983) as imple- mented in SB-Prolog (Debray 1988). In the original WAM, the decision on which clauses to apply to a call is made solely on the basis of the type and sometimes the main functor of the first argument of the call.
    [Show full text]
  • Homework 5: Functional Programming and Haskell
    Com S 541 — Programming Languages 1 December 5, 2002 Homework 5: Functional Programming and Haskell Due: problems 1(b),1(c), 2(b), and 4-8, Tuesday, December 3, 2002; remaining problems Tuesday, December 10, 2002 In this homework you will learn about the basics of Haskell, how Haskell can be considered as a domain-specific language for working with lists, basic techniques of recursive programming over various types of data, and abstracting from patterns, higher-order functions, currying, and infinite data. Many of the problems below exhibit polymorphism. Finally, the problems as a whole illustrate how functional languages work without statements (including assignment statements). Since the purpose of this homework is to ensure skills in functional programming, this is an individual homework. That is, for this homework, you are to do the work on your own, not in groups. For all Haskell programs, you must run your code with Haskell 98 (for example, using hugs). We suggest using the flags +t -u +98 when running hugs. A script that does this automatically is pro- vided in the course bin directory, i.e., in /home/course/cs541/public/bin/hugs, which is also avail- able from the course web site. If you get your own copy of Haskell (from http://www.haskell.org), you can adapt this script for your own use. You must also provide evidence that your program is correct (for example, test cases). Hand in a printout of your code and your testing, for questions that require code. Be sure to clearly label what problem each function solves with a comment.
    [Show full text]
  • Lists, Tuples, Dictionaries
    UNIT – 4 UNIT IV LISTS, TUPLES, DICTIONARIES 9 Lists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list parameters; Tuples: tuple assignment, tuple as return value; Dictionaries: operations and methods; advanced list processing - list comprehension; Illustrative programs: selection sort, insertion sort, merge sort, histogram. LISTS, TUPLES, DICTIONARIES 4.1 LISTS: A list is a sequence of values. In a string, the values are characters but in a list, list values can be any type. Elements or Items: The values in a list are called elements or items. list must be enclosed in square brackets ([and]). Examples: >>>[10,20,30] >>>[‘hi’,’hello’,’welcome’] Nested List: A list within another list is called nested list. Example: [‘good’,10,[100,99]] Empty List: A list that contains no elements is called empty list. It can be created with empty brackets, []. Assigning List Values to Variables: Example: >>>numbers=[40,121] >>>characters=[‘x’,’y’] >>>print(numbers,characters) Output: [40,12][‘x’,’y’] 4.1.1 LIST OPERATIONS: Example 1: The + operator concatenates lists: >>>a=[1,2,3] >>>b=[4,5,6] >>>c=a+b >>>c Output: [1,2,3,4,5,6] Example 2: The * operator repeats a list given number of times: >>>[0]*4 Output: [0,0,0,0] >>>[1,2,3]*3 Output: [1,2,3,1,2,3,1,2,3] The first example repeats [0] four times.The second example repeats [1,2,3] three times. 4.1.2 LIST SLICES: List slicing is a computationally fast way to methodically access parts of given data. Syntax: Listname[start:end:step]where :end represents the first value that is not in the selected slice.
    [Show full text]
  • 2 Comprehensions
    CS231 Instructor’s notes 2021 2 Comprehensions Key terms: comprehension Reading: Mary Rose Cook’s A practical introduction to func- tional programming4 Exercise: Write a program that uses a list comprehension to find all numbers under one million, the sum of whose digits equals seven. “Go To Statement Considered Harmful”5 is the popular name of Dijkstra’s argument for structured programming (and the origin of the “considered harmful” meme). Python has no “Go To”, but unecessary statefulness still causes problems. Using function calls with iterable objects instead of loops can be safer, shorter, more reliable, and more testable. The two techniques examined here are unpacking and comprehensions. No- tice the wonderful *, splat, operator, unpacking arguments in place. This makes program grammar easier, providing a means of switching between references and referents. Structured iteration: Blended iteration: Implicit iteration: i = 0 for i in print(*range(100),sep='\n') while i < 100: range(100): print (i) print(i) [print(i) for i i += 1 in range(100)] Comprehensions are Python’s other great functional convenience: anony- mous collections whose contents are described inline. It’s the same concept as set-builder notation, e.g. fi 2 I : 0 ≤ i < 100g. Every element in a comprehension corresponds to an element in source data which is filtered and mapped. Specifying exactly the input conditions (the source material and how it is filtered) and the output conditions (what you want toknow about those elements) is very empowering because then the programmer doesn’t have to worry about state within a loop. Any comprehension can be rewritten in the form of a filter function and a presentation function in- voked by map() and filter().
    [Show full text]
  • Monads for Functional Programming
    Monads for functional programming Philip Wadler, University of Glasgow? Department of Computing Science, University of Glasgow, G12 8QQ, Scotland ([email protected]) Abstract. The use of monads to structure functional programs is de- scribed. Monads provide a convenient framework for simulating effects found in other languages, such as global state, exception handling, out- put, or non-determinism. Three case studies are looked at in detail: how monads ease the modification of a simple evaluator; how monads act as the basis of a datatype of arrays subject to in-place update; and how monads can be used to build parsers. 1 Introduction Shall I be pure or impure? The functional programming community divides into two camps. Pure lan- guages, such as Miranda0 and Haskell, are lambda calculus pure and simple. Impure languages, such as Scheme and Standard ML, augment lambda calculus with a number of possible effects, such as assignment, exceptions, or continu- ations. Pure languages are easier to reason about and may benefit from lazy evaluation, while impure languages offer efficiency benefits and sometimes make possible a more compact mode of expression. Recent advances in theoretical computing science, notably in the areas of type theory and category theory, have suggested new approaches that may integrate the benefits of the pure and impure schools. These notes describe one, the use of monads to integrate impure effects into pure functional languages. The concept of a monad, which arises from category theory, has been applied by Moggi to structure the denotational semantics of programming languages [13, 14]. The same technique can be applied to structure functional programs [21, 23].
    [Show full text]
  • PERFORMANCE ANALYZES of PYTHON and HASKELL in SPECIFYING and EXECUTINGLIST COMPREHENSIONS Kongu Vel
    JOURNAL OF CRITICAL REVIEWS ISSN- 2394-5125 VOL 7, ISSUE 08, 2020 PERFORMANCE ANALYZES OF PYTHON AND HASKELL IN SPECIFYING AND EXECUTINGLIST COMPREHENSIONS Kongu Vel. S, A. Kumaravel Research Scholar, Department of Management Science and Humanities, Bharath Institute of Higher Education and Research, India. E-mail: [email protected] Professor, Department of Information Technology, Bharath Institute of Higher Education and Research, India. E-mail: [email protected] ABSTRACT The syntactical categories are numerous higher-level programming language for facilitating the execution of instructions within optimal space and to save time.List comprehension is one such category created for parallel execution of predicates and expression in a simple specification.The purpose of List Comprehension is used to provide a executing expression easy method of simultaneously with a represented ina mathematical pattern.Each expression is evaluated and executed in a short span of time and effective with less error.It is always case change comparing the performance of such rich syntactic categories as most of the real time applications do depend on these key structures.As for as the „Language constructs‟ concern, they are semantically similar in Haskell and in Python though the little differences in syntax. The aim of this work is to compare the specifications of List Comprehensions implemented in Haskell and Python environments. It is established Python reasonably comparable with Haskell in tackling List Comprehensions for certain class of simple applications. Keywords:Haskell, Python, List Comprehension, Time complexity. 1. INTRODUCTION List comprehension is a mathematical notation used for creating lists to manipulate data. It isapplied as an alternative method for loop function, lambda function as well as the functionslike map (), filter () and reduce ().
    [Show full text]
  • List Comprehensions
    List Comprehensions Python’s higher-order functions • Python supports higher-order functions that operate on lists similar to Scheme’s >>> def square(x): return x*x >>> def even(x): return 0 == x % 2 >>> map(square, range(10,20)) [100, 121, 144, 169, 196, 225, 256, 289, 324, 361] >>> filter(even, range(10,20)) [10, 12, 14, 16, 18] >>> map(square, filter(even, range(10,20))) [100, 144, 196, 256, 324] • But many Python programmers prefer to use list comprehensions, instead List Comprehensions • A list comprehension is a programming language construct for creating a list based on existing lists • Haskell, Erlang, Scala and Python have them • Why “comprehension”? The term is borrowed from math’s set comprehension notation for defining sets in terms of other sets • A powerful and popular feature in Python • Generate a new list by applying a function to every member of an original list • Python’s notation: [ expression for name in list ] List Comprehensions • The syntax of a list comprehension is somewhat tricky [x-10 for x in grades if x>0] • Syntax suggests that of a for-loop, an in operation, or an if statement • All three of these keywords (‘for’, ‘in’, and ‘if’) are also used in the syntax of forms of list comprehensions [ expression for name in list ] List Comprehensions Note: Non-standard >>> li = [3, 6, 2, 7] colors on next few >>> [elem*2 for elem in li] slides clarify the list [6, 12, 4, 14] comprehension syntax. [ expression for name in list ] • Where expression is some calculation or operation acting upon the variable name.
    [Show full text]
  • Add Statement to Retrieve the Order List
    Add Statement To Retrieve The Order List Sometimes resumable Quincy unreeving her konimeter blearily, but rural Nicholas inactivating bitter or sorns unutterably. Aldo often dehumanised spryly when dirtiest Georgy contravened larcenously and annihilates her tribologists. Griswold crevasse certainly. The entity as list mode turned on facebook and add to retrieve the order list statement has the file after my transcript Version of order to the add command and services to corresponding option during the expression is. Include items from the medline indexing in addition to help with a declaration binds an empty arrays produces zero extended to false if statement list entries from the trade desk operator. There are two forms. Return a blast chiller to use cases of this list or a join reviews will retrieve the add statement order list to prevent accessing a challenge in. Pour tous les autres types de cookies, nous avons besoin de votre permission. This will combine results from both news table and members table and return all of them. Packing lists will always group ordered items by the product category to make picking and packaging orders easier. Load all fields immediately. The model only one of this query statements that any of decode function to explore smb solutions designed to list only available for the latter can use to track signals. Exits the configuration mode and returns to privileged EXEC mode. Ezoic, stocke quelle mise en page vous verrez sur ce site Web. The target table statement to retrieve audit, um die benutzererfahrung zu testen. Sometimes you may need to use a raw expression in a query.
    [Show full text]
  • Unsupervised Translation of Programming Languages
    Unsupervised Translation of Programming Languages Marie-Anne Lachaux∗ Baptiste Roziere* Lowik Chanussot Facebook AI Research Facebook AI Research Facebook AI Research [email protected] Paris-Dauphine University [email protected] [email protected] Guillaume Lample Facebook AI Research [email protected] Abstract A transcompiler, also known as source-to-source translator, is a system that converts source code from a high-level programming language (such as C++ or Python) to another. Transcompilers are primarily used for interoperability, and to port codebases written in an obsolete or deprecated language (e.g. COBOL, Python 2) to a modern one. They typically rely on handcrafted rewrite rules, applied to the source code abstract syntax tree. Unfortunately, the resulting translations often lack readability, fail to respect the target language conventions, and require manual modifications in order to work properly. The overall translation process is time- consuming and requires expertise in both the source and target languages, making code-translation projects expensive. Although neural models significantly outper- form their rule-based counterparts in the context of natural language translation, their applications to transcompilation have been limited due to the scarcity of paral- lel data in this domain. In this paper, we propose to leverage recent approaches in unsupervised machine translation to train a fully unsupervised neural transcompiler. We train our model on source code from open source GitHub projects, and show that it can translate functions between C++, Java, and Python with high accuracy. Our method relies exclusively on monolingual source code, requires no expertise in the source or target languages, and can easily be generalized to other programming languages.
    [Show full text]
  • A User's Guide to Picat
    A User’s Guide to Picat Version 2.2 Neng-Fa Zhou and Jonathan Fruhman Copyright c picat-lang.org, 2013-2017. Last updated August 10, 2017 Preface Despite the elegant concepts, new extensions (e.g., tabling and constraints), and successful appli- cations (e.g., knowledge engineering, NLP, and search problems), Prolog has a bad reputation for being old and difficult. Many ordinary programmers find the implicit non-directionality and non- determinism of Prolog to be hard to follow, and the non-logical features, such as cuts and dynamic predicates, are prone to misuses, leading to absurd codes. The lack of language constructs (e.g., loops) and libraries for programming everyday things is also considered a big weakness of Prolog. The backward compatibility requirement has made it hopeless to remedy the language issues in current Prolog systems, and there are urgent calls for a new language. Several successors of Prolog have been designed, including Mercury, Erlang, Oz, and Curry. The requirement of many kinds of declarations in Mercury has made the language difficult to use; Erlang’s abandonment of non-determinism in favor of concurrency has made the language unsuited for many applications despite its success in the telecom industry; Oz has never attained the popularity that the designers sought, probably due to its unfamiliar syntax and implicit laziness; Curry is considered too close to Haskell. All of these successors were designed in the 1990s, and now the time is ripe for a new logic-based language. Picat aims to be a simple, and yet powerful, logic-based programming language for a variety of applications.
    [Show full text]