Retrie: Haskell refactoring made easy

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

游戏引擎架构

游戏引擎架构

[美] Jason Gregory (杰森.格雷戈瑞) / 叶劲峰 / 电子工业出版社 / 2014-1 / 128.00元

《游戏引擎架构》同时涵盖游戏引擎软件开发的理论及实践,并对多方面的题目进行探讨。本书讨论到的概念及技巧实际应用于现实中的游戏工作室,如艺电及顽皮狗。虽然书中采用的例子通常依据一些专门的技术,但是讨论范围远超于某个引擎或API。文中的参考及引用也非常有用,可让读者继续深入游戏开发过程的任何特定方向。 《游戏引擎架构》为一个大学程度的游戏编程课程而编写,但也适合软件工程师、业余爱好者、自学游戏程......一起来看看 《游戏引擎架构》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具