SML uses signature abstype

I am writing a library for processing simple images in standard ML. It must support the various types used as the color for each pixel, for example. bool, Word8.word, etc.

I have abstype 'a imagewith all the common functions defined independently of the view ( 'a- this is the color view), but the output formats are different, so I would like to have different structures.

Is there a way to “open” abstypeinside a structure? I can make this work very ugly:

abstype 'clr absimage = Image of {width : int, height : int, data : 'clr array array}
with
    fun createFunc (w, h) f = Image {width = w, height = h, data = ...}
    fun createBlank (w, h) clr = createFunc (w, h) (fn _ => clr)
    ...
end
signature IMAGE = sig
    type colour
    type image
    val createFunc : (int * int) -> (int * int -> colour) -> image
    val createBlank : (int * int) -> colour -> image
    ...
    val toBinPPM : image -> string -> unit
end
functor ImageFn(C : sig type colour end) = struct
    open C
    type image = colour absimage
    val createFunc = createFunc
    val createBlank = createBlank   
    ...
end
structure Image8 :> IMAGE = struct
    structure T = ImageFn(struct type colour = Word8.word end)
    open T

    fun toBinPPM img filename = ...
end

In particular, to define a functor, it is required to write expressions such as val name = namefor all functions defined in the with ... endpart abstype.

Or is my approach completely wrong?

abstype signature - OOP abstype signature

P.S. SML , open (ImageFn(struct ... end)), (T )?

+4
1

abstype SML. . . , , ( :>), . , - .

abstype image datatype ImageFn , :

signature IMAGE =
sig
  type colour
  type image
  val createFunc : int * int -> (int * int -> colour) -> image
  val createBlank : int * int -> colour -> image
  ...
end

signature IMAGE8 =
sig
  include IMAGE
  val toBinPPM : image -> string -> unit
end

functor ImageFn(type colour) :> IMAGE =
struct
  datatype image = Image of {width : int, height : int, data : colour array array}
  fun createFunc (w, h) f = Image {width = w, height = h, data = ...}
  fun createBlank (w, h) clr = createFunc (w, h) (fn _ => clr)
  ...
end

structure Image8 :> IMAGE8 =
struct
  structure T = ImageFn(type colour = Word8.word)
  open T
  fun toBinPPM img filename = ...
end

: image . :

type image = {width : int, height : int, data : colour array array}

PS: , . . SML .

+5

Source: https://habr.com/ru/post/1677600/


All Articles