News for hackers. Stuff that matters.
Re:Unix system programming in OCaml (2014) by emersion on Thursday July 12, 2018 @07:10AM (#17513478)
We need optional values for bytes, as a way to say "this byte is undefined", so we can't use Bytes directly.

But yes, we're doing it wrong for sure.

EDIT: seems like you edited your reply, so let me add this to mine :)

>Looked at your code, damn it's bad, I'd really suggest to dive into BAP sources and documentation a bit to understand how to write OCaml. It feels like you are trying to write in OCaml as in some different language, Haskell maybe, not using modules and functors. Reminds me how J people write C.

Oh, I also think it's bad don't worry. However we're using a subset of OCaml (Lem) so we don't have things like modules for instance. And no, I really don't want to start using complicated module features and functors, for the reasons I explained above.

Re:Unix system programming in OCaml (2014) by ernst_klim on Thursday July 12, 2018 @07:20AM (#17513519)
Well, you could use arrays of optional bytes, but are use sure it's clever?

Maybe it's much better to define an abstract module of type

    module type Mem = sig
      type t
      val get_byte : t -> ~offset:int -> char option  
    end

And invent some clever (maybe lazy) implementation using just Bigarray?

BAP also has something like this in

https://github.com/BinaryAnalysisPlatform/bap/blob/master/li...

You could ask about BAP in general on

https://discuss.ocaml.org/

especially

https://discuss.ocaml.org/u/ivg/summary

(one of the guys behind BAP, very clever and talented in explaining stuff)

Re:Unix system programming in OCaml (2014) by dpwm on Thursday July 12, 2018 @07:31AM (#17513555)
You can always do something like:

  type optional_bytes = (Bytes.t * Bytes.t)

  let get i (data, mask) =
    match mask.[i] with
    | '\x00' -> None
    | _ -> Some data.[i]
If you need immutability and you're not changing often, you can use strings.

If you're using lists because you're doing all manner of comprehensions, you'd still be better iterating over the Bytes and rebuilding using a Buffer.

Re:Unix system programming in OCaml (2014) by ernst_klim on Thursday July 12, 2018 @07:37AM (#17513588)
>I really don't want to start using complicated module features and functors, for the reasons I explained above.

And why is that? Because you really don't want to write ML without modules (and you are using them anyway).

Edit: Ah, I see, you have an auto-generated ocaml code, which is used along with the Isabelle. That makes sense (although your code generator is rather strange and there is still no need to use lists for memory representations since Isabelle reasoning about memory chunks shouldn't be that hard). Anyway I wouldn't claim that OCaml is that bad for low level programming basing on such a subset of a language.