Skip to content
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

removebottomコマンドの追加 #44

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@
(modules s2e)
(libraries birds))

(executable
(public_name removebottom)
(name removebottom)
(modules removebottom)
(libraries birds))

13 changes: 13 additions & 0 deletions bin/removebottom.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
open Birds

let _ =
if Array.length Sys.argv < 2 then
print_endline "Invalid arguments. File name must be passed."
else begin
let filename = Sys.argv.(1) in
let chan = open_in filename in
let lexbuf = Lexing.from_channel chan in
let ast = Parser.main Lexer.token lexbuf in
let result = Remove_bottom.remove_bottom ast in
print_endline @@ Expr.to_string result
end
33 changes: 33 additions & 0 deletions examples/bottom1.dl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
source brands('BRAND_ID':real, 'BRAND_NAME':string).
source cars('CAR_ID':real, 'CAR_NAME':string, 'BRAND_ID':real).
view all_cars('CAR_ID':real, 'CAR_NAME':string, 'BRAND_ID':real, 'BRAND_NAME':string).
% primary key
%PK(brands,['BRAND_ID']).
%PK(cars, ['CAR_ID']).

% fd on base relations
% CAR_ID -> CAR_NAME, BRAND_ID on cars
_|_ :- cars(CI, CN1, _), cars(CI, CN2, _), CN1 <> CN2.
_|_ :- cars(CI, _, BI1), cars(CI, _, BI2), BI1 <> BI2.
% BRAND_ID -> BRAND_NAME on brands
_|_ :- brands(BI, BN1), brands(BI, BN2), BN1 <> BN2.

%% fd on views
% BRAND_ID -> BRAND_NAME on all_cars
_|_ :- all_cars(_,_,BI,BN1), all_cars(_,_,BI,BN2), BN1 <> BN2.
% CAR_ID -> CAR_NAME, BRAND_ID on all_cars
_|_ :- all_cars(CI, CN1, _, _), all_cars(CI, CN2, _, _), CN1 <>CN2.
_|_ :- all_cars(CI, _, BI1, _), all_cars(CI, _, BI2, _), BI1 <> BI2.

% foreign key
_|_ :- cars(_, _, BI), not brands(BI, _).

% view definition
all_cars(CI, CN, BI, BN) :- cars(CI, CN, BI), brands(BI, BN).

% update strategy

-cars(CI, CN, BI) :- cars(CI, CN, BI), not all_cars(CI, CN, BI, _).
-brands(BI, BN) :- brands(BI, BN), all_cars(_, _, BI, _), not all_cars(_, _, BI, BN).
+cars(CI, CN, BI) :- all_cars(CI, CN, BI, _), not cars(CI, CN, BI).
+brands(BI, BN) :- all_cars(_, _, BI, BN), not brands(BI, BN).
21 changes: 21 additions & 0 deletions src/remove_bottom.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

open Expr

(** The prefix used for generated predicates. *)
let constraint_predicate_prefix = "__constraint__"

let remove_bottom (ast: expr): expr =

let preds =
Seq.iterate (fun i -> i + 1) 0 |>
Seq.take (List.length ast.constraints) |>
List.of_seq |>
List.map (fun i -> Pred (constraint_predicate_prefix ^ Int.to_string i, []))
in

let crules =
(Pred (constraint_predicate_prefix, []), List.map (fun p -> Not p) preds) ::
List.map2 (fun c pred -> match c with (_, body) -> (pred, body)) ast.constraints preds
in

{ ast with rules = List.append ast.rules crules; constraints = [] }
4 changes: 4 additions & 0 deletions src/remove_bottom.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

open Expr

val remove_bottom : expr -> expr
Loading