(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 2 of the License, or  *)
(*  (at your option) any later version.  This file is also distributed *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Error reporting and the error monad. *)

Require Import String.
Require Import Coqlib.

Close Scope string_scope.

Set Implicit Arguments.

(** * Representation of error messages. *)

(** Compile-time errors produce an error message, represented in Coq
  as a list of either substrings or positive numbers encoding
  a source-level identifier (see module AST). *)

Inductive errcode: Type :=
  | MSG: string -> errcode
  | CTX: positive -> errcode    (* a top-level identifier *)
  | POS: positive -> errcode.   (* a positive integer, e.g. a PC *)

Definition errmsg: Type := list errcode.

Definition msg (s: string) : errmsg := MSG s :: nil.

(** * The error monad *)

(** Compilation functions that can fail have return type [res A].
  The return value is either [OK res] to indicate success,
  or [Error msg] to indicate failure. *)

Inductive res (A: Type) : Type :=
| OK: A -> res A
| Error: errmsg -> res A.

Arguments Error [A].

(** To automate the propagation of errors, we use a monadic style
  with the following [bind] operation. *)

Definition bind (A B: Type) (f: res A) (g: A -> res B) : res B :=
  match f with
  | OK x => g x
  | Error msg => Error msg
  end.

Definition bind2 (A B C: Type) (f: res (A * B)) (g: A -> B -> res C) : res C :=
  match f with
  | OK (x, y) => g x y
  | Error msg => Error msg
  end.

(** The [do] notation, inspired by Haskell's, keeps the code readable. *)

Notation "'do' X <- A ; B" := (bind A (fun X => B))
 (at level 200, X ident, A at level 100, B at level 200)
 : error_monad_scope.

Notation "'do' ( X , Y ) <- A ; B" := (bind2 A (fun X Y => B))
 (at level 200, X ident, Y ident, A at level 100, B at level 200)
 : error_monad_scope.

Remark bind_inversion:
  forall (A B: Type) (f: res A) (g: A -> res B) (y: B),
  bind f g = OK y ->
  exists x, f = OK x /\ g x = OK y.
Proof.
(* Goal: forall (A B : Type) (f : res A) (g : forall _ : A, res B) (y : B) (_ : @eq (res B) (@bind A B f g) (@OK B y)), @ex A (fun x : A => and (@eq (res A) f (@OK A x)) (@eq (res B) (g x) (@OK B y))) *)
  intros until y. destruct f; simpl; intros.
(* Goal: @ex A (fun x : A => and (@eq (res A) (@OK A a) (@OK A x)) (@eq (res B) (g x) (@OK B y))) *)
(* Goal: @ex A (fun x : A => and (@eq (res A) (@Error A e) (@OK A x)) (@eq (res B) (g x) (@OK B y))) *)
  exists a; auto.
(* Goal: @ex A (fun x : A => and (@eq (res A) (@Error A e) (@OK A x)) (@eq (res B) (g x) (@OK B y))) *)
  discriminate.
Qed.

Remark bind2_inversion:
  forall (A B C: Type) (f: res (A*B)) (g: A -> B -> res C) (z: C),
  bind2 f g = OK z ->
  exists x, exists y, f = OK (x, y) /\ g x y = OK z.
Proof.
(* Goal: forall (A B C : Type) (f : res (prod A B)) (g : forall (_ : A) (_ : B), res C) (z : C) (_ : @eq (res C) (@bind2 A B C f g) (@OK C z)), @ex A (fun x : A => @ex B (fun y : B => and (@eq (res (prod A B)) f (@OK (prod A B) (@pair A B x y))) (@eq (res C) (g x y) (@OK C z)))) *)
  intros until z. destruct f; simpl.
(* Goal: forall _ : @eq (res C) (let (x, y) := p in g x y) (@OK C z), @ex A (fun x : A => @ex B (fun y : B => and (@eq (res (prod A B)) (@OK (prod A B) p) (@OK (prod A B) (@pair A B x y))) (@eq (res C) (g x y) (@OK C z)))) *)
(* Goal: forall _ : @eq (res C) (@Error C e) (@OK C z), @ex A (fun x : A => @ex B (fun y : B => and (@eq (res (prod A B)) (@Error (prod A B) e) (@OK (prod A B) (@pair A B x y))) (@eq (res C) (g x y) (@OK C z)))) *)
  destruct p; simpl; intros. exists a; exists b; auto.
(* Goal: @ex A (fun x : A => and (@eq (res A) (@Error A e) (@OK A x)) (@eq (res B) (g x) (@OK B y))) *)
  intros; discriminate.
Qed.

(** Assertions *)

Definition assertion_failed {A: Type} : res A := Error(msg "Assertion failed").

Notation "'assertion' A ; B" := (if A then B else assertion_failed)
  (at level 200, A at level 100, B at level 200)
  : error_monad_scope.

(** This is the familiar monadic map iterator. *)

Local Open Scope error_monad_scope.

Fixpoint mmap (A B: Type) (f: A -> res B) (l: list A) {struct l} : res (list B) :=
  match l with
  | nil => OK nil
  | hd :: tl => do hd' <- f hd; do tl' <- mmap f tl; OK (hd' :: tl')
  end.

Remark mmap_inversion:
  forall (A B: Type) (f: A -> res B) (l: list A) (l': list B),
  mmap f l = OK l' ->
  list_forall2 (fun x y => f x = OK y) l l'.
Proof.
(* Goal: forall (A B : Type) (f : forall _ : A, res B) (l : list A) (l' : list B) (_ : @eq (res (list B)) (@mmap A B f l) (@OK (list B) l')), @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) l l' *)
  induction l; simpl; intros.
(* Goal: @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) (@nil A) l' *)
(* Goal: @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) (@cons A a l) l' *)
  inversion_clear H. constructor.
(* Goal: @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) (@cons A a l) l' *)
  destruct (bind_inversion _ _ H) as [hd' [P Q]].
(* Goal: @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) (@cons A a l) l' *)
  destruct (bind_inversion _ _ Q) as [tl' [R S]].
(* Goal: @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) (@cons A a l) l' *)
  inversion_clear S.
(* Goal: @list_forall2 A B (fun (x : A) (y : B) => @eq (res B) (f x) (@OK B y)) (@cons A a l) (@cons B hd' tl') *)
  constructor. auto. auto.
Qed.

(** * Reasoning over monadic computations *)

(** The [monadInv H] tactic below simplifies hypotheses of the form
<<
        H: (do x <- a; b) = OK res
>>
    By definition of the bind operation, both computations [a] and
    [b] must succeed for their composition to succeed.  The tactic
    therefore generates the following hypotheses:

         x: ...
        H1: a = OK x
        H2: b x = OK res
*)

Ltac monadInv1 H :=
  match type of H with
  | (OK _ = OK _) =>
      inversion H; clear H; try subst
  | (Error _ = OK _) =>
      discriminate
  | (bind ?F ?G = OK ?X) =>
      let x := fresh "x" in (
      let EQ1 := fresh "EQ" in (
      let EQ2 := fresh "EQ" in (
      destruct (bind_inversion F G H) as [x [EQ1 EQ2]];
      clear H;
      try (monadInv1 EQ2))))
  | (bind2 ?F ?G = OK ?X) =>
      let x1 := fresh "x" in (
      let x2 := fresh "x" in (
      let EQ1 := fresh "EQ" in (
      let EQ2 := fresh "EQ" in (
      destruct (bind2_inversion F G H) as [x1 [x2 [EQ1 EQ2]]];
      clear H;
      try (monadInv1 EQ2)))))
  | (match ?X with left _ => _ | right _ => assertion_failed end = OK _) =>
      destruct X; [try (monadInv1 H) | discriminate]
  | (match (negb ?X) with true => _ | false => assertion_failed end = OK _) =>
      destruct X as [] eqn:?; [discriminate | try (monadInv1 H)]
  | (match ?X with true => _ | false => assertion_failed end = OK _) =>
      destruct X as [] eqn:?; [try (monadInv1 H) | discriminate]
  | (mmap ?F ?L = OK ?M) =>
      generalize (mmap_inversion F L H); intro
  end.

Ltac monadInv H :=
  monadInv1 H ||
  match type of H with
  | (?F _ _ _ _ _ _ _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ _ _ _ _ _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ _ _ _ _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ _ _ _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ _ _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  | (?F _ = OK _) =>
      ((progress simpl in H) || unfold F in H); monadInv1 H
  end.
