playground/coq/third-party/extlib-hlist.v
2023-05-22 14:41:06 +05:30

102 lines
2.7 KiB
Coq

(* coq-ext-lib *)
(* https://coq-community.org/coq-ext-lib/v0.10.3/ExtLib.Data.HList.html *)
(* https://coq-community.org/coq-ext-lib/v0.10.3/ExtLib.Data.Member.html *)
From ExtLib Require Import HList.
Module HListNotations.
Declare Scope hlist_scope.
Delimit Scope hlist_scope with hlist.
Notation "« »" := Hnil : hlist_scope.
Notation "« x »" := (Hcons x Hnil) : hlist_scope.
Notation "« x ; .. ; y »" := (Hcons x .. (Hcons y Hnil) ..) : hlist_scope.
Infix "++" := hlist_app: hlist_scope.
End HListNotations.
Require Import String List.
Open Scope string.
Import ListNotations.
Import HListNotations.
Open Scope hlist.
Check «3; true» : hlist (fun x:Set=>x) [nat; bool].
(* « 3; true » : hlist (fun x : Set => x) [nat; bool] *)
Example eg1 := «3» : hlist (fun x:Set=>x) [nat].
Example eg2 := «2; true»: hlist (fun x:Set=>x) [nat; bool].
Example eg3 := «(0,3)» : hlist (fun x:Set=>(x*nat)%type) [nat].
Example eg4 := «2; true; "h"; (2,3)»: hlist (fun x:Set=>x) [nat; bool; string; (nat*nat)%type].
Check «» : hlist (fun x:Set=>x) [].
(* « » : hlist (fun x : Set => x) [] *)
Check « "hi" » : hlist (fun x:Set=>x) [string].
(* « "hi" » : hlist (fun x : Set => x) [string] *)
Check «(0,3); (1,true)» : hlist (fun x:Set=>(nat*x)%type) [nat; bool].
(* « (0, 3); (1, true) » *)
Compute hlist_hd eg1.
(* = 3 : nat *)
Compute hlist_hd eg3.
(* = (0, 3) : nat * nat *)
Compute hlist_tl eg1.
(* = « » : hlist (fun x : Set => x) [] *)
Compute hlist_tl eg2.
(* = « true » : hlist (fun x : Set => x) [bool] *)
Compute eg1 ++ eg2 ++ eg2.
(* = « 3; 2; true; 2; true » *)
Compute hlist_tl (eg1 ++ eg2 ++ eg2).
(* = « 2; true; 2; true » *)
From ExtLib Require Import Member.
(* [Member.member] is like the proof relevant version of [List.In] *)
(* Kinda like the relation between [sumbool] and [bool] *)
Print member.
(** Indexing *)
Compute hlist_get (MZ _ _) eg2.
(* = 2 : nat *)
Compute hlist_get (MN _ (MZ _ _)) eg2.
(* = true : bool *)
Compute hlist_get (MN _ (MN _ (MZ _ _))) eg4.
(* = "h" : string *)
Compute hlist_get (MN _ (MN _ (MN _ (MZ _ _)))) eg4.
(* = (2, 3) : nat * nat *)
(** Getting nth element *)
Compute hlist_nth eg4 2.
(* = "h"
: match nth_error [nat; bool; string; (nat * nat)%type] 2 with
| Some t => t
| None => unit
end
*)
Compute nth_error[nat; bool; string; (nat * nat)%type] 2.
(* = Some string : option Set *)
Unset Printing Notations.
(** Converting list to hlist ?? *)
Compute hlist_gen _ [1].
(* = Hcons (?f 1) Hnil
: hlist ?F (cons 1 nil) *)
Compute hlist_gen (fun x=>x) [1].
(* = Hcons 1 Hnil
: hlist (fun _ : nat => nat) (cons 1 nil) *)
(** Converting hlist to list ?? *)
(* https://coq-community.org/coq-ext-lib/v0.10.3/ExtLib.Data.HList.html *)
Fail Check hlist_erase eg1.