lthms / coqffi

Coq to OCaml FFI made easy [maintainer=@lthms]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

coqffi

Travis Contributing Code of Conduct Zulip

coqffi generates the necessary Coq boilerplate to use OCaml functions in a Coq development, and configures the Coq extraction mechanism accordingly.

Meta

  • Author(s):
    • Thomas Letan
    • Li-yao Xia
    • Yann Régis-Gianas
    • Yannick Zakowski
  • Coq-community maintainer(s):
  • License: MIT License
  • Compatible Coq versions: 8.12 or later
  • Compatible OCaml versions: 4.10
  • Additional dependencies:
  • Coq namespace: CoqFFI
  • Related publication(s): none

Building and installation instructions

git clone https://github.com/coq-community/coqffi.git
cd coqffi
dune build -p coq-coqffi
dune install

Example

Suppose the following OCaml header file (file.mli) is given:

open Coqbase

type fd

val fd_equal : fd -> fd -> bool

val openfile : Bytestring.t -> fd [@@impure]
val read_all : fd -> Bytestring.t [@@impure]
val write : fd -> Bytestring.t -> unit [@@impure]
val closefile : fd -> unit [@@impure]

coqffi then generates the necessary Coq boilerplate to use these functions in a Coq development:

(* This file has been generated by coqffi. *)

Set Implicit Arguments.
Unset Strict Implicit.
Set Contextual Implicit.
Generalizable All Variables.

From Base Require Import Prelude Extraction.
From SimpleIO Require Import IO_Monad.
From CoqFFI Require Import Interface.

(** * Types *)

Axiom (fd : Type).

Extract Constant fd => "Examples.File.fd".

(** * Pure Functions *)

Axiom (fd_equal : fd -> fd -> bool).

Extract Constant fd_equal => "Examples.File.fd_equal".

(** * Impure Primitives *)

(** ** Monad *)

Class MonadFile (m : Type -> Type) : Type :=
  { openfile : bytestring -> m fd
  ; read_all : fd -> m bytestring
  ; write : fd -> bytestring -> m unit
  ; closefile : fd -> m unit
  }.

(** ** [IO] Instance *)

Axiom (io_openfile : bytestring -> IO fd).
Axiom (io_read_all : fd -> IO bytestring).
Axiom (io_write : fd -> bytestring -> IO unit).
Axiom (io_closefile : fd -> IO unit).

Extract Constant io_openfile =>
  "(fun x0 k__ -> k__ (Examples.File.openfile x0))".
Extract Constant io_read_all =>
  "(fun x0 k__ -> k__ (Examples.File.read_all x0))".
Extract Constant io_write =>
  "(fun x0 x1 k__ -> k__ (Examples.File.write x0 x1))".
Extract Constant io_closefile =>
  "(fun x0 k__ -> k__ (Examples.File.closefile x0))".

Instance MonadFile_IO : MonadFile IO :=
  { openfile := io_openfile
  ; read_all := io_read_all
  ; write := io_write
  ; closefile := io_closefile
  }.

See the coqffi man pages for more information on how to use it.

About

Coq to OCaml FFI made easy [maintainer=@lthms]

License:MIT License


Languages

Language:OCaml 54.0%Language:Coq 46.0%