Search code examples
idrisstate-monad

Control.ST pure type


pure : (result : ty) -> STrans m ty (out_fn result) out_fn from http://docs.idris-lang.org/en/latest/st/state.html#strans-primitive-operations

I'm not sure what (out_fn result) out_fn means. Is it about constraining out_fn to be a function of result? Does it actually say anything about the input resource list?

The given explanation seems to be "...provided that the current list of resources is correct when producing that value" but I'm not sure how to interpret it.


Solution

  • STrans : (m : Type -> Type) ->
             (result : Type) ->
             (inRes : Resources) ->
             (outRes : result -> Resources) ->
             Type
    

    You see that the input resources don't depend on the result of a computation, but the output resources do. Now, let's say we have

    MyResultType : Type
    myResult : MyResultType
    

    What's the type of pure myResult? It is STrans m MyResultType (f myResult) f. What are the input resources? f myResult, which could be anything. What are the output resources? Well, that depends on the result. But, this is pure, so the result is always myResult, and so the output resources are also f myResult. You see that the signature of pure is saying that the input and output resources can be anything, and that "anything" depends on f and myResult, but they have to be the same "anything"s no matter what.

    I believe an equivalent way to see the type of pure is

    pure' : (result : ty) -> STrans m ty resources (const resources)
    

    Which is probably clearer. I don't know why this signature is not used.