fizzbee-io / fizzbee

Easiest-ever formal methods language! Designed for developers crafting distributed systems, microservices, and cloud applications
https://fizzbee.io
Apache License 2.0
150 stars 8 forks source link

Implement deepcopy builtin #62

Closed jp-fizzbee closed 3 months ago

jp-fizzbee commented 3 months ago

Usage:

Color = enum('RED', 'YELLOW', 'GREEN')

action Init:
    # Define a record
    nested = record(a=1, b=[2, 3])
    msg = record(node=5, color=Color.RED, message='Hello', nested=nested)
    msg2 = msg
    msg3 = deepcopy(msg)
    print("orig", msg, msg2, msg3)

    msg.color = Color.GREEN
    msg.nested.a = -1
    msg.nested.b[0] = -2

    print("after", msg, msg2, msg3)

Limitations:

  1. Does not handle recursive data - it will go on an infinite loop :( .
  2. If two fields refer to a common data in the original, in the copy, it will be two separate data. So they are not structurally equivalent.
    action Init:
    arr = [2, 3]
    m1 = record(arr1=arr, arr2=arr)
    m2 = deepcopy(m1)
    print("orig", m1, m2)
    m1.arr1[0] = -4
    m2.arr1[0] = -3
    print("after", m1, m2)

    In the original, m1.arr1 and m1.arr2 points to the same array. So changing arr1 changes arr2 as well. But in the deepcopy, m2.arr1 and m2.arr2 DO NOT point to the same array, so changing arr1 doesn't change arr2.