Skip to content

Instantly share code, notes, and snippets.

@gdsfh
Created April 9, 2012 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdsfh/27fabcf4a198ad29c05e to your computer and use it in GitHub Desktop.
Save gdsfh/27fabcf4a198ad29c05e to your computer and use it in GitHub Desktop.
Require Import List.
Definition list1 (A : Type) := { lst1 : list A | lst1 <> nil }.
Definition of_h_t (A : Type) (h : A) (t : list A) : list1 A.
refine (exist _ (h :: t) _).
discriminate.
Defined.
Extraction of_h_t.
Lemma nil_neq_nil : forall A:Type, (nil <> (nil : list A)) -> False.
intros A H.
case H.
reflexivity.
Qed.
Definition list1_destruct (A : Type) (lst1 : list1 A)
: (A * list A)
:=
match lst1 with
| exist nil proof => False_rect _ (nil_neq_nil A proof)
| exist (cons h t) _proof => (h, t)
end
.
Extraction list1_destruct.
Definition map' (A B : Type) (f : A -> B) (a : list1 A) : list1 B :=
let (ahd, atl) := list1_destruct A a in
let bhd := f ahd in
let btl := List.map f atl in
of_h_t B bhd btl
.
Extraction map'.
Definition map (A B : Type) (f : A -> B) (a : list1 A) : list1 B.
refine (
match a with
| exist alst _ => exist _ (List.map f alst) _
end
).
destruct alst; unfold map; try discriminate; auto.
Defined.
Extraction map.
@gdsfh
Copy link
Author

gdsfh commented Apr 10, 2012

$ coqc List1.v
type 'a list1 = 'a list
(** val of_h_t : 'a1 -> 'a1 list -> 'a1 list1 **)

let of_h_t h t =
  h :: t
(** val list1_destruct : 'a1 list1 -> 'a1 * 'a1 list **)

let list1_destruct = function
| [] -> assert false (* absurd case *)
| h :: t -> (h, t)
(** val map' : ('a1 -> 'a2) -> 'a1 list1 -> 'a2 list1 **)

let map' f a =
  let (ahd, atl) = list1_destruct a in
  let bhd = f ahd in let btl = map f atl in of_h_t bhd btl
(** val map0 : ('a1 -> 'a2) -> 'a1 list1 -> 'a2 list1 **)

let map0 f a =
  map f a

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