How to represent this FSM in GADT

I was watching this video and learned about GADTs in Scala.

I was able to represent the below state machine

Here’s my code

type Idle
type Moving

enum Direction:
  case East, West, North, South
import Direction.*

enum Command[From, To]:
  case Turn(direction: Direction) extends Command[Idle, Idle]
  case Start extends Command[Idle, Moving]
  case Stop extends Command[Moving, Idle]
  case Chain[A, B, C](command1: Command[A, B], command2: Command[B, C])
      extends Command[A, C]

import Command.*

extension [A, B, C](command1: Command[A, B])
  infix def ~>(command2: Command[B, C]): Command[A, C] =
    Chain(command1, command2)

Now I am looking to change one detail. I want to give the ability to Turn when Idle and also when Moving. And after Turn Command, I want the machine to retain whatever state it was in. For example, if a machine was moving and it takes a turn, I want the state machine to retain that information.

For instance

  • Start ~> Turn(West) should be Command[Idle,Moving]
  • Start ~> Stop ~> Turn(West) should be Command[Idle,Idle]

1 Like

I have solved it just by adding a type parameter to Turn command

4 Likes