Skip to content

Commit b89435c

Browse files
author
André Videla
committed
Add support for latest Typedefs API
1 parent 5858200 commit b89435c

File tree

6 files changed

+61
-103
lines changed

6 files changed

+61
-103
lines changed

elba.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ id = 'statebox/idris-ct@git+https://github.com/statebox/idris-ct#fbc7f633e0d86bf
77
version = '0.1.0'
88

99
[[packages.dependencies]]
10-
id = 'typedefs/typedefs@git+https://github.com/typedefs/typedefs#a0ce68a1e467ec845917cb1e1411250cfb89b881'
10+
id = 'typedefs/typedefs@git+https://github.com/typedefs/typedefs#17eea0cb3ef7b4ae7ec3f0d2e89d2ea1b8635250'
1111
version = '0.1.0'
1212

1313
[[packages]]
@@ -16,7 +16,7 @@ version = '0.1.0'
1616
dependencies = []
1717

1818
[[packages]]
19-
id = 'typedefs/typedefs@git+https://github.com/typedefs/typedefs#a0ce68a1e467ec845917cb1e1411250cfb89b881'
19+
id = 'typedefs/typedefs@git+https://github.com/typedefs/typedefs#17eea0cb3ef7b4ae7ec3f0d2e89d2ea1b8635250'
2020
version = '0.1.0'
2121

2222
[[packages.dependencies]]

elba.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ version = "0.1.0"
44
authors = []
55

66
[dependencies]
7-
"typedefs/typedefs" = { git = "https://github.com/typedefs/typedefs" , tag="a0ce68a1e467ec845917cb1e1411250cfb89b881"}
8-
"statebox/idris-ct" = { git = "https://github.com/statebox/idris-ct" , tag="master"}
7+
"typedefs/typedefs" = { git = "https://github.com/typedefs/typedefs" , tag="17eea0cb3ef7b4ae7ec3f0d2e89d2ea1b8635250"}
8+
"statebox/idris-ct" = { git = "https://github.com/statebox/idris-ct" , tag="416ad13ded92a88dc93761d0125d14061ed6140e"}
99

1010
[[targets.bin]]
1111
path = "src"

src/JSONFormat.idr

+2-33
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,14 @@ import TGraph
3131

3232
import Typedefs.Typedefs
3333

34-
public export
35-
ParseError : Type -> Type
36-
ParseError = Either String
37-
38-
public export
39-
JSONParser : Type -> Type
40-
JSONParser t = JSON -> ParseError t
41-
4234
export
43-
expectNat : JSONParser Nat
35+
expectNat : JSON -> Either String Nat
4436
expectNat (JNumber n) = if n < 0 then Left "Expected Nat"
4537
else pure $ Prelude.toNat {a=Int} $ cast n
4638
expectNat _ = Left "Expected Nat"
4739

48-
expectEdges : JSONParser (Nat, Nat)
49-
expectEdges (JObject [("input", a),("output", b)])= [| MkPair (expectNat a) (expectNat b) |]
50-
expectEdges _ = Left "Expected List of edges"
51-
52-
expectList : JSONParser (List JSON)
53-
expectList (JArray ls) = pure ls
54-
expectList _ = Left "Expected List"
55-
56-
export
57-
expectListNat : JSONParser (List Nat)
58-
expectListNat js = expectList js >>= traverse expectNat
59-
60-
export
61-
expectListEdges : JSONParser (List (Nat, Nat))
62-
expectListEdges js = expectList js >>= traverse expectEdges
63-
64-
expectPair : (JSONParser a) -> (JSONParser b) -> JSONParser (a, b)
65-
expectPair pa pb (JObject [("_0", a), ("_1", b)]) = [| MkPair (pa a) (pb b) |]
66-
expectPair pa pb _ = Left "Expected Pair"
67-
68-
export
69-
expectListListEdges : JSON -> Either String (List (List Nat, List Nat))
70-
expectListListEdges js = expectList js >>= traverse (expectPair expectListNat expectListNat)
7140

7241
public export
73-
TResult : TDefR 1
42+
TResult : TDefR 0
7443
TResult = TSum [T1, TFSMErr]
7544

src/Main.idr

+15-15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import Discrete.DiscreteCategory
3434
import Typedefs.Typedefs
3535
import Typedefs.TermParse
3636
import Typedefs.TermWrite
37+
import Typedefs.Idris
38+
import Typedefs.Library
3739

3840
-- FSM-Oracle
3941
import TGraph
@@ -52,30 +54,28 @@ import Language.JSON
5254

5355
checkFSM : JSON -> FSMCheck ()
5456
checkFSM content = do
55-
fsm <- mapLeft InvalidFSM (Typedefs.TermParse.deserialiseJSON FSMExec
56-
[ (Nat ** expectNat)
57-
, (List (Nat, Nat) ** expectListEdges)
58-
, (List Nat ** expectListNat)
59-
]
60-
content)
57+
fsm <- mapLeft InvalidFSM (Typedefs.TermParse.deserialise
58+
StandardParsers
59+
[]
60+
FSMExec
61+
content)
6162
(cat ** a ** b ** m) <- validateExec fsm
6263
let v = lastStep cat a b m
6364
pure ()
6465

6566
checkPetri : JSON -> FSMCheck ()
6667
checkPetri content = do
67-
petri' <- mapLeft InvalidFSM (Typedefs.TermParse.deserialiseJSON TPetriExec
68-
[ (Nat ** expectNat)
69-
, (List (List Nat, List Nat) ** expectListListEdges)
70-
, (List Nat ** expectListNat)
71-
]
72-
content)
68+
petri' <- mapLeft InvalidFSM (Typedefs.TermParse.deserialise
69+
StandardParsers
70+
[liftParse expectNat]
71+
TPetriExec
72+
content)
7373
petri <- mapLeft InvalidFSM (convertExec $ petri')
7474
let True = isJust $ composeWithId (Spec petri) (Path petri) (State petri)
7575
| Left InvalidPath
7676
pure ()
7777

78-
toTDef : FSMCheck () -> Ty [String] TResult
78+
toTDef : FSMCheck () -> Ty' StandardIdris [] TResult
7979
toTDef (Left err) = Right (toTDefErr err)
8080
toTDef (Right r) = Left r
8181

@@ -85,7 +85,7 @@ parseMode : String -> Maybe OracleMode
8585
parseMode "-f" = Just FSM
8686
parseMode "--fsm" = Just FSM
8787
parseMode "-p" = Just Petri
88-
parseMode "-petri" = Just Petri
88+
parseMode "--petri" = Just Petri
8989
parseMode _ = Nothing
9090

9191
printHelp : IO ()
@@ -106,5 +106,5 @@ main = do
106106
let result = do fileContent <- mapLeft (const FSError) content
107107
jsonContent <- maybeToEither JSONError (parse fileContent)
108108
(pickChecker pmode) jsonContent
109-
printLn (TermWrite.serialiseJSON [String] [JString] TResult (toTDef result))
109+
printLn (TermWrite.serialise StandardContext [] TResult (toTDef result))
110110

src/PetriFormat.idr

+21-28
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,33 @@
22
module PetriFormat
33

44
import Typedefs.Typedefs
5+
import Typedefs.Library
6+
import Typedefs.Idris
57
import Typedefs.Names
68
import Data.Vect
79
import PetriGraph
810

911
public export
10-
TNat : TDefR 2
11-
TNat = RRef 0
12+
App : TDef' n b -> Vect n (TDef' m b) -> TDef' m b
13+
App td args = TApp (TName "" td) args
1214

1315
public export
14-
TEdges : TDefR 2
15-
TEdges = RRef 1
16+
TEdges : TDefR 1
17+
TEdges = App TList [TProd [TList, TList]]
1618

1719
public export
18-
TState : TDefR 3
19-
TState = RRef 2
20+
TState : TDefR 1
21+
TState = TList
2022

2123
public export
22-
TPetriSpec : TDefR 2
23-
TPetriSpec = TProd [TNat, TEdges]
24+
TPetriSpec : TDefR 1
25+
TPetriSpec = TProd [TNat1, TEdges]
2426

2527
public export
26-
convertSpec : Ty [Nat, List (List Nat, List Nat)] TPetriSpec -> Maybe (n ** PetriSpec n)
28+
convertSpec : Ty' StandardIdris [Nat] TPetriSpec -> Maybe (n ** PetriSpec n)
2729
convertSpec (places, edges) =
28-
MkDPair (length edges) . MkPetriSpec places <$> convertList places (fromList edges)
30+
MkDPair (length edges) . MkPetriSpec places
31+
<$> convertList places (fromList edges)
2932
where
3033
convertList : (p : Nat) -> (Vect n (List Nat, List Nat))
3134
-> Maybe (Vect n (List (Fin p), List (Fin p)))
@@ -43,14 +46,13 @@ TTree = TMu
4346
]
4447

4548
-- converts from TDef to Tree
46-
convertTree : Ty [Nat] TTree -> (Tree Nat Nat)
49+
convertTree : Ty' StandardIdris [Nat] TTree -> Tree Nat Nat
4750
convertTree (Inn (Left (a, b))) = Tensor (convertTree a) (convertTree b)
4851
convertTree (Inn (Right (Left (a, b)))) = Sequence (convertTree a) (convertTree b)
4952
convertTree (Inn (Right (Right (Left (a, b))))) = Sym a b
5053
convertTree (Inn (Right (Right (Right (Left i))))) = Id i
5154
convertTree (Inn (Right (Right (Right (Right m))))) = Mor m
5255

53-
5456
-- converts from Tree to TDef
5557
export
5658
convertTree' : Tree Nat Nat -> Ty [Nat] TTree
@@ -60,31 +62,22 @@ convertTree' (Sym a b) = (Inn (Right (Right (Left (a, b)))))
6062
convertTree' (Id x) = (Inn (Right (Right (Right (Left x)))))
6163
convertTree' (Mor x) = (Inn (Right (Right (Right (Right x)))))
6264

63-
example : Tree Nat Nat
64-
example = Id Z
65-
66-
export
67-
exampleTDef : Ty [Nat] TTree
68-
exampleTDef = convertTree' example
69-
7065
public export
7166
convertState : (spec : PetriSpec k) -> List Nat -> Maybe (PetriState spec)
7267
convertState spec = traverse (\s => natToFin s (Places spec))
7368

7469
public export
75-
TPetriExec : TDefR 3
76-
TPetriExec = TProd [ TProd [RRef 0 , RRef 1]
77-
, RRef 2
78-
, weakenTDef TTree 3 (LTESucc LTEZero)
70+
TPetriExec : TDefR 1
71+
TPetriExec = TProd [ TPetriSpec -- spec
72+
, TState -- initial state
73+
-- /!\ the type argument is shared between Tree and List
74+
, TTree -- execution
7975
]
8076

81-
dropContext : Ty [Nat, a, b] (weakenTDef TTree 3 (LTESucc LTEZero)) -> Ty [Nat] TTree
82-
dropContext tdef = really_believe_me tdef
83-
8477
public export
85-
convertExec : Ty [Nat, List (List Nat, List Nat), List Nat] TPetriExec -> Either String PetriExec
78+
convertExec : Ty' StandardIdris [Nat] TPetriExec -> Either String PetriExec
8679
convertExec ((a, b), c, d) = do (k ** spec) <- maybeToEither "indices are wrong" $ convertSpec (a , b)
87-
path <- maybeToEither "illegal tree" $ checkTree spec (convertTree $ dropContext d)
80+
path <- maybeToEither "illegal tree" $ checkTree spec (convertTree d)
8881
state <- maybeToEither "Illegal states" $ convertState spec c
8982
pure $ MkPetriExec spec path state
9083

src/TGraph.idr

+19-23
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,41 @@ import Graph.Graph
3232
-- typedefs
3333
import Typedefs.Names
3434
import Typedefs.Typedefs
35+
import Typedefs.Idris
36+
import Typedefs.Library
3537

3638
%access public export
3739
%default total
3840

39-
-- Base definitions
40-
41-
-- Defines naturals
42-
TNat : TDefR 3
43-
TNat = RRef 0
44-
4541
-- Graph definitions
4642

4743
||| The type definition for vertices in the graph is jsut
4844
||| A natural enumerating the vertexes. e.g. 5 means
4945
||| That there are 5 vertexes, denoted 0,1,2,3,4
50-
FSMVertex : TDefR 3
46+
FSMVertex : TDefR 0
5147
FSMVertex = TNat
5248

5349
||| The type definition for edges in the graph is just a couple
5450
||| of vertexes defining the edge source and target
55-
FSMEdges : TDefR 3
56-
FSMEdges = RRef 1
51+
FSMEdges : TDefR 0
52+
FSMEdges = TApp (TName "" TList) [TProd [TNat, TNat]]
5753

5854
||| A Finite State Machine is defined by its vertices and a list of edges
5955
||| The definition might not be valid if edges endpoints are out of range
60-
FSMSpec : TDefR 3
56+
FSMSpec : TDefR 0
6157
FSMSpec = TProd [FSMVertex, FSMEdges]
6258

6359
||| A state is a vertex in the graph (might be out of range)
64-
FSMState : TDefR 3
60+
FSMState : TDefR 0
6561
FSMState = FSMVertex
6662

6763
||| A path is a list of edges (might not be valid)
68-
FSMPath : TDefR 3
69-
FSMPath = RRef 2-- TList `ap` [FSMEdge]
64+
FSMPath : TDefR 0
65+
FSMPath = TApp (TName "" TList) [TNat]
7066

7167
||| An execution is a FSM, a state representing an inital edge and a path from that edge.
7268
||| The execution might not be valid.
73-
FSMExec : TDefR 3
69+
FSMExec : TDefR 0
7470
FSMExec = TProd [FSMSpec, FSMState, FSMPath]
7571

7672
||| Errors related to checking if a FSM description is valid
@@ -86,15 +82,15 @@ data FSMError =
8682
||| Error when reading the file
8783
FSError
8884

89-
TFSMErr : TDefR 1
90-
TFSMErr = TMu [("InvalidFSM", RRef 1),
91-
("InvalidState", T1),
92-
("InvalidPath", T1),
93-
("JSONError", T1),
94-
("FSError", T1)
85+
TFSMErr : TDefR 0
86+
TFSMErr = TMu [("InvalidFSM", TString1)
87+
,("InvalidState", T1)
88+
,("InvalidPath", T1)
89+
,("JSONError", T1)
90+
,("FSError", T1)
9591
]
9692

97-
toTDefErr : FSMError -> Ty [String] TFSMErr
93+
toTDefErr : FSMError -> Ty' StandardIdris [] TFSMErr
9894
toTDefErr (InvalidFSM s) = Inn (Left s)
9995
toTDefErr InvalidState = Inn (Right (Left ()))
10096
toTDefErr InvalidPath = Inn (Right (Right (Left ())))
@@ -108,8 +104,8 @@ Show FSMError where
108104
show JSONError = "JSON parsing error"
109105
show FSError = "Filesystem error"
110106

111-
IdrisType : TDefR 3 -> Type
112-
IdrisType = Ty [Nat, List (Nat, Nat), List Nat]
107+
IdrisType : TDefR 0 -> Type
108+
IdrisType = Ty' StandardIdris []
113109

114110
||| Monad to check errors when compiling FSMs
115111
FSMCheck : Type -> Type

0 commit comments

Comments
 (0)