Library CoqFFI.Data.Seq

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

From Coq Require Import Extraction.

Types


Inductive t (a : Type) : Type :=
| Seq : (unit node a) t a
with node (a : Type) : Type :=
| Nil : node a
| Cons (x0 : a) (x1 : t a) : node a.

Pure functions


Definition unseq `(s : t a) : node a :=
  match s with
  | Seq ff tt
  end.

Definition unpack `(s : t a) : option (a × t a) :=
  match unseq s with
  | Cons x rstSome (x, rst)
  | _None
  end.

Definition empty {a} : t a := Seq (fun _Nil).

Definition ret `(x : a) : t a := Seq (fun _Cons x empty).

Definition cons `(x : a) (rst : t a) := Seq (fun _Cons x rst).

Fixpoint append `(x : t a) (y : t a) : t a :=
  Seq (fun _match unseq x with
                | Cons x rstCons x (append rst y)
                | Nilunseq y
                end).

Fixpoint map `(f : a b) (x : t a) : t b :=
  Seq (fun _match unseq x with
                | Cons x rstCons (f x) (map f rst)
                | NilNil
                end).

Fixpoint filter `(f : a bool) (x : t a) : t a :=
  Seq (fun _match unseq x with
                | Cons x rstif f x
                                then Cons x (filter f rst)
                                else unseq (filter f rst)
                | NilNil
                end).

Fixpoint filter_map `(f : a option b) (x : t a) : t b :=
  Seq (fun _match unseq x with
                | Cons x rstmatch f x with
                                | Some yCons y (filter_map f rst)
                                | _unseq (filter_map f rst)
                                end
                | NilNil
                end).

Fixpoint flat_map `(f : a t b) (x : t a) : t b :=
  Seq (fun _match unseq x with
                | Cons x rstunseq (append (f x) (flat_map f rst))
                | NilNil
                end).

Fixpoint fold_left `(f : a b a) (acc : a) (x : t b) : a :=
  match unseq x with
  | Cons x rstfold_left f (f acc x) rst
  | Nilacc
  end.

Module SeqExtraction.
  Extract Inductive t ⇒ "Stdlib.Seq.t" [""].
  Extract Inductive node ⇒ "Stdlib.Seq.node"
    [ "Stdlib.Seq.Nil" "Stdlib.Seq.Cons" ].
  Extract Constant empty ⇒ "Stdlib.Seq.empty".
  Extract Constant ret ⇒ "Stdlib.Seq.return".
  Extract Constant cons ⇒ "Stdlib.Seq.cons".
  Extract Constant append ⇒ "Stdlib.Seq.append".
  Extract Constant map ⇒ "Stdlib.Seq.map".
  Extract Constant filter ⇒ "Stdlib.Seq.filter".
  Extract Constant filter_map ⇒ "Stdlib.Seq.filter_map".
  Extract Constant flat_map ⇒ "Stdlib.Seq.flat_map".
  Extract Constant fold_left ⇒ "Stdlib.Seq.fold_left".
End SeqExtraction.