quox/src/Quox/Syntax/Qty.idr

68 lines
1.2 KiB
Idris
Raw Normal View History

2021-07-20 16:05:19 -04:00
module Quox.Syntax.Qty
import Quox.Pretty
import Data.Fin
public export
2021-09-03 11:56:04 -04:00
data Qty = Zero | One | Any
2021-07-20 16:05:19 -04:00
%name Qty.Qty pi, rh
private Repr : Type
Repr = Fin 3
private %inline repr : Qty -> Repr
2021-09-03 11:56:04 -04:00
repr pi = case pi of Zero => 0; One => 1; Any => 2
2021-07-20 16:05:19 -04:00
export Eq Qty where (==) = (==) `on` repr
export Ord Qty where compare = compare `on` repr
export
PrettyHL Qty where
prettyM pi = hl Qty <$>
case pi of
Zero => ifUnicode "𝟬" "0"
One => ifUnicode "𝟭" "1"
2021-09-03 11:56:04 -04:00
Any => ifUnicode "𝛚" "*"
2021-07-20 16:05:19 -04:00
export %inline
prettyQtyBinds : Pretty.HasEnv m => List Qty -> m (Doc HL)
2021-07-20 16:05:19 -04:00
prettyQtyBinds =
map (align . sep) .
traverse (\pi => [|pretty0M pi <++> pure (hl Delim "|")|])
public export
2021-09-09 17:51:45 -04:00
plus : Qty -> Qty -> Qty
plus Zero rh = rh
plus pi Zero = pi
plus _ _ = Any
2021-07-20 16:05:19 -04:00
public export
2021-09-09 17:51:45 -04:00
times : Qty -> Qty -> Qty
times Zero _ = Zero
times _ Zero = Zero
times One rh = rh
times pi One = pi
times Any Any = Any
2021-07-20 16:05:19 -04:00
infix 6 <=.
public export
2021-09-09 17:51:45 -04:00
compat : Qty -> Qty -> Bool
compat pi rh = rh == Any || pi == rh
public export
interface IsQty q where
zero, one : q
(+), (*) : q -> q -> q
(<=.) : q -> q -> Bool
public export
IsQty Qty where
zero = Zero; one = One
(+) = plus; (*) = times
(<=.) = compat