Why are flexible types not allowed in record type definitions?

I am trying to do this:

type TS1<'state, 'action> = { actions : 'state -> #seq<'action> move : 'state -> 'action -> 'state state0 : 'state } 

But type checking doesn't let me:

 .../stdin(2,29):error FS0715: Anonymous type variables are not permitted in this declaration 

However, if I breed the definition of a flexible type, I am fine:

 type TS2<'state, 'action, 'actions when 'actions :> seq<'action>> = { actions : 'state -> 'actions move : 'state -> 'action -> 'state state0 : 'state } 

I don't like adding a variable like 'actions - this makes connecting to deterministic transition systems, as mathematical objects are less obvious.

I donโ€™t see what could go wrong if flexible types are allowed in record definitions. Is it somehow dangerous? Is there any other way to get the clarity of the definition I would like?

Update . I was hoping I could write functions on TS types that use well-known implementations; that is, I want to be able to define a function

 let has_action a ts s = Set.contains a <| ts.actions s 

This will not explicitly be printed if the member type is actions : 'state -> seq<'action> . I can do this with the second definition, in which case has_action is of type

 has_action : a:'a -> ts:TS2<'s,'a,Set<'a>> -> s: -> bool when 'a : comparison 

The type of this example suggests that the flexible type in TS1 might not have helped. Can't I avoid a useless third type parameter in TS2? It seems to me that the exact implementation of a collection of actions for a state is an implementation detail that should not be displayed in a type.

+5
source share
2 answers

This is simply a limitation of the current compiler implementation of allowed signature types. For example, if you define your type in the same way conceptually, but using an interface or an abstract class instead of writing, it compiles fine:

 type TS1<'state, 'action> = abstract actions : 'state -> #seq<'action> abstract move : 'state -> 'action -> 'state abstract state0 : 'state 
+1
source

You pretty much answered your question here ... The second option works because you enter another type argument, i.e. you are abstracting over 'actions . The bottom line is what you need cannot determine the general values โ€‹โ€‹in the record definition. Note that the second option is not common for types, since 'state and 'actions defined.

+2
source

Source: https://habr.com/ru/post/1216175/


All Articles