Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created March 23, 2013 15:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t0yv0/5228123 to your computer and use it in GitHub Desktop.
Save t0yv0/5228123 to your computer and use it in GitHub Desktop.
A simple example of how to use Coq as a programming language - how to compute more efficient programs out of declarative programs, and then extract the efficient programs to ML. This facility is very obvious to serious users of Coq, but it took me a while as a beginner to realize this is practical and how can it be done. Compilers like Haskell d…
Require Import String.
Definition Parser (a : Type) :=
string -> option (a * string).
Definition Return {a} (x: a) : Parser a :=
fun s => Some (x, s).
Definition Bind {a b} (x: Parser a) (f: a -> Parser b) : Parser b :=
fun s =>
match x s with
| None => None
| Some (x, s) => f x s
end.
Definition Map {a b} (f : a -> b) (x : Parser a) : Parser b :=
Bind x (fun x => Return (f x)).
Definition MapOptimized {a b} (f: a -> b) (x: Parser a) : Parser b.
Proof.
let r := (eval compute in (@Map a b f x)) in set (R := r).
unfold Parser.
apply R.
Defined.
Recursive Extraction MapOptimized.
@siraben
Copy link

siraben commented Apr 15, 2021

On Coq 8.13, this needs Require Import Extraction. just before the extraction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment