simplest_model_with_mod: fixes & model spec fixes

This commit is contained in:
ProgramSnail 2026-03-02 16:34:34 +00:00
parent c3775feccb
commit 607743a66f
2 changed files with 46 additions and 40 deletions

View file

@ -44,23 +44,23 @@ struct
(* --- *) (* --- *)
let is_read (tag : tag) : bool = match tag with let is_read (tag : tag) : bool = match tag with
| (Rd, _, _, _, _) -> true | (Rd, _, _, _, _) -> true
| (NRd, _, _, _, _) -> false | (NRd, _, _, _, _) -> false
let is_write (tag : tag) : bool = match tag with let is_write (tag : tag) : bool = match tag with
| (_, Wr, _, _, _) -> true | (_, Wr, _, _, _) -> true
| (_, NWr, _, _, _) -> false | (_, NWr, _, _, _) -> false
let is_copy (tag : tag) : bool = match tag with let is_copy (tag : tag) : bool = match tag with
| (_, _, Cp, _, _) -> true | (_, _, Cp, _, _) -> true
| (_, _, NCp, _, _) -> false | (_, _, NCp, _, _) -> false
let is_in (tag : tag) : bool = match tag with let is_in (tag : tag) : bool = match tag with
| (_, _, _, In, _) -> true | (_, _, _, In, _) -> true
| (_, _, _, NIn, _) -> false | (_, _, _, NIn, _) -> false
let is_out (tag : tag) : bool = match tag with let is_out (tag : tag) : bool = match tag with
| (_, _, _, _, Out) -> true | (_, _, _, _, Out) -> true
| (_, _, _, _, NOut) -> false | (_, _, _, _, NOut) -> false
(* --- *) (* --- *)
@ -95,8 +95,8 @@ struct
let mem_add (state : state) (value : value) : state = match state with let mem_add (state : state) (value : value) : state = match state with
(env, mem, mem_len, visited) -> let mem = value :: mem in (env, mem, mem_len + 1, visited) (env, mem, mem_len, visited) -> let mem = value :: mem in (env, mem, mem_len + 1, visited)
let mem_check (state : state) (id : data) : state = let mem_check (state : state) (id : data) : unit =
if mem_get state id == BotV then raise @@ Incorrect_memory_access id else state if mem_get state id == BotV then raise @@ Incorrect_memory_access id else ()
let arg_to_value (state : state) (arg : arg) : value = match arg with let arg_to_value (state : state) (arg : arg) : value = match arg with
@ -106,10 +106,10 @@ struct
let st_mem_len (state : state) : int = let st_mem_len (state : state) : int =
match state with (_, _, mem_len, _) -> mem_len match state with (_, _, mem_len, _) -> mem_len
(* TODO *)
let check_tag_correct (tag : tag) (id : data) : unit = let check_tag_correct (tag : tag) (id : data) : unit =
if (* (is_in tag && not (is_read tag)) || *) (* TODO: required ?? *) if (* (is_in tag && not (is_read tag)) || *) (* TODO: required ?? *)
is_out tag > is_write tag is_out tag > is_write tag ||
is_read tag > is_in tag
(* || is_copy tag && is_out tag *) (* ?? *) (* || is_copy tag && is_out tag *) (* ?? *)
then raise @@ Invalid_argument_tag id then raise @@ Invalid_argument_tag id
else () else ()
@ -131,25 +131,19 @@ struct
else let state_ext = env_add state id arg_tag mem_id in else let state_ext = env_add state id arg_tag mem_id in
mem_set state_ext id BotV mem_set state_ext id BotV
(* TODO: FIXME: do not spoil out arguments *) (* TODO: use state_before ?? or state in some order (both orders ?, mod and then check ?) *)
(* TODO: FIXME: do write to tags that are out for args (code + semantics fix)
-> write to out args *)
let st_spoil_by_args (state : state) (arg_tags : tag list) (args : data list) : state = let st_spoil_by_args (state : state) (arg_tags : tag list) (args : data list) : state =
match state with (env, mem, mem_len, _visited) -> match state with (env, mem, mem_len, _visited) ->
let state_before = state in
let spoil_folder (state : state) (tag : tag) (id : data) : state = let spoil_folder (state : state) (tag : tag) (id : data) : state =
let parent_tag = fst (env_get state id) in let parent_tag = fst (env_get state id) in
if not (is_copy tag) && not (is_out tag) if is_write tag > is_write parent_tag then raise @@ Incorrect_const_cast id
then (if is_write tag > is_write parent_tag else let state' = if is_read tag then (mem_check state_before id; state) else state (* NOTE: state override *)
(* || is_read tag > is_read parent_tag *) (* TODO FIXME: check that can read *) in if not @@ is_write tag then state'
then raise @@ Incorrect_const_cast id else match is_out tag with
else let state_checked = if is_read tag | true -> mem_set state' id UnitV
then mem_check state id | false -> if is_copy tag then state'
else state else mem_set state' id BotV
in
if is_write tag
then mem_set state_checked id BotV
else state_checked)
else state
in List.fold_left2 spoil_folder state arg_tags args in List.fold_left2 spoil_folder state arg_tags args
let list_drop n xs = List.of_seq @@ Seq.drop n @@ List.to_seq xs let list_drop n xs = List.of_seq @@ Seq.drop n @@ List.to_seq xs
@ -382,7 +376,7 @@ struct
[%expect {| done! |}] [%expect {| done! |}]
(* NOTE: changed semantics by comporasion with prev analyzer, new test *) (* NOTE: changed semantics by comporasion with prev analyzer, new test *)
let%expect_test "function with ref two same ref args, read & write both & nothing" = let%expect_test "function with ref two same ref args, read both & nothing" =
eval_prog ( eval_prog (
[([ri_ref; ri_ref],[Read 0; Read 1; Read 1])], [([ri_ref; ri_ref],[Read 0; Read 1; Read 1])],
([wi_value], [Write 0; Call (0, [0; 0]); ])); ([wi_value], [Write 0; Call (0, [0; 0]); ]));
@ -390,21 +384,33 @@ struct
[%expect {| done! |}] [%expect {| done! |}]
(* NOTE: changed semantics by comporasion with prev analyzer, new test *) (* NOTE: changed semantics by comporasion with prev analyzer, new test *)
let%expect_test "function with ref & copy of the same arg, read both & nothing" = let%expect_test "function with ref & copy of the same arg, read & write both & nothing" =
eval_prog ( eval_prog (
[([rwi_ref; rwi_value],[Read 0; Read 1; Write 0; Write 1; Read 1])], [([rwi_ref; rwi_value],[Read 0; Read 1; Write 0; Write 1; Read 1])],
([wi_value], [Write 0; Call (0, [0; 0]); ])); ([wi_value], [Write 0; Call (0, [0; 0]); ]));
Printf.printf "done!"; Printf.printf "done!";
[%expect {| done! |}] [%expect {| done! |}]
(* NOTE: changed semantics by comporasion with prev analyzer, new test *)
let%expect_test "function with copy & ref of the same arg, read & write both & nothing" =
eval_prog (
[([rwi_value; rwi_ref],[Read 0; Read 1; Write 0; Write 1; Read 1])],
([wi_value], [Write 0; Call (0, [0; 0]); ]));
Printf.printf "done!";
[%expect {| done! |}]
(* TODO: FIXME: now correct (use state before for mem check), is this good ?, proper way to fix ? *)
(* NOTE: maybe important case in the future *) (* NOTE: maybe important case in the future *)
let%expect_test "function with ref two same ref args, read & write both, error" = let%expect_test "function with ref two same ref args, read & write both, error" =
try (eval_prog ( (* try ( *)
eval_prog (
[([rwi_ref; rwi_ref],[Read 0; Read 1; Write 0; Write 1; Read 1])], [([rwi_ref; rwi_ref],[Read 0; Read 1; Write 0; Write 1; Read 1])],
([wi_value], [Write 0; Call (0, [0; 0]); ])); ([wi_value], [Write 0; Call (0, [0; 0]); ]));
[%expect.unreachable]) (* [%expect.unreachable]) *)
with Incorrect_memory_access id -> Printf.printf "%i" id; (* with Incorrect_memory_access id -> Printf.printf "%i" id; *)
[%expect {| 0 |}] (* [%expect {| 0 |}] *)
Printf.printf "done!";
[%expect {| done! |}]
(* >> tests with several functions *) (* >> tests with several functions *)
@ -468,4 +474,8 @@ struct
eval_prog ([([rwi_value], [Write 0; Read 0; Write 0; Call (0, [0])])], ([wi_value], [Write 0; Call (0, [0]) ])); eval_prog ([([rwi_value], [Write 0; Read 0; Write 0; Call (0, [0])])], ([wi_value], [Write 0; Call (0, [0]) ]));
Printf.printf "done!"; Printf.printf "done!";
[%expect {| done! |}] [%expect {| done! |}]
(* --- *)
(* TODO: out arguments test, etc. *)
end end

View file

@ -29,19 +29,19 @@
#let prog = `prog` #let prog = `prog`
#bnf( #bnf(
Prod(`read`, Prod(`read`,
{ Or[Read][] { Or[Read][read passed value]
Or[Not Read][] } ), Or[Not Read][] } ),
Prod(`write`, Prod(`write`,
{ Or[Write][] { Or[Write][write to passed variable]
Or[Not Write][] } ), Or[Not Write][] } ),
Prod(`copy`, Prod(`copy`,
{ Or[Ref][] { Or[Ref][pass reference to the value]
Or[Value][] } ), Or[Value][pass copy of te value] } ),
Prod(`in`, Prod(`in`,
{ Or[In][] { Or[In][parameter value used as input]
Or[Not In][] } ), Or[Not In][] } ),
Prod(`out`, Prod(`out`,
{ Or[Out][] { Or[Out][parametr value returned]
Or[Not Out][] } ), Or[Not Out][] } ),
Prod( Prod(
`tag`, `tag`,
@ -88,8 +88,6 @@
) )
== Семантика статического интерпретатора == Семантика статического интерпретатора
*TODO: исправить с обычного интерпретатора*
#h(10pt) #h(10pt)
$V := value$ - значения памяти $V := value$ - значения памяти
@ -100,8 +98,6 @@ $X$ - можество переменных
*TODO: специфицировать доступ* *TODO: специфицировать доступ*
*TODO: формально описать isCorrect для tag-а*
*TODO: формально описать accessor-ы tag* *TODO: формально описать accessor-ы tag*
$sigma : X -> tag times L$ - #[ позиции памяти, соответстующие переменным контекста, $sigma : X -> tag times L$ - #[ позиции памяти, соответстующие переменным контекста,