<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Edwin Brady</title><link href="https://www.type-driven.org.uk/edwinb/" rel="alternate"/><link href="https://www.type-driven.org.uk/edwinb/feeds/atom.xml" rel="self"/><id>https://www.type-driven.org.uk/edwinb/</id><updated>2026-10-26T17:00:00+00:00</updated><entry><title>Notes: 26th October 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-26th-october-2026.html" rel="alternate"/><published>2026-10-26T17:00:00+00:00</published><updated>2026-10-26T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-10-26:/edwinb/notes-26th-october-2026.html</id><summary type="html">&lt;p&gt;Continuing on the surface language constructs. Still finding some surprising
bits of complexity. Idris 2's implementation was very much &amp;quot;move fast and
break things&amp;quot; which sort of made since because I needed the thing running,
but that means, well, you can tell when looking at the implementation. So
this time …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Continuing on the surface language constructs. Still finding some surprising
bits of complexity. Idris 2's implementation was very much &amp;quot;move fast and
break things&amp;quot; which sort of made since because I needed the thing running,
but that means, well, you can tell when looking at the implementation. So
this time it's more &amp;quot;move slowly and understand what you're doing&amp;quot;.&lt;/p&gt;
&lt;p&gt;Here's a little detail that Idris 2 gets wrong, and I don't know if anybody has
actually noticed, but could be the source of some confusion if you write
something just the wrong way - I've just been implementing the elaborator for
variables, something you'd &lt;em&gt;think&lt;/em&gt; would be simple, because you check if it's
defined locally, then check if it's defined globally, then if it's neither,
report an error. But &amp;quot;locally&amp;quot; could mean one of two things: is it in the
environment (pattern variables or lambda, let, or pi bound things) or is it a
locally defined function (e.g. in a where clause, or a local pattern matching
definition). So Idris first looks for local definitions, then in the local
environment, then globally. While this'll get the right answer most of them
time (because hopefully you're not using the same name for a local function
definition and a local variable) you could contrive something that makes it
not do the right thing (because if you want to write something odd, or indeed
have a machine generate something odd, the compiler should support you!)&lt;/p&gt;
&lt;p&gt;Anyway. The fix is simple: keep track of the order names are defined so that
when you encounter a name, you know where to look. It'd be really annoying
to do this fix in Idris 2 (for reasons I won't go into here) but fortunately
this time, it's no big deal.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 19th October 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-19th-october-2026.html" rel="alternate"/><published>2026-10-19T17:00:00+00:00</published><updated>2026-10-19T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-10-19:/edwinb/notes-19th-october-2026.html</id><summary type="html">&lt;p&gt;I see I'm a bit behind here. Hello again. I've spent the last little while
working on a parser for the surface language, and the structure of the
elaborator and some of the machinery that entails. A lot of this has involved
trying to remind myself how Idris 2 itself …&lt;/p&gt;</summary><content type="html">&lt;p&gt;I see I'm a bit behind here. Hello again. I've spent the last little while
working on a parser for the surface language, and the structure of the
elaborator and some of the machinery that entails. A lot of this has involved
trying to remind myself how Idris 2 itself works, partly because it seems to
make sense to borrow some code where appropriate, and partly because if I
remember why we do things a certain way there, then maybe I can make some more
informed decisions this time around.&lt;/p&gt;
&lt;p&gt;The main thing I've noticed is: wow, it's grown to become complex, and probably
more complex than it needs to be! For example, currently I'm looking at
unbound implicits in the new elaborator - essentially that's where if we
see a variable we don't know about in the type declaration (under some
syntactic constraints) then we implicitly bind it. It's mostly a syntactic
convenience, but can be a bit more than that if there's a lot of dependency in
a type. Anyway, I was looking at the Idris 2 implementation for reference
and trying to work out why the code was so complex, since it feels like a
simple feature after all.&lt;/p&gt;
&lt;p&gt;It turns out (to cut a long story short...) that a big reason for the complexity
is because in Idris 2, rather than elaborating the surface language directly,
there is a desugaring step that goes to an intermediate language called
&lt;cite&gt;TTImp&lt;/cite&gt; (for TT with implicits). At the start, that seemed like a good idea,
because it'd keep the language I needed to elaborate smaller, and it's fairly
obvious how most high level features are desugared. Unfortunately there are
some big disadvantages:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Error messages are based on the desugared code, not the code you wrote, which
can be confusing&lt;/li&gt;
&lt;li&gt;Not all features have an immediately obvious desugaring. Interfaces, for
example, translate into records, but there's a lot of work to do and it's not
purely syntactic&lt;/li&gt;
&lt;li&gt;As I've stumbled on recently, a lot of complexity creeps into the
implementation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this case, the complexity arises because there is a construct in &lt;cite&gt;TTImp&lt;/cite&gt;
called &lt;cite&gt;IBindHere&lt;/cite&gt; that says &amp;quot;I need you to bind the unbound implicits at this
point&amp;quot; and because of the desugaring of some more complex structures, it's
not always at the top of a type declaration.&lt;/p&gt;
&lt;p&gt;Anyway I don't know if this ramble helped anyone bu myself, but I do keep
finding this examples where, in the interests of making Idris 2 easier to
write in the first place, it has become much harder to maintain now that it's
grown.&lt;/p&gt;
&lt;p&gt;I'd already decided to dispense with &lt;cite&gt;TTImp&lt;/cite&gt;, and I'm even happier that I did
now.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 21 September 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-21-september-2026.html" rel="alternate"/><published>2026-09-21T17:00:00+00:00</published><updated>2026-09-21T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-09-21:/edwinb/notes-21-september-2026.html</id><summary type="html">&lt;p&gt;Last week was largely spent doing some plumbing to get the unification working.
Thankfully it's pretty much as before, except that we need to keep track of
local definitions as well as everything else, just so that we have the whole
context if we encounter a unification problem that's stuck …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Last week was largely spent doing some plumbing to get the unification working.
Thankfully it's pretty much as before, except that we need to keep track of
local definitions as well as everything else, just so that we have the whole
context if we encounter a unification problem that's stuck awaiting further
information.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Aside&lt;/em&gt;: without going into all of the details, and being informal about it,
unification is the crucial part of type checking that works out whether it
is possible for two types/values to be the same, and, if so, how to make
them the same. &amp;quot;Make them the same&amp;quot; essentially means filling in any
metavariables (in practice in Idris that's implicit arguments) encountered on
the way. In a dependently typed language, you occasionally encounter a
unification problem that is not yet solvable, but might be once further
information turns up (like, the result of a another unification constraint)
which allows evaluation to continue. So this means unification can have one
of three results:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Yes, and here's the metavariable solutions I found&lt;/li&gt;
&lt;li&gt;Definitely no, and here's why not&lt;/li&gt;
&lt;li&gt;Maybe, ask me again later, and here's the unsolved constraints&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So that means we need to do a bit of bookkeeping so that when it's time to
retry, we have all the information we need.&lt;/p&gt;
&lt;p&gt;Anyway, that's all done now. Local definitions don't make it any more
complicated because we're working with already evaluated terms. It's just that
we might need to evaluate some more once more information arrives.&lt;/p&gt;
&lt;p&gt;So that means that I can finally move on to the interest bit, hooray! Today
I've started on the surface language. Keeping it small for now, only dealing
with lambda, pi, and function applications with implicit arguments so that I
can get the basic structure of the elaborator working. There's so much I want
to change about elaboration since Idris 2 that I've decided it's best just
to start from scratch. That's not quite as bad as it sounds because many of
the fiddly details like dealing with unbound implicits, pattern variables,
shuffling the context around and case tree compilation will be similar enough
that I can borrow things from Idris 2. But the overall structure had got
messy (to be honest, it began messy) so it deserves a bit of a rethink.&lt;/p&gt;
&lt;p&gt;Anyway. More ramblings on this to come.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 14 September 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-14-september-2026.html" rel="alternate"/><published>2026-09-14T17:00:00+00:00</published><updated>2026-09-14T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-09-14:/edwinb/notes-14-september-2026.html</id><summary type="html">&lt;p&gt;Hello again. It's been a few weeks, so I'll post this on a Monday this time...
I took some time off which turns out to have been a really good idea, then did
some other bits and pieces. But it's time to get cracking on the new Idris
properly now …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Hello again. It's been a few weeks, so I'll post this on a Monday this time...
I took some time off which turns out to have been a really good idea, then did
some other bits and pieces. But it's time to get cracking on the new Idris
properly now. Research leave has started! Hooray! This week, I've started
porting unification over to the new core. Some reminders of the main things
that are new, as much for myself as anyone else:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;The intention is to have a new architecture that is much more interactive.
So, rather than being based on loading entire files at a time, we're
processing a definition at a time and keeping track of what needs
rechecking at a much finer granularity.&lt;/li&gt;
&lt;li&gt;Case trees and local definitions (that is, things defined in &lt;cite&gt;where&lt;/cite&gt; blocks)
are part of the core now, not always lifted to the top level. This was a hack
that was convenient for Idris 2, but is limiting in quite a few ways. e.g.
for reasons I won't go into here it means that let bindings behave in ways
that might be surprising.&lt;/li&gt;
&lt;li&gt;We use a glued representation of values, which, in short, means that during
conversion checking and unification we have more control over when
subexpressions get evaluated (in short: only when they need to be).&lt;/li&gt;
&lt;li&gt;I'm rethinking surface language features that seemed a good idea at the
time. This means that (sorry everyone) I'm not taking backwards
compatibility into account.&lt;/li&gt;
&lt;li&gt;I'm remembering that I'm primarily an academic researching what is
possible and investigating how to program with expressive type systems,
and not here to make a product! So if that means I need to break something,
so be it.&lt;/li&gt;
&lt;li&gt;I'm still not using LLMs, whatever anyone says. Despite everything, I
still believe that almost all uses of LLMs can be achieved better
and cheaper by the surprisingly underrated technique of &amp;quot;thinking a little
bit&amp;quot;. If you disagree, good for you. If I'm wrong, they'll still be there
when I'm ready and if they're as good as you claim, I'll catch up in no
time.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Anyway. I've spent what feels like forever experimenting with different ways
of doing things. So it's definitely time to concentrate on making that thing,
especially now I have time to think for a little bit!&lt;/p&gt;
&lt;p&gt;Latest small observation (might have said this earlier but it's becoming
more important): having local definitions as part of the core means that we
need to keep track of them all over. Everywhere, we have an environment,
which consists of the local variables (let bound, pi bound, lambda bound).
Within that, there can be local function/data definitions, which I'm calling
&lt;cite&gt;LocalDefs&lt;/cite&gt;, that are indexed by the environment they're defined in. So
why not make them part of the environment, wouldn't that be simpler? Well,
no, sadly, because function and data definitions can be forward declared,
to allow mutual definitions, and we want to be able to do that locally too.
We still have a global context (called &lt;cite&gt;Defs&lt;/cite&gt; containing definitions in
an empty context). &lt;cite&gt;LocalDefs&lt;/cite&gt; are tracked during elaboration and evaluation
and can contain all the things &lt;cite&gt;Defs&lt;/cite&gt; might contain, but in a potentially
larger scope.&lt;/p&gt;
&lt;p&gt;Anyway. That's enough thinking out loud for now. Back to the lambda mines.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 31st July 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-31st-july-2026.html" rel="alternate"/><published>2026-07-31T17:00:00+00:00</published><updated>2026-07-31T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-07-31:/edwinb/notes-31st-july-2026.html</id><summary type="html">&lt;p&gt;This week, work on conversion checking in LTT3 and progress towards
unification. Conversion checking turns out to be pretty much the same as
before, Unification is a bit trickier because unification problems can be
suspended pending further information (like, other metavariables being
unsolved means that we may need to postpone …&lt;/p&gt;</summary><content type="html">&lt;p&gt;This week, work on conversion checking in LTT3 and progress towards
unification. Conversion checking turns out to be pretty much the same as
before, Unification is a bit trickier because unification problems can be
suspended pending further information (like, other metavariables being
unsolved means that we may need to postpone a unification problem until
another one is solved) so we need to remember which local definitions were
in scope. It's not that it's tricky, it just needs a bit more infrastructure
(which we'll probably use everywhere). Also, to do these things, I've
needed some more details in the context, which fortunately are no different
than previous attempts at implementing the core, so can be reused.&lt;/p&gt;
&lt;p&gt;Some other thoughts from the week...  One thing Idris 2 takes a while to do
(okay, not that long, but it's noticeable and annoying) is loading in libraries
that have been compiled to a binary representation of the type checked terms,
and all the dependencies. That is, keeping track of the database of definitions
and the relationships between them. And another thing it does too much of is
rechecking things that don't actually need rechecking.&lt;/p&gt;
&lt;p&gt;The goal here is to have a really interactive system and part of this means
not reloading everything every time a definition changes. So we need to be
a bit cleverer than tracking dependencies between source files, and there
are things we can learn from Query Based compiler architectures.&lt;/p&gt;
&lt;p&gt;Hang on, did I say database? And relations? Hmm... anyway, there's some
perfectly good tools that can already do what I think we need to do, so once
the basic type system is working, my plan is to see if it makes sense to
represent type checked collections of definitions in an SQLite database,
so that we can quickly find out what needs rechecking any time a single
definition changes and save doing unnecessary rechecking of things which
aren't affected. Plenty of details to work out here, of course, but that's
the basic idea.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 24th July 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-24th-july-2026.html" rel="alternate"/><published>2026-07-24T17:00:00+00:00</published><updated>2026-07-24T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-07-24:/edwinb/notes-24th-july-2026.html</id><summary type="html">&lt;p&gt;Been a few weeks, sorry! Partly been on the road, partly taking a bit of a
break because it's been just too hot. So, just briefly: progress on
the new core (currently going by the name LTT3 and I can't even remember why
but I'll stick with that as a …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Been a few weeks, sorry! Partly been on the road, partly taking a bit of a
break because it's been just too hot. So, just briefly: progress on
the new core (currently going by the name LTT3 and I can't even remember why
but I'll stick with that as a shorthand until it turns into an actual
programming language) continues, working through the basic bits of machinery
needed for evaluation, conversion checking and unification. So far
nothing too surprising has come up in having to deal with local definitions,
so let's hope it continues that way.  Also, I got distracted for a few days by
thinking about compiling to C via continuations, and looking how Scheme in
particular does it.  There's some &lt;a class="reference external" href="https://www.wingolog.org/archives/2026/02/09/six-thoughts-on-generating-c"&gt;interesting thoughts from Andy Wingo&lt;/a&gt;
on this topic. We used to compile Idris to C, via defunctionalisation and ANF,
and the performance was pretty decent though not as good as the current Chez
Scheme based back end. But even so there are possible advantages to going via
C, if only for portability and reducing the number of dependencies so maybe
I'll revisit properly one day. But not just yet.&lt;/p&gt;
&lt;p&gt;I went to the &lt;a class="reference external" href="https://2026.ecoop.org/home/unsound-2026"&gt;UNSOUND workshop&lt;/a&gt; at
ECOOP and talked about sources of unsoundness in Idris. I might have talked
myself into revisiting universes, which I never got around to implementing in
Idris 2. But, again, not just yet, because I really need to get that core
working... Anyway, there were plenty of interesting talks at UNSOUND and I
particularly enjoyed hearing from Gina Banyard about &lt;a class="reference external" href="https://2026.ecoop.org/details/unsound-2026-papers/5/PHP-Type-System-in-Distress"&gt;PHP's type system&lt;/a&gt;
(no, honestly, it's not an oxymoron...).&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 26th June 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-26th-june-2026.html" rel="alternate"/><published>2026-06-26T17:00:00+00:00</published><updated>2026-06-26T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-06-26:/edwinb/notes-26th-june-2026.html</id><summary type="html">&lt;p&gt;I don't really know why I'm doing this, but I'm trying emacs again. I
used to use it all the time, but in 2012 I switched to vim for a month
and I'm still there. The lure of the Idris mode in emacs is perhaps
too much, and experience so …&lt;/p&gt;</summary><content type="html">&lt;p&gt;I don't really know why I'm doing this, but I'm trying emacs again. I
used to use it all the time, but in 2012 I switched to vim for a month
and I'm still there. The lure of the Idris mode in emacs is perhaps
too much, and experience so far is that it is nice to not have to
leave the editor window much. It did only take me four days to crack and
switch on evil mode though (the vim emulation) because there's some things
that my fingers just know how to do much quicker that way.&lt;/p&gt;
&lt;p&gt;Anyway. Other things. I have discovered, as far as the new core
implementation goes, that I'm making it unnecessarily complicated by
putting local definitions in the context and indexing the context by
the scope. Sure, it can work that way, but there's a lot of gadgets
needed to, for example, weaken local definitions, and to make sure
we're only doing so when we need to. Instead, we hang on to them where
needed, which is during elaboration and evaluation. For evaluation, we
hold the values that the local definitions evaluate to. For type
checking/elaboration, we hold on to their types. It means passing
another thing around when we need it, but it also saves building a
load of gadgets for manipulating the global context every time we go
in to a new scope, which is often (every time we go under a binder, or
a case branch). After all that, I have the evaluator done using a &amp;quot;glued&amp;quot;
approach, meaning we have lots of control over what actually needs to get
evaluation - which includes local definitions, that we can avoid looking at if
they're not needed. Not that it's well tested yet, especially not the
evaluation of local definitions! It's going to be tricky to do that
properly until I have at least a good chunk of a core type checker
written (fortunately, we don't need a full elaborator for that) since
I don't particularly want to be constructing programs by hand that are
big enough to be interesting.&lt;/p&gt;
&lt;p&gt;Also doing some reviewing for &lt;a class="reference external" href="https://tydeworkshop.org/2026"&gt;TyDe 2026&lt;/a&gt;,
and next week I'll be giving a talk on Unsoundness in Idris at
the &lt;a class="reference external" href="https://2026.ecoop.org/home/unsound-2026"&gt;Unsound workshop&lt;/a&gt;
which is part of &lt;a class="reference external" href="https://2026.ecoop.org/"&gt;ECOOP&lt;/a&gt;. I've been thinking a bit
about what I actually want to say. Hopefully I'll have worked out
more precisely what that is by next Tuesday...&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 19th June 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-19th-june-2026.html" rel="alternate"/><published>2026-06-19T17:00:00+00:00</published><updated>2026-06-19T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-06-19:/edwinb/notes-19th-june-2026.html</id><summary type="html">&lt;p&gt;Not much to say this week really, just that I went to a workshop in Bonn on
Mathematics, Formalisation and AI while continuing to fiddle about with the
new Idris core and evaluator.&lt;/p&gt;
&lt;p&gt;The workshop was interesting in lots of ways, with lots of exciting
possibilities discussed. Though it was …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Not much to say this week really, just that I went to a workshop in Bonn on
Mathematics, Formalisation and AI while continuing to fiddle about with the
new Idris core and evaluator.&lt;/p&gt;
&lt;p&gt;The workshop was interesting in lots of ways, with lots of exciting
possibilities discussed. Though it was also (to be kind) a bit
annoying in others, mostly because I didn't feel there was enough
acknowledgement of the ethical and environmental implications or the
impact on education, or, really, much about the negatives at
all. Still, I learned how Mathematicians are using LLMs to translate
proofs into Lean, and it's really cool that it works at all and I'm
sure we can learn a lot from them. I gave a talk about Idris (just a
general introduction) and talked about places where I felt LLMs might
have some value, and how I believe that a well defined checkable core
is essential if we're going to trust these things, while also
expressing some scepticism and general concern about the ethical and
environmental implications.&lt;/p&gt;
&lt;p&gt;I have lots to say on this topic, but I think I'll save it for when I see
people in 3D and not for a ramble on the internet. Except to say that several
participants seemed to believe that LLMs are exciting because they're going to
keep getting better for ever. I'll just leave that there.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 12th June 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-12th-june-2026.html" rel="alternate"/><published>2026-06-12T17:00:00+00:00</published><updated>2026-06-12T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-06-12:/edwinb/notes-12th-june-2026.html</id><summary type="html">&lt;p&gt;More work on the evaluator in the new core.  It's interesting how much needs to
be done to support local definitions, and thinking about laziness, of weakening
and evaluation of let blocks. Why? Firstly: the context, which we pass around
everywhere, now contains definitions in the local scope, and not …&lt;/p&gt;</summary><content type="html">&lt;p&gt;More work on the evaluator in the new core.  It's interesting how much needs to
be done to support local definitions, and thinking about laziness, of weakening
and evaluation of let blocks. Why? Firstly: the context, which we pass around
everywhere, now contains definitions in the local scope, and not just the
global definitions. So, that means in the implementation, it needs to be
indexed by the local scope. Not really a big deal, but we do need to make sure
that the usual weakening and embedding gadgets are not overly expensive.  When
we go under another binder, we will need to weaken the locally defined names
too. But we may never look at them... so we need to take care to do this
lazily. Also, say you're evaluating &lt;cite&gt;let x = y in z&lt;/cite&gt; which results in &lt;cite&gt;let x =
y' in z'&lt;/cite&gt;, it's likely (but not impossible) that we'll never have to look at
&lt;cite&gt;y'&lt;/cite&gt;. We might if, say, we want to print the full result, or continue
evaluation (say if &lt;cite&gt;z&lt;/cite&gt; was blocked, awaiting a metavariable solution). But,
usually, if we're using the result of evaluation for conversion or unification,
we're not going to need it.&lt;/p&gt;
&lt;p&gt;Anyway, so that means I've been doing all that plumbing, as well as catching
up with the Scottish PL folks at SPLS.&lt;/p&gt;
&lt;p&gt;Also: I'm going to a workshop next week, where I'll be giving a talk on
reasoning in Idris to some mathematicians who are thinking about AI and proof.
I have opinions in that, but it's not for this space :). So I've been spending
a bit of time thinking about that, and how to present what Idris even is.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 5th June 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-5th-june-2026.html" rel="alternate"/><published>2026-06-05T17:00:00+00:00</published><updated>2026-06-05T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-06-05:/edwinb/notes-5th-june-2026.html</id><summary type="html">&lt;p&gt;A two week update this time, simply because last week and this things happened
that meant I didn't make much progress on the things I want to write about
here.  Not bad things, just... Things :). PhD progress reviews, family visits,
curriculum planning, that sort of thing. And it's still maybe …&lt;/p&gt;</summary><content type="html">&lt;p&gt;A two week update this time, simply because last week and this things happened
that meant I didn't make much progress on the things I want to write about
here.  Not bad things, just... Things :). PhD progress reviews, family visits,
curriculum planning, that sort of thing. And it's still maybe a couple of weeks
until I can really start focussing on the next Idris, but here we are.&lt;/p&gt;
&lt;p&gt;Anyway. Progress continues on the latest experimental thing, which (I can't
remember if I said anything about this...) is starting from scratch but
intending to pull in all the good bits once I get going, and having case
trees and local definitions as first class in the Idris core. I have some
of the basic machinery up and running, so this week it's the evaluator.
Having to deal with local definitions makes this a bit more complicated
than before, but my plan is to deal with more of the complexity at this
level, to reduce the number of horribly hacky ways we need in Idris 2 to deal
with not having local definitions in the core and having to lift everything
out, and manage scope appropriately in the elaborator. So, progress proceeds
on that, as it does.&lt;/p&gt;
&lt;p&gt;Also I got distracted by thinking a bit about other compilers and lightweight
runtime systems, as I do occasionally. A couple of interesting things I looked
at, without going into any more depth or commenting further here, are &lt;a class="reference external" href="https://github.com/augustss/MicroHs"&gt;Lennart Augustsson's MicroHS&lt;/a&gt; which is based on combinators,
and &lt;a class="reference external" href="https://c9x.me/compile/"&gt;QBE&lt;/a&gt; which is a compiler backend that aims
to be much more lightweight than, say, LLVM.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 22nd May 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-22nd-may-2026.html" rel="alternate"/><published>2026-05-22T17:00:00+00:00</published><updated>2026-05-22T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-05-22:/edwinb/notes-22nd-may-2026.html</id><summary type="html">&lt;p&gt;It's the Idris developer meeting this week! So a chance to get my
Research Brain working again after not having had significant stretches of
time to think for a while. Although, Monday is spent travelling back from
a Thing in Germany, popping between coffee shops and airports and looking
over …&lt;/p&gt;</summary><content type="html">&lt;p&gt;It's the Idris developer meeting this week! So a chance to get my
Research Brain working again after not having had significant stretches of
time to think for a while. Although, Monday is spent travelling back from
a Thing in Germany, popping between coffee shops and airports and looking
over the new Idris code base trying to remember how things work, and how I'm
planning to make things work.&lt;/p&gt;
&lt;p&gt;And in the end, I deleted lots of things, and started yet another experimental
new thing. And it's basically because of this: &lt;cite&gt;case&lt;/cite&gt; blocks in Idris 2
involve a variety of hacks upon hacks, which is deeply unsatisfying,
especially when it comes to local definitions (that is, &lt;cite&gt;where&lt;/cite&gt; blocks).
I won't go into the reasons why here (except to say that, as always, it's
about names and scopes) but my conclusion was that, instead of tweaking
detail after detail in the new version, it would be more productive and
more fun to rip it all up and start again. So, now I have yet another attempt
at implementing the core type theory, this time with case blocks and local
definitions as first class features. I have so many changes I want to make
to the way things are done - in unification, in elaboration - that this gives
me an easier opportunity to actually make those changes throughout.&lt;/p&gt;
&lt;p&gt;Now, in the end, this doesn't necessarily mean ripping &lt;em&gt;everything&lt;/em&gt; up and
throwing all the existing work away. Experience suggests that once the
core language works as intended, bringing the high level language back
doesn't take very long, and a lot of the existing work is reusable.&lt;/p&gt;
&lt;p&gt;Optimising for fun, as always :).&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Notes: 15th May 2026</title><link href="https://www.type-driven.org.uk/edwinb/notes-15th-may-2026.html" rel="alternate"/><published>2026-05-15T17:00:00+00:00</published><updated>2026-05-15T17:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2026-05-15:/edwinb/notes-15th-may-2026.html</id><summary type="html">&lt;p&gt;I've recently stumbled on the concept of &lt;a class="reference external" href="https://promo.cymru/resource-articles/weeknotes/"&gt;Weeknotes&lt;/a&gt; so I thought I'd give it a
go.  It's mostly to remind myself of progress when I wonder what it is I've
even done at some point in the future, but also so that maybe
people can see what progress is being …&lt;/p&gt;</summary><content type="html">&lt;p&gt;I've recently stumbled on the concept of &lt;a class="reference external" href="https://promo.cymru/resource-articles/weeknotes/"&gt;Weeknotes&lt;/a&gt; so I thought I'd give it a
go.  It's mostly to remind myself of progress when I wonder what it is I've
even done at some point in the future, but also so that maybe
people can see what progress is being made with the new Idris, and to
make sure that week to week some progress actually does happen! Rather than
the, up to now, constant changing of mind about how to do things. Maybe I'll
write about where I'm stuck or where I can't make my mind up about something.
They are not intended to be well written, well organised, or even particularly
interesting. Let's see how long it lasts. I give it a fortnight but maybe if
they're public I'll shame myself into doing it for longer.&lt;/p&gt;
&lt;p&gt;Because apparently this needs saying these days: No I'm not getting an LLM to
do this. Or anything. For goodness sake. Learn to speak for yourself again.
Use your own voice, not the bland average voice of every user that went
before you. Use em dashes if you want.&lt;/p&gt;
&lt;p&gt;Some context, since this is where it starts. I've been rethinking Idris,
because I've been working on it and with it for a while and over the last
couple of years, Idris 2 has been in the more than capable hands of a team
of volunteers without all that much input from me. I consider this a win :).
While they've been doing that, I've been rethinking both the design and
implementation, because what I really want from Idris (or indeed any language)
is something that is interactive, responsive, where programming is a
conversation between developer and machine, in a language that both parties
understand and which is formal and clearly and unambiguously defined. (At
this point, I ought so say something about LLMs. So that's got that out of the
way).&lt;/p&gt;
&lt;p&gt;Anyway: I'm working on this new thing (private repo for now). Much as I still
like Idris, some design mistakes are obvious in hindsight (honestly some were
obvious at the time but I made them anyway). There's too much implicit
cleverness, in things like resolving ambiguities, implicit laziness and maybe
some bits of syntactic sugar. In the implementation, I don't like that the
granularity of the system is per-file, meaning that a small change can lead to
a lot of rebuilding. The evaluator often does too much work anyway - I have a
new system based on a glued representation of values, which essentially means
that you can always see where an evaluated term came from (what did the user
write) and have full control over when you evaluate subexpressions which,
based on early experiments, seems to have lots of benefits not only in
performance but in making error messages clearer since they actually show
what the programmer wrote, and not what it might have reduced to at some
stage.&lt;/p&gt;
&lt;p&gt;So, the new thing is, for the moment, my own little (or not so little) project
in which I not only try to address the above, but try to remember that my
job is to do research rather than (or maybe as well as) create a product.
The current state is an elaborator for the intermediate language, with a new
evaluator but quite a bit borrowed from an earlier state of Idris 2. It is,
alas, well out of sync with Idris 2 by now, but I'll worry about that later
(remember: researcher, not making a product!). Next step is to work through
the bits of the elaborator that I find unsatisfying, and this week I've
been looking at how the &lt;cite&gt;case&lt;/cite&gt; construct works. Or, maybe more accurately,
doing it again, so that we have a dependent pattern matching construct that can
appear anywhere in a term, and support the &lt;cite&gt;with&lt;/cite&gt; construct. If this works the
way I think it should, it will simplify the elaborator and make it much easier
to maintain, which is a large part of the point of this increasingly large
exercise. For reasons of grading and travelling I haven't done much more than
adding some syntax to the (experimental) implementation and scribbled some
notes on paper, but that's a start at least.&lt;/p&gt;
&lt;p&gt;Other stuff: nearly the end of semester. Marking of group projects is done!
Still have some final year project dissertations to read. Always enjoyable to
see what the students have achieved throughout the year.&lt;/p&gt;
</content><category term="Posts"/><category term="weeknotes"/></entry><entry><title>Why is Idris 2 so much faster than Idris 1?</title><link href="https://www.type-driven.org.uk/edwinb/why-is-idris-2-so-much-faster-than-idris-1.html" rel="alternate"/><published>2020-05-25T20:00:00+00:00</published><updated>2020-05-25T20:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2020-05-25:/edwinb/why-is-idris-2-so-much-faster-than-idris-1.html</id><summary type="html">&lt;p&gt;I've just released a new version of &lt;a class="reference external" href="https://www.idris-lang.org/idris-2-version-020-released.html"&gt;Idris 2&lt;/a&gt;, which is a
significant release in that it's the first one which is able to compile itself.
Thus, it's the first one where we can really see how the performance of Idris 2
improves on the performance of Idris 1.&lt;/p&gt;
&lt;p&gt;A …&lt;/p&gt;</summary><content type="html">&lt;p&gt;I've just released a new version of &lt;a class="reference external" href="https://www.idris-lang.org/idris-2-version-020-released.html"&gt;Idris 2&lt;/a&gt;, which is a
significant release in that it's the first one which is able to compile itself.
Thus, it's the first one where we can really see how the performance of Idris 2
improves on the performance of Idris 1.&lt;/p&gt;
&lt;p&gt;A question people sometimes ask when discussing Idris on various parts of
the Internet is &amp;quot;Why is Idris 2 so much faster than Idris 1?&amp;quot; Sometimes
it involves the phrase &amp;quot;the author claims that...&amp;quot; so here are some
numbers, as measured today on my Dell XPS 13:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Idris 1 building Idris 2 (bootstrap version): 743s&lt;/li&gt;
&lt;li&gt;Idris 2 building Idris 2 (with the Chez Scheme runtime): 76s&lt;ul&gt;
&lt;li&gt;Update (2020-05-27): It turns out there was an error in processing
imports that made it take too long when importing modules. Combined
with some other small improvements, the new time is: 48.5s&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is for type checking and generating/optimising intermediate code only, and
doesn't include generating and compiling the C/Chez Scheme output.
The bootstrap version is a slightly earlier version, and a little bit
smaller, but they are similar enough to be able to make a useful comparison,
and certainly give you a feel of how much of an improvement Idris 2 is. That's
nearly an order of magnitude!&lt;/p&gt;
&lt;p&gt;I wouldn't necessarily say the result is
&lt;em&gt;fast&lt;/em&gt; in an absolute sense &lt;a class="footnote-reference" href="#footnote-1" id="footnote-reference-1"&gt;[1]&lt;/a&gt; and there are no doubt many improvements
still to be found, but it is still interesting to try to understand why
the difference. Idris 1 is implemented in Haskell, but that has
little (if anything) to do with the difference. Rather, it's a combination
of:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;my improved understanding of what it takes to implement a dependently
typed language (or, indeed, any language)&lt;/li&gt;
&lt;li&gt;insights gained from other people's experiments, most notably
Andras Kovacs' work on &lt;a class="reference external" href="https://github.com/AndrasKovacs/smalltt"&gt;smalltt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Lots of profiling, via &lt;a class="reference external" href="http://sourceware.org/binutils/docs/gprof/index.html"&gt;gprof&lt;/a&gt; while bootstrapping,
and the &lt;a class="reference external" href="https://cisco.github.io/ChezScheme/csug9.5/system.html#./system:h7"&gt;Chez Scheme profiling tools&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A lot of the improvements are down to avoiding make bad decisions in the
first place! This is easy to say in hindsight, but Idris 1 was originally a
vehicle for experimenting with some ideas in dependently typed language
design that were hard to do just by modifying other systems and so I was
more interested in having a system sooner, rather than having a better
system. Unfortunately, some of those early decisions proved hard to undo
as the system grew and as it gained users.&lt;/p&gt;
&lt;p&gt;The rest of this post summarises some of the most significant internal
differences.&lt;/p&gt;
&lt;div class="section" id="the-global-context"&gt;
&lt;h2&gt;The global context&lt;/h2&gt;
&lt;p&gt;The global context stores all of the information that might be needed
for typechecking: function and data types and definitions, among other
things. We need function definitions as well as types, even for imported
modules, since we may need to do evaluation at the type level during
type checking.&lt;/p&gt;
&lt;p&gt;The big difference between Idris 1 and Idris 2 is that Idris 2 represents
the context as an array, with constant time access and update. Yes, it's
mutable! We recognise that there's going to be a lot of updates: mainly
solving unification problems as type checking proceeds, but also during
interface resolution. And, we only ever have one context (ignoring
ambiguity resolution, but I won't go into that here). So, having a single
mutable context with constant time access has been valuable. Yes, it's
a bit ugly to a pure functional programmer, but I'm not going to let that
get in the way of performance! At least the types are precise about where
updates might happen.&lt;/p&gt;
&lt;p&gt;This turns out to have another benefit, which is that on loading an
imported module, we don't need to add definitions to the context until
they are actually referenced. Most names from imports are never referenced
- at least not all in the same file - so Idris 2 lazily decodes definitions
and mutates the context when they are needed. In fact profiling Idris 1
suggests that it spends a huge amount of time unnecessarily loading
definitions.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="unification"&gt;
&lt;h2&gt;Unification&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Unification_(computer_science)"&gt;Unification&lt;/a&gt;
is an important part of type checking a dependently typed language, to
resolve implicit arguments. For example, if you have a function&lt;/p&gt;
&lt;pre class="literal-block"&gt;
append : Vect n a -&amp;gt; Vect m a -&amp;gt; Vect (n + m) a
&lt;/pre&gt;
&lt;p&gt;and an application&lt;/p&gt;
&lt;pre class="literal-block"&gt;
append [1,2,3] [4,5,6,7]
&lt;/pre&gt;
&lt;p&gt;...then the typechecker needs to infer that &lt;tt class="docutils literal"&gt;n = 3&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;m = 4&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;a = Integer&lt;/tt&gt;.  Idris 2 implements
&lt;a class="reference external" href="http://adam.gundry.co.uk/pub/pattern-unify/"&gt;dynamic pattern unification&lt;/a&gt;.
In Idris 1, metavariables (that is, the implicits that need to be solved)
are stored locally to a term, so solving them requires searching through
the term and substituting the definition, which can be expensive. Idris 2,
on the other hand, stores metavariables globally, in the mutable global
context, following roughly the scheme
described in &lt;a class="reference external" href="http://www.cse.chalmers.se/~ulfn/papers/thesis.html"&gt;Ulf Norell's thesis&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Furthermore, we don't substitute the solution of a metavariable in a term
unless we &lt;em&gt;really really need to&lt;/em&gt; (in practice, that's only while checking
linear binders are used appropriately, so fairly rare). This preserves
sharing as far as possible, which can be important in terms with
dependent types. For example (with apologies for using a vector again):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
[1,2,3]
&lt;/pre&gt;
&lt;p&gt;elaborates into&lt;/p&gt;
&lt;pre class="literal-block"&gt;
((::) {a=Integer} {n=S (S Z))} 1
  ((::) {a = Integer} {n=S Z} 2
    ((::3) {a = Integer} {n=Z} (Nil {a = Integer})))
&lt;/pre&gt;
&lt;p&gt;That &lt;tt class="docutils literal"&gt;Z&lt;/tt&gt; appears three times, the subexpression &lt;tt class="docutils literal"&gt;S Z&lt;/tt&gt; appears twice, and
this gets worse as the vector grows. So sharing is important! This is the main
insight gained from Andras Kovacs' work on &lt;a class="reference external" href="https://github.com/AndrasKovacs/smalltt"&gt;smalltt&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="compile-time-evaluation"&gt;
&lt;h2&gt;Compile-time evaluation&lt;/h2&gt;
&lt;p&gt;Type checking a dependently typed language involves evaluating expressions,
possibly in the presence of unknown variables, to check whether two terms
are equal or to infer values for metavariables.
For example, we might need to solve for &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;?meta&lt;/span&gt;&lt;/tt&gt; in the following:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
plus (S (S x)) (S (S y)) = S ?meta
&lt;/pre&gt;
&lt;p&gt;Idris 1 does this by evaluating the terms, leading to the equation&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S (S (plus x (S (S y)))
&lt;/pre&gt;
&lt;p&gt;...then deciding &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;?meta&lt;/span&gt; = S (plus (x (S (S &lt;span class="pre"&gt;y))))&lt;/span&gt;&lt;/tt&gt;. This is fine for small
terms, but what if the expression is&lt;/p&gt;
&lt;pre class="literal-block"&gt;
plus 100 100 = S ?meta
&lt;/pre&gt;
&lt;p&gt;...or something even larger? We don't need to evaluate fully to normal form to
establish that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;?meta&lt;/span&gt; = plus 99 100&lt;/tt&gt;. Idris 2 therefore reduces to
&lt;a class="reference external" href="http://foldoc.org/Head+Normal+Form"&gt;head normal form&lt;/a&gt; during type checking,
which it probably should have done all along!
&lt;a class="reference external" href="https://help.luddy.indiana.edu/techreports/TRNNN.cgi?trnum=TR44"&gt;Cons should not evaluate its arguments&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Normally, you won't notice this. But, sometimes, in Idris 1, compile time
evaluation really hurts performance, and a dependently typed language needs
to recognise that compile time evaluation is going to happen a lot!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="chez-scheme-runtime"&gt;
&lt;h2&gt;Chez Scheme runtime&lt;/h2&gt;
&lt;p&gt;Idris 2 benefits from a robust, well-engineered and optimised run time
system, by compiling to Chez Scheme. One might expect that Chez Scheme would
be slower than the C code generated by Idris 1, since it is dynamically
typed, but this ignores two important points:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;It is possible to turn off the dynamic type checks in Chez Scheme.
So, the run time never checks if a constructor argument is within bounds of
the vector where we store it, and never checks whether a primitive is the
right type.&lt;/li&gt;
&lt;li&gt;Even more importantly, there has never been significant effort put into the
Idris 1 run time. Generating good C code corresponding to a functional
program which might have lots of small (and higher order) definitions is
extremely challenging, so it is better to take advantage of someone
else's work here.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="abandoning-tactic-based-elaboration"&gt;
&lt;h2&gt;Abandoning tactic-based elaboration&lt;/h2&gt;
&lt;p&gt;Idris 1 used a tactic-based elaborator, much as you'd see in
&lt;a class="reference external" href="https://coq.inria.fr/refman/proof-engine/tactics.html"&gt;Coq proofs&lt;/a&gt;. This
made implementing new high level features relatively easy, using an
elaboration monad which was, in a sense, an embedded domain specific
language for writing new tactics. This is usable directly by
&lt;a class="reference external" href="http://docs.idris-lang.org/en/latest/elaboratorReflection/elaborator-reflection.html"&gt;elaborator reflection&lt;/a&gt;
meaning that users could try extending Idris in Idris itself. While this
is a fun way to work on the language, it took a lot of effort to get it
to perform even as well as it did (which, as you see above, is not that
well). Idris 2 takes a much simpler approach, elaborating syntax directly
into the core representation.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="non-reasons"&gt;
&lt;h2&gt;Non-reasons&lt;/h2&gt;
&lt;p&gt;One performance problem in Idris 1 was due to space leaks in the Haskell
caused by using lazy data structures where they weren't needed.
Despite our best efforts, we were never able to resolve these fully.
No doubt it would have been better to consider where strict data structures
would have been better. But, we didn't. This isn't to say that Idris has
proved a better implementation choice because it's strict, just to say that
we needed to put more thought, earlier, into where we needed strict data
in Haskell.&lt;/p&gt;
&lt;table class="docutils footnote" frame="void" id="footnote-1" rules="none"&gt;
&lt;colgroup&gt;&lt;col class="label" /&gt;&lt;col /&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td class="label"&gt;&lt;a class="fn-backref" href="#footnote-reference-1"&gt;[1]&lt;/a&gt;&lt;/td&gt;&lt;td&gt;It's about 45,000 lines of Idris 2 code. No doubt a Go programmer, or
even a Pascal programmer in the 1970s, would scoff at the idea that a mere
45,000 lines would take 76 seconds to type check. But, those 45,000 lines are
pulling a lot of weight, and the type checker is doing a lot of work!  For
example, the fact that it typechecks means that all of the functions cover all
possible inputs, and all expressions throughout all phases are guaranteed
to be scope correct, as is unification. This is a major source of bugs in a
type checker and compiler.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
</content><category term="Posts"/><category term="idris"/></entry><entry><title>Idris 2: Quantitative Type Theory in Action</title><link href="https://www.type-driven.org.uk/edwinb/idris-2-quantitative-type-theory-in-action.html" rel="alternate"/><published>2020-02-27T00:00:00+00:00</published><updated>2020-02-27T00:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2020-02-27:/edwinb/idris-2-quantitative-type-theory-in-action.html</id><summary type="html">&lt;p class="first last"&gt;A paper on Idris 2 and QTT&lt;/p&gt;
</summary><content type="html">&lt;p&gt;A new &lt;a class="reference external" href="https://www.type-driven.org.uk/edwinb/papers/idris2.pdf"&gt;draft&lt;/a&gt; is available, describing the design of Idris 2 and some of
the things you can do in a language based on Quantitative Type Theory
(beyond the brief introduction I give &lt;a class="reference external" href="https://www.type-driven.org.uk/edwinb/linearity-and-erasure-in-idris-2.html"&gt;here&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://www.type-driven.org.uk/edwinb/papers/idris2.pdf"&gt;Idris 2: Quantitative Type Theory in Action&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;em&gt;Abstract:&lt;/em&gt; Dependent types allow us to express precisely &lt;em&gt;what&lt;/em&gt; a function is intended
to do.  Recent work on Quantitative Type Theory (QTT) extends dependent
type systems with &lt;em&gt;linearity&lt;/em&gt;, also allowing precision in expressing &lt;em&gt;when&lt;/em&gt;
a function can run.  This is promising, because it suggests the ability to
design and reason about resource usage protocols, such as we might find in
distributed and concurrent programming, where the state of a communication
channel changes throughout program execution.  As yet, however, there has
not been a full-scale programming language with which to experiment with
these ideas.  Idris 2 is a new version of the dependently typed language
Idris, with a new core language based on QTT, supporting linear and
dependent types.  In this paper, we introduce Idris 2, and describe how QTT
has influenced its design. We give several examples of the benefits of QTT
in practice including: clearly expressing which data is erased at run time,
at the type level; improving interactive program development by reducing
the search space for type-driven program synthesis; and, resource tracking
in the type system leading to type-safe concurrent programming with session
types.&lt;/blockquote&gt;
</content><category term="Posts"/><category term="idris"/><category term="paper"/></entry><entry><title>Linearity and Erasure in Idris 2</title><link href="https://www.type-driven.org.uk/edwinb/linearity-and-erasure-in-idris-2.html" rel="alternate"/><published>2020-01-10T11:00:00+00:00</published><updated>2020-01-10T11:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2020-01-10:/edwinb/linearity-and-erasure-in-idris-2.html</id><summary type="html">&lt;p class="first last"&gt;A discussion on erasure and linearity in Idris 2&lt;/p&gt;
</summary><content type="html">&lt;p&gt;I'm now actively working on &lt;a class="reference external" href="https://www.github.com/edwinb/Idris2"&gt;a new implementation of Idris&lt;/a&gt;, Idris 2, and although there's still a
long way to go, it's making very good progress.
The &lt;a class="reference external" href="https://idris2.readthedocs.org"&gt;documentation&lt;/a&gt;, as you
see, is still essentially a placeholder, and there's no release yet.
Nevertheless, now seems as good a time as any to start writing about some of
the changes since Idris 1, some of the new features, and some of the
improvements.  Maybe I'll copy and paste it into the official documentation
later :).&lt;/p&gt;
&lt;p&gt;The biggest difference (internally) between Idris 1 and Idris 2 is that Idris 2 is
based on &lt;a class="reference external" href="https://bentnib.org/quantitative-type-theory.html"&gt;Quantitative Type Theory (QTT)&lt;/a&gt;, a core language
developed by Bob Atkey and Conor McBride. In practice, this means that every
variable in Idris 2 has a &lt;em&gt;quantity&lt;/em&gt; associated with it. A quantity is one of:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;0&lt;/tt&gt;, meaning that the variable is &lt;em&gt;erased&lt;/em&gt; at run time&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;1&lt;/tt&gt;, meaning that the variable is used &lt;em&gt;exactly once&lt;/em&gt; at run time&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Unrestricted&lt;/em&gt;, which is the same behaviour as Idris 1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Multiplicities are written in function types as follows:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;ignoreN : (0 n : Nat) &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; Vect n a &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; Nat&lt;/tt&gt; - this function cannot look at
&lt;tt class="docutils literal"&gt;n&lt;/tt&gt; at run time&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;duplicate : (1 x : a) &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; (a, a)&lt;/tt&gt; - this function must use &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; exactly
once (so good luck implementing it, by the way. There is no implementation
because it would need to use &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; twice!)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If there is no multiplicity annotation, the argument is unrestricted.
If, on the other hand, a name is implicitly bound (like &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; in both examples above)
the argument is erased. So, the above types could also be written as:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;ignoreN : {0 a : _} &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; (0 n : Nat) &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; Vect n a &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; Nat&lt;/tt&gt;&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;duplicate : {0 a : _} &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; (1 x : a) &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; (a, a)&lt;/tt&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the rest of this post, I'll describe what this means for your Idris
programs, with several examples. In particular, I'll describe:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference internal" href="#erasure"&gt;Erasure&lt;/a&gt; - how to know what is relevant at run time and what is erased&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#linearity"&gt;Linearity&lt;/a&gt; - using the type system to encode &lt;em&gt;resource usage protocols&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference internal" href="#pattern-matching-on-types"&gt;Pattern matching on types&lt;/a&gt; - truly first class types&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The most important of these for most programs, and the thing you'll need to
know about if converting Idris 1 programs to work with Idris 2, is &lt;a class="reference internal" href="#erasure"&gt;erasure&lt;/a&gt;.
The most interesting, however, and the thing which gives Idris 2 much more
expressivity, is &lt;a class="reference internal" href="#linearity"&gt;linearity&lt;/a&gt;, so we'll start there.&lt;/p&gt;
&lt;div class="section" id="linearity"&gt;
&lt;h2&gt;Linearity&lt;/h2&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; multiplicity expresses that a variable must be used exactly once.
First, we'll see how this works on some small examples of functions and
data types, then see how it can be used to encode &lt;a class="reference internal" href="#resource-protocols"&gt;resource protocols&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Above, we saw the type of &lt;tt class="docutils literal"&gt;duplicate&lt;/tt&gt;. Let's try to write it interactively,
and see what goes wrong. We'll start by giving the type and a skeleton
definition with a hole:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
duplicate : (1 x : a) -&amp;gt; (a, a)
duplicate x = ?help
&lt;/pre&gt;
&lt;p&gt;Checking the type of a hole tells us the multiplicity of each variable in
scope. If we check the type of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;?help&lt;/span&gt;&lt;/tt&gt; we'll see that we can't
use &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; at run time, and we have to use &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; exactly once:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t help
 0 a : Type
 1 x : a
-------------------------------------
help : (a, a)
&lt;/pre&gt;
&lt;p&gt;If we use &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; for one part of the pair...:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
duplicate : (1 x : a) -&amp;gt; (a, a)
duplicate x = (x, ?help)
&lt;/pre&gt;
&lt;p&gt;...then the type of the remaining hole tells us we can't use it for the other:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t help
 0 a : Type
 0 x : a
-------------------------------------
help : a
&lt;/pre&gt;
&lt;p&gt;The same happens if we try defining &lt;tt class="docutils literal"&gt;duplicate x = &lt;span class="pre"&gt;(?help,&lt;/span&gt; x)&lt;/tt&gt; (try it!)
The intution behind multiplicity &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; is that if we have a function with
a type of the following form...:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
f : (1 x : a) -&amp;gt; b
&lt;/pre&gt;
&lt;p&gt;...then the guarantee given by the type system is that &lt;em&gt;if&lt;/em&gt; &lt;tt class="docutils literal"&gt;f x&lt;/tt&gt; &lt;em&gt;is used
exactly once, then&lt;/em&gt; &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; &lt;em&gt;is used exactly once&lt;/em&gt;. So, if we insist on
trying to define &lt;tt class="docutils literal"&gt;duplicate&lt;/tt&gt;...:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
duplicate x = (x, x)
&lt;/pre&gt;
&lt;p&gt;...then Idris will complain:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
pmtype.idr:2:15--8:1:While processing right hand side of Main.duplicate at pmtype.idr:2:1--8:1:
There are 2 uses of linear name x
&lt;/pre&gt;
&lt;p&gt;A similar intuition applies for data types. Consider the following types,
&lt;tt class="docutils literal"&gt;Lin&lt;/tt&gt; which wraps an argument that must be used once, and &lt;tt class="docutils literal"&gt;Unr&lt;/tt&gt; which
wraps an argument with unrestricted use:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
data Lin : Type -&amp;gt; Type where
     MkLin : (1 x : a) -&amp;gt; Lin a

data Unr : Type -&amp;gt; Type where
     MkUnr : (x : a) -&amp;gt; Unr a
&lt;/pre&gt;
&lt;p&gt;If &lt;tt class="docutils literal"&gt;MkLin x&lt;/tt&gt; is used once, then &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; is used once. But if &lt;tt class="docutils literal"&gt;MkUnr x&lt;/tt&gt; is
used once, there is no guarantee on how often &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; is used. We can see this a
bit more clearly by starting to write projection functions for &lt;tt class="docutils literal"&gt;Lin&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;Unr&lt;/tt&gt; to extract the argument:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
getLin : (1 val : Lin a) -&amp;gt; a
getLin (MkLin x) = ?howmanyLin

getUnr : (1 val : Unr a) -&amp;gt; a
getUnr (MkUnr x) = ?howmanyUnr
&lt;/pre&gt;
&lt;p&gt;Checking the types of the holes shows us that, for &lt;tt class="docutils literal"&gt;getLin&lt;/tt&gt;, we must use
&lt;tt class="docutils literal"&gt;x&lt;/tt&gt; exactly once (Because the &lt;tt class="docutils literal"&gt;val&lt;/tt&gt; argument is used once,
by pattern matching on it as &lt;tt class="docutils literal"&gt;MkLin x&lt;/tt&gt;, and if &lt;tt class="docutils literal"&gt;MkLin x&lt;/tt&gt; is used once,
&lt;tt class="docutils literal"&gt;x&lt;/tt&gt; must be used once):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t howmanyLin
 0 a : Type
 1 x : a
-------------------------------------
howmanyLin : a
&lt;/pre&gt;
&lt;p&gt;For &lt;tt class="docutils literal"&gt;getUnr&lt;/tt&gt;, however, we still have to use &lt;tt class="docutils literal"&gt;val&lt;/tt&gt; once, again by pattern
matching on it, but using &lt;tt class="docutils literal"&gt;MkUnr x&lt;/tt&gt; once doesn't place any restrictions on
&lt;tt class="docutils literal"&gt;x&lt;/tt&gt;. So, &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; has unrestricted use in the body of &lt;tt class="docutils literal"&gt;getUnr&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t howmanyUnr
 0 a : Type
   x : a
-------------------------------------
howmanyUnr : a
&lt;/pre&gt;
&lt;p&gt;If &lt;tt class="docutils literal"&gt;getLin&lt;/tt&gt; has an unrestricted argument...:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
getLin : (val : Lin a) -&amp;gt; a
getLin (MkLin x) = ?howmanyLin
&lt;/pre&gt;
&lt;p&gt;...then &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; is unrestricted in &lt;tt class="docutils literal"&gt;howmanyLin&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t howmanyLin
 0 a : Type
   x : a
-------------------------------------
howmanyLin : a
&lt;/pre&gt;
&lt;p&gt;Remember the intuition from the type of &lt;tt class="docutils literal"&gt;MkLin&lt;/tt&gt; is that if &lt;tt class="docutils literal"&gt;MkLin x&lt;/tt&gt; is
used exactly once, &lt;tt class="docutils literal"&gt;x&lt;/tt&gt; is used exactly once. But, we didn't say that
&lt;tt class="docutils literal"&gt;MkLin x&lt;/tt&gt; would be used exactly once, so there is no restriction on &lt;tt class="docutils literal"&gt;x&lt;/tt&gt;.&lt;/p&gt;
&lt;div class="section" id="resource-protocols"&gt;
&lt;h3&gt;Resource protocols&lt;/h3&gt;
&lt;p&gt;One way to take advantage of being able to express linear usage of an argument
is in defining resource usage protocols, where we can use linearity to ensure
that any unique external resource has only one instance, and we can use
functions which are linear in their arguments to represent state transitions on
that resource. A door, for example, can be in one of two states, &lt;tt class="docutils literal"&gt;Open&lt;/tt&gt; or
&lt;tt class="docutils literal"&gt;Closed&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
data DoorState = Open | Closed

data Door : DoorState -&amp;gt; Type where
     MkDoor : (doorId : Int) -&amp;gt; Door st
&lt;/pre&gt;
&lt;p&gt;(Okay, we're just pretending here - imagine the &lt;tt class="docutils literal"&gt;doorId&lt;/tt&gt; is a reference
to an external resource!)&lt;/p&gt;
&lt;p&gt;We can define functions for opening and closing the door which explicitly
describe how they change the state of a door, and that they are linear in
the door:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
openDoor : (1 d : Door Closed) -&amp;gt; Door Open
closeDoor : (1 d : Door Open) -&amp;gt; Door Closed
&lt;/pre&gt;
&lt;p&gt;Remember, the intuition is that if &lt;tt class="docutils literal"&gt;openDoor d&lt;/tt&gt; is used exactly once,
then &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; is used exactly once. So, provided that a door &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; has
multiplicity &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; when it's created, we &lt;em&gt;know&lt;/em&gt; that once we call
&lt;tt class="docutils literal"&gt;openDoor&lt;/tt&gt; on it, we won't be able to use &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; again. Given that
&lt;tt class="docutils literal"&gt;d&lt;/tt&gt; is an external resource, and &lt;tt class="docutils literal"&gt;openDoor&lt;/tt&gt; has changed it's state,
this is a good thing!&lt;/p&gt;
&lt;p&gt;We can ensure that any door we create has multiplicity &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; by
creating them with a &lt;tt class="docutils literal"&gt;newDoor&lt;/tt&gt; function with the following type:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
newDoor : (1 p : (1 d : Door Closed) -&amp;gt; IO ()) -&amp;gt; IO ()
&lt;/pre&gt;
&lt;p&gt;That is, &lt;tt class="docutils literal"&gt;newDoor&lt;/tt&gt; takes a function, which it runs exactly once. That
function takes a door, which is used exactly once. We'll run it in
&lt;tt class="docutils literal"&gt;IO&lt;/tt&gt; to suggest that there is some interaction with the outside world
going on when we create the door. Since the multiplicity &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; means the
door has to be used exactly once, we need to be able to delete the door
when we're finished:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
deleteDoor : (1 d : Door Closed) -&amp;gt; IO ()
&lt;/pre&gt;
&lt;p&gt;So an example correct door protocol usage would be:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
doorProg : IO ()
doorProg
    = newDoor $ \d =&amp;gt;
          let d' = openDoor d
              d'' = closeDoor d' in
              deleteDoor d''
&lt;/pre&gt;
&lt;p&gt;It's instructive to build this program interactively, with holes along
the way, and see how the multiplicities of &lt;tt class="docutils literal"&gt;d&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;d'&lt;/tt&gt; etc change. For
example:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
doorProg : IO ()
doorProg
    = newDoor $ \d =&amp;gt;
          let d' = openDoor d in
              ?whatnow
&lt;/pre&gt;
&lt;p&gt;Checking the type of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;?whatnow&lt;/span&gt;&lt;/tt&gt; shows that &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; is now spent, but we
still have to use &lt;tt class="docutils literal"&gt;d'&lt;/tt&gt; exactly once:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t whatnow
 0 d : Door Closed
 1 d' : Door Open
-------------------------------------
whatnow : IO ()
&lt;/pre&gt;
&lt;p&gt;Note that the &lt;tt class="docutils literal"&gt;0&lt;/tt&gt; multiplicity for &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; means that we can still &lt;em&gt;talk&lt;/em&gt;
about it - in particular, we can still reason about it in types - but we
can't use it again in a relevant position in the rest of the program.
It's also fine to shadow the name &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; throughout:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
doorProg : IO ()
doorProg
    = newDoor $ \d =&amp;gt;
          let d = openDoor d
              d = closeDoor d in
              deleteDoor d
&lt;/pre&gt;
&lt;p&gt;If we don't follow the protocol correctly - create the door, open it, close
it, then delete it - then the program won't type check. For example, we
can try not to delete the door before finishing:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
doorProg : IO ()
doorProg
    = newDoor $ \d =&amp;gt;
          let d' = openDoor d
              d'' = closeDoor d' in
              putStrLn &amp;quot;What could possibly go wrong?&amp;quot;
&lt;/pre&gt;
&lt;p&gt;This gives the following error:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Door.idr:15:19--15:38:While processing right hand side of Main.doorProg at Door.idr:13:1--17:1:
There are 0 uses of linear name d''
&lt;/pre&gt;
&lt;p&gt;There's a lot more to be said about the details here! But, this shows at
a high level how we can use linearity to capture resource usage protocols
at the type level. If we have an external resource which is guaranteed to
be used linearly, like &lt;tt class="docutils literal"&gt;Door&lt;/tt&gt;, we don't need to run operations on that
resource in an &lt;tt class="docutils literal"&gt;IO&lt;/tt&gt; monad, since we're already enforcing an ordering on
operations and don't have access to any out of date resource states. This is
similar to the way interactive programs work in
&lt;a class="reference external" href="https://clean.cs.ru.nl/Clean"&gt;the Clean programming language&lt;/a&gt;, and in
fact is how &lt;tt class="docutils literal"&gt;IO&lt;/tt&gt; is implemented internally in Idris 2, with a special
&lt;tt class="docutils literal"&gt;%World&lt;/tt&gt; type for representing the state of the outside world that is
always used linearly:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
public export
data IORes : Type -&amp;gt; Type where
     MkIORes : (result : a) -&amp;gt; (1 x : %World) -&amp;gt; IORes a

export
data IO : Type -&amp;gt; Type where
     MkIO : (1 fn : (1 x : %World) -&amp;gt; IORes a) -&amp;gt; IO a
&lt;/pre&gt;
&lt;p&gt;Having multiplicities in the type system raises several interesting
questions, such as:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Can we use linearity information to inform memory management and, for
example, have type level guarantees about functions which will not need
to perform garbage collection?&lt;/li&gt;
&lt;li&gt;How should multiplicities be incorporated into interfaces such as
&lt;tt class="docutils literal"&gt;Functor&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Applicative&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;Monad&lt;/tt&gt;?&lt;/li&gt;
&lt;li&gt;If we have &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; as multiplicities, why stop there? Why not have
&lt;tt class="docutils literal"&gt;2&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;3&lt;/tt&gt; and more (like &lt;a class="reference external" href="https://granule-project.github.io/granule.html"&gt;Granule&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;What about multiplicity polymorphism, as in the &lt;a class="reference external" href="https://arxiv.org/abs/1710.09756"&gt;Linear Haskell proposal&lt;/a&gt;?&lt;/li&gt;
&lt;li&gt;Even without all of that, what can we do &lt;em&gt;now&lt;/em&gt;?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I don't know the answer to any of these yet, but it's going to be fun
finding out!&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="erasure"&gt;
&lt;h2&gt;Erasure&lt;/h2&gt;
&lt;p&gt;The &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; multiplicity give us many new possibilities in the kinds of
properties we can express. But, the &lt;tt class="docutils literal"&gt;0&lt;/tt&gt; multiplicity is perhaps more
important in that it allows us to be precise about which values are
relevant at run time, and which are compile time only (that is, which are
erased). Using the &lt;tt class="docutils literal"&gt;0&lt;/tt&gt; multiplicity means a function's type now tells us
exactly what it needs at run time.&lt;/p&gt;
&lt;p&gt;For example, in Idris 1 you could get the length of a vector as follows:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
vlen : Vect n a -&amp;gt; Nat
vlen {n} xs = n
&lt;/pre&gt;
&lt;p&gt;This is fine, since it runs in constant time, but the trade off is that
&lt;tt class="docutils literal"&gt;n&lt;/tt&gt; has to be available at run time, so at run time we always need the length
of the vector to be available if we ever call &lt;tt class="docutils literal"&gt;vlen&lt;/tt&gt;. Idris 1 can infer whether
the length is needed, but there's no easy way for a programmer to be sure.&lt;/p&gt;
&lt;p&gt;In Idris 2, we'd need to state explicitly that &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; is needed at run time:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
vlen : {n : Nat} -&amp;gt; Vect n a -&amp;gt; Nat
vlen xs = n
&lt;/pre&gt;
&lt;p&gt;(Incidentally, also note that in Idris 2, names bound in types are also available
in the definition without explicitly rebinding them.)&lt;/p&gt;
&lt;p&gt;This also means that when you call &lt;tt class="docutils literal"&gt;vlen&lt;/tt&gt;, you need the length available. For
example, this will give an error:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sumLengths : Vect m a -&amp;gt; Vect n a —&amp;gt; Nat
sumLengths xs ys = vlen xs + vlen ys
&lt;/pre&gt;
&lt;p&gt;Idris 2 reports:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
vlen.idr:7:20--7:28:While processing right hand side of Main.sumLengths at vlen.idr:7:1--10:1:
m is not accessible in this context
&lt;/pre&gt;
&lt;p&gt;This means that it needs to use &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; as an argument to pass to &lt;tt class="docutils literal"&gt;vlen xs&lt;/tt&gt;,
where it needs to be available at run time, but &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; is not available in
&lt;tt class="docutils literal"&gt;sumLengths&lt;/tt&gt; because it has multiplicity &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;We can see this more clearly by replacing the right hand side of
&lt;tt class="docutils literal"&gt;sumLengths&lt;/tt&gt; with a hole...:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sumLengths : Vect m a -&amp;gt; Vect n a -&amp;gt; Nat
sumLengths xs ys = ?sumLengths_rhs
&lt;/pre&gt;
&lt;p&gt;...then checking the hole's type at the REPL:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t sumLengths_rhs
 0 n : Nat
 0 a : Type
 0 m : Nat
   ys : Vect n a
   xs : Vect m a
-------------------------------------
sumLengths_rhs : Nat
&lt;/pre&gt;
&lt;p&gt;Instead, we need to give bindings for &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; with
unrestricted multiplicity:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sumLengths : {m, n : _} -&amp;gt; Vect m a -&amp;gt; Vect n a —&amp;gt; Nat
sumLengths xs ys = vLen xs + vLen xs
&lt;/pre&gt;
&lt;p&gt;Remember that giving no multiplicity on a binder, as with &lt;tt class="docutils literal"&gt;m&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; here,
means that the variable has unrestricted usage.&lt;/p&gt;
&lt;p&gt;(By the way, for any readers thinking &amp;quot;oh no, why does it have to be vectors all
the time?&amp;quot; - I make no apology for using extremely familiar examples to introduce
an unfamiliar new concept!)&lt;/p&gt;
&lt;p&gt;If you're converting Idris 1 programs to work with Idris 2, this is probably the
biggest thing you need to think about - along with the Idris 2 Prelude being much
thinner, which is a topic I'll save for another time. It is important to note,
though, that if you have bound implicits, such as...:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
excitingFn : {t : _} -&amp;gt; Coffee t -&amp;gt; Moonbase t
&lt;/pre&gt;
&lt;p&gt;...then it's a good idea to make sure &lt;tt class="docutils literal"&gt;t&lt;/tt&gt; really is needed, or performance
might suffer due to the run time building the instance of &lt;tt class="docutils literal"&gt;t&lt;/tt&gt; unnecessarily!&lt;/p&gt;
&lt;p&gt;One final note on erasure: it is an error to try to pattern match on an
argument with multiplicity &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;, unless its value is inferrable from
elsewhere. So, the following definition is rejected:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
badNot : (0 x : Bool) -&amp;gt; Bool
badNot False = True
badNot True = False
&lt;/pre&gt;
&lt;p&gt;This is rejected with the error:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
badnot.idr:2:1--3:1:Attempt to match on erased argument False in
Main.badNot
&lt;/pre&gt;
&lt;p&gt;The following, however, is fine, because in &lt;tt class="docutils literal"&gt;sNot&lt;/tt&gt;, even though we appear
to match on the erased argument &lt;tt class="docutils literal"&gt;x&lt;/tt&gt;, its value is uniquely inferrable from
the type of the second argument:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
data SBool : Bool -&amp;gt; Type where
     SFalse : SBool False
     STrue  : SBool True

sNot : (0 x : Bool) -&amp;gt; SBool x -&amp;gt; Bool
sNot False SFalse = True
sNot True  STrue  = False
&lt;/pre&gt;
&lt;p&gt;My own experience so far suggests that, most of the time, as long as you're
using unbound implicits in your Idris 1 programs, they will work without
much modification in Idris 2. The Idris 2 type checker will point out where
you require an unbound implicit argument at run time - sometimes this is
both surprising and enlightening!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="pattern-matching-on-types"&gt;
&lt;h2&gt;Pattern Matching on Types&lt;/h2&gt;
&lt;p&gt;One way to think about dependent types is to think of them as &amp;quot;first class&amp;quot;
objects in the language, in that they can be assigned to variables,
passed around and returned from functions, just like any other construct.
But, if they're truly first class, we should be able to pattern match on
them too! Idris 2 allows us to do this. For example:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
showType : Type -&amp;gt; String
showType Int = &amp;quot;Int&amp;quot;
showType (List a) = &amp;quot;List of &amp;quot; ++ showType a
showType _ = &amp;quot;something else&amp;quot;
&lt;/pre&gt;
&lt;p&gt;We can try this as follows:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; showType Int
&amp;quot;Int&amp;quot;
Main&amp;gt; showType (List Int)
&amp;quot;List of Int&amp;quot;
Main&amp;gt; showType (List Bool)
&amp;quot;List of something else&amp;quot;
&lt;/pre&gt;
&lt;p&gt;Pattern matching on function types is interesting, because the return type
may depend on the input value. For example, let's add a case to
&lt;tt class="docutils literal"&gt;showType&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
showType (Nat -&amp;gt; a) = ?help
&lt;/pre&gt;
&lt;p&gt;Inspecting the type of &lt;tt class="docutils literal"&gt;help&lt;/tt&gt; tells us:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Main&amp;gt; :t help
   a : Nat -&amp;gt; Type
-------------------------------------
help : String
&lt;/pre&gt;
&lt;p&gt;So, the return type &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; depends on the input value of type &lt;tt class="docutils literal"&gt;Nat&lt;/tt&gt;, and
we'll need to come up with a value to use &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;, for example:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
showType (Nat -&amp;gt; a) = &amp;quot;Function from Nat to &amp;quot; ++ showType (a Z)
&lt;/pre&gt;
&lt;p&gt;Note that multiplicities on the binders, and the ability to pattern match
on &lt;em&gt;non-erased&lt;/em&gt; types mean that the following two types are distinct:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
id : a -&amp;gt; a
notId : {a : Type} -&amp;gt; a -&amp;gt; a
&lt;/pre&gt;
&lt;p&gt;In the case of &lt;tt class="docutils literal"&gt;notId&lt;/tt&gt;, we can match on &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; and get a function which
is certainly not the identity function:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
notId {a = Int} x = x + 1
notId x = x

Main&amp;gt; notId 93
94
Main&amp;gt; notId &amp;quot;???&amp;quot;
&amp;quot;???&amp;quot;
&lt;/pre&gt;
&lt;p&gt;There is an important consequence of being able to distinguish between relevant
and irrelevant type arguments, which is that a function is &lt;em&gt;only&lt;/em&gt; parametric in
&lt;tt class="docutils literal"&gt;a&lt;/tt&gt; if &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; has multiplicity &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;. So, in the case of &lt;tt class="docutils literal"&gt;notId&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; is
&lt;em&gt;not&lt;/em&gt; a parameter, and so we can't draw any conclusions about the way the
function will behave because it is polymorphic, because the type tells us it
might pattern match on &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;On the other hand, it is merely a coincidence that, in non-dependently typed
languages, types are &lt;em&gt;irrelevant&lt;/em&gt; and get erased, and values are &lt;em&gt;relevant&lt;/em&gt;
and remain at run time. Idris 2, being based on QTT, allows us to make the
distinction between relevant and irrelevant arguments precise. Types can be
relevant, values (such as the &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; index to vectors) can be irrelevant.&lt;/p&gt;
&lt;p&gt;That's all for now. I hope this is enough to get started with dealing with
multiplicities in Idris 2!&lt;/p&gt;
&lt;/div&gt;
</content><category term="Posts"/><category term="idris"/></entry><entry><title>New site</title><link href="https://www.type-driven.org.uk/edwinb/new-site.html" rel="alternate"/><published>2020-01-09T11:00:00+00:00</published><updated>2020-01-09T11:00:00+00:00</updated><author><name>Edwin</name></author><id>tag:www.type-driven.org.uk,2020-01-09:/edwinb/new-site.html</id><content type="html">&lt;p&gt;I've moved here from wordpress, because I wanted a nice simple static site
that lives on a domain that's under my control. Also, for uninteresting
reasons, I can't log in to my old site anyway...&lt;/p&gt;
</content><category term="Posts"/><category term="meta"/></entry></feed>