Logic programming languages present clear advantages in terms of declarativeness and conciseness. However, the ideas of logic programming have been met with resistance in other programming communities, and have not generally been adopted by other paradigms and languages. This paper proposes a novel way to incorporate logic programming in an existing codebase in a typed functional programming language. Our approach integrates with the host language without sacrificing static typing, and leverages strengths of typed functional programming such as polymorphism and higher-order. We do so by combining three ideas. First, we use the extensible types technique to allow values of the host language to contain logic variables. Second, we implement a unification algorithm that works for any data structure that supports certain operations.Third, we introduce a domain-specific language to define and query predicates. We demonstrate our proposal via a series of examples, and provide aids to make the notation convenient for users, showing that the proposed approach is not just technically possible but also practical. Our ideas have been implemented in the language Haskell with very good results.
翻译:逻辑编程语言在声明性与简洁性方面具有明显优势。然而,逻辑编程的思想在其他编程社区中遇到了阻力,并未被其他范式和语言普遍采纳。本文提出了一种在类型化函数式编程语言的现有代码库中融入逻辑编程的新方法。我们的方法在与宿主语言集成时不牺牲静态类型,并充分利用类型化函数式编程的优势,如多态性和高阶函数。我们通过结合三个核心理念实现这一目标:首先,采用可扩展类型技术,使宿主语言的值能够包含逻辑变量;其次,实现适用于任何支持特定操作的数据结构的统一算法;第三,引入领域特定语言来定义和查询谓词。我们通过一系列示例展示该方案的可行性,并提供辅助工具以提升用户书写便利性,证明所提方法不仅技术上可行且具有实用性。这些思想已在Haskell语言中实现并取得良好效果。