on master help sign in
Handle constant negation in parsing phase
commitab3985ae96ab0ab52018d1232aa359b62e2d758a authorZack Grannan <zgrannan@gmail.com> dateSat 11 Aug 2018, 23:28 (8 years ago) parent66f8363
4 files changed+14−2
src/Idris/Parser/Ops.hs+10−1 test/regression002/Negation.idr+2 test/regression002/expected+1−1 test/regression002/run+1
src/Idris/Parser/Ops.hs+10−1
@@ -30,13 +30,22 @@ import qualified Text.Megaparsec.Expr as P
3030-- using pre-build and user-defined operator/fixity declarations
3131table :: [FixDecl] -> [[P.Operator IdrisParser PTerm]]
3232table fixes
33− = [[prefix "-" (\fc x -> PApp fc (PRef fc [fc] (sUN "negate")) [pexp x])]] ++
33+ = [[prefix "-" negateExpr]] ++
3434 toTable (reverse fixes) ++
3535 [[noFixityBacktickOperator],
3636 [binary "$" P.InfixR $ \fc _ x y -> flatten $ PApp fc x [pexp y]],
3737 [binary "=" P.InfixL $ \fc _ x y -> PApp fc (PRef fc [fc] eqTy) [pexp x, pexp y]],
3838 [noFixityOperator]]
3939 where
40+
41+ negateExpr :: FC -> PTerm -> PTerm
42+ negateExpr _ (PConstant fc (I int)) = PConstant fc $ I $ negate int
43+ negateExpr _ (PConstant fc (BI bigInt)) = PConstant fc $ BI $ negate bigInt
44+ negateExpr _ (PConstant fc (Fl dbl)) = PConstant fc $ Fl $ negate dbl
45+ negateExpr _ (PConstSugar fc term) = negateExpr fc term
46+ negateExpr fc (PAlternative ns tp terms) = PAlternative ns tp $ map (negateExpr fc) terms
47+ negateExpr fc x = PApp fc (PRef fc [fc] (sUN "negate")) [pexp x]
48+
4049 flatten :: PTerm -> PTerm -- flatten application
4150 flatten (PApp fc (PApp _ f as) bs) = flatten (PApp fc f (as ++ bs))
4251 flatten t = t
test/regression002/Negation.idrnew file+2
@@ -0,0 +1,2 @@
1+f : Int -> Int
2+f (-1) = 1
test/regression002/expected+1−1
@@ -346,7 +346,7 @@ When checking argument value to function Prelude.Basics.the:
346346 Type mismatch between
347347 x = x (Type of Refl)
348348 and
349− (-0.0) = 0.0 (Expected type)
349+ -0.0 = 0.0 (Expected type)
350350
351351 Specifically:
352352 Type mismatch between
test/regression002/run+1
@@ -35,6 +35,7 @@ ${IDRIS:-idris} $@ --nobanner --nocolour --quiet --port none <<!
3535:load reg077.idr
3636:load DoubleEquality.idr
3737:load Canonicity.idr
38+:load Negation.idr
3839!
3940
4041rm -f *.ibc