How to make a type with restrictions

前端 未结 5 1911
我在风中等你
我在风中等你 2020-12-01 12:06

For example I want to make a type MyType of integer triples. But not just Cartesian product of three Integer, I want the type to represent all (x, y, z) such that x + y + z

5条回答
  •  感动是毒
    2020-12-01 12:57

    I think the trick here is that you don't enforce it on the type-level, you use "smart constructors": i.e. only allow creation of such "tuples" via a function that generates such values:

    module Test(MyType,x,y,z,createMyType) where
    
    data MyType = MT { x :: Int, y :: Int, z :: Int }
    
    createMyType :: Int -> Int -> MyType
    createMyType myX myY = MT { x = myX, y = myY, z = 5 - myX - myY }
    

    If you want to generate all possible such values, then you can write a function to do so, either with provided or specified bounds.

    It may very well be possible to use type-level Church Numerals or some such so as to enforce creation of these, but it's almost definitely too much work for what you probably want/need.

    This might not be what you want (i.e. "Except of using just (x, y) since z = 5 - x - y") but it makes more sense than trying to have some kind of enforced restriction on the type level for allowing valid values.

    Types can ensure the correct "type" of value (no pun intended); to ensure validity of values you hide the constructor and only allow creation via approved functions that guarantee any invariants you require.

提交回复
热议问题