Statically enforcing that two objects were created from the same (Int) “seed”

前端 未结 2 1698
盖世英雄少女心
盖世英雄少女心 2021-01-14 01:27

In a library I\'m working on, I have an API similar to the following:

data Collection a = Collection Seed {-etc...-}
type Seed = Int

newCollection :: Seed -         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 01:55

    I believe this is impossible with the constraint that the seeds come from runtime values, like user input. The typechecker as a tool can only reject invalid programs if we can determine the program is invalid at compiletime. Supposing there is a type such that the typechecker is able to reject programs based on user input, we could deduce that the typechecker is doing some sort of time travel or is able to wholly simulate our deterministic universe. The best you can do as a library author is to smuggle your types into something like ExceptT, which documents the seed precondition and exports awareness for it.

提交回复
热议问题