@@ -18,7 +18,7 @@ forceArgs :: Name -> Type -> Idris ()
1818forceArgs n t = do i <- getIState
1919 let fargs = force i 0 t
2020 copt <- case lookupCtxt n (idris_optimisation i) of
21− [] -> return $ Optimise False [] []
21+ [] -> return $ Optimise False False [] []
2222 (op:_) -> return op
2323 let opts = addDef n (copt { forceable = fargs }) (idris_optimisation i)
2424 putIState (i { idris_optimisation = opts })
@@ -62,8 +62,22 @@ collapseCons ty cons =
6262 let cons' = map (\ (n, t) -> (n, map snd (getArgTys t))) cons
6363 allFR <- mapM (forceRec i) cons'
6464 if and allFR then detaggable (map getRetTy (map snd cons))
65− else return () -- not collapsible as not detaggable
65+ else checkNewType cons'
6666 where
67+ -- one constructor; if one remaining argument, treat as newtype
68+ checkNewType [(n, ts)] = do
69+ i <- getIState
70+ case lookupCtxt n (idris_optimisation i) of
71+ (oi:_) -> do let remaining = length ts - length (forceable oi)
72+ if remaining == 1 then
73+ do let oi' = oi { isnewtype = True }
74+ let opts = addDef n oi' (idris_optimisation i)
75+ putIState (i { idris_optimisation = opts })
76+ else return ()
77+ _ -> return ()
78+
79+ checkNewType _ = return ()
80+
6781 setCollapsible :: Name -> Idris ()
6882 setCollapsible n
6983 = do i <- getIState
@@ -72,7 +86,7 @@ collapseCons ty cons =
7286 (oi:_) -> do let oi' = oi { collapsible = True }
7387 let opts = addDef n oi' (idris_optimisation i)
7488 putIState (i { idris_optimisation = opts })
75− [] -> do let oi = Optimise True [] []
89+ [] -> do let oi = Optimise True False [] []
7690 let opts = addDef n oi (idris_optimisation i)
7791 putIState (i { idris_optimisation = opts })
7892 addIBC (IBCOpt n)
@@ -256,7 +270,13 @@ applyDataOptRT oi n tag arity args
256270 doOpts n args _ forced
257271 = let args' = filter keep (zip (map (\x -> x `elem` forced) [0..])
258272 args) in
259− mkApp (P (DCon tag (arity - length forced)) n Erased) (map snd args')
273+ if isnewtype oi
274+ then case args' of
275+ [(_, val)] -> val
276+ _ -> error "Can't happen (not isnewtype)"
277+ else
278+ mkApp (P (DCon tag (arity - length forced)) n Erased)
279+ (map snd args')
260280
261281 keep (forced, _) = not forced
262282