Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: add syntax trick to use Unicode instead of Haskell-compatible ASCII identifiers #293

Open
omelkonian opened this issue Feb 29, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@omelkonian
Copy link
Contributor

Although users should generally use Haskell-compile identifiers for the definitions they compile with agda2hs, one could use syntax or pattern declarations in Agda to emulate Unicode mixfix definitions that are translated away at the time we compile the internal syntax where all this has been de-sugared.

As an example, we can use an infix notations for vectors:

open import Haskell.Prelude hiding ([]; _∷_)

data Vec (a : Set) : (@0 n : Nat)  Set where
  Nil : Vec a 0
  Cons : {@0 n : Nat}  a  Vec a n  Vec a (suc n)
{-# COMPILE AGDA2HS Vec #-}

pattern [] = Nil
pattern _∷_ x xs = Cons x xs

mapV : {a b : Set} {@0 n : Nat} (f : a  b)  Vec a n  Vec b n

syntax mapV f xs = f ⟨$⟩ xs

mapV f [] = []
mapV f (x ∷ xs) = f x ∷ (f ⟨$⟩ xs)
{-# COMPILE AGDA2HS mapV #-}

incV : {@0 n : Nat}  Vec Nat n  Vec Nat n
incV xs = (1 +_) ⟨$⟩ xs
{-# COMPILE AGDA2HS incV #-}

tailV : {a : Set} {@0 n : Nat}  Vec a (suc n)  Vec a n
tailV (x ∷ xs) = xs
{-# COMPILE AGDA2HS tailV #-}

that still compiles to the expected Haskell:

import Numeric.Natural (Natural)

data Vec a = Nil
           | Cons a (Vec a)

mapV :: (a -> b) -> Vec a -> Vec b
mapV f Nil = Nil
mapV f (Cons x xs) = Cons (f x) (mapV f xs)

incV :: Vec Natural -> Vec Natural
incV xs = mapV (1 +) xs

tailV :: Vec a -> Vec a
tailV (Cons x xs) = xs

(Credits to Ramsay Taylor of IOG for the idea)

@omelkonian omelkonian added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 29, 2024
@flupe
Copy link
Contributor

flupe commented Mar 12, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants