SML | Scala |
---|---|
unit | Unit |
int | Int |
string | String |
ty1 * ty2 | ( ty1 , ty2 ) |
ty1 -> ty2 | ty1 => ty2 |
ty option | Option[ty] |
ty list | List[ty] |
... |
SML | Scala |
---|---|
val name = value | val name = value |
val name = ref value | var name = value |
For nullary constructor datatypes:
SML | Scala |
---|---|
datatype ty-name = | sealed trait ty-name |
nullary value constructors: | |
vconstructor-name | case object vconstructor-name extends ty-name |
>0 -ary value constructors: |
vconstructor-name of tys... | case class vconstructor-name ( tys... ) extends ty-name
For example, to convert the ty
datatype declaration below
datatype ty = ANY
| ANYVAL
| BOOL
| INT
| ANYREF
| STRING
| ARRAY of ty
| RECORD of (Symbol.symbol * ty) list
| NULL
| NOTHING
| META of ty ref
follow the steps
- replace
datatype ty =
withsealed trait Ty
- replace nullary value constructor
ANY
withcase object ANY extends Ty
- replace n-ary value constructor
ARRAY of ty
withcase class ARRAY(ty: Ty) extends Ty
- for
ref
types use avar
constructor argument (vsval
for everything else), e.g.META of ty ref
will becomescase class META(var ty: Ty) extends Ty
Here is the Scala construct corresponding to the SML
datatype
declaratio above:
sealed trait Ty
case object ANY extends Ty
case object ANYVAL extends Ty
case object BOOL extends Ty
case object INT extends Ty
case object ANYREF extends Ty
case object STRING extends Ty
case class ARRAY(ty: Ty) extends Ty
case class RECORD(lts: List[(String, Ty)]) extends Ty
case object NULL extends Ty
case object NOTHING extends Ty
case class META(var ty: Ty) extends Ty
SML | Scala |
---|---|
signature name | sealed trait name |
structure name : sig-name | object name extends sig-name |
= struct | private object nameImp extends sig-name |
For example for converting file stringpool.sml
containing the following
signature/struture:
signature STRING_POOL = sig
type entry
val id : entry -> int
val str: entry -> string
val clear : unit -> unit
val size : unit -> int
val add : string -> entry
val lookup: string -> entry option
val emit : (int -> unit) -> unit
end
structure StringPool : STRING_POOL = struct
<structure implementaton>
end
follow the steps:
-
create a new file
stringpool.scala
(or copystringpool.sml
intostringpool.scala
if you want to retain comments, headers, aso asf) -
create a
sealed trait STRING_POOL
and copy all declarations from the SML signature into it, like so:sealed trait STRING_POOL { type Entry val id : Entry => Int val str: Entry => String val clear : Unit => Unit val size : Unit => Int val add : String => Entry val lookup: String => Option[Entry] val emit : (Int => Unit) => Unit }
-
create an
object StringPool
extending theSTRING_POOL
trait, and assign everyval
in the object to the corresponding implementation fromStringPoolImp
:object StringPool extends STRING_POOL { type Entry = (String, Int) val id = StringPoolImp.id val str= StringPoolImp.str val clear = StringPoolImp.clear val size = StringPoolImp.size val add = StringPoolImp.add val lookup= StringPoolImp.lookup val emit = StringPoolImp.emit }
-
create a private
StringPoolImp
object which will contain the actual implementation from theStringPool
SML structure:private object StringPoolImp extends STRING_POOL { <structure implementaton goes here> }