on master help sign in
Merge branch 'master' of github.com:edwinb/Idris-dev
commit69f73f8be4c85233c2d54aa52ea8c9ebe8c1cab0 authorTimo Petteri Sinnemäki <timo.sinnemaki@helsinki.fi> dateMon 20 Jan 2014, 02:08 (13 years ago) parent9150e950877929
15 files changed+159−56
idris.cabal+2−2 libs/prelude/Prelude.idr+3−3 src/IRTS/CodegenC.hs+18−1 src/IRTS/CodegenJavaScript.hs+69−10 src/Idris/AbsSyntax.hs+6−6 src/Idris/AbsSyntaxTree.hs+5−5 src/Idris/Core/CaseTree.hs+1 src/Idris/DataOpts.hs+6−3 src/Idris/ElabDecls.hs+24−15 src/Idris/ParseData.hs+13−9 src/Idris/ParseExpr.hs+1−1 src/Idris/UnusedArgs.hs+2−1 test/reg031/expected+1 test/reg031/reg031.idr+4 test/reg031/run+4
idris.cabal+2−2
@@ -14,7 +14,7 @@ Description: Idris is a general purpose language with full dependent types.
1414 Dependent types allow types to be predicated on values,
1515 meaning that some aspects of a program's behaviour can be
1616 specified precisely in the type. The language is closely
17− related to Epigram and Agda. There is a tutorial at
17+ related to Epigram and Agda. There is a tutorial at
1818 <http://www.idris-lang.org/documentation>.
1919 Features include:
2020 .
@@ -462,7 +462,7 @@ Library
462462 if os(freebsd)
463463 cpp-options: -DFREEBSD
464464 build-depends: unix
465− if os(dragonfly)
465+ if os(dragonfly)
466466 cpp-options: -DDRAGONFLY
467467 build-depends: unix
468468 if os(darwin)
libs/prelude/Prelude.idr+3−3
@@ -334,7 +334,7 @@ instance Traversable (Vect n) where
334334 traverse f (x::xs) = [| Vect.(::) (f x) (traverse f xs) |]
335335
336336---- some mathematical operations
337−---- XXX this should probably go some place else,
337+---- XXX this should probably go some place else,
338338pow : (Num a) => a -> Nat -> a
339339pow x Z = 1
340340pow x (S n) = x * (pow x n)
@@ -376,7 +376,7 @@ instance Enum Integer where
376376 enumFromThen n inc = n :: enumFromThen (inc + n) inc
377377 enumFromTo n m = if n <= m
378378 then go (natRange (S (cast {to = Nat} (m - n))))
379− else []
379+ else []
380380 where go : List Nat -> List Integer
381381 go [] = []
382382 go (x :: xs) = n + cast x :: go xs
@@ -392,7 +392,7 @@ instance Enum Int where
392392 toNat n = cast n
393393 fromNat n = cast n
394394 enumFromThen n inc = n :: enumFromThen (inc + n) inc
395− enumFromTo n m = if n <= m
395+ enumFromTo n m = if n <= m
396396 then go (natRange (S (cast {to = Nat} (m - n))))
397397 else []
398398 where go : List Nat -> List Int
src/IRTS/CodegenC.hs+18−1
@@ -10,6 +10,7 @@ import Idris.Core.TT
1010import Paths_idris
1111import Util.System
1212
13+import Numeric
1314import Data.Char
1415import Data.List (intercalate)
1516import System.Process
@@ -104,6 +105,22 @@ toC f code
104105 indent 1 ++ "INITFRAME;\n" ++
105106 concatMap (bcc 1) code ++ "}\n\n"
106107
108+showCStr :: String -> String
109+showCStr s = '"' : foldr ((++) . showChar) "\"" s
110+ where
111+ showChar :: Char -> String
112+ showChar '"' = "\\\""
113+ showChar '\\' = "\\\\"
114+ showChar c
115+ -- Note: we need the double quotes around the codes because otherwise
116+ -- "\n3" would get encoded as "\x0a3", which is incorrect.
117+ -- Instead, we opt for "\x0a""3" and let the C compiler deal with it.
118+ | ord c < 0x10 = "\"\"\\x0" ++ showHex (ord c) "\"\""
119+ | ord c < 0x20 = "\"\"\\x" ++ showHex (ord c) "\"\""
120+ | ord c < 0x7f = [c] -- 0x7f = \DEL
121+ | ord c < 0x100 = "\"\"\\x" ++ showHex (ord c) "\"\""
122+ | otherwise = error $ "non-8-bit character in string literal: " ++ show c
123+
107124bcc :: Int -> BC -> String
108125bcc i (ASSIGN l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
109126bcc i (ASSIGNCONST l c)
@@ -114,7 +131,7 @@ bcc i (ASSIGNCONST l c)
114131 | otherwise = "MKBIGC(vm,\"" ++ show i ++ "\")"
115132 mkConst (Fl f) = "MKFLOAT(vm, " ++ show f ++ ")"
116133 mkConst (Ch c) = "MKINT(" ++ show (fromEnum c) ++ ")"
117− mkConst (Str s) = "MKSTR(vm, " ++ show s ++ ")"
134+ mkConst (Str s) = "MKSTR(vm, " ++ showCStr s ++ ")"
118135 mkConst (B8 x) = "idris_b8const(vm, " ++ show x ++ ")"
119136 mkConst (B16 x) = "idris_b16const(vm, " ++ show x ++ ")"
120137 mkConst (B32 x) = "idris_b32const(vm, " ++ show x ++ ")"
src/IRTS/CodegenJavaScript.hs+69−10
@@ -64,6 +64,7 @@ data JS = JSRaw String
6464 | JSArray [JS]
6565 | JSObject [(String, JS)]
6666 | JSString String
67+ | JSChar String
6768 | JSNum JSNum
6869 | JSAssign JS JS
6970 | JSAlloc String (Maybe JS)
@@ -153,6 +154,9 @@ compileJS (JSObject fields) =
153154compileJS (JSString str) =
154155 show str
155156
157+compileJS (JSChar chr) =
158+ chr
159+
156160compileJS (JSNum num)
157161 | JSInt i <- num = show i
158162 | JSFloat f <- num = show f
@@ -170,8 +174,16 @@ compileJS (JSIndex lhs rhs) =
170174 compileJS lhs ++ "[" ++ compileJS rhs ++ "]"
171175
172176compileJS (JSCond branches) =
173− intercalate " else " $ map createIfBlock branches
177+ intercalate " else " $ map createIfBlock (eliminateDeadBranches branches)
174178 where
179+ eliminateDeadBranches (e@(JSTrue, _):_) = [e]
180+ eliminateDeadBranches (x:xs) = x : eliminateDeadBranches xs
181+ eliminateDeadBranches [] = []
182+
183+ createIfBlock (JSTrue, e) =
184+ "{\n"
185+ ++ "return " ++ compileJS e
186+ ++ ";\n}"
175187 createIfBlock (cond, e) =
176188 "if (" ++ compileJS cond ++") {\n"
177189 ++ "return " ++ compileJS e
@@ -233,6 +245,7 @@ jsLet name value body =
233245 )
234246 ) [value]
235247
248+
236249jsSubst :: String -> JS -> JS -> JS
237250jsSubst var new (JSVar old)
238251 | var == translateVariableName old = new
@@ -260,6 +273,16 @@ jsSubst var new (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
260273 JSReturn $ JSApp (jsSubst var new fun) (map (jsSubst var new) args)
261274 )]
262275
276+jsSubst var new (JSApp (JSProj (JSFunction args body) "apply") vals)
277+ | var `notElem` args =
278+ JSApp (JSProj (JSFunction args (jsSubst var new body)) "apply") (
279+ map (jsSubst var new) vals
280+ )
281+ | otherwise =
282+ JSApp (JSProj (JSFunction args body) "apply") (
283+ map (jsSubst var new) vals
284+ )
285+
263286jsSubst var new (JSApp (JSProj obj field) args) =
264287 JSApp (JSProj (jsSubst var new obj) field) $ map (jsSubst var new) args
265288
@@ -293,7 +316,38 @@ jsSubst var new (JSCond conds) =
293316
294317jsSubst _ _ js = js
295318
319+
320+isJSConstant :: JS -> Bool
321+isJSConstant js
322+ | JSString _ <- js = True
323+ | JSChar _ <- js = True
324+ | JSNum _ <- js = True
325+ | JSType _ <- js = True
326+
327+ | JSApp (JSIdent "__IDRRT__bigInt") _ <- js = True
328+ | otherwise = False
329+
296330inlineJS :: JS -> JS
331+inlineJS (JSApp (JSProj (JSFunction args (JSReturn body)) "apply") [
332+ JSThis,JSProj (JSIdent var) "vars"
333+ ])
334+ | var /= "cse" =
335+ inlineApply args body 0
336+ where
337+ inlineApply [] body _ = body
338+ inlineApply (a:as) body n =
339+ inlineApply as (
340+ jsSubst a (JSIndex (JSProj (JSIdent var) "vars") (JSNum (JSInt n))) body
341+ ) (n + 1)
342+
343+inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
344+ | isJSConstant val = val
345+
346+inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
347+ JSFunction [] (JSReturn val)
348+ ])
349+ | isJSConstant val = val
350+
297351inlineJS (JSApp (JSFunction [] (JSSeq ret)) []) =
298352 JSApp (JSFunction [] (JSSeq (map inlineJS ret))) []
299353
@@ -365,6 +419,9 @@ inlineJS (JSCond cases) =
365419inlineJS (JSObject fields) =
366420 JSObject (map (second inlineJS) fields)
367421
422+inlineJS (JSOp op lhs rhs) =
423+ JSOp op (inlineJS lhs) (inlineJS rhs)
424+
368425inlineJS js = js
369426
370427reduceJS :: [JS] -> [JS]
@@ -536,6 +593,12 @@ reduceLoop reduced (cons, program) =
536593 reduceCall funs (JSProj obj field) =
537594 JSProj (reduceCall funs obj) field
538595
596+ reduceCall funs (JSIndex obj idx) =
597+ JSIndex (reduceCall funs obj) (reduceCall funs idx)
598+
599+ reduceCall funs (JSOp op rhs lhs) =
600+ JSOp op (reduceCall funs rhs) (reduceCall funs lhs)
601+
539602 reduceCall funs (JSCond conds) =
540603 JSCond $ map (reduceCall funs *** reduceCall funs) conds
541604
@@ -699,6 +762,9 @@ translateSpecialName name
699762translateConstant :: Const -> JS
700763translateConstant (I i) = JSNum (JSInt i)
701764translateConstant (Fl f) = JSNum (JSFloat f)
765+translateConstant (Ch '\DEL') = JSChar "'\\u007F'"
766+translateConstant (Ch '\a') = JSChar "'\\u0007'"
767+translateConstant (Ch '\SO') = JSChar "'\\u000E'"
702768translateConstant (Ch c) = JSString [c]
703769translateConstant (Str s) = JSString s
704770translateConstant (AType (ATInt ITNative)) = JSType JSIntTy
@@ -800,23 +866,16 @@ translateDeclaration (path, SFun name params stackSize body)
800866 JSFunction (p ++ map snd aux) (
801867 JSSeq $
802868 zipWith assignVar [0..] p ++
803− map allocVar [numP .. (numP + stackSize - 1)] ++
804869 map assignAux aux ++
805870 [JSReturn body]
806871 )
807872 where
808− numP :: Int
809− numP = length params
810−
811− allocVar :: Int -> JS
812− allocVar n = JSAlloc (jsVar n) Nothing
813−
814873 assignVar :: Int -> String -> JS
815874 assignVar n s = JSAlloc (jsVar n) (Just $ JSIdent s)
816875
817876 assignAux :: (LVar, String) -> JS
818− assignAux (var, val) =
819− JSAssign (JSIdent $ translateVariableName var) (JSIdent val)
877+ assignAux (Loc var, val) =
878+ JSAlloc (jsVar var) (Just $ JSIdent val)
820879
821880 p :: [String]
822881 p = map translateName params
src/Idris/AbsSyntax.hs+6−6
@@ -698,7 +698,7 @@ inferDecl = PDatadecl inferTy
698698 PType
699699 [("", inferCon, PPi impl (sMN 0 "iType") PType (
700700 PPi expl (sMN 0 "ival") (PRef bi (sMN 0 "iType"))
701− (PRef bi inferTy)), bi)]
701+ (PRef bi inferTy)), bi, [])]
702702inferOpts = []
703703
704704infTerm t = PApp bi (PRef bi inferCon) [pimp (sMN 0 "iType") Placeholder True, pexp t]
@@ -721,7 +721,7 @@ primNames = [unitTy, unitCon,
721721unitTy = sMN 0 "__Unit"
722722unitCon = sMN 0 "__II"
723723unitDecl = PDatadecl unitTy PType
724− [("", unitCon, PRef bi unitTy, bi)]
724+ [("", unitCon, PRef bi unitTy, bi, [])]
725725unitOpts = [DefaultEliminator]
726726
727727falseTy = sMN 0 "__False"
@@ -736,7 +736,7 @@ pairDecl = PDatadecl pairTy (piBind [(n "A", PType), (n "B", PType)] PType)
736736 PPi expl (n "a") (PRef bi (n "A")) (
737737 PPi expl (n "b") (PRef bi (n "B"))
738738 (PApp bi (PRef bi pairTy) [pexp (PRef bi (n "A")),
739− pexp (PRef bi (n "B"))])))), bi)]
739+ pexp (PRef bi (n "B"))])))), bi, [])]
740740 where n a = sMN 0 a
741741pairOpts = []
742742
@@ -750,7 +750,7 @@ eqDecl = PDatadecl eqTy (piBind [(n "A", PType), (n "B", PType),
750750 (PApp bi (PRef bi eqTy) [pimp (n "A") Placeholder False,
751751 pimp (n "B") Placeholder False,
752752 pexp (PRef bi (n "x")),
753− pexp (PRef bi (n "x"))])), bi)]
753+ pexp (PRef bi (n "x"))])), bi, [])]
754754 where n a = sMN 0 a
755755eqOpts = []
756756
@@ -921,8 +921,8 @@ expandParamsD rhs ist dec ps ns (PData doc syn fc co pd)
921921 then PDatadecl (dec n) (piBind ps (expandParams dec ps ns [] ty))
922922 (map econ cons)
923923 else PDatadecl n (expandParams dec ps ns [] ty) (map econ cons)
924− econ (doc, n, t, fc)
925− = (doc, dec n, piBindp expl ps (expandParams dec ps ns [] t), fc)
924+ econ (doc, n, t, fc, fs)
925+ = (doc, dec n, piBindp expl ps (expandParams dec ps ns [] t), fc, fs)
926926expandParamsD rhs ist dec ps ns (PParams f params pds)
927927 = PParams f (ps ++ map (mapsnd (expandParams dec ps ns [])) params)
928928 (map (expandParamsD True ist dec ps ns) pds)
src/Idris/AbsSyntaxTree.hs+5−5
@@ -478,7 +478,7 @@ deriving instance NFData PClause'
478478-- | Data declaration
479479data PData' t = PDatadecl { d_name :: Name, -- ^ The name of the datatype
480480 d_tcon :: t, -- ^ Type constructor
481− d_cons :: [(String, Name, t, FC)] -- ^ Constructors
481+ d_cons :: [(String, Name, t, FC, [Name])] -- ^ Constructors
482482 }
483483 -- ^ Data declaration
484484 | PLaterdecl { d_name :: Name, d_tcon :: t }
@@ -505,7 +505,7 @@ declared (PPostulate _ _ _ _ n t) = [n]
505505declared (PClauses _ _ n _) = [] -- not a declaration
506506declared (PCAF _ n _) = [n]
507507declared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
508− where fstt (_, a, _, _) = a
508+ where fstt (_, a, _, _, _) = a
509509declared (PData _ _ _ _ (PLaterdecl n _)) = [n]
510510declared (PParams _ _ ds) = concatMap declared ds
511511declared (PNamespace _ ds) = concatMap declared ds
@@ -525,7 +525,7 @@ tldeclared (PPostulate _ _ _ _ n t) = [n]
525525tldeclared (PClauses _ _ n _) = [] -- not a declaration
526526tldeclared (PRecord _ _ _ n _ _ c _) = [n, c]
527527tldeclared (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
528− where fstt (_, a, _, _) = a
528+ where fstt (_, a, _, _, _) = a
529529tldeclared (PParams _ _ ds) = []
530530tldeclared (PMutual _ ds) = concatMap tldeclared ds
531531tldeclared (PNamespace _ ds) = concatMap tldeclared ds
@@ -540,7 +540,7 @@ defined (PPostulate _ _ _ _ n t) = []
540540defined (PClauses _ _ n _) = [n] -- not a declaration
541541defined (PCAF _ n _) = [n]
542542defined (PData _ _ _ _ (PDatadecl n _ ts)) = n : map fstt ts
543− where fstt (_, a, _, _) = a
543+ where fstt (_, a, _, _, _) = a
544544defined (PData _ _ _ _ (PLaterdecl n _)) = []
545545defined (PParams _ _ ds) = concatMap defined ds
546546defined (PNamespace _ ds) = concatMap defined ds
@@ -945,7 +945,7 @@ showDImp :: Bool -> PData -> String
945945showDImp impl (PDatadecl n ty cons)
946946 = "data " ++ show n ++ " : " ++ showImp Nothing impl False ty ++ " where\n\t"
947947 ++ showSep "\n\t| "
948− (map (\ (_, n, t, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
948+ (map (\ (_, n, t, _, _) -> show n ++ " : " ++ showImp Nothing impl False t) cons)
949949
950950getImps :: [PArg] -> [(Name, PTerm)]
951951getImps [] = []
src/Idris/Core/CaseTree.hs+1
@@ -157,6 +157,7 @@ directUse (Bind n (Let t v) sc) = nub $ directUse v ++ (directUse sc \\ [n])
157157directUse (Bind n b sc) = nub $ directUse (binderTy b) ++ (directUse sc \\ [n])
158158directUse fn@(App f a)
159159 | (P Ref n _, args) <- unApply fn = [] -- need to know what n does with them
160+ | (P (TCon _ _) n _, args) <- unApply fn = [] -- type constructors not used at runtime
160161 | otherwise = nub $ directUse f ++ directUse a
161162directUse (Proj x i) = nub $ directUse x
162163directUse _ = []
src/Idris/DataOpts.hs+6−3
@@ -18,14 +18,16 @@ type ForceMap = M.IntMap Forceability
1818
1919-- Calculate the forceable arguments to a constructor
2020-- and update the set of optimisations.
21−forceArgs :: Name -> Name -> Type -> Idris ()
22−forceArgs typeName n t = do
21+forceArgs :: Name -> Name -> [Int] -> Type -> Idris ()
22+forceArgs typeName n expforce t = do
2323 ist <- getIState
2424 let fargs = getForcedArgs ist typeName t
2525 copt = case lookupCtxt n (idris_optimisation ist) of
2626 [] -> Optimise False False [] []
2727 op:_ -> op
28− opts = addDef n (copt { forceable = M.toList fargs }) (idris_optimisation ist)
28+ opts = addDef n (copt { forceable = M.toList fargs ++
29+ zip expforce (repeat Unconditional) })
30+ (idris_optimisation ist)
2931 putIState (ist { idris_optimisation = opts })
3032 addIBC (IBCOpt n)
3133 iLOG $ "Forced: " ++ show n ++ " " ++ show fargs ++ "\n from " ++ show t
@@ -319,3 +321,4 @@ applyDataOptRT oi n tag arity args
319321 where
320322 ctor' = (P (DCon tag (arity - forcedCnt forceMap)) n Erased)
321323 args' = [t | (f, t) <- zip (forcedArgSeq oi) args, f /= Just Unconditional]
324+
src/Idris/ElabDecls.hs+24−15
@@ -304,7 +304,8 @@ elabData info syn doc fc opts (PDatadecl n t_in dcons)
304304type EliminatorState = StateT (Map.Map String Int) Idris
305305
306306-- TODO: Use uniqueName for generating names, rewrite everything to use idris_implicits instead of manual splitting, generally just rewrite
307−elabEliminator :: [Int] -> Name -> PTerm -> [(String, Name, PTerm, FC)] -> ElabInfo -> EliminatorState ()
307+elabEliminator :: [Int] -> Name -> PTerm -> [(String, Name, PTerm, FC, [Name])] ->
308+ ElabInfo -> EliminatorState ()
308309elabEliminator paramPos n ty cons info = do
309310 elimLog $ "Elaborating eliminator"
310311 let (cnstrs, _) = splitPi ty
@@ -312,7 +313,7 @@ elabEliminator paramPos n ty cons info = do
312313 generalParams <- namePis False pms
313314 motiveIdxs <- namePis False idxs
314315 let motive = mkMotive n paramPos generalParams motiveIdxs
315− consTerms <- mapM (\(c@(_,cnm,_,_)) -> do
316+ consTerms <- mapM (\(c@(_,cnm,_,_,_)) -> do
316317 name <- freshName $ "elim_" ++ simpleName cnm
317318 consTerm <- extractConsTerm c generalParams
318319 return (name, expl, consTerm)) cons
@@ -469,8 +470,8 @@ elabEliminator paramPos n ty cons info = do
469470 splitArgPms _ = ([],[])
470471
471472
472− implicitIndexes :: (String, Name, PTerm, FC) -> EliminatorState [(Name, Plicity, PTerm)]
473− implicitIndexes (cns@(doc, cnm, ty, fc)) = do
473+ implicitIndexes :: (String, Name, PTerm, FC, [Name]) -> EliminatorState [(Name, Plicity, PTerm)]
474+ implicitIndexes (cns@(doc, cnm, ty, fc, fs)) = do
474475 i <- State.lift getIState
475476 implargs' <- case lookupCtxt cnm (idris_implicits i) of
476477 [] -> do fail $ "Error while showing implicits for " ++ show cnm
@@ -484,11 +485,11 @@ elabEliminator paramPos n ty cons info = do
484485 in return $ filter (\(n,_,_) -> not (n `elem` oldParams))implargs
485486 _ -> return implargs
486487
487− extractConsTerm :: (String, Name, PTerm, FC) -> [(Name, Plicity, PTerm)] -> EliminatorState PTerm
488− extractConsTerm (doc, cnm, ty, fc) generalParameters = do
488+ extractConsTerm :: (String, Name, PTerm, FC, [Name]) -> [(Name, Plicity, PTerm)] -> EliminatorState PTerm
489+ extractConsTerm (doc, cnm, ty, fc, fs) generalParameters = do
489490 let cons' = replaceParams paramPos generalParameters ty
490491 let (args, resTy) = splitPi cons'
491− implidxs <- implicitIndexes (doc, cnm, ty, fc)
492+ implidxs <- implicitIndexes (doc, cnm, ty, fc, fs)
492493 consArgs <- namePis True args
493494 let recArgs = findRecArgs consArgs
494495 let recMotives = map applyRecMotive recArgs
@@ -520,12 +521,12 @@ elabEliminator paramPos n ty cons info = do
520521 convertImplPi (PImp {getTm = t, pname = n}) = Just (n, expl, t)
521522 convertImplPi _ = Nothing
522523
523− generateEliminatorClauses :: (String, Name, PTerm, FC) -> Name -> [PArg] -> [(Name, Plicity, PTerm)] -> EliminatorState PClause
524− generateEliminatorClauses (doc, cnm, ty, fc) cnsElim generalArgs generalParameters = do
524+ generateEliminatorClauses :: (String, Name, PTerm, FC, [Name]) -> Name -> [PArg] -> [(Name, Plicity, PTerm)] -> EliminatorState PClause
525+ generateEliminatorClauses (doc, cnm, ty, fc, fs) cnsElim generalArgs generalParameters = do
525526 let cons' = replaceParams paramPos generalParameters ty
526527 let (args, resTy) = splitPi cons'
527528 i <- State.lift getIState
528− implidxs <- implicitIndexes (doc, cnm, ty, fc)
529+ implidxs <- implicitIndexes (doc, cnm, ty, fc, fs)
529530 let (_, generalIdxs') = splitArgPms resTy
530531 let generalIdxs = map pexp generalIdxs'
531532 consArgs <- namePis True args
@@ -698,7 +699,7 @@ elabTransform info fc safe lhs_in rhs_in
698699elabRecord :: ElabInfo -> SyntaxInfo -> String -> FC -> Name ->
699700 PTerm -> String -> Name -> PTerm -> Idris ()
700701elabRecord info syn doc fc tyn ty cdoc cn cty
701− = do elabData info syn doc fc [] (PDatadecl tyn ty [(cdoc, cn, cty, fc)])
702+ = do elabData info syn doc fc [] (PDatadecl tyn ty [(cdoc, cn, cty, fc, [])])
702703 cty' <- implicit syn cn cty
703704 i <- getIState
704705 cty <- case lookupTy cn (tt_ctxt i) of
@@ -805,8 +806,8 @@ elabRecord info syn doc fc tyn ty cdoc cn cty
805806 return (pn, pfnTy, PClauses fc [] setname [pclause])
806807
807808elabCon :: ElabInfo -> SyntaxInfo -> Name -> Bool ->
808− (String, Name, PTerm, FC) -> Idris (Name, Type)
809−elabCon info syn tn codata (doc, n, t_in, fc)
809+ (String, Name, PTerm, FC, [Name]) -> Idris (Name, Type)
810+elabCon info syn tn codata (doc, n, t_in, fc, forcenames)
810811 = do checkUndefined fc n
811812 ctxt <- getContext
812813 i <- getIState
@@ -829,7 +830,10 @@ elabCon info syn tn codata (doc, n, t_in, fc)
829830 addIBC (IBCDef n)
830831 addDocStr n doc
831832 addIBC (IBCDoc n)
832− forceArgs tn n cty'
833+ let fs = map (getNamePos 0 t) forcenames
834+ -- FIXME: 'forcenames' is an almighty hack! Need a better way of
835+ -- erasing non-forceable things
836+ forceArgs tn n (mapMaybe (getNamePos 0 t) forcenames) cty'
833837 return (n, cty')
834838 where
835839 tyIs (Bind n b sc) = tyIs sc
@@ -841,6 +845,11 @@ elabCon info syn tn codata (doc, n, t_in, fc)
841845 mkLazy (PPi pl n ty sc) = PPi (pl { plazy = True }) n ty (mkLazy sc)
842846 mkLazy t = t
843847
848+ getNamePos :: Int -> PTerm -> Name -> Maybe Int
849+ getNamePos i (PPi _ n _ sc) x | n == x = Just i
850+ | otherwise = getNamePos (i + 1) sc x
851+ getNamePos _ _ _ = Nothing
852+
844853-- | Elaborate a collection of left-hand and right-hand pairs - that is, a
845854-- top-level definition.
846855elabClauses :: ElabInfo -> FC -> FnOpts -> Name -> [PClause] -> Idris ()
@@ -1628,7 +1637,7 @@ elabClass info syn doc fc constraints tn ps ds
16281637 let cty = impbind ps $ conbind constraints
16291638 $ pibind (map (\ (n, ty) -> (nsroot n, ty)) methods)
16301639 constraint
1631− let cons = [("", cn, cty, fc)]
1640+ let cons = [("", cn, cty, fc, [])]
16321641 let ddecl = PDatadecl tn tty cons
16331642 logLvl 5 $ "Class data " ++ showDImp True ddecl
16341643 elabData info (syn { no_imp = no_imp syn ++ mnames }) doc fc [] ddecl
src/Idris/ParseData.hs+13−9
@@ -49,7 +49,7 @@ record syn = do (doc, acc) <- try (do
4949 ty <- typeExpr (allowImp syn)
5050 let tyn = expandNS syn tyn_in
5151 reserved "where"
52− (cdoc, cn, cty, _) <- indentedBlockS (constructor syn)
52+ (cdoc, cn, cty, _, _) <- indentedBlockS (constructor syn)
5353 accData acc tyn [cn]
5454 let rsyn = syn { syn_namespace = show (nsroot tyn) :
5555 syn_namespace syn }
@@ -113,7 +113,7 @@ data_ syn = do (doc, acc, dataOpts) <- try (do
113113 option (PData doc syn fc dataOpts (PLaterdecl tyn ty)) (do
114114 reserved "where"
115115 cons <- indentedBlock (constructor syn)
116− accData acc tyn (map (\ (_, n, _, _) -> n) cons)
116+ accData acc tyn (map (\ (_, n, _, _, _) -> n) cons)
117117 return $ PData doc syn fc dataOpts (PDatadecl tyn ty cons))) <|> (do
118118 args <- many name
119119 let ty = bindArgs (map (const PType) args) PType
@@ -133,10 +133,10 @@ data_ syn = do (doc, acc, dataOpts) <- try (do
133133 cons <- sepBy1 (simpleConstructor syn) (lchar '|')
134134 terminator
135135 let conty = mkPApp fc (PRef fc tyn) (map (PRef fc) args)
136− cons' <- mapM (\ (doc, x, cargs, cfc) ->
136+ cons' <- mapM (\ (doc, x, cargs, cfc, fs) ->
137137 do let cty = bindArgs cargs conty
138− return (doc, x, cty, cfc)) cons
139− accData acc tyn (map (\ (_, n, _, _) -> n) cons')
138+ return (doc, x, cty, cfc, fs)) cons
139+ accData acc tyn (map (\ (_, n, _, _, _) -> n) cons')
140140 return $ PData doc syn fc dataOpts (PDatadecl tyn ty cons')))
141141 <?> "data type declaration"
142142 where
@@ -152,20 +152,24 @@ data_ syn = do (doc, acc, dataOpts) <- try (do
152152{- | Parses a type constructor declaration
153153 Constructor ::= DocComment? FnName TypeSig;
154154-}
155−constructor :: SyntaxInfo -> IdrisParser (String, Name, PTerm, FC)
155+constructor :: SyntaxInfo -> IdrisParser (String, Name, PTerm, FC, [Name])
156156constructor syn
157157 = do doc <- option "" (docComment '|')
158158 cn_in <- fnName; fc <- getFC
159159 let cn = expandNS syn cn_in
160160 lchar ':'
161+ -- FIXME: 'forcenames' is an almighty hack! Need a better way of
162+ -- erasing non-forceable things
163+ fs <- option [] (do lchar '%'; reserved "erase"
164+ sepBy1 name (lchar ','))
161165 ty <- typeExpr (allowImp syn)
162− return (doc, cn, ty, fc)
166+ return (doc, cn, ty, fc, fs)
163167 <?> "constructor"
164168
165169{- | Parses a constructor for simple discriminative union data types
166170 SimpleConstructor ::= FnName SimpleExpr* DocComment?
167171-}
168−simpleConstructor :: SyntaxInfo -> IdrisParser (String, Name, [PTerm], FC)
172+simpleConstructor :: SyntaxInfo -> IdrisParser (String, Name, [PTerm], FC, [Name])
169173simpleConstructor syn
170174 = do cn_in <- fnName
171175 let cn = expandNS syn cn_in
@@ -173,7 +177,7 @@ simpleConstructor syn
173177 args <- many (do notEndApp
174178 simpleExpr syn)
175179 doc <- option "" (docComment '^')
176− return (doc, cn, args, fc)
180+ return (doc, cn, args, fc, [])
177181 <?> "constructor"
178182
179183{- | Parses a dsl block declaration
src/Idris/ParseExpr.hs+1−1
@@ -580,7 +580,7 @@ implicitArg syn = do lchar '{'
580580 v <- option (PRef fc n) (do lchar '='
581581 expr syn)
582582 lchar '}'
583− return (pimp n v False)
583+ return (pimp n v True)
584584 <?> "implicit function argument"
585585
586586{-| Parses a constraint argument (for selecting a named type class instance)
src/Idris/UnusedArgs.hs+2−1
@@ -23,6 +23,7 @@ traceUnused n
2323 recused <- mapM (\ (argn, i, (g, j)) ->
2424 do u <- used [(n, i)] g j
2525 return (argn, u)) fargs
26+ logLvl 4 $ show n ++ " recused TRACE: " ++ show recused
2627 let fused = nub $ usedns ++ map fst (filter snd recused)
2728 logLvl 1 $ show n ++ " used args: " ++ show fused
2829 let unusedpos = mapMaybe (getUnused fused) (zip [0..] args)
@@ -50,7 +51,7 @@ used path g j
5051 recused <- mapM (\ (argn, j, (g', j')) ->
5152 used ((g,j):path) g' j') garg
5253 -- used on any route from here, or not used recursively
53− return (directuse || null recused || or recused)
54+ return (directuse || or recused)
5455 _ -> return True -- no definition, assume used
5556
5657getFargpos :: [(Name, [[Name]])] -> (Name, Int) -> [(Name, Int, (Name, Int))]
test/reg031/expectednew file+1
@@ -0,0 +1 @@
1+[10, -128, -55, -1, 10, 51, 10, 52]
test/reg031/reg031.idrnew file+4
@@ -0,0 +1,4 @@
1+module Main
2+
3+main : IO ()
4+main = print . map ord . unpack $ "\x0a\x80\xC9\xFF\n3\n4"
test/reg031/runnew file+4
@@ -0,0 +1,4 @@
1+#!/usr/bin/env bash
2+idris $@ reg031.idr -o reg031
3+./reg031
4+rm -f *.ibc reg031