playground/coq/unfinished/regex-harper.v

58 lines
1.2 KiB
Coq

Require Import List.
Import ListNotations.
CoInductive b3 : Type := Ctrue | Cfalse | Dunno.
Inductive re (A:Type) : Type :=
| Char: (A->b3) -> re A
| Cat: re A -> re A -> re A
| Alt: re A -> re A -> re A
| Star: re A -> re A.
Arguments Char {A}.
Arguments Cat {A}.
Arguments Alt {A}.
Arguments Star {A}.
CoFixpoint acc {A:Type} (old:b3) (r:re A)
(l:list A) (k:list A -> b3) : b3 :=
match old with
| Ctrue => Ctrue
| Cfalse => Cfalse
| _ =>
match r with
| Char f =>
match l with
| [] => Cfalse
| x::xs =>
match f x with
| Ctrue => k xs
| _ => Cfalse
end
end
| Cat r1 r2 => acc old r1 l (fun xs => acc old r2 xs k)
| Alt r1 r2 => Ctrue
| Star r1 => Ctrue
end
end.
Fixpoint acc {A:Type} (r:re A) (l:list A) (k:list A -> bool)
: bool :=
match r,l with
| Char _, [] => false
| Char f, x::xs =>
if f x then k xs
else false
| Cat r1 r2, _ => acc r1 l (fun xs => acc r2 xs k)
| Alt r1 r2, _ =>
let rv1 := acc r1 l k in
let rv2 := acc r2 l k in
orb rv1 rv2
| Star r1, _ =>
let rv1 := k l in
let rv2 := acc (Star r1) l k in
orb rv1 rv2
end.