Kind1 icon indicating copy to clipboard operation
Kind1 copied to clipboard

Improve monadic syntax to support switch and case

Open elmattic opened this issue 4 years ago • 1 comments

It could help to have support of switch and case/if inside monads. E.g. for a binary format reader:

Wasm.Reader.val_type: Wasm.Reader(Wasm.ValType)
  (rst)
  open rst
  let len = 1#32
  def f = ((rst)
    rst@offset <- U32.add(rst@offset, len)
  ) :: Wasm.Reader.State -> Wasm.Reader.State
  if Wasm.Reader.State.ensure_bounds(rst, len) then
    let byte = Buffer8.get(rst.offset, rst.buffer)
    switch U8.eql(byte) {
      Wasm.Encoding.i32: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.i32),
      Wasm.Encoding.i64: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.i64),
      Wasm.Encoding.f32: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.f32),
      Wasm.Encoding.f64: Wasm.Reader.Reply.value!(f(rst), Wasm.ValType.f64),
    } default Wasm.Reader.Reply.fail!(
      rst.start,
      rst.offset,
      Wasm.Reader.Error.Code.invalid_val_type
    )
  else
    let code = Wasm.Reader.Error.Code.unexpected_end
    Wasm.Reader.Reply.fail!(rst.start, rst.offset, code)

It could be rewritten maybe as:

Wasm.Reader.val_type: Wasm.Reader(Wasm.ValType)
  Wasm.Reader {
    get byte = Wasm.Reader.u8
    switch U8.eql(byte) {
      Wasm.Encoding.i32: return Wasm.ValType.i32,
      Wasm.Encoding.i64: return Wasm.ValType.i64,
      Wasm.Encoding.f32: return Wasm.ValType.f32,
      Wasm.Encoding.f64: return Wasm.ValType.f64,
    } default Wasm.Reader.fail("Expected a Value Type")
  }

elmattic avatar Jun 29 '21 07:06 elmattic

I'm thinking about that, but that would be different to the way Haskell does it. I'm not sure if there is a good reason not to do that.

VictorTaelin avatar Jul 11 '21 13:07 VictorTaelin

Closed because the repository now only contains the code for the Kind2 language.

algebraic-dev avatar Sep 21 '22 16:09 algebraic-dev