on master help sign in
Syntax for linear arguments

Enabled with %language LinearTypes, although not yet checked.

commit491633b778343a69846ff00497a1bb9948e73b54 authorEdwin Brady <ecb10@st-andrews.ac.uk> dateSat 17 Dec 2016, 20:15 (10 years ago) parent488c5b7
7 files changed+63−44
src/Idris/AbsSyntaxTree.hs+5 src/Idris/Core/Elaborate.hs+7−7 src/Idris/Core/ProofState.hs+6−6 src/Idris/Elab/Term.hs+14−3 src/Idris/Parser.hs+5−4 src/Idris/Parser/Expr.hs+23−21 src/Idris/Parser/Helpers.hs+3−3
src/Idris/AbsSyntaxTree.hs+5
@@ -186,6 +186,7 @@ ppOptionIst = ppOption . idris_options
186186
187187data LanguageExt = TypeProviders | ErrorReflection | UniquenessTypes
188188 | DSLNotation | ElabReflection | FCReflection
189+ | LinearTypes
189190 deriving (Show, Eq, Read, Ord, Generic)
190191
191192-- | The output mode in use
@@ -1820,6 +1821,10 @@ pprintPTerm ppo bnd docArgs infixes = prettySe (ppopt_depth ppo) startPrec bnd
18201821 kwd "let" <+> (group . align . hang 2 $ prettyBindingOf n False <+> text "=" <$> prettySe (decD d) startPrec bnd v) </>
18211822 kwd "in" <+> (group . align . hang 2 $ prettySe (decD d) startPrec ((n, False):bnd) sc)
18221823 prettySe d p bnd (PPi (Exp l s _ rig) n _ ty sc)
1824+ | Rig1 <- rig =
1825+ depth d . bracket p startPrec . group $
1826+ enclose lparen rparen (group . align $ text "1" <+> prettyBindingOf n False <+> colon <+> prettySe (decD d) startPrec bnd ty) <+>
1827+ st <> text "->" <$> prettySe (decD d) startPrec ((n, False):bnd) sc
18231828 | n `elem` allNamesIn sc || ppopt_impl ppo && uname n || n `elem` docArgs
18241829 || ppopt_pinames ppo && uname n =
18251830 depth d . bracket p startPrec . group $
src/Idris/Core/Elaborate.hs+7−7
@@ -441,8 +441,8 @@ intro n = processTactic' (Intro n)
441441introTy :: Raw -> Maybe Name -> Elab' aux ()
442442introTy ty n = processTactic' (IntroTy ty n)
443443
444−forall :: Name -> Maybe ImplicitInfo -> Raw -> Elab' aux ()
445−forall n i t = processTactic' (Forall n i t)
444+forall :: Name -> RigCount -> Maybe ImplicitInfo -> Raw -> Elab' aux ()
445+forall n r i t = processTactic' (Forall n r i t)
446446
447447letbind :: Name -> Raw -> Raw -> Elab' aux ()
448448letbind n t v = processTactic' (LetBind n t v)
@@ -845,11 +845,11 @@ dep_app fun arg str =
845845
846846-- Abstract over an argument of unknown type, giving a name for the hole
847847-- which we'll fill with the argument type too.
848−arg :: Name -> Maybe ImplicitInfo -> Name -> Elab' aux ()
849−arg n i tyhole = do ty <- unique_hole tyhole
850− claim ty RType
851− movelast ty
852− forall n i (Var ty)
848+arg :: Name -> RigCount -> Maybe ImplicitInfo -> Name -> Elab' aux ()
849+arg n r i tyhole = do ty <- unique_hole tyhole
850+ claim ty RType
851+ movelast ty
852+ forall n r i (Var ty)
853853
854854-- try a tactic, if it adds any unification problem, return an error
855855no_errors :: Elab' aux () -> Maybe Err -> Elab' aux ()
src/Idris/Core/ProofState.hs+6−6
@@ -88,7 +88,7 @@ data Tactic = Attack
8888 | CheckIn Raw
8989 | Intro (Maybe Name)
9090 | IntroTy Raw (Maybe Name)
91− | Forall Name (Maybe ImplicitInfo) Raw
91+ | Forall Name RigCount (Maybe ImplicitInfo) Raw
9292 | LetBind Name Raw Raw
9393 | ExpandLet Name Term
9494 | Rewrite Raw
@@ -684,13 +684,13 @@ intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
684684 _ -> lift $ tfail $ CantIntroduce t'
685685intro n ctxt env _ = fail "Can't introduce here."
686686
687−forall :: Name -> Maybe ImplicitInfo -> Raw -> RunTactic
688−forall n impl ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
687+forall :: Name -> RigCount -> Maybe ImplicitInfo -> Raw -> RunTactic
688+forall n rig impl ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
689689 do (tyv, tyt) <- lift $ check ctxt env ty
690690 unify' ctxt env (tyt, Nothing) (TType (UVar [] 0), Nothing)
691691 unify' ctxt env (t, Nothing) (TType (UVar [] 0), Nothing)
692− return $ Bind n (Pi RigW impl tyv (TType (UVar [] 0))) (Bind x (Hole t) (P Bound x t))
693−forall n impl ty ctxt env _ = fail "Can't pi bind here"
692+ return $ Bind n (Pi rig impl tyv (TType (UVar [] 0))) (Bind x (Hole t) (P Bound x t))
693+forall n rig impl ty ctxt env _ = fail "Can't pi bind here"
694694
695695patvar :: Name -> RunTactic
696696patvar n ctxt env (Bind x (Hole t) sc) =
@@ -1149,7 +1149,7 @@ process t h = tactic (Just h) (mktac t)
11491149 mktac WHNF_ComputeArgs = whnf_compute_args
11501150 mktac (Intro n) = intro n
11511151 mktac (IntroTy ty n) = introTy ty n
1152− mktac (Forall n i t) = forall n i t
1152+ mktac (Forall n r i t) = forall n r i t
11531153 mktac (LetBind n t v) = letbind n t v
11541154 mktac (ExpandLet n b) = expandLet n b
11551155 mktac (Rewrite t) = rewrite t
src/Idris/Elab/Term.hs+14−3
@@ -705,7 +705,13 @@ elab ist info emode opts fn tm
705705 solve
706706 highlightSource nfc (AnnBoundName n False)
707707 elab' ina fc (PPi p n nfc Placeholder sc)
708− = do attack; arg n (is_scoped p) (sMN 0 "phTy")
708+ = do attack;
709+ case pcount p of
710+ RigW -> return ()
711+ _ -> unless (LinearTypes `elem` idris_language_extensions ist
712+ || e_qq ina) $
713+ lift $ tfail $ At nfc (Msg "You must turn on the LinearTypes extension to use a linear argument")
714+ arg n (pcount p) (is_scoped p) (sMN 0 "phTy")
709715 addAutoBind p n
710716 addPSname n -- okay for proof search
711717 elabE (ina { e_inarg = True, e_intype = True }) fc sc
@@ -717,7 +723,12 @@ elab ist info emode opts fn tm
717723 n' <- case n of
718724 MN _ _ -> unique_hole n
719725 _ -> return n
720− forall n' (is_scoped p) (Var tyn)
726+ case pcount p of
727+ RigW -> return ()
728+ _ -> unless (LinearTypes `elem` idris_language_extensions ist
729+ || e_qq ina) $
730+ lift $ tfail $ At nfc (Msg "You must turn on the LinearTypes extension to use a linear argument")
731+ forall n' (pcount p) (is_scoped p) (Var tyn)
721732 addAutoBind p n'
722733 addPSname n' -- okay for proof search
723734 focus tyn
@@ -2250,7 +2261,7 @@ runElabAction info ist fc env tm ns = do tm' <- eval tm
22502261 = do ~[n, ty] <- tacTmArgs 2 tac args
22512262 n' <- reifyTTName n
22522263 ty' <- reifyRaw ty
2253− forall n' Nothing ty'
2264+ forall n' RigW Nothing ty'
22542265 returnUnit
22552266 | n == tacN "Prim__PatVar"
22562267 = do ~[n] <- tacTmArgs 1 tac args
src/Idris/Parser.hs+5−4
@@ -763,7 +763,7 @@ Params ::=
763763params :: SyntaxInfo -> IdrisParser [PDecl]
764764params syn =
765765 do reservedHL "parameters"; lchar '('; ns <- typeDeclList syn; lchar ')'
766− let ns' = [(n, ty) | (n, _, ty) <- ns]
766+ let ns' = [(n, ty) | (_, n, _, ty) <- ns]
767767 openBlock
768768 let pvars = syn_params syn
769769 ds <- many (decl syn { syn_params = pvars ++ ns' })
@@ -903,7 +903,7 @@ interface_ syn = do (doc, argDocs, acc)
903903 return (doc, argDocs, acc))
904904 fc <- getFC
905905 cons <- constraintList syn
906− let cons' = [(c, ty) | (c, _, ty) <- cons]
906+ let cons' = [(c, ty) | (_, c, _, ty) <- cons]
907907 (n_in, nfc) <- fnName
908908 let n = expandNS syn n_in
909909 cs <- many carg
@@ -953,11 +953,11 @@ implementation kwopt syn
953953 fc <- getFC
954954 en <- optional implementationName
955955 cs <- constraintList syn
956− let cs' = [(c, ty) | (c, _, ty) <- cs]
956+ let cs' = [(c, ty) | (_, c, _, ty) <- cs]
957957 (cn, cnfc) <- fnName
958958 args <- many (simpleExpr syn)
959959 let sc = PApp fc (PRef cnfc [cnfc] cn) (map pexp args)
960− let t = bindList (PPi constraint) cs sc
960+ let t = bindList (\r -> PPi constraint { pcount = r }) cs sc
961961 pnames <- implementationUsing
962962 ds <- implementationBlock syn
963963 return [PImplementation doc argDocs syn fc cs' pnames acc opts cn cnfc args [] t en ds]
@@ -1430,6 +1430,7 @@ pLangExt :: IdrisParser LanguageExt
14301430pLangExt = (reserved "TypeProviders" >> return TypeProviders)
14311431 <|> (reserved "ErrorReflection" >> return ErrorReflection)
14321432 <|> (reserved "UniquenessTypes" >> return UniquenessTypes)
1433+ <|> (reserved "LinearTypes" >> return LinearTypes)
14331434 <|> (reserved "DSLNotation" >> return DSLNotation)
14341435 <|> (reserved "ElabReflection" >> return ElabReflection)
14351436 <|> (reserved "FirstClassReflection" >> return FCReflection)
src/Idris/Parser/Expr.hs+23−21
@@ -914,7 +914,7 @@ TypeExpr ::= ConstraintList? Expr;
914914typeExpr :: SyntaxInfo -> IdrisParser PTerm
915915typeExpr syn = do cs <- if implicitAllowed syn then constraintList syn else return []
916916 sc <- expr (allowConstr syn)
917− return (bindList (PPi constraint) cs sc)
917+ return (bindList (\r -> PPi (constraint { pcount = r })) cs sc)
918918 <?> "type signature"
919919
920920{- | Parses a lambda expression
@@ -941,7 +941,7 @@ lambda syn = do lchar '\\' <?> "lambda expression"
941941 ((do xt <- try $ tyOptDeclList (disallowImp syn)
942942 fc <- getFC
943943 sc <- lambdaTail
944− return (bindList (PLam fc) xt sc))
944+ return (bindList (\r -> PLam fc) xt sc))
945945 <|>
946946 (do ps <- sepBy (do fc <- getFC
947947 e <- simpleExpr (disallowImp (syn { inPattern = True }))
@@ -1078,7 +1078,7 @@ explicitPi opts st syn
10781078 = do xt <- try (lchar '(' *> typeDeclList syn <* lchar ')')
10791079 binder <- bindsymbol opts st syn
10801080 sc <- expr (allowConstr syn)
1081− return (bindList (PPi binder) xt sc)
1081+ return (bindList (\r -> PPi (binder { pcount = r })) xt sc)
10821082
10831083autoImplicit opts st syn
10841084 = do kw <- reservedFC "auto"
@@ -1088,8 +1088,8 @@ autoImplicit opts st syn
10881088 symbol "->"
10891089 sc <- expr (allowConstr syn)
10901090 highlightP kw AnnKeyword
1091− return (bindList (PPi
1092− (TacImp [] Dynamic (PTactics [ProofSearch True True 100 Nothing [] []]) RigW)) xt sc)
1091+ return (bindList (\r -> PPi
1092+ (TacImp [] Dynamic (PTactics [ProofSearch True True 100 Nothing [] []]) r)) xt sc)
10931093
10941094defaultImplicit opts st syn = do
10951095 kw <- reservedFC "default"
@@ -1102,7 +1102,7 @@ defaultImplicit opts st syn = do
11021102 symbol "->"
11031103 sc <- expr (allowConstr syn)
11041104 highlightP kw AnnKeyword
1105− return (bindList (PPi (TacImp [] Dynamic script RigW)) xt sc)
1105+ return (bindList (\r -> PPi (TacImp [] Dynamic script r)) xt sc)
11061106
11071107normalImplicit opts st syn = do
11081108 xt <- typeDeclList syn <* lchar '}'
@@ -1115,15 +1115,15 @@ normalImplicit opts st syn = do
11151115 constraint)
11161116 else (Imp opts st False (Just (Impl False False False)) True RigW,
11171117 Imp opts st False (Just (Impl True False False)) True RigW)
1118− return (bindList (PPi im) xt
1119− (bindList (PPi cl) cs sc))
1118+ return (bindList (\r -> PPi (im { pcount = r })) xt
1119+ (bindList (\r -> PPi (cl { pcount = r })) cs sc))
11201120
11211121constraintPi opts st syn =
11221122 do cs <- constraintList1 syn
11231123 sc <- expr syn
11241124 if implicitAllowed syn
1125− then return (bindList (PPi constraint) cs sc)
1126− else return (bindList (PPi (Imp opts st False (Just (Impl True False False)) True RigW))
1125+ then return (bindList (\r -> PPi constraint { pcount = r }) cs sc)
1126+ else return (bindList (\r -> PPi (Imp opts st False (Just (Impl True False False)) True r))
11271127 cs sc)
11281128
11291129implicitPi opts st syn =
@@ -1181,11 +1181,11 @@ ConstraintList ::=
11811181 ;
11821182@
11831183-}
1184−constraintList :: SyntaxInfo -> IdrisParser [(Name, FC, PTerm)]
1184+constraintList :: SyntaxInfo -> IdrisParser [(RigCount, Name, FC, PTerm)]
11851185constraintList syn = try (constraintList1 syn)
11861186 <|> return []
11871187
1188−constraintList1 :: SyntaxInfo -> IdrisParser [(Name, FC, PTerm)]
1188+constraintList1 :: SyntaxInfo -> IdrisParser [(RigCount, Name, FC, PTerm)]
11891189constraintList1 syn = try (do lchar '('
11901190 tys <- sepBy1 nexpr (lchar ',')
11911191 lchar ')'
@@ -1193,13 +1193,13 @@ constraintList1 syn = try (do lchar '('
11931193 return tys)
11941194 <|> try (do t <- opExpr (disallowImp syn)
11951195 reservedOp "=>"
1196− return [(defname, NoFC, t)])
1196+ return [(RigW, defname, NoFC, t)])
11971197 <?> "type constraint list"
11981198 where nexpr = try (do (n, fc) <- name; lchar ':'
11991199 e <- expr (disallowImp syn)
1200− return (n, fc, e))
1200+ return (RigW, n, fc, e))
12011201 <|> do e <- expr (disallowImp syn)
1202− return (defname, NoFC, e)
1202+ return (RigW, defname, NoFC, e)
12031203 defname = sMN 0 "constraint"
12041204
12051205{- | Parses a type declaration list
@@ -1217,16 +1217,18 @@ FunctionSignatureList ::=
12171217 ;
12181218@
12191219-}
1220−typeDeclList :: SyntaxInfo -> IdrisParser [(Name, FC, PTerm)]
1221−typeDeclList syn = try (sepBy1 (do (x, xfc) <- fnName
1220+typeDeclList :: SyntaxInfo -> IdrisParser [(RigCount, Name, FC, PTerm)]
1221+typeDeclList syn = try (sepBy1 (do rig <- option RigW (do lchar '1'
1222+ return Rig1)
1223+ (x, xfc) <- fnName
12221224 lchar ':'
12231225 t <- typeExpr (disallowImp syn)
1224− return (x, xfc, t))
1226+ return (rig, x, xfc, t))
12251227 (lchar ','))
12261228 <|> do ns <- sepBy1 name (lchar ',')
12271229 lchar ':'
12281230 t <- typeExpr (disallowImp syn)
1229− return (map (\(x, xfc) -> (x, xfc, t)) ns)
1231+ return (map (\(x, xfc) -> (RigW, x, xfc, t)) ns)
12301232 <?> "type declaration list"
12311233
12321234{- | Parses a type declaration list with optional parameters
@@ -1241,11 +1243,11 @@ TypeOptDeclList ::=
12411243NameOrPlaceHolder ::= Name | '_';
12421244@
12431245-}
1244−tyOptDeclList :: SyntaxInfo -> IdrisParser [(Name, FC, PTerm)]
1246+tyOptDeclList :: SyntaxInfo -> IdrisParser [(RigCount, Name, FC, PTerm)]
12451247tyOptDeclList syn = sepBy1 (do (x, fc) <- nameOrPlaceholder
12461248 t <- option Placeholder (do lchar ':'
12471249 expr syn)
1248− return (x, fc, t))
1250+ return (RigW, x, fc, t))
12491251 (lchar ',')
12501252 <?> "type declaration list"
12511253 where nameOrPlaceholder :: IdrisParser (Name, FC)
src/Idris/Parser/Helpers.hs+3−3
@@ -455,9 +455,9 @@ getFC = do s <- position
455455
456456{-* Syntax helpers-}
457457-- | Bind constraints to term
458−bindList :: (Name -> FC -> PTerm -> PTerm -> PTerm) -> [(Name, FC, PTerm)] -> PTerm -> PTerm
459−bindList b [] sc = sc
460−bindList b ((n, fc, t):bs) sc = b n fc t (bindList b bs sc)
458+bindList :: (RigCount -> Name -> FC -> PTerm -> PTerm -> PTerm) -> [(RigCount, Name, FC, PTerm)] -> PTerm -> PTerm
459+bindList b [] sc = sc
460+bindList b ((r, n, fc, t):bs) sc = b r n fc t (bindList b bs sc)
461461
462462{- | @commaSeparated p@ parses one or more occurences of `p`,
463463 separated by commas and optional whitespace. -}