<commit_before>module L0.Normalizer where

import L0.AbSyn

import Data.Loc

intConst :: Int -> SrcLoc -> Exp Type
intConst v s = Literal $ IntVal v s

realConst :: Double -> SrcLoc -> Exp Type
realConst v s = Literal $ RealVal v s

zero :: SrcLoc -> Exp Type
zero s = intConst 0 s

one :: SrcLoc -> Exp Type
one s = intConst 1 s

negOne :: SrcLoc -> Type -> Exp Type
negOne s (Int _) = intConst (-1) s
negOne s (Real _) = realConst (-1.0) s
negOne _ _ = error "Partial functions are baaad."

singleton :: Exp Type -> Type -> SrcLoc -> Exp Type
singleton x t s = ArrayLit [x] t s

-- don't know what to do about maybe types when declaring built-ins, so run
-- this after the typechecker has put in types.

binopLambda :: BinOp -> Type -> SrcLoc -> Lambda Type
binopLambda op t s =
  let
    x = Ident "x" t s
    y = Ident "y" t s
  in
    AnonymFun [x,y] (BinOp op (Var x) (Var y) t s) t s

normalize :: Exp Type -> Exp Type
normalize (Literal v) = Literal v
normalize (BinOp Minus x y t s) =
  -- x - y = x + (-1) * y
  -- assume no type coersion (yielding a partial method)<commit_msg>Eta-reduce in Normalizer.hs!<commit_after>module L0.Normalizer where

import L0.AbSyn

import Data.Loc

intConst :: Int -> SrcLoc -> Exp Type
intConst v s = Literal $ IntVal v s

realConst :: Double -> SrcLoc -> Exp Type
realConst v s = Literal $ RealVal v s

zero :: SrcLoc -> Exp Type
zero = intConst 0

one :: SrcLoc -> Exp Type
one = intConst 1

negOne :: SrcLoc -> Type -> Exp Type
negOne s (Int _) = intConst (-1) s
negOne s (Real _) = realConst (-1.0) s
negOne _ _ = error "Partial functions are baaad."

singleton :: Exp Type -> Type -> SrcLoc -> Exp Type
singleton x = ArrayLit [x]

-- don't know what to do about maybe types when declaring built-ins, so run
-- this after the typechecker has put in types.

binopLambda :: BinOp -> Type -> SrcLoc -> Lambda Type
binopLambda op t s =
  let
    x = Ident "x" t s
    y = Ident "y" t s
  in
    AnonymFun [x,y] (BinOp op (Var x) (Var y) t s) t s

normalize :: Exp Type -> Exp Type
normalize (Literal v) = Literal v
normalize (BinOp Minus x y t s) =
  -- x - y = x + (-1) * y
  -- assume no type coersion (yielding a partial method)
{'fraction_non_alphanumeric': 0.11264367816091954, 'fraction_numerical': 0.0073563218390804595, 'mean_word_length': 2.8242530755711774, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4970742', 'n_tokens_mistral': 782, 'n_tokens_neox': 766, 'n_words': 337}