Hello,
Here I try to implement a function based on the counter using closures and
a reference in the ATS book, but incorporating a string(n), to make a
"string stream"
I’ve tried several things, but my “best shot” (as far as I can tell)
results in the error: d2exp_tr_up: not implemented yet: d2e0 =
D2Esym(string_length)
Any advice would be appreciated.
Perhaps a more fruitful approach would be defining an auxiliary function
for “peek” to do the work, and just let peek be concerned with enforcing
constraints, but it wasn’t clear to me that this would work either.
//typedef sizeLte (n:int) = [i:nat | i <= n] size_t (i)
typedef sizeLteRef(n:int) = ref(sizeLte(n))
typedef strstream = '{
peek = () - char,
next = () - void
//cose() - if using ptrs
}
//Get the minimum of two sizes, assert the minimum via type
fun {m,n:nat} minim (x:size_t m, y:size_t n): size_t(min(m,n))= if x < y
then x else y
// make a “string stream” that can return the current char (peek()) or
advance
// the position in the string by 1 (next())
fun {n:nat} {i:nat | i <= n} new_strstream(astring: string(n)): strstream =
let
val posn = ref<size_t> 0
val rstring = ref<string(n)> astring
//Error: d2exp_tr_up: not implemented yet: d2e0 = D2Esym(string_length)
val sz = ref<size_t n> string_length(!rstring)
in
’{ peek = lam() => !rstring[minim(size1_of_size(!posn), sz)],
next = lam() =>
let
val+ notend = string_isnot_atend(!rstring, size1_of_size(!posn))
val _ = case+ notend of
| false => !posn
| true => !posn := !posn+1
in
()
end
}
end