-
Notifications
You must be signed in to change notification settings - Fork 55
Add basic support for del key word in Python->Laurel
#1054
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
base: main2
Are you sure you want to change the base?
Changes from 5 commits
6b20af8
a247211
4d0db12
312c974
a2cb07c
228dd05
dff2df9
fec063c
8b41562
5c39ff9
080b72b
4627fe1
e6cfede
a61b257
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 |
|---|---|---|
|
|
@@ -398,7 +398,6 @@ function List_slice_non_neg (l : ListAny, start : int, stop: int) : ListAny | |
| else List_take (List_drop (l, start), int_min(stop, List_len(l)) - start) | ||
| }; | ||
|
|
||
|
|
||
| function List_slice (l : ListAny, start : int, stop: int) : ListAny | ||
| { | ||
| List_slice_non_neg (l, | ||
|
|
@@ -407,6 +406,26 @@ function List_slice (l : ListAny, start : int, stop: int) : ListAny | |
| ) | ||
| }; | ||
|
|
||
| function List_remove_non_neg(l: ListAny, i: int) : ListAny | ||
| requires i >= 0 && i < List_len(l) | ||
| { | ||
| List_extend(List_take(l, i),List_drop(l, i + 1)) | ||
| }; | ||
|
|
||
| function List_remove(l: ListAny, i: int) : ListAny | ||
| requires i >= - List_len(l) && i < List_len(l) | ||
| { | ||
| if i >= 0 then List_remove_non_neg(l, i) | ||
| else List_remove_non_neg(l, List_len(l) + i) | ||
| }; | ||
|
|
||
| function List_remove_slice(l: ListAny, start: int, stop: int) : ListAny | ||
| { | ||
| List_extend( | ||
| List_take(l, if start >= 0 then int_min(start, List_len(l)) else int_max(List_len(l) + start, 0)), | ||
| List_drop(l, if stop >= 0 then int_min(stop, List_len(l)) else int_max(List_len(l) + stop, 0))) | ||
| }; | ||
|
Comment on lines
+418
to
+437
Contributor
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. Proof coverage — four cheap theorems that would lock in Python semantics. All four new runtime functions have closed-form element/length specifications. These can live in a new
Even if only (3) lands, that's the regression-proof version of the concern above, and is the highest-value theorem in the set. Similarly worth adding for
Contributor
Author
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 added the proofs for |
||
|
|
||
| function List_set_non_neg (l : ListAny, i : int, v: Any) : ListAny | ||
| requires i >= 0 && i < List_len(l) | ||
| { | ||
|
|
@@ -465,6 +484,13 @@ function DictStrAny_insert (d : DictStrAny, key: string, val: Any) : DictStrAny | |
| else DictStrAny_cons(DictStrAny..key!(d), DictStrAny..val!(d), DictStrAny_insert(DictStrAny..tail!(d), key, val)) | ||
| }; | ||
|
|
||
| function DictStrAny_remove (d : DictStrAny, key: string) : DictStrAny | ||
| { | ||
| if DictStrAny..isDictStrAny_empty(d) then DictStrAny_empty() | ||
| else if DictStrAny..key!(d) == key then DictStrAny..tail!(d) | ||
| else DictStrAny_cons(DictStrAny..key!(d), DictStrAny..val!(d), DictStrAny_remove(DictStrAny..tail!(d), key)) | ||
| }; | ||
|
|
||
| function Any_get (dictOrList: Any, index: Any): Any | ||
| requires (Any..isfrom_DictStrAny(dictOrList) && Any..isfrom_str(index) && DictStrAny_contains(Any..as_Dict!(dictOrList), Any..as_string!(index))) || | ||
| (Any..isfrom_ListAny(dictOrList) && Any..isfrom_int(index) && Any..as_int!(index) >= - List_len(Any..as_ListAny!(dictOrList)) && Any..as_int!(index) < List_len(Any..as_ListAny!(dictOrList))) | ||
|
|
@@ -535,6 +561,35 @@ function Any_sets! (indices: ListAny, dictOrList: Any, val: Any): Any | |
| Any_sets!(ListAny..tail!(indices), Any_get!(dictOrList, ListAny..head!(indices)), val)) | ||
| }; | ||
|
|
||
| function Any_remove (dictOrList: Any, index: Any): Any | ||
| { | ||
| if Any..isexception(dictOrList) then dictOrList | ||
| else if Any..isexception(index) then index | ||
| else if !(Any..isfrom_DictStrAny(dictOrList) && Any..isfrom_str(index)) && !(Any..isfrom_ListAny(dictOrList) && Any..isfrom_int(index)) then | ||
| exception (TypeError("Invalid subscription type")) | ||
| else if Any..isfrom_DictStrAny(dictOrList) && Any..isfrom_str(index) && DictStrAny_contains(Any..as_Dict!(dictOrList), Any..as_string!(index)) then | ||
| from_DictStrAny(DictStrAny_remove(Any..as_Dict!(dictOrList), Any..as_string!(index))) | ||
| else if Any..isfrom_ListAny(dictOrList) && Any..isfrom_int(index) && Any..as_int!(index) >= - List_len(Any..as_ListAny!(dictOrList)) && Any..as_int!(index) < List_len(Any..as_ListAny!(dictOrList)) then | ||
| from_ListAny(List_remove(Any..as_ListAny!(dictOrList), Any..as_int!(index))) | ||
| else | ||
| exception (IndexError("Invalid subscription")) | ||
| }; | ||
|
thanhnguyen-aws marked this conversation as resolved.
|
||
|
|
||
| function Any_remove_slice (list: Any, index: Any): Any | ||
| { | ||
| if Any..isexception(list) then list | ||
| else if Any..isexception(index) then index | ||
| else if !(Any..isfrom_ListAny(list) && Any..isfrom_Slice(index)) then | ||
| exception (TypeError("Invalid subscription type")) | ||
| else | ||
| from_ListAny(List_remove_slice( | ||
| Any..as_ListAny!(list), | ||
| Any..start!(index), | ||
| if OptionInt..isOptSome(Any..stop!(index)) | ||
| then OptionInt..unwrap!(Any..stop!(index)) | ||
| else List_len(Any..as_ListAny!(list)))) | ||
| }; | ||
|
|
||
| function Any_len (v: Any) : int; | ||
|
|
||
| function Any_len_to_Any (v: Any) : Any { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| test_del_dict_key.py(4, 4): ✅ pass - assert_assert(56)_calls_PNotIn_0 | ||
| test_del_dict_key.py(4, 4): ✅ pass - key deleted | ||
| DETAIL: 2 passed, 0 failed, 0 inconclusive | ||
| RESULT: Analysis success |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| test_del_list_item.py(4, 4): ✅ pass - assert_assert(49)_calls_Any_get_0 | ||
| test_del_list_item.py(4, 4): ✅ pass - first unchanged | ||
| test_del_list_item.py(5, 4): ✅ pass - assert_assert(90)_calls_Any_get_0 | ||
| test_del_list_item.py(5, 4): ✅ pass - second shifted | ||
| DETAIL: 4 passed, 0 failed, 0 inconclusive | ||
| RESULT: Analysis success |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| test_del_list_negative_index.py(4, 4): ✅ pass - assert_assert(53)_calls_Any_get_0 | ||
| test_del_list_negative_index.py(4, 4): ✅ pass - first unchanged | ||
| test_del_list_negative_index.py(5, 4): ✅ pass - assert_assert(94)_calls_Any_get_0 | ||
| test_del_list_negative_index.py(5, 4): ✅ pass - second unchanged | ||
| test_del_list_negative_index.py(6, 4): ✅ pass - assert_assert(136)_calls_Any_get_0 | ||
| test_del_list_negative_index.py(6, 4): ✅ pass - third unchanged | ||
| DETAIL: 6 passed, 0 failed, 0 inconclusive | ||
| RESULT: Analysis success |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| test_del_list_slice.py(4, 4): ✅ pass - assert_assert(57)_calls_Any_get_0 | ||
| test_del_list_slice.py(4, 4): ✅ pass - first unchanged | ||
| test_del_list_slice.py(5, 4): ✅ pass - assert_assert(98)_calls_Any_get_0 | ||
| test_del_list_slice.py(5, 4): ✅ pass - fourth shifted | ||
| test_del_list_slice.py(6, 4): ✅ pass - assert_assert(138)_calls_Any_get_0 | ||
| test_del_list_slice.py(6, 4): ✅ pass - fifth shifted | ||
| DETAIL: 6 passed, 0 failed, 0 inconclusive | ||
| RESULT: Analysis success |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| def test(): | ||
| xs = [1, 2, 3, 4] | ||
| del xs[-1] | ||
| assert xs[0] == 1, "first unchanged" | ||
| assert xs[1] == 2, "second unchanged" | ||
| assert xs[2] == 3, "third unchanged" | ||
| test() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| def test(): | ||
| xs = [1, 2, 3, 4, 5] | ||
| del xs[1:3] | ||
| assert xs[0] == 1, "first unchanged" | ||
| assert xs[1] == 4, "fourth shifted" | ||
| assert xs[2] == 5, "fifth shifted" | ||
| test() | ||
|
thanhnguyen-aws marked this conversation as resolved.
Outdated
|
||
Uh oh!
There was an error while loading. Please reload this page.