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