-
Notifications
You must be signed in to change notification settings - Fork 36
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
Term transformation helper function #781
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
open Core | ||
open Timed | ||
open Lplib | ||
open Extra | ||
|
||
let rewrite_with (sign : Sign.t) (rules : (Term.sym * Term.rule) list) : | ||
Term.term -> Term.term = | ||
let exec t = | ||
(* Remove rules and definitions from {b!all} signatures *) | ||
let strip_sym _ s = | ||
s.Term.sym_rules := []; | ||
s.Term.sym_def := None; | ||
Tree.update_dtree s | ||
in | ||
Common.Path.Map.iter | ||
(fun _ s -> StrMap.iter strip_sym !(s.Sign.sign_symbols)) | ||
Timed.(!Sign.loaded); | ||
(* Add a set of custom rules *) | ||
List.iter (fun (s, r) -> Sign.add_rule sign s r) rules; | ||
Eval.snf [] t | ||
in | ||
pure_apply exec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
(** Test the Meta module. *) | ||
open Common | ||
|
||
open Handle | ||
open Parsing | ||
open Core | ||
|
||
let term : Term.term Alcotest.testable = | ||
( module struct | ||
type t = Term.term | ||
|
||
let pp = Print.pp_term | ||
let equal = Rewrite.eq | ||
end ) | ||
|
||
let scope_term ss t = | ||
Scope.scope_term true ss [] (Term.new_problem ()) (Fun.const None) | ||
(Fun.const None) t | ||
|
||
let () = Library.set_lib_root (Some "/tmp") | ||
|
||
(** The signature of [OK/bool.lp] *) | ||
let bool_sig = Compile.Pure.compile_file "OK/bool.lp" | ||
|
||
(** The signature state with [OK/bool.lp] opened. *) | ||
let bool_sig_st = | ||
let open Core in | ||
let ss = Sig_state.of_sign bool_sig in | ||
Sig_state.open_sign ss bool_sig | ||
|
||
(** The term [bool_or true true] *) | ||
let or_true_true = | ||
Syntax.P.(appl_list (iden "bool_or") [ iden "true"; iden "true" ]) | ||
|> scope_term bool_sig_st | ||
|
||
(** Unit tests functions *) | ||
|
||
let no_rewrite () = | ||
let no_rewrite = Tool.Meta.rewrite_with bool_sig [] or_true_true in | ||
Alcotest.(check term) "same term" or_true_true no_rewrite | ||
|
||
let simple_rewrite () = | ||
let lhs = Syntax.P.iden "bool_or" in | ||
let rhs = Syntax.P.iden "bool_and" in | ||
let r = | ||
Scope.scope_rule bool_sig_st (Pos.none (lhs, rhs)) | ||
in | ||
let r = (r.Pos.elt.pr_sym, Scope.rule_of_pre_rule r) in | ||
let rewritten = | ||
Tool.Meta.rewrite_with bool_sig [ r ] or_true_true | ||
in | ||
let and_true_true = | ||
Syntax.P.(appl_list (iden "bool_and") [ iden "true"; iden "true" ]) | ||
|> scope_term bool_sig_st | ||
in | ||
Alcotest.(check term) "same term" and_true_true rewritten | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which equality is used here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use syntactic equality (the infamous |
||
|
||
(* Meta rewrite rules can rewrite symbols noted "constant". *) | ||
let rewrite_constant () = | ||
let lhs = Syntax.P.iden "true" in (* [true] is constant *) | ||
let rhs = Syntax.P.iden "false" in | ||
let r = | ||
Scope.scope_meta_rule bool_sig_st (Pos.none (lhs, rhs)) | ||
in | ||
let r = (r.Pos.elt.pr_sym, Scope.rule_of_pre_rule r) in | ||
let or_false_false = | ||
Syntax.P.(appl_list (iden "bool_or") [ iden "false"; iden "false" ]) | ||
|> scope_term bool_sig_st | ||
in | ||
let rewritten = Tool.Meta.rewrite_with bool_sig [ r ] or_true_true in | ||
Alcotest.check term "same term" or_false_false rewritten | ||
|
||
let () = | ||
let open Alcotest in | ||
run "Meta" | ||
[ ( "standard" | ||
, [ ("no rewriting with signature's rules", `Quick, no_rewrite) | ||
; ("rewrite symbol", `Quick, simple_rewrite) | ||
; ("rewrite constant", `Quick, rewrite_constant) | ||
] ) | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using parse_string instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we don't have a
parse_term
yet, we can only parse complete commands (unless we uselambdapi/tests/rewriting.ml
Line 6 in c1337fd