title: What is an appropriate type for smart contracts?
body: <p>I'm wondering what is the best way to express smart contracts in typed languages such as Haskell or Idris (so you could, for example, compile it to run on the Ethereum network). My main concern is: what is the type that captures everything that a contract could do?</p>
  <h3>Naive solution: EthIO</h3>
  <p>A naive solution would be to define a contract as a member of an <code>EthIO</code> type. Such type would be like Haskell's <code>IO</code>, but instead of enabling system calls, it would include blockchain calls, i.e., it would enable reading from and writing to the blockchain's state, calling other contracts, getting block data and so on.</p>
  <pre><code>-- incrementer.contract
  
  main: EthIO
  main = do
     x &lt;- SREAD 0x123456789ABCDEF
     SSTORE (x + 1) 0x123456789ABCDEF
  </code></pre>
  <p>This is clearly sufficient to implement any contract, but:</p>
  <ol>
  <li><p>Would be too powerful.</p>
  </li>
  <li><p>Would be very coupled to the Ethereum blockchain specifically.</p>
  </li>
  </ol>
  <h3>Conservative solution: event sourcing pattern</h3>
  <p>Under that idea, a contract would be defined as fold over a list of actions:</p>
  <pre><code>type Contract action state = {
      act  : UserID -&gt; action -&gt; state -&gt; state,
      init : state
  }
  </code></pre>
  <p>So, a program would look like:</p>
  <pre><code>incrementer.contract
  
  main : Contract
  main = {
      act _ _ state = state + 1,
      init          = 0
  }
  </code></pre>
  <p>That is, you define an initial state, a type of action, and how that state changes when a user submits an action. That would allow one to define any arbitrary contract that doesn't involve sending/receiving money. Most blockchains have some kind of currency and most useful contracts involve money somehow, so that type would be way too restrictive.</p>
  <h3>Less conservative solution: events + currency</h3>
  <p>We can make the type above aware of currencies by hardcoding a currency logic into the type above. We'd, thus, get something like:</p>
  <pre><code>type Contract action state = {
      act        : UserID -&gt; action -&gt; state -&gt; state,
      init       : state,
      deposit    : UserID -&gt; Amount -&gt; state -&gt; state,
      withdrawal : UserID -&gt; Amount -&gt; state -&gt; Maybe state
  }
  </code></pre>
  <p>I.e., the contract developer would need to explicitly define how to deal with monetary deposits and withdrawals. That type would be enough to define any self-contained contract which can interact with the host blockchain's currency. Sadly, <strong>such a contract wouldn't be able to interact with other contracts</strong>. In practice, contracts often interact with each other. An Exchange, for example, needs to communicate with its exchanged Token contracts to query balances and so on.</p>
  <h3>Generalization: global state?</h3>
  <p>So, let's take a step back and rewrite the conservative solution as this:</p>
  <pre><code>type Contract = {
      act  : UserID -&gt; Action -&gt; Map ContractID State -&gt; State,
      init : State
  }
  </code></pre>
  <p>Under this definition, the <code>act</code> function would have access not only to the contract's own state but the state of every other contract on the same blockchain. Since every contract can read each other's state, one could easily implement a communication protocol on top of this, and, thus, such type is sufficient to implement arbitrarily interacting contracts. Also, if the blockchain's currency was itself implemented as a contract (possibly using a wrapper), then that type would also be sufficient to deal with money, despite not having it hardcoded on the type. But that solution has 2 problems:</p>
  <ol>
  <li><p>Peeking at the other contract's state looks like a very &quot;hacky&quot; way to enable communication;</p>
  </li>
  <li><p>A contract defined this way wouldn't be able to interact with existing contracts which aren't aware of that solution.</p>
  </li>
  </ol>
  <h3>What now?</h3>
  <p>Now I'm in the dark. I know I'm not in the right abstraction for this problem, but I'm not sure what it would be. <em>It looks like the root of the problem is that I'm not able to capture the phenomenon of cross-contract communications properly.</em> What concrete type would be more suitable to define arbitrary smart-contracts?</p>