Retrie: Haskell refactoring made easy

栏目: IT技术 · 发布时间: 5年前

内容简介:We’ve open-sourced Retrie, a code refactoring tool for Haskell that makes codemodding faster, easier, and safer. Using Retrie, developers can efficiently rewrite large codebases (exceeding 1 million lines), express rewrites as equations in Haskell syntax i

What’s new:

We’ve open-sourced Retrie, a code refactoring tool for Haskell that makes codemodding faster, easier, and safer. Using Retrie, developers can efficiently rewrite large codebases (exceeding 1 million lines), express rewrites as equations in Haskell syntax instead of regular expressions, and avoid large classes of codemodding errors.

Retrie’s features include the ability to rewrite expressions, types, and patterns; the ability to script rewrites and add side conditions; and a library for scripting more advanced rewrites. Retrie also respects and maintains local scoping, preserves white space, and does not rewrite code comments.

Why it matters:

Refactoring improves the overall design of a codebase, but it can be a meticulous, time-consuming task. To avoid errors, refactoring is often done by hand, in small increments. As the size of a codebase grows, this approach becomes infeasible, and tool support is critical.

In the spectrum of refactoring tools, there are two extremes. At one end, there are find-and-replace string manipulation tools, such as sed. These tools are fast, but it is difficult to express complicated rewrites. At the other end of the spectrum are tools for parsing and manipulating an abstract syntax tree (AST). AST manipulation tools are very powerful but require extensive domain knowledge and are typically slow compared with string replacement.

Retrie occupies a comfortable middle ground. Expressing rewrites as equations in the syntax of the target language (in this case, Haskell) is easier than defining a complex regular expression or AST traversal. Since equations are more powerful than regular expressions and rewrites can be scripted, Retrie is more powerful than string replacement alone. Retrie also leverages several techniques to narrow the search space before parsing and efficiently finding matches, which makes it faster than typical AST manipulation tools.

As an example of using Retrie, let’s say you have some code, including functions like foo :

module MyModule where 
foo :: [Int] -> [Int] 
foo ints = map bar (map baz ints)

You realize that traversing the list ints twice is slower than doing it just once. Instead of fixing the code manually, you can express the transformation as an equation and apply it everywhere in the current directory:

retrie --adhoc "forall f g xs. map f (map g xs) = map (f . g) xs"

In this example, Retrie will make the following edit:

module MyModule where 
foo :: [Int] -> [Int] 
-foo ints = map bar (map baz ints) 
+foo ints = map (bar . baz) ints

Use it for:

Haskell powers our anti-abuse rule engine, Sigma . To manage the growing scale and complexity of rules, we migrated Sigma to Haskell in 2015. Sigma blocks spam, phishing attacks, and malware before they can affect people on Facebook. Retrie has allowed us to quickly and safely migrate Sigma’s rules to new APIs and libraries. We’re releasing it to the wider community so that other developers can take advantage of its speed, ease of use, and power for their own codemodding tasks.

Get it on GitHub:

Retrie


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JSP & Servlet学习笔记

JSP & Servlet学习笔记

【台湾】林信良 / 清华大学出版社 / 2012-5 / 58.00元

本书是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习JSP & Servlet时遇到的概念、操作、应用或认证考试等问题及解决方案。 本书针对Servlet 3.0的新功能全面改版,无论是章节架构与范例程序代码,都做了全面更新。书中详细介绍了Servlet/ JSP与Web容器之间的关系,必要时从Tomcat源代码分析,了解Servlet/ JSP如何与容器互动。本书还涵盖了文本处理......一起来看看 《JSP & Servlet学习笔记》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具