Relational Conversion for OCaml Petr Lozov

Dmitri Boulytchev

St.Petersburg State University Saint-Petersburg, Russia [email protected] [email protected]

Abstract We address the problem of transforming typed functional programs into relational form. In this form a program can be run in various “directions” with various arguments left free, making it possible to acquire different behaviors from a single specification. We present an implementation of relational convertor for a subset of Objective Caml and evaluate it on a number of benchmarks, obtaining some relational programs never written before.

1.

Introduction

Relational programming is an attractive technique, based on the idea of constructing programs as relations. Many logic programming languages, such as Prolog, Mercury1 , or Curry2 to some extent can be considered as relational. In this paper we focus on miniKanren [1–3]. miniKanren3 was originally designed as a small relational DSL, embedded in Scheme/Racket. The advantage of this approach is the flexibility in combining functional and logic features; in addition miniKanren possesses some quite appealing features (complete search, purity, declarativity, etc). With relational approach, it becomes possible to give simple and elegant solutions for problems otherwise considered as tricky, tough, tedious, or boring. For example, relational interpreters can be used to derive quines — programs, which reduce to themselves, as well as twines or trines (a pairs or triples of programs, reducing to each other) [4]; a straightforward relational description of simply typed lambda calculus [5] inference rules works both as type inferencer and inhabitation problem solver [6]; relational list sorting can be used to generate all permutations [7], etc. On the other hand, writing relational specification can sometimes be a tricky and error-prone task. Fortunately, many specifications can be written systematically by “generalizing” a certain functional program. From the very beginning the conversion from functional to relational form was considered as an element of relational programming thesaurus [1]. However, the traditional approach — unnest1 https://mercurylang.org

ing — was formulated for the untyped case, worked only for specifically written programs and, to our knowledge, was never implemented. We present a generalized form of relational conversion, which can be applied to typed terms in general form. We study the relational conversion for a certain subset of Objective Caml, retaining Hindley-Milner type system with let-polymorphism [8]. As target relational language we use OCanren [7] — a typed shallow embedding of miniKanren in Objective Caml; as a matter of fact, we transform a functional language into its relational extension. Our contribution includes the formal description of the semantics for both the source language and its relational extension and the proofs of static and dynamic correctness of the conversion. Due to space considerations we do not present the formal part here; the report was accepted for presentation at the symposium on Trends in Functional Programming4 .

2.

Relational Extension and Conversion

OCanren can be seen as a relational extension for Objective Caml. The central notion in this extension is the goal, which can be an arbitrary expression of reserved goal type G. There are only five syntactic forms of goals: • conjunction g1 ∧ g2 and disjunction g1 ∨ g2 of goals g1

and g2 ; • fresh variable introduction fresh (x) g for goal g; • unification t1 ≡ t2 and disequality constraint t1 6≡ t2 .

The two last forms constitute the basis for goal construction; here t1 and t2 are terms. In OCanren a term is an arbitrary expression of polymorphic logic type ↑ α. The simplest expression of logic type is a variable, bound in fresh. Another example is a value, injected into the logic domain with a built-in primitive “↑”, such as ↑ 3 of type ↑int. As an example of relational specification in OCanren, consider the following code snippet: let is_zero n b = (n ≡ ↑0 ∧ b ≡ ↑True) ∨ (n 6≡ ↑0 ∧ b ≡ ↑False)

2 http://www-ps.informatik.uni-kiel.de/currywiki 3 http://minikanren.org

4 https://www.cs.kent.ac.uk/events/tfp17

Function is_zero implements a binary relation between integers and booleans; when called with specific arguments, it returns a goal which can be executed, returning a stream of answers. An element of the stream contains the description of certain constraints for logical variables, which have to be respected in order for the relation to hold. For example, [ ( q=0,p=True ) ; ( q=_ . 0 (6≡0) ,p=False ) ]

iz_zero q p

The returned stream of answers contains two elements: the first one assigns q and p values “0” and “True”, respectively; the second assigns “False” to p and the constraint to be everything, except zero, to q; here “_ .0 ” corresponds to a free variable, and (6≡0) to its disequality constraint. Our relational conversion is based on the following very simple idea on the type level: we transform a source term of type t into its relational counterpart of type [t], where the type transformation function [•] is defined as follows: [a] = a → G [t1 → t2 ] = [t1 ] → [t2 ] [∀α.t] = ∀α. [t] where a — arbitrary ground (non-polymorphic, nonfunctional type). Despite its type-based description, the conversion itself does not make use of types. The static correctness property, which we have proven, claims that for properly typed source terms the results of conversion are always properly typed in relational sense. The similar result holds for dynamic correctness.

3.

Evaluation

We implemented the conversion5 and applied it to a number of programs, providing their relational implementations. First, we implemented an interpreter for a simple Scheme-like language, converted it into relational interpreter and reproduced quines, twines and trines benchmarks [4]. Next, we implemented the type inference for HindleyMilner type system and tested it in various directions. For example, our relational inferencer is capable of inferring types: nat_type_inference (↑Abst (↑”x” , ↑Var ↑”x”)) q [q=Just (TFun (TVar Z , TVar Z ) ) ]

as well as finding some inhabitants for a given type: nat_type_inference q ↑(Just ↑TBool) [q=Lit (LBool _ . 2 4 ) ; q=Let (_.18 , Lit (LInt _.32) , Lit (LBool _ . 8 0 ) ) ; q=Let (_.18 , Lit (LBool _.32) , Lit (LBool _ . 8 0 ) ) ; q=Let (_.89 , Lit (LBool _.32) , Var _ . 8 9 ) ; ... ]

The first answer corresponds to a boolean constant (true of false), the second and third — to expressions of the

form let x = A in B, where A — some integer or boolean constant, B — some boolean constant, and the fourth — to the expression let x = B in x, where B — some boolean constant. Our relational inferencer can also be used to deduce a complete term from an incomplete term and its desirable type: nat_type_inference (↑Let (↑”f” , q , ↑App (↑Var ↑”f” , ↑Abst (↑”x” , ↑App (↑Var ↑”f” , ↑Var ↑”x ” ) ) ) ) ) ↑(Just ↑TBool ) ) [q=Abst (_.74 , Var (_ . 7 4 ) ) ; q=Abst (_.44 , Abst (_.90 , Var (_ . 9 0 ) ) ) ; ... ]

Here we provide the type inferencer an incomplete term let f =  in f (λ x → f x), where  is a hole, and expected type bool. The answers provide us with the terms, which can be plugged into the hole: the first one is λ x → x, the second — λ x y → y. Note, that this example essentially uses the polymorphic part of the type system, as the term λ f → f (λ x → f x) can not be typed in STLC. Finally, we implemented an interpreter of OCanren-like relational language and converted it into relational form. As a result, we can run relational programs relationally. For example, for this relational program with a hole () let rec add a b c = ( ( a ≡ Z) ∧ (b ≡ c ) ) ∨ (fresh (a0 c0 ) (a ≡ S a0 ) ∧  ∧ (add a0 b c0 ) ) in fresh (x y z) (add x y z)

and a number of answers, describing the results of addition of natural numbers in Peano form, our relational interpreter for relational language finds a feasible term to plug into the hole: c ≡ S c0 . Our relational language supports disequality constraints as well, which makes it different from existing works6 .

References [1] Daniel P. Friedman, William E.Byrd, Oleg Kiselyov. The Reasoned Schemer. The MIT Press, 2005. [2] Jason Hemann, Daniel P. Friedman. µKanren: A Minimal Core for Relational Programming // Proceedings of the 2013 Workshop on Scheme and Functional Programming (Scheme ’13). 6 https://github.com/jasonhemann/micro-in-mini,

5 https://bitbucket.org/peter

lozov/translator-to-minikanren

https://github.com/jpt4/muko

[3] Claire E. Alvis, Jeremiah J. Willcock, Kyle M. Carter, William E. Byrd, Daniel P. Friedman. cKanren: miniKanren with Constraints // Proceedings of the 2011 Workshop on Scheme and Functional Programming (Scheme ’11). [4] William E. Byrd, Eric Holk, Daniel P. Friedman. miniKanren, Live and Untagged: Quine Generation via Relational Interpreters (Programming Pearl) // Proceedings of the 2012 Workshop on Scheme and Functional Programming (Scheme ’12). [5] Henk Barendregt. Lambda Calculi with Types, Handbook of Logic in Computer Science (Vol. 2), 1992. [6] William E. Byrd. Relational Programming in miniKanren: Techniques, Applications, and Implementations. PhD Thesis, Indiana University, Bloomington, IN, September 30, 2009. [7] Dmitry Kosarev, Dmitry Boulytchev. Typed Embedding of a Relational Language in OCaml // International Workshop on ML, 2016. [8] Benjamin Pierce. Types and Programming Languages. MIT Press, 2002.

Relational Conversion for OCaml - ML Family Workshop

preters (Programming Pearl) // Proceedings of the 2012 Work- shop on Scheme and Functional Programming (Scheme '12). [5] Henk Barendregt. Lambda ...

144KB Sizes 0 Downloads 293 Views

Recommend Documents

Relational Conversion for OCaml - The ML Family Workshop
St.Petersburg State University .... Logic in Computer Science (Vol. 2), 1992. [6] William E. ... Indiana University, Bloomington, IN, September 30, 2009. [7] Dmitry ...

VOCAL – A Verified OCAml Library - ML Family Workshop
Libraries are the basic building blocks of any realistic programming project. It is thus of utmost .... verification of object-oriented programs. In 21st International ...

VOCAL – A Verified OCAml Library - ML Family Workshop
OCaml is the implementation language of systems used worldwide where stability, safety, and correctness are of ... An overview of JML tools and applications.

Mergeable Types - ML Family Workshop
systems with the ability to define and compose distributed ML computations around ... library on a single machine, this implementation behaves as expected.

Arduino programing of ML-style in ATS - ML Family Workshop
binaries generated from ATS source are very close (in terms of size) to those generated from the C counterpart. 2. ATS programming language. ATS is a programming language equipped with a highly expressive type system rooted in the framework Applied T

Tierless Modules - The ML Family Workshop
Web, client/server, OCaml, ML, Eliom, functional, module. 1 INTRODUCTION. Traditional Web applications are composed of several dis- tinct tiers: Web pages ...

Ambiguous pattern variables - The ML Family Workshop
Jul 29, 2016 - Let us define .... where the Bi,k are binding sets, sets of variables found ... new rows bind to a different position. [Bi,1 ... Bi,l. | K(q1,...,qk) pi,2.

Sundials/ML: interfacing with numerical solvers - ML Family Workshop
Sep 22, 2016 - 4. REFERENCES. [1] T. Bourke and M. Pouzet. Zélus: A synchronous language with ODEs. In HSCC, pages 113–118. ACM. Press, Apr. 2013.

Sundials/ML: interfacing with numerical solvers - ML Family Workshop
Sep 22, 2016 - [email protected]. Jun Inoue. National Institute of Advanced. Industrial Science and. Technology. [email protected]. Marc Pouzet. Univ. Pierre et Marie Curie. École normale supérieure,. PSL Research University. Inria Paris.

Typer: An infix statically typed Lisp - The ML Family Workshop
Oxford, UK, September 2017 (ML'2017), 2 pages. ... the syntax of macro calls is just as exible as that of any other .... Conference on Functional Programming.

GADTs and exhaustiveness: looking for the impossible - ML Family ...
... !env expected_ty) expected_ty k else k (mkpat Tpat_any expected_ty). | Ppat_or (sp1, sp2) -> (* or pattern *) if mode = Check then let state = save_state env in try type_pat sp1 expected_ty k with exn ->. 3The code is available through OCaml's Su

GADTs and exhaustiveness: looking for the impossible - ML Family ...
log's SLD resolution, for which counter-example genera- tion (i.e. construction of a witness term) is known to be only semi-decidable. Another way to see it is that ...

Extracting from F* to C: a progress report - The ML Family Workshop
raphy (ECC) primitives, and on extracting this code to C. ... verification extract the code back to C. .... pointers are made up of a block identifier along with an.

Extracting from F* to C: a progress report - The ML Family Workshop
sub-tree untouched. In short, hyperheaps provide framing guarantees. Each sub-tree is assigned a region-id (rid), and a hyperheap maps an rid to a heap.

Nullable Type Inference - OCaml
Dec 11, 2002 - [1] Apple (2014): Swift, a new programming language for iOS and. OS X. Available at https://developer.apple.com/swift. [2] Facebook (2014): ...

Polymorphism, subtyping and type inference in MLsub - ML Family ...
Sep 3, 2015 - Polymorphism, subtyping and type inference in. MLsub. Stephen Dolan and Alan Mycroft ... We have two tricks for getting around the difficulties: • Define types properly. • Only use half of them. 2 ... Any two types have a greatest c

Billerica Public Schools Family Workshop ...
Mar 8, 2016 - This workshop is an introduction to Google Apps such as Google Docs, Slides, Calendar, and Gmail ... Parents will learn about how to use Aspen to it's greatest potential by reviewing settings, setting up .... home by explaining the posi

Nullable Type Inference - OCaml
Dec 11, 2002 - Imperative programming languages, such as C or Java deriva- tives, make abundant ... In languages using the ML type discipline, the option type type α option ..... //docs.hhvm.com/manual/en/hack.nullable.php. [3] Facebook ...

Polymorphism, subtyping and type inference in MLsub - ML Family ...
Sep 3, 2015 - Polymorphism, subtyping and type inference in. MLsub. Stephen Dolan and Alan Mycroft ... We have two tricks for getting around the difficulties: • Define types properly. • Only use half of them. 2 ... Any two types have a greatest c

ocaml for scientists pdf
Page 1 of 1. File: Ocaml for scientists pdf. Download now. Click here if your download doesn't start automatically. Page 1 of 1. ocaml for scientists pdf. ocaml for ...

ocaml for scientists pdf
ocaml for scientists pdf. ocaml for scientists pdf. Open. Extract. Open with. Sign In. Main menu. Displaying ocaml for scientists pdf.

Page 1 Z 7654 ML ML LEAL ML ML 8_2m1L _22.13_ _BML _BML ...
S e e e cl S t L_l cl 1 o. TITLE: ñrch BLE v1.84. Design: v? 32. 31. 29. 28. || 27. 26. 25. 19. En „3 21. En ai 22. En „5 23. En ná 24. 123456789 ...