shithub: MicroHs

Download patch

ref: 4e5693250ab7b0e505d854593c7d45c65d8e6cbf
parent: c76697906da9e7fba921bcbc1219c5b579f25140
author: Lennart Augustsson <lennart@augustsson.net>
date: Sat Sep 2 11:11:21 EDT 2023

Add :: in expressions.

--- a/src/MicroHs/Desugar.hs
+++ b/src/MicroHs/Desugar.hs
@@ -166,6 +166,7 @@
               dsExpr (EIf c (ECompr e stmts) (EList []))
             SLet ds ->
               dsExpr (ELet ds (ECompr e stmts))
+    ESign e _ -> dsExpr e
     EAt _ _ -> undefined
     EUVar _ -> undefined
     ECon c ->
--- a/src/MicroHs/Expr.hs
+++ b/src/MicroHs/Expr.hs
@@ -73,6 +73,7 @@
   | ESectR Ident Expr
   | EIf Expr Expr Expr
   | ECompr Expr [EStmt]
+  | ESign Expr EType
   | EAt Ident Expr  -- only in patterns
   -- Only while type checking
   | EUVar Int
@@ -228,6 +229,7 @@
     ESectR i e -> i : allVarsExpr e
     EIf e1 e2 e3 -> allVarsExpr e1 ++ allVarsExpr e2 ++ allVarsExpr e3
     ECompr e ss -> allVarsExpr e ++ concatMap allVarsStmt ss
+    ESign e _ -> allVarsExpr e
     EAt i e -> i : allVarsExpr e
     EUVar _ -> []
     ECon c -> [conIdent c]
@@ -317,6 +319,7 @@
     ESectR i e -> "(" ++ showIdent i ++ " " ++ showExpr e ++ ")"
     EIf e1 e2 e3 -> "if " ++ showExpr e1 ++ " then " ++ showExpr e2 ++ " else " ++ showExpr e3
     ECompr _ _ -> "ECompr"
+    ESign e t -> showExpr e ++ " :: " ++ showEType t
     EAt i e -> showIdent i ++ "@" ++ showExpr e
     EUVar i -> "a" ++ showInt i
     ECon c -> showCon c
--- a/src/MicroHs/Parse.hs
+++ b/src/MicroHs/Parse.hs
@@ -405,7 +405,10 @@
 pExprApp = P.do
   f <- pAExpr
   as <- emany pAExpr
-  pure $ foldl EApp f as
+  mt <- optional (pSymbol "::" *> pType)
+  let
+    r = foldl EApp f as
+  pure $ maybe r (ESign r) mt
 
 pLam :: P Expr
 pLam = ELam <$> (pSymbol "\\" *> esome pAPat) <*> (pSymbol "->" *> pExpr)
--- a/src/MicroHs/TypeCheck.hs
+++ b/src/MicroHs/TypeCheck.hs
@@ -757,6 +757,11 @@
         tr = tApp tList ta
       munify (getSLocExpr ae) mt tr
       T.return (ECompr ea rss, tr)
+    ESign e t -> T.do
+      (tt, _) <- withTypeTable $ tcType (Just kType) t
+      (ee, _) <- tcExpr (Just tt) e
+      munify (getSLocExpr ae) mt tt
+      T.return (ESign ee tt, tt)
     EAt i e -> T.do
       (ee, t) <- tcExpr mt e
       (_, ti) <- tLookupInst "impossible!" i
--