Skip to content

Commit 1caf16c

Browse files
committed
Merge remote-tracking branch 'dk/master' into parse
2 parents d8f2434 + b5faa85 commit 1caf16c

20 files changed

+215
-238
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ bnf:
2828
sanity_check: misc/sanity_check.sh
2929
@./$<
3030

31-
.PHONY: tests
32-
tests: test
31+
.PHONY: test_all
32+
test_all: test
3333
$(MAKE) test_load
3434
$(MAKE) test_export_dk
3535
$(MAKE) test_export_lp

src/core/eval.ml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ let snf : ?dtree:(sym -> dtree) -> term reducer = fun ?dtree ?tags c t ->
490490

491491
let snf ?dtree = time_reducer mk_Kind (snf ?dtree)
492492

493+
let snf_beta t = snf ~tags:[`NoRw; `NoExpand] [] t
494+
493495
(** [hnf c t] computes a hnf of [t], unfolding the variables defined in the
494496
context [c], and using user-defined rewrite rules. *)
495497
let hnf : term reducer = fun ?tags c t ->
@@ -530,23 +532,6 @@ let whnf : term reducer = fun ?tags c t ->
530532

531533
let whnf = time_reducer mk_Kind whnf
532534

533-
(** [beta_simplify c t] computes a beta whnf of [t] in context [c] belonging
534-
to the set S such that (1) terms of S are in beta whnf normal format, (2)
535-
if [t] is a product, then both its domain and codomain are in S. *)
536-
let beta_simplify : ctxt -> term -> term = fun c ->
537-
let tags = [`NoRw; `NoExpand] in
538-
let rec simp t =
539-
match get_args (whnf ~tags c t) with
540-
| Prod(a,b), _ ->
541-
let x, b = unbind b in
542-
mk_Prod (simp a, bind_var x (simp b))
543-
| h, ts -> add_args_map h (whnf ~tags c) ts
544-
in simp
545-
546-
let beta_simplify =
547-
let open Stdlib in let r = ref mk_Kind in fun c t ->
548-
Debug.(record_time Rewriting (fun () -> r := beta_simplify c t)); !r
549-
550535
(** If [s] is a non-opaque symbol having a definition, [unfold_sym s t]
551536
replaces in [t] all the occurrences of [s] by its definition. *)
552537
let unfold_sym_opt : sym -> term -> term option =

src/core/eval.mli

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ val pure_eq_modulo : ?tags:rw_tag list -> ctxt -> term -> term -> bool
6464
defined in the context [c]. The function [dtree] maps symbols to decision
6565
trees. *)
6666
val snf : ?dtree:(sym -> dtree) -> ?tags:rw_tag list -> ctxt -> term -> term
67+
val snf_beta : term -> term
6768

6869
(** [snf_opt ~dtree ~tags c t] computes the snf of [t] in the context [c]. The
6970
function [dtree] maps symbols to decision trees. *)
@@ -74,11 +75,6 @@ val snf_opt : ?dtree:(sym -> dtree) -> ?tags:rw_tag list
7475
context [c]. *)
7576
val hnf : ?tags:rw_tag list -> ctxt -> term -> term
7677

77-
(** [beta_simplify c t] computes a beta whnf of [t] in context [c] belonging
78-
to the set S such that (1) terms of S are in beta whnf normal format, (2)
79-
if [t] is a product, then both its domain and codomain are in S. *)
80-
val beta_simplify : ctxt -> term -> term
81-
8278
(** If [s] is a non-opaque symbol having a definition, [unfold_sym s t]
8379
replaces in [t] all the occurrences of [s] by its definition. *)
8480
val unfold_sym : sym -> term -> term

src/core/libMeta.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ let meta_counter : int Stdlib.ref = Stdlib.ref (-1)
1414
(** [reset_meta_counter ()] resets the counter used to produce meta keys. *)
1515
let reset_meta_counter () = Stdlib.(meta_counter := -1)
1616

17-
(** [fresh p ?name a n] creates a fresh metavariable of type [a] and arity [n]
18-
with the optional name [name], and adds it to [p]. *)
17+
(** [fresh p a n] creates a fresh metavariable of type [a] and arity [n] and
18+
adds it to [p]. *)
1919
let fresh : problem -> term -> int -> meta = fun p a n ->
2020
let m = {meta_key = Stdlib.(incr meta_counter; !meta_counter);
2121
meta_type = ref a; meta_arity = n; meta_value = ref None } in

src/core/print.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ let sym : sym pp = fun ppf s ->
123123

124124
let var : var pp = fun ppf x -> uid ppf (base_name x)
125125

126-
let meta : meta pp = fun ppf m -> out ppf "?%d" m.meta_key
127-
128126
(** Exception raised when trying to convert a term into a nat. *)
129127
exception Not_a_nat
130128

@@ -264,6 +262,11 @@ and quantifier idmap ppf s args =
264262
end
265263
| _ -> assert false
266264

265+
and meta idmap ppf m =
266+
if !print_meta_types then
267+
out ppf "(?%d:%a)" m.meta_key (func idmap) !(m.meta_type)
268+
else out ppf "?%d" m.meta_key
269+
267270
and head idmap wrap ppf t =
268271
let env ppf ts =
269272
if Array.length ts > 0 then
@@ -282,11 +285,7 @@ and head idmap wrap ppf t =
282285
| Type -> out ppf "TYPE"
283286
| Kind -> out ppf "KIND"
284287
| Symb(s) -> sym ppf s
285-
| Meta(m,e) ->
286-
if !print_meta_types then
287-
out ppf "(?%d:%a)" m.meta_key (func idmap) !(m.meta_type)
288-
else out ppf "?%d" m.meta_key;
289-
if !print_meta_args then env ppf e
288+
| Meta(m,e) -> meta idmap ppf m; if !print_meta_args then env ppf e
290289
| Plac(_) -> out ppf "_"
291290
| Patt(_,n,e) -> out ppf "$%a%a" uid n env e
292291
| Bvar _ -> assert false
@@ -420,6 +419,8 @@ let constrs : constr list pp = fun ppf cs ->
420419
let pp_sep ppf () = out ppf "@ ;" in
421420
out ppf "@[<v>[%a]@]" (Format.pp_print_list ~pp_sep constr) cs
422421

422+
let meta : meta pp = meta StrMap.empty
423+
423424
let metaset : MetaSet.t pp =
424425
D.iter ~sep:(fun ppf () -> out ppf ",") MetaSet.iter meta
425426

src/core/unif.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let instantiation :
131131
| None -> None
132132
| Some(vs, map) ->
133133
if LibMeta.occurs m c u then None
134-
else let u = Eval.beta_simplify c (sym_to_var map u) in
134+
else let u = Eval.snf_beta (sym_to_var map u) in
135135
Some ((*Logger.set_debug_in false 'm'*) (bind_mvar vs) u)
136136

137137
(** Checking type or not during meta instanciation. *)

src/handle/command.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Common open Error open Pos
66
open Core open Term open Sign open Sig_state open Print
77
open Parsing open Syntax open Scope
88
open Proof
9+
open Goal
910

1011
(** Type alias for a function that compiles a Lambdapi module. *)
1112
type compiler = Path.t -> Sign.t
@@ -582,7 +583,7 @@ let get_proof_data : compiler -> sig_state -> p_command -> cmd_output =
582583
in
583584
(* Create the proof state. *)
584585
let pdata_state =
585-
let proof_goals = Proof.add_goals_of_problem p [] in
586+
let proof_goals = add_goals_of_problem p [] in
586587
if p_sym_def then
587588
(* Add a new focused goal and refine on it. *)
588589
let m = LibMeta.fresh p a 0 in

src/handle/goal.ml

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
(** Goal. *)
2+
3+
open Lplib open Base open Extra
4+
open Timed
5+
open Core open Term open Print
6+
7+
type goal_typ =
8+
{ goal_meta : meta (** Goal metavariable. *)
9+
; goal_hyps : Env.t (** Precomputed scoping environment. *)
10+
; goal_type : term (** Precomputed type. *) }
11+
12+
type ('a,'b) t = Typ of 'a | Unif of 'b
13+
14+
type goal = (goal_typ, constr) t
15+
16+
let is_typ : goal -> bool = function Typ _ -> true | Unif _ -> false
17+
let is_unif : goal -> bool = function Typ _ -> false | Unif _ -> true
18+
19+
let get_constr : goal -> constr =
20+
function Unif c -> c | Typ _ -> invalid_arg (__FILE__ ^ "get_constr")
21+
22+
let get_names : goal -> int StrMap.t = function
23+
| Unif(c,_,_) -> Ctxt.names c
24+
| Typ gt -> Env.names gt.goal_hyps
25+
26+
(** The string representation of a goal [g] is a tuple [(l,b,s,t)] where [l]
27+
is a list of pairs of strings [(assumption_name,assumption_type)] and, if
28+
[g] is a typing goal, then [b=true], [s] is the goal meta name and [t] is
29+
the goal type, and if [g] is a unification goal, then [b=false] and [s]
30+
and [t] are the LHS and RHS of the goal respectively. *)
31+
type info = (string * string) list * (string * string, string) t
32+
33+
(** [ctxt g] returns the typing context of the goal [g]. *)
34+
let ctxt : goal -> ctxt = fun g ->
35+
match g with
36+
| Typ gt -> Env.to_ctxt gt.goal_hyps
37+
| Unif (c,_,_) -> c
38+
39+
(** [env g] returns the scoping environment of the goal [g]. *)
40+
let env : goal -> Env.t = fun g ->
41+
match g with
42+
| Unif (c,_,_) ->
43+
let t, n = Ctxt.to_prod c mk_Type in
44+
(try fst (Env.of_prod_nth c n t)
45+
with Invalid_argument _ -> assert false)
46+
| Typ gt -> gt.goal_hyps
47+
48+
(** [of_meta m] creates a goal from the meta [m]. *)
49+
let of_meta : meta -> goal = fun m ->
50+
let goal_hyps, goal_type =
51+
try Env.of_prod_nth [] m.meta_arity !(m.meta_type)
52+
with Invalid_argument _ -> assert false
53+
in
54+
Typ {goal_meta = m; goal_hyps; goal_type }
55+
56+
(** [simpl_opt f g] tries to simplify the goal [g] with the function [f]. *)
57+
let simpl_opt : (ctxt -> term -> term option) -> goal -> goal option =
58+
fun f g ->
59+
match g with
60+
| Typ gt ->
61+
begin
62+
match f (Env.to_ctxt gt.goal_hyps) gt.goal_type with
63+
| None -> None
64+
| Some goal_type -> Some(Typ {gt with goal_type})
65+
end
66+
| Unif(c,t,u) ->
67+
match f c t, f c u with
68+
| Some t, Some u -> Some(Unif(c,t,u))
69+
| _ -> None
70+
71+
(** [simpl f g] simplifies the goal [g] with the function [f]. *)
72+
let simpl : (ctxt -> term -> term) -> goal -> goal = fun f g ->
73+
match g with
74+
| Typ gt ->
75+
Typ {gt with goal_type = f (Env.to_ctxt gt.goal_hyps) gt.goal_type}
76+
| Unif (c,t,u) -> Unif (c, f c t, f c u)
77+
78+
(** [typ_or_def idmap ppf (_,ty,def)] prints in [ppf] the type [ty] or the
79+
definition [def] if there is one. *)
80+
let typ_or_def idmap ppf (ty,def) =
81+
let term = term_in idmap in
82+
match def with
83+
| None -> out ppf "%a" term (Eval.snf_beta ty)
84+
| Some u -> out ppf "≔ %a" term u
85+
86+
(** [ctxt_elt idmap ppf x] prints in [ppf] the conttext element [x]. *)
87+
let ctxt_elt idmap ppf (v,ty,def) =
88+
out ppf "%a%a" var v (typ_or_def idmap) (ty,def)
89+
90+
(** [env_elt idmap ppf x] prints in [ppf] the environment element [x]. *)
91+
let env_elt idmap ppf (s,(_,ty,def)) =
92+
out ppf "%a%a" uid s (typ_or_def idmap) (ty,def)
93+
94+
(** [hyps ppf g] prints on [ppf] the beta-normal forms of the hypotheses of
95+
the goal [g]. *)
96+
let hyps : int StrMap.t -> goal pp =
97+
let hyps elt ppf l =
98+
if l <> [] then
99+
out ppf "%a---------------------------------------------\
100+
---------------------------------\n"
101+
(List.pp (fun ppf -> out ppf "%a\n" elt) "") (List.rev l)
102+
in
103+
fun idmap ppf g ->
104+
match g with
105+
| Typ gt -> hyps (env_elt idmap) ppf gt.goal_hyps
106+
| Unif (c,_,_) -> hyps (ctxt_elt idmap) ppf c
107+
108+
(** [concl ppf g] prints on [ppf] the beta-normal form of the conclusion of
109+
the goal [g]. *)
110+
let concl : int StrMap.t -> goal pp = fun idmap ppf g ->
111+
let term = term_in idmap in
112+
match g with
113+
| Typ gt ->
114+
out ppf "?%d: %a" gt.goal_meta.meta_key
115+
term (Eval.snf_beta gt.goal_type)
116+
| Unif (_, t, u) ->
117+
out ppf "%a ≡ %a" term (Eval.snf_beta t) term (Eval.snf_beta u)
118+
119+
(** [pp ppf g] prints on [ppf] the beta-normal form of the goal [g] with its
120+
hypotheses. *)
121+
let pp ppf g = let idmap = get_names g in hyps idmap ppf g; concl idmap ppf g
122+
123+
(** [pp_no_hyp ppf g] prints on [ppf] the beta-normal form of the conclusion
124+
of the goal [g] without its hypotheses. *)
125+
let pp_no_hyp ppf g = concl (get_names g) ppf g
126+
127+
(** [to_info g] converts the goal [g] into an [info] data structure.*)
128+
let to_info : goal -> info =
129+
let buf = Buffer.create 80 in
130+
let ppf = Format.formatter_of_buffer buf in
131+
let to_string f x =
132+
f ppf x;
133+
Format.pp_print_flush ppf ();
134+
let res = Buffer.contents buf in
135+
Buffer.clear buf;
136+
res
137+
in
138+
fun g ->
139+
let idmap = get_names g in
140+
let term = term_in idmap in
141+
match g with
142+
| Typ gt ->
143+
let f (s,(_,ty,def)) = s, to_string (typ_or_def idmap) (ty,def) in
144+
List.rev_map f gt.goal_hyps,
145+
Typ("?"^to_string int gt.goal_meta.meta_key,
146+
to_string term gt.goal_type)
147+
| Unif(c,t,u) ->
148+
let f (v,ty,def) =
149+
to_string var v, to_string (typ_or_def idmap) (ty,def) in
150+
List.rev_map f c,
151+
Unif(to_string term t^""^to_string term u)
152+
153+
(** [add_goals_of_problem p gs] extends the list of goals [gs] with the
154+
metavariables and constraints of [p]. *)
155+
let add_goals_of_problem : problem -> goal list -> goal list = fun p gs ->
156+
let gs = MetaSet.fold (fun m gs -> of_meta m :: gs) !p.metas gs in
157+
let f gs c = Unif c :: gs in
158+
let gs = List.fold_left f gs !p.to_solve in
159+
List.fold_left f gs !p.unsolved

0 commit comments

Comments
 (0)