Skip to content

Commit fe8fb4c

Browse files
author
gabrielhdt
committed
Add starting point to DkMeta
Introduce a function that allows to rewrite a term with a specific set of rewrite rules. In particular, it may be used to implement tactics that transform terms.
1 parent e538672 commit fe8fb4c

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/tool/meta.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
open Core
2+
open Timed
3+
open Lplib
4+
open Extra
5+
6+
let rewrite_with (sign : Sign.t) (rules : (Term.sym * Term.rule) list) :
7+
Term.term -> Term.term =
8+
let exec t =
9+
(* Remove rules and definitions from {b!all} signatures *)
10+
let strip_sym _ (s, _) =
11+
s.Term.sym_rules := [];
12+
s.Term.sym_def := None;
13+
Tree.update_dtree s
14+
in
15+
Common.Path.Map.iter
16+
(fun _ s -> StrMap.iter strip_sym !(s.Sign.sign_symbols))
17+
Timed.(!Sign.loaded);
18+
(* Add a set of custom rules *)
19+
List.iter (fun (s, r) -> Sign.add_rule sign s r) rules;
20+
Eval.snf [] t
21+
in
22+
pure_apply exec

tests/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(tests
2-
(names utils ok_ko rewriting)
2+
(names utils ok_ko rewriting meta)
33
(deps
44
(glob_files OK/*.lp)
55
(glob_files OK/*.dk)

tests/meta.ml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(** Test the Meta module. *)
2+
open Common
3+
4+
open Handle
5+
open Parsing
6+
open Core
7+
8+
let term : Term.term Alcotest.testable =
9+
( module struct
10+
type t = Term.term
11+
12+
let pp = Print.pp_term
13+
let equal = Rewrite.eq
14+
end )
15+
16+
let scope_term ss t =
17+
Scope.scope_term true ss [] (Term.new_problem ()) (Fun.const None)
18+
(Fun.const None) t
19+
20+
let () = Library.set_lib_root (Some "/tmp")
21+
22+
(** The signature of [OK/bool.lp] *)
23+
let bool_sig = Compile.Pure.compile_file "OK/bool.lp"
24+
25+
(** The signature state with [OK/bool.lp] opened. *)
26+
let bool_sig_st =
27+
let open Core in
28+
let ss = Sig_state.of_sign bool_sig in
29+
Sig_state.open_sign ss bool_sig
30+
31+
(** The term [bool_or true true] *)
32+
let or_true_true =
33+
Syntax.P.(appl_list (iden "bool_or") [ iden "true"; iden "true" ])
34+
|> scope_term bool_sig_st
35+
36+
(** Unit tests functions *)
37+
38+
let no_rewrite () =
39+
let no_rewrite =
40+
Tool.Meta.rewrite_with bool_sig [] or_true_true
41+
in
42+
Alcotest.(check term) "same term" or_true_true no_rewrite
43+
44+
let simple_rewrite () =
45+
let lhs = Syntax.P.iden "bool_or" in
46+
let rhs = Syntax.P.iden "bool_and" in
47+
let Pos.{ elt = r; _ } =
48+
Scope.scope_rule true bool_sig_st (Pos.none (lhs, rhs))
49+
in
50+
let r = (r.pr_sym, Scope.rule_of_pre_rule r) in
51+
let rewritten =
52+
Tool.Meta.rewrite_with bool_sig [ r ] or_true_true
53+
in
54+
let and_true_true =
55+
Syntax.P.(appl_list (iden "bool_and") [ iden "true"; iden "true" ])
56+
|> scope_term bool_sig_st
57+
in
58+
Alcotest.(check term) "same term" and_true_true rewritten
59+
60+
let () =
61+
let open Alcotest in
62+
run "Meta"
63+
[ ( "standard"
64+
, [ ("no rewriting with signature's rules", `Quick, no_rewrite)
65+
; ("rewrite symbol", `Quick, simple_rewrite)
66+
] )
67+
]

0 commit comments

Comments
 (0)