Skip to content

Commit

Permalink
make AddInput work in con fields
Browse files Browse the repository at this point in the history
Signed-off-by: George Thomas <[email protected]>
  • Loading branch information
georgefst committed May 18, 2023
1 parent 84a3d02 commit 544ebf6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
35 changes: 23 additions & 12 deletions primer/src/Primer/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Foreword hiding (mod)
import Control.Monad.Fresh (MonadFresh)
import Data.Aeson (Value)
import Data.Bifunctor.Swap qualified as Swap
import Data.Data (Data)
import Data.Generics.Product (typed)
import Data.List (findIndex)
import Data.List.NonEmpty qualified as NE
Expand Down Expand Up @@ -897,6 +898,7 @@ renameForall b zt = case target zt of

-- | Convert a high-level 'Available.NoInputAction' to a concrete sequence of 'ProgAction's.
toProgActionNoInput ::
(HasID a, Data a) =>
DefMap ->
Either (ASTTypeDef a) ASTDef ->
Selection' ID ->
Expand Down Expand Up @@ -930,18 +932,27 @@ toProgActionNoInput defs def0 sel0 = \case
-- on the domain (left) side of the arrow.
toProgAction [ConstructArrowL, Move Child1]
Available.AddInput -> do
-- This action traverses the function type and adds a function arrow to the end of it,
-- resulting in a new argument type. The result type is unchanged.
-- The cursor location is also unchanged.
-- e.g. A -> B -> C ==> A -> B -> ? -> C
id <- nodeID
def <- termDef
type_ <- case findType id $ astDefType def of
Just t -> pure t
Nothing -> case map fst $ findNodeWithParent id $ astDefExpr def of
Just (TypeNode t) -> pure t
Just sm -> Left $ NeedType sm
Nothing -> Left $ IDNotFound id
type_ <- case def0 of
Left def -> do
(_, vcName, field) <- conFieldSel
let id = field.meta
vc <- maybeToEither (ConNameNotFound vcName) $ find ((== vcName) . valConName) $ astTypeDefConstructors def
t <- maybeToEither (FieldIndexNameNotFound vcName field.index) $ flip atMay field.index $ valConArgs vc
case findType id t of
Just t' -> pure $ forgetTypeMetadata t'
Nothing -> Left $ IDNotFound id
Right def -> do
-- This action traverses the function type and adds a function arrow to the end of it,
-- resulting in a new argument type. The result type is unchanged.
-- The cursor location is also unchanged.
-- e.g. A -> B -> C ==> A -> B -> ? -> C
id <- nodeID
forgetTypeMetadata <$> case findType id $ astDefType def of
Just t -> pure t
Nothing -> case map fst $ findNodeWithParent id $ astDefExpr def of
Just (TypeNode t) -> pure t
Just sm -> Left $ NeedType sm
Nothing -> Left $ IDNotFound id
l <- case type_ of
TFun _ a b -> pure $ NE.length $ fst $ unfoldFun a b
t -> Left $ NeedTFun t
Expand Down
6 changes: 4 additions & 2 deletions primer/src/Primer/Action/Errors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Data.Aeson (FromJSON (..), ToJSON (..))
import Primer.Action.Actions (Action)
import Primer.Action.Available qualified as Available
import Primer.Action.Movement (Movement)
import Primer.Core (Expr, GVarName, ID, LVarName, ModuleName, Type)
import Primer.Core (Expr, GVarName, ID, LVarName, ModuleName, Type', ValConName)
import Primer.JSON (CustomJSON (..), PrimerJSON)
import Primer.Typecheck.TypeError (TypeError)
import Primer.Zipper (SomeNode)
Expand Down Expand Up @@ -57,7 +57,7 @@ data ActionError
-- The extra unit is to avoid having two constructors with a single
-- TypeError field, breaking our MonadNestedError machinery...
ImportFailed () TypeError
| NeedTFun Type
| NeedTFun (Type' ())
| NeedType SomeNode
| NeedGlobal Available.Option
| NeedLocal Available.Option
Expand All @@ -71,5 +71,7 @@ data ActionError
| NeedTypeDefConsFieldSelection
| NeedTypeDefParamSelection
| NoNodeSelection
| ConNameNotFound ValConName
| FieldIndexNameNotFound ValConName Int
deriving stock (Eq, Show, Read, Generic)
deriving (FromJSON, ToJSON) via PrimerJSON ActionError

0 comments on commit 544ebf6

Please sign in to comment.