diff --git a/lib/haskell/natural4/src/LS/Renamer.hs b/lib/haskell/natural4/src/LS/Renamer.hs index 8d9f67e6f..3efc5da5e 100644 --- a/lib/haskell/natural4/src/LS/Renamer.hs +++ b/lib/haskell/natural4/src/LS/Renamer.hs @@ -67,7 +67,6 @@ import Data.List qualified as List import Data.List.NonEmpty (NonEmpty) import Data.List.NonEmpty qualified as NE import Data.Map.Strict qualified as Map -import Data.Set qualified as Set import Data.Text (Text) import Data.Text qualified as Text import Data.Text.IO qualified as Text @@ -715,12 +714,8 @@ renameMultiTermExpression tracer ctx = \case Nothing | Just literal <- isTextLiteral name -> pure (RnExprLit $ RnString literal, ctx') - | isL4BuiltIn name -> do - -- ANDRES: I'm not convinced that built-ins should be renamed, and - -- if we already detected that they're built-ins, perhaps we should - -- just use a different dedicated constructor for this case. - rnName <- RnExprName <$> rnL4Builtin tracer name - pure (rnName, ctx') + | Just builtin <- isL4BuiltIn (mkSimpleOccName name) -> do + pure (RnExprBuiltin builtin, ctx') | ctx.multiTermContextInSelector -> do rnName <- RnExprName <$> insertName tracer (mkSimpleOccName name) RnSelector pure (rnName, ctx') @@ -754,23 +749,6 @@ renameMultiTermExpression tracer ctx = \case (t'', '"') <- unsnoc t' pure t'' --- ---------------------------------------------------------------------------- --- Builtins --- ---------------------------------------------------------------------------- - -isL4BuiltIn :: Text -> Bool -isL4BuiltIn name = Set.member name (Set.fromList l4Builtins) - -rnL4Builtin :: Tracer Log -> Text -> Renamer RnName -rnL4Builtin tracer name = do - lookupOrInsertName tracer (mkSimpleOccName name) RnBuiltin - -l4Builtins :: [Text] -l4Builtins = [oTHERWISE] - -oTHERWISE :: Text -oTHERWISE = "OTHERWISE" - -- ---------------------------------------------------------------------------- -- Typed Errors -- ---------------------------------------------------------------------------- diff --git a/lib/haskell/natural4/src/LS/Renamer/Rules.hs b/lib/haskell/natural4/src/LS/Renamer/Rules.hs index a20f64edb..e9e85adee 100644 --- a/lib/haskell/natural4/src/LS/Renamer/Rules.hs +++ b/lib/haskell/natural4/src/LS/Renamer/Rules.hs @@ -32,6 +32,11 @@ module LS.Renamer.Rules ( Unique, mkSimpleOccName, + -- * Builtins + RnBuiltin (..), + isL4BuiltIn, + l4Builtins, + -- * Pretty functions for types that do not have a canonical 'Pretty' unique prettyMT, ) where @@ -43,6 +48,8 @@ import LS.Types qualified as LS import Data.List.NonEmpty (NonEmpty) import Data.List.NonEmpty qualified as NE +import Data.Map.Strict (Map) +import Data.Map.Strict qualified as Map import Data.Text (Text) import Data.Text qualified as Text import GHC.Generics (Generic) @@ -140,11 +147,11 @@ data RnNameType | RnVariable | RnType | RnEnum - | RnBuiltin deriving (Eq, Ord, Show, Generic) data RnExpr = RnExprName RnName + | RnExprBuiltin RnBuiltin | RnExprLit RnLit deriving (Eq, Ord, Show, Generic) @@ -174,6 +181,26 @@ type OccName = NonEmpty LS.MTExpr mkSimpleOccName :: Text -> OccName mkSimpleOccName = NE.singleton . LS.MTT +-- ---------------------------------------------------------------------------- +-- Builtins +-- ---------------------------------------------------------------------------- + +data RnBuiltin + = RnOtherwise + deriving (Eq, Ord, Show, Generic) + +isL4BuiltIn :: OccName -> Maybe RnBuiltin +isL4BuiltIn name = Map.lookup name l4Builtins + +l4Builtins :: Map OccName RnBuiltin +l4Builtins = + Map.fromList + [ (oTHERWISE, RnOtherwise) + ] + +oTHERWISE :: OccName +oTHERWISE = mkSimpleOccName "OTHERWISE" + -- ---------------------------------------------------------------------------- -- Pretty instances -- ---------------------------------------------------------------------------- @@ -191,4 +218,3 @@ instance Pretty RnNameType where RnVariable -> "Variable" RnType -> "Type" RnEnum -> "Enum" - RnBuiltin -> "Builtin" diff --git a/lib/haskell/natural4/src/LS/XPile/Simala/Transpile.hs b/lib/haskell/natural4/src/LS/XPile/Simala/Transpile.hs index 3a203547f..833811f69 100644 --- a/lib/haskell/natural4/src/LS/XPile/Simala/Transpile.hs +++ b/lib/haskell/natural4/src/LS/XPile/Simala/Transpile.hs @@ -585,6 +585,7 @@ isProjection mtHead args = do exprToSimala :: RnExpr -> Simala.Expr exprToSimala (RnExprName name) = Simala.Var $ toSimalaName name +exprToSimala (RnExprBuiltin builtin) = builtinToSimala builtin exprToSimala (RnExprLit lit) = litToSimala lit litToSimala :: RnLit -> Simala.Expr @@ -607,6 +608,7 @@ isExprOfType :: RnExpr -> (RnNameType -> Bool) -> Maybe RnName isExprOfType (RnExprName name) hasTy | hasTy name.rnNameType = Just name | otherwise = Nothing +isExprOfType (RnExprBuiltin _) _ = Nothing isExprOfType (RnExprLit _) _ = Nothing -- ---------------------------------------------------------------------------- @@ -636,7 +638,9 @@ rnNameTypePrefix = \case RnVariable -> "v" RnType -> "t" RnEnum -> "e" - RnBuiltin -> "b" + +builtinToSimala :: RnBuiltin -> Simala.Expr +builtinToSimala RnOtherwise = Simala.Var "otherwise" -- ---------------------------------------------------------------------------- -- Assertion helpers diff --git a/lib/haskell/natural4/test/testdata/golden/renamer/decide-with-attributes.expected b/lib/haskell/natural4/test/testdata/golden/renamer/decide-with-attributes.expected index da083fab3..d6947a6ff 100644 --- a/lib/haskell/natural4/test/testdata/golden/renamer/decide-with-attributes.expected +++ b/lib/haskell/natural4/test/testdata/golden/renamer/decide-with-attributes.expected @@ -150,16 +150,7 @@ Right ] , rnHcBody = Just ( Leaf - ( RnRelationalTerm - [ RnExprName - ( RnName - { rnOccName = MTT "OTHERWISE" :| [] - , rnUniqueId = 4 - , rnNameType = RnBuiltin - } - ) - ] - ) + ( RnRelationalTerm [ RnExprBuiltin RnOtherwise ] ) ) } ] diff --git a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-record.expected b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-record.expected index 64663ee85..58a62e278 100644 --- a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-record.expected +++ b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-record.expected @@ -1 +1 @@ - f_g_1 = fun (v_d_0) => let v_y_2 = {s_book_3 = if v_d_0 > 0 then 'green else if b_OTHERWISE_4 then 'red else undefined} in v_y_2 + f_g_1 = fun (v_d_0) => let v_y_2 = {s_book_3 = if v_d_0 > 0 then 'green else if otherwise then 'red else undefined} in v_y_2 diff --git a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-attributes-conditionals-2.expected b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-attributes-conditionals-2.expected index deb4adfef..a3a1a185d 100644 --- a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-attributes-conditionals-2.expected +++ b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-attributes-conditionals-2.expected @@ -1 +1 @@ - f_f_1 = fun (v_x_0) => let v_y_2 = {s_p_4 = if v_x_0 > 5 then v_x_0 else if b_OTHERWISE_5 then v_x_0 + v_x_0 else undefined,s_z_3 = if v_x_0 > 3 then 5 else if b_OTHERWISE_5 then 0 else undefined} in v_y_2 + f_f_1 = fun (v_x_0) => let v_y_2 = {s_p_4 = if v_x_0 > 5 then v_x_0 else if otherwise then v_x_0 + v_x_0 else undefined,s_z_3 = if v_x_0 > 3 then 5 else if otherwise then 0 else undefined} in v_y_2 diff --git a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-1.expected b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-1.expected index ff2657cea..66400da76 100644 --- a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-1.expected +++ b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-1.expected @@ -1 +1 @@ - f_f_1 = fun (v_x_0) => if v_x_0 > 0 then 1 else if b_OTHERWISE_2 then 0 else if v_x_0 < 0 then 2 else undefined + f_f_1 = fun (v_x_0) => if v_x_0 > 0 then 1 else if otherwise then 0 else if v_x_0 < 0 then 2 else undefined diff --git a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-2.expected b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-2.expected index 610e4c2d2..1bec0387e 100644 --- a/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-2.expected +++ b/lib/haskell/natural4/test/testdata/golden/xpile/simala/function-with-conditionals-2.expected @@ -1 +1 @@ - f_f_1 = fun (v_x_0) => if v_x_0 > 0 then 1 else if b_OTHERWISE_2 then 0 else undefined + f_f_1 = fun (v_x_0) => if v_x_0 > 0 then 1 else if otherwise then 0 else undefined