Skip to content

Effect widening doesn't occur for effect parameters #857

@timbertson

Description

@timbertson

I'm not sure if I'm describing it right, but here's a simple example where a structure has a parameterised effect:

// lazy.kk

struct lazy<e, a>
  compute: () -> e a

fun get(l: lazy<e, a>): e a
  l.compute.()

fun pure<a>(value: a): lazy<total, a>
  Lazy { value }

fun map(l: lazy<e, a>, f: (a) -> e b): lazy<e, b>
  Lazy { f(l.get) }

But attempting to introduce console in a map call doesn't work:

fun main()
  val a = pure(1)
  val b = a.map fn(i)
    println("mapping over " ++ i.show)
    i.show
  
  val result = b.compute.()
  println("result: " ++ result)

I get:

std/lazy.kk(15,21): type error: effects do not match
  context        :                 fn(i)
                       println("mapping over " ++ i.show)
                       i.show
  term           :                     
                       println("mapping over " ++ i.show)
                       i.show
  inferred effect: <console/console|_e>
  expected effect: total
  unexpected     : console/console

If this were a regular function koka would simply widen the pure function's effect into a console function, is there a way to get this to happen with effect type params?


Incidentally, I don't understand the error when I mistakenly use <total> instead of total when giving a type to a (it works fine as lazy<total,int>):

	val a: lazy<<total>, int> = pure(1)

// error:
std/lazy.kk(14,31): type error: types do not match
  context      :       a
  term         :                               pure(1)
  inferred type: lazy<total,int>
  expected type: lazy<total,_a>
  because      : type cannot be instantiated to match the annotation

That definitely sounds like a problem with the value type, not the effect. I get the same if I try to manually widen the type to e.g. <div,console>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions