Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to reflect Inductive definitions #32

Merged
merged 20 commits into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/reify.ml4
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let _ = Goptions.declare_bool_option {
Goptions.optwrite = (fun a -> cast_prop:=a);
}

(* whether Set Template Cast Propositions is on, as needed for erasure in Certicoq *)
let is_cast_prop () = !cast_prop

let pp_constr fmt x = Pp.pp_with fmt (Printer.pr_constr x)
Expand Down Expand Up @@ -85,6 +86,7 @@ struct
let pair a b f s =
Term.mkApp (c_pair, [| a ; b ; f ; s |])

(* reify the constructors in Template.Ast.v, which are the building blocks of reified terms *)
let nAnon = r_reify "nAnon"
let nNamed = r_reify "nNamed"
let kVmCast = r_reify "VmCast"
Expand All @@ -105,6 +107,7 @@ struct
r_reify "tConstruct", r_reify "tConst", r_reify "tInd", r_reify "tUnknown")

let (tdef,tmkdef) = (r_reify "def", r_reify "mkdef")
let (tLocalDef,tLocalAssum) = (r_reify "LocalDef", r_reify "LocalAssum")
let (pConstr,pType,pAxiom,pIn) =
(r_reify "PConstr", r_reify "PType", r_reify "PAxiom", r_reify "PIn")
let tinductive_body = r_reify "inductive_body"
Expand Down Expand Up @@ -564,6 +567,7 @@ struct
else
not_supported trm


let from_coq_pair trm =
let (h,args) = app_full trm [] in
if Term.eq_constr h c_pair then
Expand All @@ -573,6 +577,14 @@ struct
else
not_supported trm

let denote_local_entry unq trm =
let (h,args) = app_full trm [] in
match args with
x :: [] ->
if Term.eq_constr h tLocalDef then Entries.LocalDef (unq x)
else (if Term.eq_constr h tLocalAssum then Entries.LocalAssum (unq x) else bad_term trm)
| _ -> bad_term trm

(** NOTE: Because the representation is lossy, I should probably
** come back through elaboration.
** - This would also allow writing terms with holes
Expand Down Expand Up @@ -647,6 +659,37 @@ struct
else
not_supported trm

let declare_inductive (env: Environ.env) (evm: Evd.evar_map) (body: Constrexpr.constr_expr) : unit =
let (body,_) = Constrintern.interp_constr env evm body in
let (_,args) = app_full body [] in (* check that the first component is Build_mut_ind .. *)
let one_ind b1 : Entries.one_inductive_entry =
let (_,args) = app_full b1 [] in (* check that the first component is Build_one_ind .. *)
match args with
| mt::ma::mtemp::mcn::mct::[] ->
{
mind_entry_typename = unquote_ident mt;
mind_entry_arity = denote_term ma;
mind_entry_template = from_bool mtemp;
mind_entry_consnames = List.map unquote_ident (from_coq_list mcn);
mind_entry_lc = List.map denote_term (from_coq_list mct)
}
| _ -> raise (Failure "ill-typed one_inductive_entry")
in
let mut_ind mr mf mp mi mpol mpr : Entries.mutual_inductive_entry =
{
mind_entry_record = None; (* mr *)
mind_entry_finite = Decl_kinds.Finite; (* inductive *)
mind_entry_params = List.map (fun p -> let (l,r) = (from_coq_pair p) in (unquote_ident l, (denote_local_entry denote_term r))) (from_coq_list mp);
mind_entry_inds = List.map one_ind (from_coq_list mi);
mind_entry_polymorphic = from_bool mpol;
mind_entry_universes = Univ.UContext.empty;
mind_entry_private = None (*mpr*)
} in
match args with
mr::mf::mp::mi::mpol::mpr::[] ->
Command.declare_mutual_inductive_with_eliminations (mut_ind mr mf mp mi mpol mpr) [] [];()
| _ -> raise (Failure "ill-typed mutual_inductive_entry")

end

DECLARE PLUGIN "template_plugin"
Expand All @@ -671,7 +714,7 @@ let to_ltac_val c = Tacexpr.TacDynamic(Loc.ghost,Pretyping.constr_in c)

(** From Containers **)
let declare_definition
id (loc, boxed_flag, def_obj_kind)
(id : Names.Id.t) (loc, boxed_flag, def_obj_kind)
(binder_list : Constrexpr.local_binder list) red_expr_opt constr_expr
constr_expr_opt decl_hook =
Command.do_definition
Expand Down Expand Up @@ -766,6 +809,14 @@ VERNAC COMMAND EXTEND Unquote_vernac CLASSIFIED AS SIDEFF
[] None result None (Lemmas.mk_hook (fun _ _ -> ())) ]
END;;

(* get rid of the unused ident(name)? *)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

VERNAC COMMAND EXTEND Unquote_inductive CLASSIFIED AS SIDEFF
| [ "Make" "Inductive" constr(def) ] ->
[ check_inside_section () ;
let (evm,env) = Lemmas.get_current_context () in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to check_inside_section here?

TermReify.declare_inductive env evm def ]
END;;

VERNAC COMMAND EXTEND Make_tests CLASSIFIED AS QUERY
(*
| [ "Make" "Definitions" tactic(t) ] ->
Expand Down
77 changes: 77 additions & 0 deletions test-suite/demo.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,80 @@ Make Definition two_from_syntax := (Ast.tApp (Ast.tConstruct (Ast.mkInd "Coq.Ini
Quote Recursively Definition plus_syntax := plus.

Quote Recursively Definition mult_syntax := mult.

Make Definition d''_from_syntax := ltac:(let t:= eval compute in d'' in exact t).




(** Reflecting Inductives *)

Require Import List.
Import ListNotations.
Require Import Template.Ast.

Definition one_i : one_inductive_entry :=
{|
mind_entry_typename := "demoBool";
mind_entry_arity := tSort sSet;
mind_entry_template := false;
mind_entry_consnames := ["demoTrue"; "demoFalse"];
mind_entry_lc := [tRel 1; tRel 1];
|}.

Definition one_i2 : one_inductive_entry :=
{|
mind_entry_typename := "demoBool2";
mind_entry_arity := tSort sSet;
mind_entry_template := false;
mind_entry_consnames := ["demoTrue2"; "demoFalse2"];
mind_entry_lc := [tRel 0; tRel 0];
|}.

Definition mut_i : mutual_inductive_entry :=
{|
mind_entry_record := None;
mind_entry_finite := Finite;
mind_entry_params := [];
mind_entry_inds := [one_i; one_i2];
mind_entry_polymorphic := false;
mind_entry_private := None;
|}.

(* Make Inductive ltac:(let t:= eval compute in mut_i in exact t). *)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this perform reduction so that we don't need to use tactics in terms to make it work? I don't know precisely how to do this, but I'm certain that @mattam82 could tell us in a minute.



Definition mkImpl (A B : term) : term :=
tProd nAnon A B.


Definition one_list_i : one_inductive_entry :=
{|
mind_entry_typename := "demoList";
mind_entry_arity := tSort sSet;
mind_entry_template := false;
mind_entry_consnames := ["demoNil"; "demoCons"];
mind_entry_lc := [tApp (tRel 1) [tRel 0];
mkImpl (tRel 0) (mkImpl (tApp (tRel 2) [tRel 1]) (tApp (tRel 3) [tRel 2]))];
|}.

Definition mut_list_i : mutual_inductive_entry :=
{|
mind_entry_record := None;
mind_entry_finite := Finite;
mind_entry_params := [("A", LocalAssum (tSort sSet))];
mind_entry_inds := [one_list_i];
mind_entry_polymorphic := false;
mind_entry_private := None;
|}.


Make Inductive ltac:(let t:= eval compute in mut_list_i in exact t).

Print demoList.

(*
Inductive demoList (A : Set) : Set :=
demoNil : demoList A | demoCons : A -> demoList A -> demoList A
*)

46 changes: 46 additions & 0 deletions theories/Ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,49 @@ Inductive program : Set :=
list (ident * inductive_body) -> program -> program
| PAxiom : ident -> term (* the type *) -> program -> program
| PIn : term -> program.


(** representation of mutual inductives. nearly copied from Coq/kernel/entries.mli
*)

Record one_inductive_entry : Set := {
mind_entry_typename : ident;
mind_entry_arity : term;
mind_entry_template : bool; (* template polymorphism ? *)
mind_entry_consnames : list ident;
mind_entry_lc : list term}.


Inductive local_entry : Set :=
| LocalDef : term -> local_entry (* local let binding *)
| LocalAssum : term -> local_entry.

(*
type local_entry =
| LocalDef of constr
| LocalAssum of constr
*)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the next comment.


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(** The kind of definition. See kerne/....mli **)

Inductive recursivity_kind :=
| Finite (** = inductive *)
| CoFinite (** = coinductive *)
| BiFinite (** = non-recursive, like in "Record" definitions *).


(*
type recursivity_kind =
| Finite
| CoFinite
| BiFinite
*)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an out-of-place comment.


(* kernel/entries.mli*)
Record mutual_inductive_entry : Set := {
mind_entry_record : option (option ident);
mind_entry_finite : recursivity_kind;
mind_entry_params : list (ident * local_entry);
mind_entry_inds : list one_inductive_entry;
mind_entry_polymorphic : bool;
(* mind_entry_universes : Univ.universe_context; *)
mind_entry_private : option bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed private is for the HIT trick

}.