additional syntactic sugar

- allow multiple names in infix, typesig, and dcon defs
- align fixities with Idris
This commit is contained in:
2024-10-23 21:41:36 -07:00
parent 8c8cdf4f7f
commit 8fe9613c02
8 changed files with 40 additions and 45 deletions

View File

@@ -51,7 +51,7 @@ part1 text digits =
let nums = map combine $ map digits lines in let nums = map combine $ map digits lines in
foldl _+_ 0 nums foldl _+_ 0 nums
infixl 3 _>>_ infixl 1 _>>_
_>>_ : {A B : U} -> A -> B -> B _>>_ : {A B : U} -> A -> B -> B
a >> b = b a >> b = b

View File

@@ -12,7 +12,7 @@ data Game : U where
-- Add, Sub, Mul, Neg -- Add, Sub, Mul, Neg
-- NB this is not lazy! -- NB this is not lazy!
infixl 2 _&&_ infixl 5 _&&_
_&&_ : Bool -> Bool -> Bool _&&_ : Bool -> Bool -> Bool
a && b = case a of a && b = case a of
@@ -68,7 +68,7 @@ parseGame line =
_ => Left "No Game" _ => Left "No Game"
_ => Left $ "No colon in " ++ line _ => Left $ "No colon in " ++ line
infixl 3 _>>_ infixl 1 _>>_
_>>_ : {A B : U} -> A -> B -> B _>>_ : {A B : U} -> A -> B -> B
a >> b = b a >> b = b

View File

@@ -21,7 +21,7 @@ data Either : U -> U -> U where
Right : {a b : U} -> b -> Either a b Right : {a b : U} -> b -> Either a b
infixr 4 _::_ infixr 7 _::_
data List : U -> U where data List : U -> U where
Nil : {a : U} -> List a Nil : {a : U} -> List a
_::_ : {a : U} -> a -> List a -> List a _::_ : {a : U} -> a -> List a -> List a
@@ -41,11 +41,12 @@ length : {a : U} -> List a -> Nat
length Nil = Z length Nil = Z
length (x :: xs) = S (length xs) length (x :: xs) = S (length xs)
infixr 2 _,_ infixr 0 _,_
data Pair : U -> U -> U where data Pair : U -> U -> U where
_,_ : {a b : U} -> a -> b -> Pair a b _,_ : {a b : U} -> a -> b -> Pair a b
-- Idris says it special cases to deal with unification issues
infixr 0 _$_ infixr 0 _$_
_$_ : {a b : U} -> (a -> b) -> a -> b _$_ : {a b : U} -> (a -> b) -> a -> b
@@ -104,13 +105,10 @@ pfunc _+_ : Int -> Int -> Int := "(x,y) => x + y"
pfunc _-_ : Int -> Int -> Int := "(x,y) => x - y" pfunc _-_ : Int -> Int -> Int := "(x,y) => x - y"
pfunc _*_ : Int -> Int -> Int := "(x,y) => x * y" pfunc _*_ : Int -> Int -> Int := "(x,y) => x * y"
pfunc _/_ : Int -> Int -> Int := "(x,y) => x / y" pfunc _/_ : Int -> Int -> Int := "(x,y) => x / y"
infix 3 _<_
infix 3 _<=_
infixl 4 _-_
infixl 4 _+_
infixl 5 _*_
infixl 5 _/_
infix 6 _<_ _<=_
infixl 8 _+_ _-_
infixl 9 _*_ _/_
-- Ideally we'd have newt write the arrows for us to keep things correct -- Ideally we'd have newt write the arrows for us to keep things correct
-- We'd still have difficulty with callbacks... -- We'd still have difficulty with callbacks...
@@ -134,7 +132,7 @@ pfunc slen : String -> Int := "s => s.length"
pfunc sindex : String -> Int -> Char := "(s,i) => s[i]" pfunc sindex : String -> Int -> Char := "(s,i) => s[i]"
infixl 4 _++_ infixl 7 _++_
pfunc _++_ : String -> String -> String := "(a,b) => a + b" pfunc _++_ : String -> String -> String := "(a,b) => a + b"
@@ -159,6 +157,6 @@ map f Nil = Nil
map f (x :: xs) = f x :: map f xs map f (x :: xs) = f x :: map f xs
infixl 7 _._ infixl 9 _._
_._ : {A B C : U} -> (B -> C) -> (A -> B) -> A -> C _._ : {A B C : U} -> (B -> C) -> (A -> B) -> A -> C
(f . g) x = f ( g x) (f . g) x = f ( g x)

View File

@@ -244,13 +244,13 @@ typeExpr = binders
export export
parseSig : Parser Decl parseSig : Parser Decl
parseSig = TypeSig <$> getPos <*> (ident <|> uident) <* keyword ":" <*> typeExpr parseSig = TypeSig <$> getPos <*> some (ident <|> uident) <* keyword ":" <*> typeExpr
parseImport : Parser Import parseImport : Parser Import
parseImport = MkImport <$> getPos <* keyword "import" <*> uident parseImport = MkImport <$> getPos <* keyword "import" <*> uident
-- Do we do pattern stuff now? or just name = lambda? -- Do we do pattern stuff now? or just name = lambda?
-- TODO multiple names
parseMixfix : Parser Decl parseMixfix : Parser Decl
parseMixfix = do parseMixfix = do
fc <- getPos fc <- getPos
@@ -258,9 +258,9 @@ parseMixfix = do
<|> InfixR <$ keyword "infixr" <|> InfixR <$ keyword "infixr"
<|> Infix <$ keyword "infix" <|> Infix <$ keyword "infix"
prec <- token Number prec <- token Number
op <- token MixFix ops <- some $ token MixFix
addOp op (cast prec) fix for_ ops $ \ op => addOp op (cast prec) fix
pure $ PMixFix fc op (cast prec) fix pure $ PMixFix fc ops (cast prec) fix
getName : Raw -> Parser String getName : Raw -> Parser String
getName (RVar x nm) = pure nm getName (RVar x nm) = pure nm

View File

@@ -1,6 +1,7 @@
module Lib.ProcessDecl module Lib.ProcessDecl
import Data.IORef import Data.IORef
import Data.String
import Lib.Elab import Lib.Elab
import Lib.Parser import Lib.Parser
@@ -30,20 +31,23 @@ collectDecl (x :: xs) = x :: collectDecl xs
export export
processDecl : Decl -> M () processDecl : Decl -> M ()
-- REVIEW I supposed I could have updated top here instead of the dance with the parser...
processDecl (PMixFix{}) = pure () processDecl (PMixFix{}) = pure ()
processDecl (TypeSig fc nm tm) = do processDecl (TypeSig fc names tm) = do
top <- get top <- get
for_ names $ \nm => do
let Nothing := lookup nm top let Nothing := lookup nm top
| _ => error fc "\{show nm} is already defined" | _ => error fc "\{show nm} is already defined"
pure ()
putStrLn "-----" putStrLn "-----"
putStrLn "TypeSig \{nm} \{show tm}" putStrLn "TypeSig \{unwords names} : \{show tm}"
ty <- check (mkCtx top.metas fc) tm (VU fc) ty <- check (mkCtx top.metas fc) tm (VU fc)
putStrLn "got \{pprint [] ty}" putStrLn "got \{pprint [] ty}"
-- I was doing this previously, but I don't want to over-expand VRefs -- I was doing this previously, but I don't want to over-expand VRefs
-- ty' <- nf [] ty -- ty' <- nf [] ty
-- putStrLn "nf \{pprint [] ty'}" -- putStrLn "nf \{pprint [] ty'}"
modify $ setDef nm ty Axiom for_ names $ \nm => modify $ setDef nm ty Axiom
processDecl (PType fc nm ty) = do processDecl (PType fc nm ty) = do
ctx <- get ctx <- get
@@ -114,11 +118,10 @@ processDecl (Data fc nm ty cons) = do
modify $ setDef nm tyty Axiom modify $ setDef nm tyty Axiom
ctx <- get ctx <- get
cnames <- for cons $ \x => case x of cnames <- for cons $ \x => case x of
-- expecting tm to be a Pi type (TypeSig fc names tm) => do
(TypeSig fc nm' tm) => do
ctx <- get ctx <- get
dty <- check (mkCtx ctx.metas fc) tm (VU fc) dty <- check (mkCtx ctx.metas fc) tm (VU fc)
debug "dty \{nm'} is \{pprint [] dty}" debug "dty \{show names} is \{pprint [] dty}"
-- We only check that codomain uses the right type constructor -- We only check that codomain uses the right type constructor
-- We know it's in U because it's part of a checked Pi type -- We know it's in U because it's part of a checked Pi type
@@ -130,15 +133,11 @@ processDecl (Data fc nm ty cons) = do
when (hn /= nm) $ when (hn /= nm) $
error (getFC codomain) "Constructor codomain is \{pprint tnames codomain} rather than \{nm}" error (getFC codomain) "Constructor codomain is \{pprint tnames codomain} rather than \{nm}"
modify $ setDef nm' dty (DCon (getArity dty) nm) for_ names $ \ nm' => modify $ setDef nm' dty (DCon (getArity dty) nm)
pure nm' pure names
_ => throwError $ E (0,0) "expected constructor declaration" _ => throwError $ E (0,0) "expected constructor declaration"
-- TODO check tm is VU or Pi ending in VU putStrLn "setDef \{nm} TCon \{show $ join cnames}"
-- Maybe a pi -> binders function modify $ setDef nm tyty (TCon (join cnames))
-- TODO we're putting in axioms, we need constructors
-- for each constructor, check and add
putStrLn "setDef \{nm} TCon \{show cnames}"
modify $ setDef nm tyty (TCon cnames)
pure () pure ()
where where
checkDeclType : Tm -> M () checkDeclType : Tm -> M ()

View File

@@ -100,13 +100,13 @@ data Import = MkImport FC Name
-- FIXME - I think I don't want "where" here, but the parser has an issue -- FIXME - I think I don't want "where" here, but the parser has an issue
public export public export
data Decl data Decl
= TypeSig FC Name Raw = TypeSig FC (List Name) Raw
| Def FC Name (List (Raw,Raw)) -- (List Clause) | Def FC Name (List (Raw,Raw)) -- (List Clause)
| DCheck FC Raw Raw | DCheck FC Raw Raw
| Data FC Name Raw (List Decl) | Data FC Name Raw (List Decl)
| PType FC Name (Maybe Raw) | PType FC Name (Maybe Raw)
| PFunc FC Name Raw String | PFunc FC Name Raw String
| PMixFix FC Name Nat Fixity | PMixFix FC (List Name) Nat Fixity
public export public export
@@ -148,7 +148,7 @@ Show Decl where
show (DCheck _ x y) = foo ["DCheck", show x, show y] show (DCheck _ x y) = foo ["DCheck", show x, show y]
show (PType _ name ty) = foo ["PType", name, show ty] show (PType _ name ty) = foo ["PType", name, show ty]
show (PFunc _ nm ty src) = foo ["PFunc", nm, show ty, show src] show (PFunc _ nm ty src) = foo ["PFunc", nm, show ty, show src]
show (PMixFix _ nm prec fix) = foo ["PMixFix", nm, show prec, show fix] show (PMixFix _ nms prec fix) = foo ["PMixFix", show nms, show prec, show fix]
export covering export covering
Show Module where Show Module where
@@ -246,11 +246,11 @@ Pretty Module where
doImport (MkImport _ nm) = text "import" <+> text nm ++ line doImport (MkImport _ nm) = text "import" <+> text nm ++ line
doDecl : Decl -> Doc doDecl : Decl -> Doc
doDecl (TypeSig _ nm ty) = text nm <+> text ":" <+> nest 2 (pretty ty) doDecl (TypeSig _ nm ty) = spread (map text nm) <+> text ":" <+> nest 2 (pretty ty)
doDecl (Def _ nm clauses) = stack $ map (\(a,b) => pretty a <+> "=" <+> pretty b) clauses doDecl (Def _ nm clauses) = stack $ map (\(a,b) => pretty a <+> "=" <+> pretty b) clauses
-- the behavior of nest is kinda weird, I have to do the nest before/around the </>. -- the behavior of nest is kinda weird, I have to do the nest before/around the </>.
doDecl (Data _ nm x xs) = text "data" <+> text nm <+> text ":" <+> pretty x <+> (nest 2 $ text "where" </> stack (map doDecl xs)) doDecl (Data _ nm x xs) = text "data" <+> text nm <+> text ":" <+> pretty x <+> (nest 2 $ text "where" </> stack (map doDecl xs))
doDecl (DCheck _ x y) = text "#check" <+> pretty x <+> ":" <+> pretty y doDecl (DCheck _ x y) = text "#check" <+> pretty x <+> ":" <+> pretty y
doDecl (PType _ nm ty) = text "ptype" <+> text nm <+> (maybe empty (\ty => ":" <+> pretty ty) ty) doDecl (PType _ nm ty) = text "ptype" <+> text nm <+> (maybe empty (\ty => ":" <+> pretty ty) ty)
doDecl (PFunc _ nm ty src) = "pfunc" <+> text nm <+> ":" <+> nest 2 (pretty ty <+> ":=" <+/> text (show src)) doDecl (PFunc _ nm ty src) = "pfunc" <+> text nm <+> ":" <+> nest 2 (pretty ty <+> ":=" <+/> text (show src))
doDecl (PMixFix _ _ _ fix) = text (show fix) doDecl (PMixFix _ names prec fix) = text (show fix) <+> text (show prec) <+> spread (map text names)

View File

@@ -11,10 +11,8 @@ module Oper
-- long term, I might want TopContext in the parser, and parse a top-level -- long term, I might want TopContext in the parser, and parse a top-level
-- declaration at a time (for incremental updates), but much longer term. -- declaration at a time (for incremental updates), but much longer term.
infixl 4 _+_ infixl 8 _+_ _-_
infixl 4 _-_ infixl 9 _*_ _/_
infixl 5 _*_
infixl 5 _/_
ptype Int ptype Int
ptype String ptype String
@@ -33,7 +31,7 @@ _+_ x y = plus x y
test : Int -> Int test : Int -> Int
test x = 42 + x * 3 + 2 test x = 42 + x * 3 + 2
infixr 2 _,_ infixr 0 _,_
data Pair : U -> U -> U where data Pair : U -> U -> U where
_,_ : {A B : U} -> A -> B -> Pair A B _,_ : {A B : U} -> A -> B -> Pair A B

View File

@@ -18,7 +18,7 @@ data List : U -> U where
-- Currently if I say _::_ = Cons, it gets curried -- Currently if I say _::_ = Cons, it gets curried
infixr 4 _::_ infixr 7 _::_
_::_ : {a : U} -> a -> List a -> List a _::_ : {a : U} -> a -> List a -> List a
_::_ x xs = Cons x xs _::_ x xs = Cons x xs