Skip to content

Commit

Permalink
Handle builtins separately from variables
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Aug 22, 2024
1 parent 5d0b368 commit f1efe65
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 41 deletions.
26 changes: 2 additions & 24 deletions lib/haskell/natural4/src/LS/Renamer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
-- ----------------------------------------------------------------------------
Expand Down
30 changes: 28 additions & 2 deletions lib/haskell/natural4/src/LS/Renamer/Rules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
-- ----------------------------------------------------------------------------
Expand All @@ -191,4 +218,3 @@ instance Pretty RnNameType where
RnVariable -> "Variable"
RnType -> "Type"
RnEnum -> "Enum"
RnBuiltin -> "Builtin"
6 changes: 5 additions & 1 deletion lib/haskell/natural4/src/LS/XPile/Simala/Transpile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,7 @@ Right
]
, rnHcBody = Just
( Leaf
( RnRelationalTerm
[ RnExprName
( RnName
{ rnOccName = MTT "OTHERWISE" :| []
, rnUniqueId = 4
, rnNameType = RnBuiltin
}
)
]
)
( RnRelationalTerm [ RnExprBuiltin RnOtherwise ] )
)
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f1efe65

Please sign in to comment.