Modular implicits Leo White

Fr´ed´eric Bour

August 2, 2014

2

We propose a system for ad-hoc polymorphism in OCaml based on using modules as type-directed implicit parameters.

1

Implicit parameters

We propose adding a new kind of function parameter (implicit M : S) where M is an identifier and S is a module type. For example, we can write the generic print function as follows:

Introduction

module type Show = sig type t val show : t -> unit end

A common criticism of OCaml is its lack of support for ad-hoc polymorphism. The classic example of this is OCaml’s separate addition operators for integers (+) and floating-point numbers (+.). Another example is the need for type-specific printing functions (print int, print string, etc.) rather than a single print function which works across multiple types.

let print ( implicit S : Show ) x = S . show x The type of print is written:

Type classes in Haskell [4] have proved an effective mechanism for supporting ad-hoc polymorphism. They use a type-directed implicit parameter passing mechanism to implement constrained polymorphism. Implicits in Scala [3] provide similar capabilities to type classes via direct support for type-directed implicit parameter passing. Chambart et al. have proposed [1] adding support for type-directed implicit parameters to OCaml to support ad-hoc polymorphism.

( implicit S : Show ) -> S . t -> unit When the print function is used the system attempts to fill-in the implicit S parameter by finding a module with the correct type. For example, print [1.2; 2.3; 4.5] will cause the compiler to search for a module with type Show with type t = float list, and pass it as the implicit argument to the print function.

Taking inspiration from “Modular Type Classes” by Dreyer et al. [2] we describe the design and implementation of a system for ad-hoc polymorphism in OCaml based on passing implicit module parameters to functions based on their module type. We argue that using modules as the basis for our implicits, rather than regular types, is a natural fit for implementing ad-hoc polymorphism. It also allows us to support advanced type class features such as associated types and constructor classes.

3 3.1

Implicit declarations Implicit modules

Our proposal also introduces a new form of module declaration, which makes the module available for use as an implicit parameter. These implicit declarations are regular module definitions annotated with the implicit keyword. For example, an implicit module for printing integers could be defined: 1

4.2

implicit module ShowInt = struct type t = int let show = print_int end

Unlike Haskell type classes, our system considers functor arguments when deciding if a search is ambiguous. For example, the ambiguity of the following implicit declarations:

This declaration both declares a regular module ShowInt and makes it available for use in implicit arguments.

3.2

Backtracking

implicit module DisplayFoo : Display with type t = foo

Implicit parameters

implicit functor DisplayOfShow ( S : Show ) In addition to implicit module declarations the sys: Display with type t = S . t tem also considers any implicit parameters currently in scope (e.g. S inside the body of the function print depends on whether or not there exists an implicit above) to be available for selection as an implicit pa- module with type Show with type t = foo. In standard Haskell, these definitions are rameter. always considered ambiguous, or using the 3.3 Implicit functors OverlappingInstances extension they are always Modules for use as implicit arguments can also be considered unambiguous. By checking the existence constructed using functors. For example, an implicit of a module for argument S of DisplayOfShow, our system is able to decide ambiguity more accurately. functor for printing lists could be defined: Handling such definitions can be seen as a form implicit functor ShowList ( S : Show ) = of backtracking. The DisplayOfShow functor is sestruct lected first but if an implicit module for its argument type t = S . t list S cannot be found the search backtracks and instead let show l = print_list S . show l selects DisplayFoo. end

4.3

This means that if the system is looking for an implicit module with type Show with type t = int list, then it can instead look for an implicit module with type Show with type t = int, and apply ShowList to it to create the required module.

4 4.1

Termination

Type classes ensure the termination of the search procedure through a number of restrictions on instance declarations. However, termination of an implicit parameter search depends on the scope in which the search is performed. For this reason, our system places restrictions on the behaviour of the search directly and reports an error only when a search which breaks these restrictions is actually attempted.

Searching for implicits Ambiguity

It is important that uses of implicit parameters are not ambiguous. If there are two implicit modules with type S in scope then using a function of type (implicit M : S) -> ... is an error. It is not sufficient that the search for implicits finds a solution; it must also ensure that the solution is unique. In cases where the search for an implicit argument would be ambiguous users can instead pass the implicit parameters explicitly. For example:

5 5.1

Future work Constructor classes

Our current prototype does not support constructor classes. For example: ( implicit M : Monad ) -> ’a -> ’a M . t is not a valid type at present, since M.t is a type constructor. However, extending the system to support constructor classes seems both achievable and highly desirable.

print ( implicit ShowInt ) 3

2

6

Acknowledgements

We would like to thank Jeremy Yallop and Anil Madhavapeddy for helpful discussions.

References [1] Pierre Chambart and Gr´egoire Henry. Experiments in generic programming. In OCaml Users and Developers Workshop, 2012. [2] Derek Dreyer, Robert Harper, Manuel M. T. Chakravarty, and Gabriele Keller. Modular type classes. In Proceedings of the 34th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL ’07, pages 63–70, New York, NY, USA, 2007. ACM. [3] Bruno C.d.S. Oliveira, Adriaan Moors, and Martin Odersky. Type classes as objects and implicits. In Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications, OOPSLA ’10, pages 341–360, New York, NY, USA, 2010. ACM. [4] Philip Wadler and Stephen Blott. How to make ad-hoc polymorphism less ad hoc. In Proceedings of the 16th ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pages 60–76. ACM, 1989.

3

Modular implicits

Aug 2, 2014 - Implicits in Scala [3] provide similar capabilities to type classes via direct support for type-directed im- plicit parameter passing. Chambart et al.

97KB Sizes 3 Downloads 277 Views

Recommend Documents

modular
proprietary cable management system cooling system extensive I/O. The V4n Micro ... technical needs while easily morphing into any company's visual identity.

modular design.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. modular design.

Fast and Robust Ray Tracing of General Implicits ... - Semantic Scholar
nents of a domain interval is insufficient to determine a convex hull over the range. This is not the case with an inclusion extension F. (b), which, when evaluated, ...

MODULAR PICS.pdf
Page 3 of 10. MODULAR PICS.pdf. MODULAR PICS.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying MODULAR PICS.pdf. Page 1 of 10.

Modular decking planks
Sep 26, 2003 - (58) Field Of Classi?cation Search .................. .. 52/177 a toP Pomona a bottom Pomona and ?rst and Second Side. 52/180, 181, 574, 579, 480, ...

Fast and Robust Ray Tracing of General Implicits ... - Semantic Scholar
correct visualization [13]. ..... precision is seldom required for accurate visualization [13]. ..... from a cubic environment map invokes no performance penalty.

Modular Origami Polyhedra.pdf
Modular Origami Polyhedra.pdf. Modular Origami Polyhedra.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying Modular Origami Polyhedra.pdf.

Small Modular Biopower Systems - NREL
Small modular biopower systems can help supply electrical power to the more than 2.5 ... require the companies to participate at a higher level. A synopsis of the ...

Modular Robotic Vehicle: Project Overview
Apr 7, 2015 - Mobility: Common Themes. • Safety is paramount. – Getting crew home is top priority in space. – Translates to earth. – Functional redundancy.

Evolution thinks modular
cellular components and may also help us to understand the ... Large complex networks arise in a vast num- ... webs. Social systems are best represented by.

Multi-level Partition of Unity Implicits
f : R3. R provides an adaptive error-controlled approximation of the signed distance function ... given by this partition of unity blending of the local shape approxi-.

Interactive Ray Tracing of Arbitrary Implicits with SIMD ...
on common laptop hardware, with a system that accurately visual- izes any implicit surface .... domains [4, 9], and ray tracing algorithms involving recursive in-.

Examples in Modular representation theory.
In particular we denote K for a field of characteristic. 0 and k for a field of characteristic p. Typically we have O, a DVR with maximal ideal p, K it's field of fractions ...

Encore Prime Modular Furniture pdf.pdf
Page 2 of 31. 07. 09. 13. 15. 08. 10. 12. 14. ENCORE prime. //02. INDEX. SOFA & LOUNGE SEATING. STORAGE COMPACTORS. STORAGE SOLUTIONS. SEATING SOLUTIONS. INSTITUTIONAL FURNITURE. ESTEEMED CLIENTS. AUDITORIUM SEATINGS. PANTRY/ CAFETERIA. DISCOVER THE

Modular reweighting users manual
mechanical analysis of biased equilibrium data”, Computer physics .... I really hope the script library gets big for the sake of beginners, but advanced simulators ...

Codes, Lattices and Modular Forms
Jun 22, 2011 - Email: [email protected] ... More recently, as coding theory ... consider from the view of coding theory, it is necessary to find a ring such ...

development of economical modular experimental ...
camera system are addressed in detail. .... The structure contains mounts for six thrusters, camera .... [13] Y. C. Sun and C. C. Cheah, "Coordinated control of.

moog modular v2.pdf
Presets forarturia modular v download. patches sounds. Page 2 of 2. moog modular v2.pdf. moog modular v2.pdf. Open. Extract. Open with. Sign In. Main menu.

Root Modular Technology Stefanko All.pdf
Second Day Assignment-Engineer a “Suspended Warren Truss” bridge design. 1) Start up a new bridge: Open up the WPBB 2014 Bridge Designer software.

Versatile microrobotics using simple modular ... - Semantic Scholar
Jul 28, 2016 - In addition, the model determines how far off-center the approach can be: the range of allowable ... which we call Δx. We varied the frequency of rotation in our model to investigate the ..... ACS Nano 4, 1799–1804. (2010). 32.

Versatile microrobotics using simple modular ... - Semantic Scholar
Jul 28, 2016 - by allowing a single system to navigate diverse environments and perform ... and requests for materials should be addressed to H.C.F. (email: Henry. ..... Automatic navigation of an untethered device in the artery of a living ...

LOGO! modular pure variants.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. LOGO! modular ...