2024-05-16 12:38:45 +02:00
|
|
|
open Hlam
|
2024-04-25 10:11:08 +02:00
|
|
|
open Lam
|
2024-05-19 20:44:07 +02:00
|
|
|
open Types
|
2024-04-25 10:11:08 +02:00
|
|
|
|
2024-05-06 00:10:23 +02:00
|
|
|
type context = (id * id * hlam * Types.ty) list
|
2024-04-25 10:11:08 +02:00
|
|
|
type goal = hlam * Types.ty * context
|
|
|
|
type proof = goal option * goal list
|
|
|
|
|
2024-04-30 11:44:28 +02:00
|
|
|
type tactic =
|
2024-05-17 14:14:28 +02:00
|
|
|
TExact_term of lam
|
|
|
|
| TExact_proof of id
|
|
|
|
| TAssumption
|
|
|
|
| TIntros
|
|
|
|
| TIntro
|
|
|
|
| TCut of ty
|
|
|
|
| TApply of id
|
|
|
|
| TSplit
|
|
|
|
| TRight
|
|
|
|
| TLeft
|
|
|
|
| TTry of tactic
|
2024-04-30 11:44:28 +02:00
|
|
|
|
2024-05-11 11:44:43 +02:00
|
|
|
let hyp_count = ref 0
|
2024-04-25 10:11:08 +02:00
|
|
|
let get_fresh_hyp () =
|
2024-05-11 11:44:43 +02:00
|
|
|
let n = string_of_int !hyp_count in
|
|
|
|
incr hyp_count;
|
2024-04-25 10:11:08 +02:00
|
|
|
let hyp_id = "H" ^ n in
|
|
|
|
let var_id = "x_" ^ n in
|
|
|
|
(hyp_id, var_id)
|
|
|
|
|
2024-05-11 11:44:43 +02:00
|
|
|
(** replace ref's in a proof *)
|
2024-05-13 18:05:28 +02:00
|
|
|
let rec clean_context assoc (c : context) : context = match c with
|
|
|
|
[] -> []
|
|
|
|
| (s1, s2, h, t)::q -> (s1, s2, clean_hlam assoc h, t)::(clean_context assoc q)
|
|
|
|
let clean_goal assoc ((h, t, c) : goal) : goal =
|
|
|
|
(clean_hlam assoc h, t, clean_context assoc c)
|
|
|
|
let clean_proof ((g, gs) : proof) : (hlam ref * hlam ref) list ref * proof =
|
2024-05-11 11:44:43 +02:00
|
|
|
let assoc = ref [] in
|
|
|
|
let g' = match g with
|
2024-05-13 18:05:28 +02:00
|
|
|
| Some g -> Some (clean_goal assoc g)
|
2024-05-11 11:44:43 +02:00
|
|
|
| None -> None
|
2024-05-13 18:05:28 +02:00
|
|
|
in assoc, (g', List.map (clean_goal assoc) gs)
|
2024-05-11 11:44:43 +02:00
|
|
|
|
2024-05-19 20:44:07 +02:00
|
|
|
(* typecheck e t cs types e against t in the typing environment defined
|
|
|
|
by cs *)
|
2024-05-06 00:10:23 +02:00
|
|
|
let typecheck (e : lam) (expected_t : Types.ty) (cs : context) : bool =
|
|
|
|
let gam_of_ctx : context -> Types.gam =
|
2024-05-19 20:44:07 +02:00
|
|
|
(fun (_, var_id, _, ty) -> (var_id, ty)) |>
|
|
|
|
List.map
|
2024-05-06 00:10:23 +02:00
|
|
|
in
|
|
|
|
let g = gam_of_ctx cs in
|
|
|
|
try Typing.typecheck g e expected_t
|
2024-05-19 20:44:07 +02:00
|
|
|
with Typing.Could_not_infer -> raise (TacticFailed "couldn't not infer all variable types")
|
2024-05-06 00:10:23 +02:00
|
|
|
|
2024-04-30 11:56:41 +02:00
|
|
|
let rec get_term_by_id (hyp : id) : context -> hlam option =
|
|
|
|
function
|
|
|
|
[] -> None
|
2024-05-06 00:10:23 +02:00
|
|
|
| (hyp',_, h, _) :: _ when hyp' = hyp -> Some h
|
2024-04-30 11:56:41 +02:00
|
|
|
| _ :: cs -> get_term_by_id hyp cs
|
|
|
|
|
2024-04-25 10:11:08 +02:00
|
|
|
let rec get_term_by_type (ty : Types.ty) : context -> hlam option =
|
|
|
|
function
|
|
|
|
[] -> None
|
2024-05-06 00:10:23 +02:00
|
|
|
| (_, _, h, ty') :: _ when ty' = ty -> Some h
|
2024-04-25 10:11:08 +02:00
|
|
|
| _ :: cs -> get_term_by_type ty cs
|
|
|
|
|
|
|
|
let next_goal (gs : goal list) : (goal option * goal list) =
|
|
|
|
match gs with
|
|
|
|
[] -> None, []
|
|
|
|
| g :: gs -> Some g, gs
|
|
|
|
|
2024-05-19 20:44:07 +02:00
|
|
|
let get_goal : goal option -> hlam * ty * context =
|
|
|
|
function
|
2024-05-01 11:07:40 +02:00
|
|
|
None -> raise (TacticFailed "no current goal")
|
2024-05-19 20:44:07 +02:00
|
|
|
| Some g -> g
|
|
|
|
|
|
|
|
let tact_exact_term ((g, gs) : proof) (e : lam) : proof =
|
|
|
|
let (h, expected_t, cs) = get_goal g in
|
|
|
|
if typecheck e expected_t cs
|
|
|
|
then
|
|
|
|
begin
|
|
|
|
fill h (hlam_of_lam e);
|
|
|
|
next_goal gs
|
|
|
|
end
|
|
|
|
else raise (TacticFailed "type mismatch")
|
|
|
|
|
|
|
|
let tact_exact_proof ((g, gs) : proof) (hyp : id) : proof =
|
|
|
|
let (h, expected_t, cs) = get_goal g in
|
|
|
|
match get_term_by_id hyp cs with
|
|
|
|
Some h' ->
|
|
|
|
if typecheck (lam_of_hlam h') expected_t cs
|
2024-05-06 00:10:23 +02:00
|
|
|
then
|
|
|
|
begin
|
2024-05-19 20:44:07 +02:00
|
|
|
fill h h';
|
2024-05-06 00:10:23 +02:00
|
|
|
next_goal gs
|
|
|
|
end
|
|
|
|
else raise (TacticFailed "type mismatch")
|
2024-05-19 20:44:07 +02:00
|
|
|
| None -> raise (TacticFailed "")
|
2024-04-30 11:48:10 +02:00
|
|
|
|
2024-04-25 10:11:08 +02:00
|
|
|
let tact_assumption ((g, gs) : proof) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
let (h, goal_ty, cs) = get_goal g in
|
|
|
|
match get_term_by_type goal_ty cs with
|
|
|
|
None -> raise (TacticFailed "no such hypothesis")
|
|
|
|
| Some h' ->
|
|
|
|
fill h h';
|
|
|
|
next_goal gs
|
2024-04-25 10:11:08 +02:00
|
|
|
|
|
|
|
let tact_intro ((g, gs) : proof) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
let (h, goal_ty, cs) = get_goal g in
|
|
|
|
match goal_ty with
|
|
|
|
Arr (t1, t2) ->
|
|
|
|
let (hyp_id, var_id) = get_fresh_hyp () in
|
|
|
|
let cs = (hyp_id, var_id, HVar var_id, t1) :: cs in
|
|
|
|
let new_h = Ref (ref Hole) in
|
|
|
|
fill h (HFun ((var_id, t1), new_h));
|
|
|
|
Some (new_h, t2, cs), gs
|
|
|
|
| _ -> raise (TacticFailed "expected an implication")
|
2024-04-25 10:11:08 +02:00
|
|
|
|
|
|
|
let tact_cut ((g, gs) : proof) (new_t : Types.ty) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
let (h, goal_ty, cs) = get_goal g in
|
|
|
|
(* subgoal 2 : new_t -> goal_ty *)
|
|
|
|
let arrow_h = Ref (ref Hole) in
|
|
|
|
let arrow_goal = (arrow_h, Arr (new_t, goal_ty), cs) in
|
|
|
|
let gs = arrow_goal :: gs in
|
|
|
|
(* subgoal 1 (main goal) : new_t *)
|
|
|
|
let new_h = Ref (ref Hole) in
|
|
|
|
fill h (HApp (arrow_h, new_h));
|
|
|
|
Some (new_h, new_t, cs), gs
|
2024-04-25 10:11:08 +02:00
|
|
|
|
|
|
|
let tact_apply ((g, gs) : proof) (hyp_id : id) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
(* check if hypothesis suits apply *)
|
2024-05-10 18:40:33 +02:00
|
|
|
let rec is_implied (goal_ty : ty) (t : ty) : bool =
|
|
|
|
match t with
|
|
|
|
t when t = goal_ty -> true
|
|
|
|
| Arr (_, t2) -> is_implied goal_ty t2
|
|
|
|
| _ -> false
|
|
|
|
in
|
2024-05-19 20:44:07 +02:00
|
|
|
(* supposes is_implied goal_ty impl_ty
|
|
|
|
goal_ty : conclusion of the implication
|
|
|
|
impl_ty : type of the implication
|
|
|
|
impl_h : building the term we will apply to goal_h
|
|
|
|
goal_h : hole of the current goal
|
|
|
|
h : current hole of impl_h*)
|
2024-05-14 11:41:10 +02:00
|
|
|
let rec generate_goals (goal_ty : ty) (impl_ty : ty) (impl_h : hlam)
|
2024-05-14 15:55:19 +02:00
|
|
|
(goal_h : hlam) (h : hlam) (cs : context) (gs : goal list) : proof =
|
2024-05-14 11:41:10 +02:00
|
|
|
match impl_ty with
|
2024-05-10 18:40:33 +02:00
|
|
|
Arr (t1, t2) when t2 = goal_ty ->
|
2024-05-14 11:41:10 +02:00
|
|
|
let sub_h = Ref (ref Hole) in
|
2024-05-14 15:55:19 +02:00
|
|
|
let _ = fill h sub_h in
|
|
|
|
let _ = fill goal_h impl_h in
|
2024-05-14 11:41:10 +02:00
|
|
|
Some (sub_h, t1, cs), gs
|
2024-05-10 18:40:33 +02:00
|
|
|
| Arr (t1, t2) ->
|
2024-05-19 20:44:07 +02:00
|
|
|
(* transforms impl_h from ((f ?x_0) ?) to (((f ?x_0) ?x_1) ?)
|
|
|
|
where ? is h and ?x_i are holes associated with
|
|
|
|
the proof of x_i*)
|
|
|
|
let sub_h = Ref (ref Hole) in (* proof of t1 *)
|
|
|
|
let new_h = Ref (ref Hole) in (* proof of t2 *)
|
2024-05-14 15:55:19 +02:00
|
|
|
let _ = fill h sub_h in
|
|
|
|
let impl_h = HApp (impl_h, new_h) in
|
2024-05-19 20:44:07 +02:00
|
|
|
let gs = (sub_h, t1, cs) :: gs in (* add the proof of t1 to goals *)
|
|
|
|
generate_goals goal_ty t2 impl_h goal_h new_h cs gs (* generate_goals for the proof of t2 *)
|
2024-05-14 11:41:10 +02:00
|
|
|
| _ -> failwith "impossible"
|
2024-05-10 18:40:33 +02:00
|
|
|
in
|
|
|
|
let rec get_hyp : context -> (hlam * ty) = function
|
2024-05-01 11:07:40 +02:00
|
|
|
[] -> raise (TacticFailed "no such hypothesis in context")
|
2024-05-06 00:10:23 +02:00
|
|
|
| (hyp_id', _, h', t') :: _ when hyp_id = hyp_id' -> (h', t')
|
2024-04-25 10:11:08 +02:00
|
|
|
| _ :: cs -> get_hyp cs
|
|
|
|
in
|
2024-05-19 20:44:07 +02:00
|
|
|
let (goal_h, goal_ty, cs) = get_goal g in
|
|
|
|
let impl_h, impl_ty = get_hyp cs in
|
|
|
|
let new_h = Ref (ref Hole) in
|
|
|
|
let impl_h_2 = HApp (impl_h, new_h) in
|
|
|
|
if is_implied goal_ty impl_ty
|
|
|
|
then generate_goals goal_ty impl_ty impl_h_2 goal_h new_h cs gs
|
|
|
|
else raise (TacticFailed "not an implication")
|
2024-04-30 11:48:10 +02:00
|
|
|
|
|
|
|
let tact_intros : proof -> proof =
|
|
|
|
let rec push (p : proof) =
|
|
|
|
try
|
|
|
|
let p = tact_intro p in
|
|
|
|
push p
|
2024-05-01 11:07:40 +02:00
|
|
|
with TacticFailed _ -> p
|
2024-04-30 11:48:10 +02:00
|
|
|
in push
|
|
|
|
|
2024-05-06 10:09:40 +02:00
|
|
|
let tact_split ((g, gs) : proof) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
let (h, goal_ty, cs) = get_goal g in
|
|
|
|
match goal_ty with
|
|
|
|
| And(t1, t2) ->
|
|
|
|
let h1 = Ref (ref Hole) in
|
|
|
|
let h2 = Ref (ref Hole) in
|
|
|
|
fill h (HPair (h1, h2));
|
|
|
|
Some (h1, t1, cs), (h2, t2, cs)::gs
|
|
|
|
| _ -> raise (TacticFailed "Not a conjonction")
|
2024-05-06 10:09:40 +02:00
|
|
|
|
|
|
|
|
2024-05-05 20:45:55 +02:00
|
|
|
let tact_right ((g, gs) : proof) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
let (h, goal_ty, cs) = get_goal g in
|
|
|
|
match goal_ty with
|
|
|
|
| Or(_, t_r) as t ->
|
|
|
|
let new_h = Ref (ref Hole) in
|
|
|
|
fill h (HRight (new_h, t));
|
|
|
|
Some (new_h, t_r, cs), gs
|
|
|
|
| _ -> raise (TacticFailed "Not a disjunction")
|
2024-05-05 20:45:55 +02:00
|
|
|
|
|
|
|
let tact_left ((g, gs) : proof) : proof =
|
2024-05-19 20:44:07 +02:00
|
|
|
let (h, goal_ty, cs) = get_goal g in
|
|
|
|
match goal_ty with
|
|
|
|
| Or(t_l, _) as t->
|
|
|
|
let new_h = Ref (ref Hole) in
|
|
|
|
fill h (HLeft (new_h, t));
|
|
|
|
Some (new_h, t_l, cs), gs
|
|
|
|
| _ -> raise (TacticFailed "Not a disjunction")
|
2024-05-05 20:45:55 +02:00
|
|
|
|
2024-05-20 15:03:50 +02:00
|
|
|
(* applies tactic to proof, returns (new_proof, true if p changed) *)
|
|
|
|
let rec apply_tactic (p : proof) (t : tactic) : proof * bool =
|
2024-04-30 11:48:10 +02:00
|
|
|
match t with
|
2024-05-20 15:03:50 +02:00
|
|
|
TExact_term e -> tact_exact_term p e, true
|
|
|
|
| TExact_proof hyp -> tact_exact_proof p hyp, true
|
|
|
|
| TIntros -> tact_intros p, true
|
|
|
|
| TIntro -> tact_intro p, true
|
|
|
|
| TAssumption -> tact_assumption p, true
|
|
|
|
| TCut t -> tact_cut p t, true
|
|
|
|
| TApply h -> tact_apply p h, true
|
|
|
|
| TSplit -> tact_split p, true
|
|
|
|
| TRight -> tact_right p, true
|
|
|
|
| TLeft -> tact_left p, true
|
|
|
|
| TTry t -> try apply_tactic p t with TacticFailed _ -> p, false
|
2024-05-01 10:44:36 +02:00
|
|
|
|