Skip to content

Latest commit

 

History

History
153 lines (122 loc) · 4.42 KB

Sml-to-Scala.md

File metadata and controls

153 lines (122 loc) · 4.42 KB

VM

Notes on Migrating from SML to Scala


Basic Types

SML Scala
unit Unit
int Int
string String
ty1 * ty2 ( ty1 , ty2 )
ty1 -> ty2 ty1 => ty2
ty option Option[ty]
ty list List[ty]
...

Variable Declarations

SML Scala
val name = value val name = value
val name = ref value var name = value

Dataypes

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

  1. replace datatype ty = with sealed trait Ty
  2. replace nullary value constructor ANY with case object ANY extends Ty
  3. replace n-ary value constructor ARRAY of ty with case class ARRAY(ty: Ty) extends Ty
  4. for ref types use a var constructor argument (vs val for everything else), e.g. META of ty ref will becomes case 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

Signatures and Structures

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:

  1. create a new file stringpool.scala (or copy stringpool.sml into stringpool.scala if you want to retain comments, headers, aso asf)

  2. 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
     }
    
  3. create an object StringPool extending the STRING_POOL trait, and assign every val in the object to the corresponding implementation from StringPoolImp:

     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
     }
    
  4. create a private StringPoolImp object which will contain the actual implementation from the StringPool SML structure:

     private object StringPoolImp extends STRING_POOL {
       <structure implementaton goes here>
     }