by guerrilla · 6y ago · 0 comments
data Coproduct a b where
InjL :: a -> Coproduct a b
InjR :: b -> Coproduct a b
elimCoproduct :: (a -> c) -> (b -> c) -> Coproduct a b -> c
elimCoproduct f g (InjL x) = f x
elimCoproduct f g (InjR y) = g y
codata Product a b where
ProjL :: Product a b -> a
ProjR :: Product a b -> b
introProduct :: (c -> a) -> (c -> b) -> c -> Product a b
ProjL (introProduct f g z) = f z
ProjR (introProduct f g z) = g zhttps://www.cs.mcgill.ca/~bpientka/papers/unnesting-copatter...
https://doi.org/10.1017/S0956796819000182
https://agda.readthedocs.io/en/latest/language/copatterns.ht...
Agda even has extra sugar on top for postfix projectors: you could write `myPair .ProjL` (as expression and as copattern). Syntax for pattern-matching anonymous function crucially uses that (yup, we have multi-clause anonymous functions!). You'd write:
introProduct f g z .ProjL = f z
introProduct f g z .ProjR = g z
or introProduct = λ { f g z .ProjL → f z
; f g z .ProjR → g z }
Note that most languages already have that "codata" thing but it's usually called "record" or "struct".I'm still fuzzy but it seems like this can be directly translated into an underlying term rewriting system and would just work almost unmodified and all we've done is opened a second route for type checking terms based on orthogonal conditions? I'll have a look at the nested copatters paper as that probably answers my question. Thank for the resources. They seem much clearer than my search results. I wonder why people don't start explaining codata with records instead of coNats and coLists. The latter was confusing.