Giter Site home page Giter Site logo

distwit's Introduction

Distwit -- "Distributed Witnesses" -- make it possible to use exceptions and extensible variant types with Marshalling.

Principle

Extension constructors (including exceptions) are generated at runtime. Each "exception" or "type t += A" statement allocate a new constructor.

A constructor witnesses the type of its arguments. If two objects have the same constructor (by physical equality), then parameters of the constructor have the same type.

But physical equality does not span across processes, it is lost when marshalling. Unmarshalling creates a new copy which will never match the existing witness.

Type safety is preserved, but equalities are lost. (Otherwise, imagine matching constructors generated in a similar way but with slightly different types and exchanged between processes: it would be possible to introduce arbitrary false equalities).

The difficulty is the generation of unique witnesses independent of an address space ("pure" values).

A solution is to delegate the generation of witnesses to an external trusted "gensym" service. As long as generated symbols don't collide, type safety can be preserved.

Distwit implements this idea: it turns symbols generated by an arbitrary service (provided as an argument to the main functor) into witnesses valid for OCaml runtime (locally bypassing the typechecker!).

Precautions for use

This implementation produces witnesses valid for OCaml 4.03 and 4.04 runtimes. They may or may not be valid for later versions.

Safety relies on the validity of the symbol generator. Freshly generated symbol should be unique. As usual with marshalling, this should be used between trusted processes.

Furthermore, symbols registered by user (via [register] function) should only be used with the exact same constructor -- otherwise incorrect equalities are generated.

Example

An example using Uuidm as a symbol generator:

module W = Distwit.Make_unsafe(struct
    type t = Uuidm.t
    let equal = Uuidm.equal
    let hash = Hashtbl.hash

    let fresh () = Uuidm.v5 Uuidm.nil "distwit"
  end)

let roundtrip (x : 'a) : 'a =
  Marshal.from_string (Marshal.to_string x []) 0

let exn = Failure "..."

let () = match roundtrip exn with
  | Failure _ ->
    prerr_endline "marshalling preserved identity, that's unexpected!";
    assert false
  | _ ->
    prerr_endline "marshalling lost identity";
    ()

let () = match W.unwrap (roundtrip (W.wrap exn)) with
  | Failure _ ->
    prerr_endline "distwit recovered identity"
  | _ ->
    prerr_endline "distwit didn't recover identity, that's wrong!";
    assert false

distwit's People

Contributors

let-def avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

distwit's Issues

Support for OCaml 4.06

Would it be possible to port this library to 4.06? I might have a use-case where I could be useful :-)

Also, any reason to restrict it to exceptions? Would it work for open types?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.