Skip to content

Commit 2577832

Browse files
committed
[ new ] --total cli flag
1 parent b35e548 commit 2577832

File tree

16 files changed

+232
-30
lines changed

16 files changed

+232
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ filter p (x :: xs) with (p x)
6565
### REPL/CLI/IDE mode changes
6666

6767
* Added `--list-packages` CLI option.
68+
* Added `--total` CLI option.
6869

6970
### Library changes
7071

src/Core/Binary.idr

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Core.UnifyState
1717

1818
import Data.Buffer
1919
import Data.List
20+
import Data.String
2021

2122
import System.File
2223

@@ -31,7 +32,7 @@ import public Libraries.Utils.Binary
3132
||| (Increment this when changing anything in the data format)
3233
export
3334
ttcVersion : Int
34-
ttcVersion = 61
35+
ttcVersion = 62
3536

3637
export
3738
checkTTCVersion : String -> Int -> Int -> Core ()
@@ -41,6 +42,7 @@ checkTTCVersion file ver exp
4142
record TTCFile extra where
4243
constructor MkTTCFile
4344
version : Int
45+
totalReq : TotalReq
4446
sourceHash : String
4547
ifaceHash : Int
4648
importHashes : List (Namespace, Int)
@@ -93,14 +95,14 @@ HasNames (Name, Name, Bool) where
9395
resolved c (n1, n2, b) = pure (!(resolved c n1), !(resolved c n2), b)
9496

9597
HasNames e => HasNames (TTCFile e) where
96-
full gam (MkTTCFile version sourceHash ifaceHash iHashes incData
98+
full gam (MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
9799
context userHoles
98100
autoHints typeHints
99101
imported nextVar currentNS nestedNS
100102
pairnames rewritenames primnames
101103
namedirectives cgdirectives trans
102104
extra)
103-
= pure $ MkTTCFile version sourceHash ifaceHash iHashes incData
105+
= pure $ MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
104106
context userHoles
105107
!(traverse (full gam) autoHints)
106108
!(traverse (full gam) typeHints)
@@ -131,14 +133,14 @@ HasNames e => HasNames (TTCFile e) where
131133
-- I don't think we ever actually want to call this, because after we read
132134
-- from the file we're going to add them to learn what the resolved names
133135
-- are supposed to be! But for completeness, let's do it right.
134-
resolved gam (MkTTCFile version sourceHash ifaceHash iHashes incData
136+
resolved gam (MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
135137
context userHoles
136138
autoHints typeHints
137139
imported nextVar currentNS nestedNS
138140
pairnames rewritenames primnames
139141
namedirectives cgdirectives trans
140142
extra)
141-
= pure $ MkTTCFile version sourceHash ifaceHash iHashes incData
143+
= pure $ MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
142144
context userHoles
143145
!(traverse (resolved gam) autoHints)
144146
!(traverse (resolved gam) typeHints)
@@ -177,6 +179,7 @@ writeTTCFile b file_in
177179
= do file <- toFullNames file_in
178180
toBuf b "TT2"
179181
toBuf @{Wasteful} b (version file)
182+
toBuf b (totalReq file)
180183
toBuf b (sourceHash file)
181184
toBuf b (ifaceHash file)
182185
toBuf b (importHashes file)
@@ -204,17 +207,20 @@ readTTCFile : TTC extra =>
204207
readTTCFile readall file as b
205208
= do hdr <- fromBuf b
206209
chunk <- get Bin
207-
when (hdr /= "TT2") $ corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
210+
when (hdr /= "TT2") $
211+
corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
208212
ver <- fromBuf @{Wasteful} b
209213
checkTTCVersion file ver ttcVersion
214+
totalReq <- fromBuf b
210215
sourceFileHash <- fromBuf b
211216
ifaceHash <- fromBuf b
212217
importHashes <- fromBuf b
213218
incData <- fromBuf b
214219
imp <- fromBuf b
215220
ex <- fromBuf b
216221
if not readall
217-
then pure (MkTTCFile ver sourceFileHash ifaceHash importHashes
222+
then pure (MkTTCFile ver totalReq
223+
sourceFileHash ifaceHash importHashes
218224
incData [] [] [] [] []
219225
0 (mkNamespace "") [] Nothing
220226
Nothing
@@ -234,7 +240,8 @@ readTTCFile readall file as b
234240
nds <- fromBuf b
235241
cgds <- fromBuf b
236242
trans <- fromBuf b
237-
pure (MkTTCFile ver sourceFileHash ifaceHash importHashes incData
243+
pure (MkTTCFile ver totalReq
244+
sourceFileHash ifaceHash importHashes incData
238245
(map (replaceNS cns) defs) uholes
239246
autohs typehs imp nextv cns nns
240247
pns rws prims nds cgds trans ex)
@@ -278,9 +285,15 @@ writeToTTC extradata sourceFileName ttcFileName
278285
ust <- get UST
279286
gdefs <- getSaveDefs (currentNS defs) (keys (toSave defs)) [] defs
280287
sourceHash <- hashFileWith defs.options.hashFn sourceFileName
281-
log "ttc.write" 5 $ "Writing " ++ ttcFileName ++ " with source hash " ++ sourceHash ++ " and interface hash " ++ show (ifaceHash defs)
288+
totalReq <- getDefaultTotalityOption
289+
log "ttc.write" 5 $ unwords
290+
[ "Writing", ttcFileName
291+
, "with source hash", sourceHash
292+
, "and interface hash", show (ifaceHash defs)
293+
]
282294
writeTTCFile bin
283-
(MkTTCFile ttcVersion (sourceHash) (ifaceHash defs) (importHashes defs)
295+
(MkTTCFile ttcVersion totalReq
296+
sourceHash (ifaceHash defs) (importHashes defs)
284297
(incData defs)
285298
gdefs
286299
(keys (userHoles defs))
@@ -507,17 +520,38 @@ getImportHashes file b
507520
when (hdr /= "TT2") $ corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
508521
ver <- fromBuf @{Wasteful} b
509522
checkTTCVersion file ver ttcVersion
523+
totalReq <- fromBuf {a = TotalReq} b
510524
sourceFileHash <- fromBuf {a = String} b
511525
interfaceHash <- fromBuf {a = Int} b
512526
fromBuf b
513527

528+
export
529+
getTotalReq : String -> Ref Bin Binary -> Core TotalReq
530+
getTotalReq file b
531+
= do hdr <- fromBuf {a = String} b
532+
when (hdr /= "TT2") $ corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
533+
ver <- fromBuf @{Wasteful} b
534+
checkTTCVersion file ver ttcVersion
535+
fromBuf b
536+
537+
export
538+
readTotalReq : (fileName : String) -> -- file containing the module
539+
Core (Maybe TotalReq)
540+
readTotalReq fileName
541+
= do Right buffer <- coreLift $ readFromFile fileName
542+
| Left err => pure Nothing
543+
b <- newRef Bin buffer
544+
catch (Just <$> getTotalReq fileName b)
545+
(\err => pure Nothing)
546+
514547
export
515548
getHashes : String -> Ref Bin Binary -> Core (String, Int)
516549
getHashes file b
517550
= do hdr <- fromBuf {a = String} b
518551
when (hdr /= "TT2") $ corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
519552
ver <- fromBuf @{Wasteful} b
520553
checkTTCVersion file ver ttcVersion
554+
totReq <- fromBuf {a = TotalReq} b
521555
sourceFileHash <- fromBuf b
522556
interfaceHash <- fromBuf b
523557
pure (sourceFileHash, interfaceHash)
@@ -529,8 +563,7 @@ readHashes fileName
529563
= do Right buffer <- coreLift $ readFromFile fileName
530564
| Left err => pure ("", 0)
531565
b <- newRef Bin buffer
532-
catch (do hashes <- getHashes fileName b
533-
pure hashes)
566+
catch (getHashes fileName b)
534567
(\err => pure ("", 0))
535568

536569
export

src/Core/Context.idr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,9 @@ clearCtxt
11561156
timings = timings defs } !initDefs)
11571157
where
11581158
resetElab : Options -> Options
1159-
resetElab = record { elabDirectives = defaultElab }
1159+
resetElab opts =
1160+
let tot = totalReq (session opts) in
1161+
record { elabDirectives = record { totality = tot } defaultElab } opts
11601162

11611163
export
11621164
getFieldNames : Context -> Namespace -> List Name
@@ -2559,6 +2561,12 @@ setSession sopts
25592561
= do defs <- get Ctxt
25602562
put Ctxt (record { options->session = sopts } defs)
25612563

2564+
%inline
2565+
export
2566+
updateSession : {auto c : Ref Ctxt Defs} ->
2567+
(Session -> Session) -> Core ()
2568+
updateSession f = setSession (f !getSession)
2569+
25622570
export
25632571
recordWarning : {auto c : Ref Ctxt Defs} -> Warning -> Core ()
25642572
recordWarning w

src/Core/Core.idr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Core.TT
55

66
import Data.List
77
import Data.List1
8+
import Data.String
89
import Data.Vect
910

1011
import Libraries.Data.IMaybe

src/Core/Options.idr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public export
144144
record Session where
145145
constructor MkSessionOpts
146146
noprelude : Bool
147+
totalReq : TotalReq
147148
nobanner : Bool
148149
findipkg : Bool
149150
codegen : CG
@@ -202,7 +203,6 @@ record Options where
202203
additionalCGs : List (String, CG)
203204
hashFn : String
204205

205-
206206
export
207207
availableCGs : Options -> List (String, CG)
208208
availableCGs o
@@ -228,7 +228,7 @@ defaultPPrint = MkPPOpts False True False
228228

229229
export
230230
defaultSession : Session
231-
defaultSession = MkSessionOpts False False False Chez [] 1000 False False
231+
defaultSession = MkSessionOpts False CoveringOnly False False Chez [] 1000 False False
232232
defaultLogLevel False False Nothing Nothing
233233
Nothing Nothing False 1 False True
234234
False [] False False

src/Core/Options/Log.idr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ knownTopics = [
129129
("specialise", Nothing),
130130
("totality", Nothing),
131131
("totality.positivity", Nothing),
132+
("totality.requirement", Nothing),
132133
("totality.termination", Nothing),
133134
("totality.termination.calc", Nothing),
134135
("totality.termination.guarded", Nothing),

src/Core/TT.idr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,16 @@ Eq TotalReq where
961961
(==) PartialOK PartialOK = True
962962
(==) _ _ = False
963963

964+
||| Bigger means more requirements
965+
||| So if a definition was checked at b, it can be accepted at a <= b.
966+
export
967+
Ord TotalReq where
968+
PartialOK <= _ = True
969+
_ <= Total = True
970+
a <= b = a == b
971+
972+
a < b = a <= b && a /= b
973+
964974
export
965975
Show TotalReq where
966976
show Total = "total"

src/Core/TTC.idr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ TTC ModuleIdent where
3131
export
3232
TTC VirtualIdent where
3333
toBuf b Interactive = tag 0
34-
3534
fromBuf b =
3635
case !getTag of
3736
0 => pure Interactive

src/Idris/CommandLine.idr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ data CLOpt
157157
||| Generate bash completion info
158158
BashCompletion String String |
159159
||| Generate bash completion script
160-
BashCompletionScript String
160+
BashCompletionScript String |
161+
||| Turn on %default total globally
162+
Total
161163

162164
||| Extract the host and port to bind the IDE socket to
163165
export
@@ -242,6 +244,8 @@ options = [MkOpt ["--check", "-c"] [] [CheckOnly]
242244
(Just "Generate profile data when compiling, if supported"),
243245

244246
optSeparator,
247+
MkOpt ["--total"] [] [Total]
248+
(Just "Require functions to be total by default"),
245249
MkOpt ["-Werror"] [] [WarningsAsErrors]
246250
(Just "Treat warnings as errors"),
247251
MkOpt ["-Wno-shadowing"] [] [IgnoreShadowingWarnings]

0 commit comments

Comments
 (0)