You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I read your book "Introduction to Programming in ATS" and I wondered if there is a way to not use a unsafe cast in the takeout_node_at function in the section about Quicksort in list_vt.
I came up with the following solutions:
Solution 1
If I understand correctly a _unfold type corresponds to a datconptr (void* in C) that points to a struct which corresponds to the constructor. For example for list_vtlist_vt_cons_unfold(l0, l1, l2) points to a struct of t@ype and ptr. So wouldn't it be sensible to be allowed to access these variables given the corresponding proofs exist (e.g. a@l1 or ptr?@l2).
For example tuple accessor syntax could be used to inspect a partially broken _unfold type. 0 could be the base pointer to l0 and the rest to the fields of the struct.
Runtime wise this is the identity function so it should have no penalty.
This approach would also eliminate the passing around of additional pointers when using an _unfold type as one could just create a _pstruct type. (For example in the insord function from insertion sort). This would eliminate the data duplication that is common practice now when using _unfold plus a bunch of pointers as all information is available from the base pointer.
I am a beginner in ATS so maybe the type of p should be t@ype or something else.
Implementation of takeout_node_at:
fun{a:t@ype} takeout_node_at{n:int}{k:nat | k < n} .<k>.
(xs: &list_vt(a, n) >> list_vt(a, n-1), k: int k) : list_vt_cons_pstruct(a, ptr?) =if k >0thenletval+ @list_vt_cons(x, xs1) = xs
val res = takeout_node_at<a>(xs1, k-1)
prval () = fold@ (xs)
in res endelseletval+ @list_vt_cons(x, xs1) = xs
val nx = xs
val () = xs := xs1
in
list_vt_cons_pstruct(view@x, view@xs1 | nx)
end
The text was updated successfully, but these errors were encountered:
Yes, you are right. The unsafe cast in my code can be replaced with safe code. Both of your solutions should work. What I would really like to support is a way to do it automatically. A big issue with ATS is that it imposes too much of a burden on the programmer for writing safe and efficient code.
I read your book "Introduction to Programming in ATS" and I wondered if there is a way to not use a unsafe cast in the
takeout_node_at
function in the section about Quicksort inlist_vt
.I came up with the following solutions:
Solution 1
If I understand correctly a
_unfold
type corresponds to adatconptr
(void*
in C) that points to a struct which corresponds to the constructor. For example forlist_vt
list_vt_cons_unfold(l0, l1, l2)
points to a struct oft@ype
andptr
. So wouldn't it be sensible to be allowed to access these variables given the corresponding proofs exist (e.g.a@l1
orptr?@l2
).For example tuple accessor syntax could be used to inspect a partially broken
_unfold
type.0
could be the base pointer tol0
and the rest to the fields of the struct.Pattern matching could also be used to realize this feature. Maybe even a combination of both.
Solution 2
It seems to me that
_pstruct
is just a_unfold
bundled with the appropriate proofs like demonstrated in this viewtype:So for example there could be function
list_vt_cons_pstruct
that turns alist_vt_cons_unfold
and the proofs into alist_vt_cons_pstruct
.Runtime wise this is the identity function so it should have no penalty.
This approach would also eliminate the passing around of additional pointers when using an
_unfold
type as one could just create a_pstruct
type. (For example in theinsord
function from insertion sort). This would eliminate the data duplication that is common practice now when using_unfold
plus a bunch of pointers as all information is available from the base pointer.I am a beginner in ATS so maybe the type of
p
should bet@ype
or something else.Implementation of
takeout_node_at
:The text was updated successfully, but these errors were encountered: