Skip to content

Commit 90331ac

Browse files
committed
Add error reporting, rewrite GoodHypergraph file
GoodHypergraphCategory was taking a _very_ long time to compile what's more, it triggered a bug in the compiler relating to how indices are tracked in lambda declarations. This problem is supposed to be resolved with #4851 (idris-lang/Idris-dev#4851) This bypasses the bug and improves compile times as well as add error reporting on the TDef returned by the API in order to more easily debug the source of failures when checking as petrinet description.
1 parent 0817ba9 commit 90331ac

File tree

9 files changed

+273
-179
lines changed

9 files changed

+273
-179
lines changed

src/Cartographer/GoodHypergraphCategory.idr

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,107 @@ getHypergraph _ {g} = g
3333

3434
postulate subsetEq : Subset.getWitness g = Subset.getWitness h -> g = h
3535

36+
---------------------------------
37+
-- GoodHypergraphCat functions --
38+
---------------------------------
39+
40+
infixr 4 ~+~>
41+
42+
||| GoodHypergraph signature
43+
(~+~>) : {s : Type} -> {ai, ao : s -> List o} -> (a , b : List o) -> Type
44+
(~+~>) a b {s} {ai} {ao} = Subset (Hypergraph s ai ao a b) GoodHypergraph
45+
46+
goodHypergraphCatCompose : {s : Type} -> {ai, ao : s -> List o} -> (a,b,c : List o)
47+
-> ((~+~>) {s} {ai} {ao} a b)
48+
-> ((~+~>) {s} {ai} {ao} b c)
49+
-> ((~+~>) {s} {ai} {ao} a c)
50+
goodHypergraphCatCompose _ _ _ f g = Element (compose (getWitness f) (getWitness g)) (HComp (getProof f) (getProof g))
51+
52+
goodHypergraphCatComposeProof : (a21 : List o) ->
53+
(b22 : List o) ->
54+
(c23 : List o) ->
55+
(d : List o) ->
56+
(f25 : Subset (Hypergraph sigma arityIn arityOut a21 b22)
57+
GoodHypergraph) ->
58+
(g26 : Subset (Hypergraph sigma arityIn arityOut b22 c23)
59+
GoodHypergraph) ->
60+
(h : Subset (Hypergraph sigma arityIn arityOut c23 d)
61+
GoodHypergraph) ->
62+
Element (compose (getWitness f25)
63+
(compose (getWitness g26) (getWitness h)))
64+
(HComp (getProof f25) (HComp (getProof g26) (getProof h))) =
65+
Element (compose (compose (getWitness f25) (getWitness g26))
66+
(getWitness h))
67+
(HComp (HComp (getProof f25) (getProof g26)) (getProof h))
68+
goodHypergraphCatComposeProof a b c d (Element f ff) (Element g gg) (Element h hh) =
69+
subsetEq (hgAssoc a b c d f g h)
70+
3671
goodHypergraphCat : (sigma : Type) -> (arityIn, arityOut : sigma -> List o) -> Category
3772
goodHypergraphCat {o} sigma arityIn arityOut = MkCategory
3873
(List o)
39-
(\k, l => Subset (Hypergraph sigma arityIn arityOut k l) GoodHypergraph)
74+
((~+~>) {s=sigma} {ai=arityIn} {ao=arityOut})
4075
(\n => Element (identity n) (Permutation (permId n)))
41-
(\_,_,_,f,g => Element (compose (getWitness f) (getWitness g)) (HComp (getProof f) (getProof g)))
76+
goodHypergraphCatCompose
4277
(\a, b, (Element g gg) => subsetEq (hgLeftId a b g))
4378
(\a, b, (Element g gg) => subsetEq (hgRightId a b g))
44-
(\a, b, c, d, (Element f ff), (Element g gg), (Element h hh) => subsetEq (hgAssoc a b c d f g h))
45-
79+
goodHypergraphCatComposeProof
4680
goodSingleton : {s : Type} -> {ai, ao : s -> List o} -> (edge : s) -> mor (goodHypergraphCat s ai ao) (ai edge) (ao edge)
4781
goodSingleton x = Element (Hypergraph.singleton x) (Singleton x)
4882

4983
goodPermutation : {s : Type} -> {ai, ao : s -> List o} -> Perm k m -> mor (goodHypergraphCat s ai ao) k m
5084
goodPermutation p = Element (permutation p) (Permutation p)
5185

52-
goodHyperGraphTensor : (s : Type) -> (ai, ao : s -> List o) -> CFunctor (productCategory (goodHypergraphCat s ai ao) (goodHypergraphCat s ai ao)) (goodHypergraphCat s ai ao)
86+
87+
------------------------------------
88+
-- GoodHyperGraphTensor functions --
89+
------------------------------------
90+
91+
tensorMor : (List o, List o) -> List o
92+
tensorMor a = Basics.fst a ++ Basics.snd a
93+
94+
goodHypergraphTensorMor : {s : Type} -> {ai, ao : s -> List o} -> (a : (List o, List o))
95+
-> (b : (List o, List o))
96+
-> (ProductMorphism (goodHypergraphCat s ai ao)
97+
(goodHypergraphCat s ai ao) a b)
98+
-> mor (goodHypergraphCat s ai ao) (tensorMor a) (tensorMor b)
99+
goodHypergraphTensorMor a b f =
100+
Element (add (Subset.getWitness $ pi1 f) (Subset.getWitness $ pi2 f) {k=fst a} {l=fst b} {m=snd a} {n=snd b})
101+
(VComp (getProof $ pi1 f) (getProof $ pi2 f))
102+
103+
goodHypergraphTensorCompose : (a, b, c : (List o, List o)) ->
104+
(f : ProductMorphism (goodHypergraphCat s ai ao) (goodHypergraphCat s ai ao) a b) ->
105+
(g : ProductMorphism (goodHypergraphCat s ai ao) (goodHypergraphCat s ai ao) b c) ->
106+
goodHypergraphTensorMor a c
107+
(productCompose a b c f g) =
108+
goodHypergraphCatCompose
109+
(tensorMor a)
110+
(tensorMor b)
111+
(tensorMor c)
112+
(goodHypergraphTensorMor a b f)
113+
(goodHypergraphTensorMor b c g)
114+
goodHypergraphTensorCompose a b c f g =
115+
subsetEq (hgPreserveCompose a b c (MkProductMorphism (getWitness $ pi1 f) (getWitness $ pi2 f))
116+
(MkProductMorphism (getWitness $ pi1 g) (getWitness $ pi2 g)))
117+
goodHyperGraphTensor : (s : Type) -> (ai, ao : s -> List o) ->
118+
CFunctor (productCategory (goodHypergraphCat s ai ao) (goodHypergraphCat s ai ao))
119+
(goodHypergraphCat s ai ao)
53120
goodHyperGraphTensor s ai ao = MkCFunctor
54-
(\a => fst a ++ snd a)
55-
(\a, b, f => Element (add (getWitness $ pi1 f) (getWitness $ pi2 f) {k=fst a} {l=fst b} {m=snd a} {n=snd b}) (VComp (getProof $ pi1 f) (getProof $ pi2 f)))
121+
tensorMor
122+
goodHypergraphTensorMor
56123
(\a => subsetEq (hgPreserveId (fst a) (snd a)))
57-
(\a, b, c, f, g => subsetEq (hgPreserveCompose a b c (MkProductMorphism (getWitness $ pi1 f) (getWitness $ pi2 f)) (MkProductMorphism (getWitness $ pi1 g) (getWitness $ pi2 g))))
124+
goodHypergraphTensorCompose
125+
126+
postulate
127+
composeGoodHypergraphProof : (a, b, c : List o) ->
128+
(d, e, f : List o) ->
129+
(g : Subset (Hypergraph s ai ao a b) GoodHypergraph) ->
130+
(h : Subset (Hypergraph s ai ao c d) GoodHypergraph) ->
131+
(k : Subset (Hypergraph s ai ao e f) GoodHypergraph) ->
132+
Element (add (getWitness g) (add (getWitness h) (getWitness k)))
133+
(VComp (getProof g) (VComp (getProof h) (getProof k))) =
134+
Element (add (add (getWitness g) (getWitness h)) (getWitness k))
135+
(VComp (VComp (getProof g) (getProof h)) (getProof k))
136+
composeGoodHypergraphProof fi fo gi go hi ho (Element f ff) (Element g gg) (Element h hh) = ?what -- subsetEq (hgTensorAssociative fi fo gi go hi ho f g h))
58137

59138
goodHypergraphSMC : (sigma : Type) -> (arityIn, arityOut : sigma -> List o) -> StrictMonoidalCategory
60139
goodHypergraphSMC s ai ao = MkStrictMonoidalCategory
@@ -64,4 +143,4 @@ goodHypergraphSMC s ai ao = MkStrictMonoidalCategory
64143
(\as => Refl)
65144
(\as => appendNilRightNeutral as)
66145
appendAssociative
67-
?proofgoodhypergraph -- (\fi, fo, gi, go, hi, ho, (Element f ff), (Element g gg), (Element h hh) => subsetEq (hgTensorAssociative fi fo gi go hi ho f g h))
146+
composeGoodHypergraphProof

src/Cartographer/HypergraphStrictMonoidalCategory.idr

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@ hgPreserveId {s} {ai} {ao} as bs with (identity {s} {ai} {ao} as)
3131
`trans` permCompLeftId (permId (as ++ bs)))
3232
`trans` permCompLeftId (permId (as ++ bs))
3333

34+
postulate
3435
hgPreserveCompose : {s : Type} -> {ai, ao : s -> List o} -> (a, b, c : (List o, List o))
3536
-> (f : ProductMorphism (hypergraphCat s ai ao) (hypergraphCat s ai ao) a b)
3637
-> (g : ProductMorphism (hypergraphCat s ai ao) (hypergraphCat s ai ao) b c)
3738
-> add (compose (pi1 f) (pi1 g)) (compose (pi2 f) (pi2 g))
3839
= compose (add (pi1 f) (pi2 f)) (add (pi1 g) (pi2 g))
39-
hgPreserveCompose a b c
40-
(MkProductMorphism (MkHypergraph pf1 sf1) (MkHypergraph pf2 sf2))
41-
(MkProductMorphism (MkHypergraph pg1 sg1) (MkHypergraph pg2 sg2))
42-
= hypergraphEq p ?w
43-
where
44-
p : Perm ((pf1 ++ pg1) ++ (pf2 ++ pg2)) ((pf1 ++ pf2) ++ (pg1 ++ pg2))
45-
p = rewriteLeft (sym $ appendAssociative pf1 pg1 (pf2 ++ pg2)) $
46-
rewriteRight (sym $ appendAssociative pf1 pf2 (pg1 ++ pg2)) $
47-
pf1 `permAddIdL` swapAddIdR pg1 pf2 pg2
40+
--hgPreserveCompose a b c
41+
-- (MkProductMorphism (MkHypergraph pf1 sf1) (MkHypergraph pf2 sf2))
42+
-- (MkProductMorphism (MkHypergraph pg1 sg1) (MkHypergraph pg2 sg2))
43+
-- = hypergraphEq p ?w
44+
-- where
45+
-- p : Perm ((pf1 ++ pg1) ++ (pf2 ++ pg2)) ((pf1 ++ pf2) ++ (pg1 ++ pg2))
46+
-- p = rewriteLeft (sym $ appendAssociative pf1 pg1 (pf2 ++ pg2)) $
47+
-- rewriteRight (sym $ appendAssociative pf1 pf2 (pg1 ++ pg2)) $
48+
-- pf1 `permAddIdL` swapAddIdR pg1 pf2 pg2
4849

4950
hgTensor : (s : Type) -> (ai, ao : s -> List o) -> CFunctor (productCategory (hypergraphCat s ai ao) (hypergraphCat s ai ao)) (hypergraphCat s ai ao)
5051
hgTensor s ai ao = MkCFunctor

src/GraphCat.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ constructNEPath g ((x, y) :: (y',z) :: pt) with (decEq y y')
6262
validateExec : IdrisType FSMExec -> FSMCheck (cat : Category ** a : obj cat ** b : obj cat ** mor cat a b)
6363
validateExec (spec, state, path) =
6464
do -- convert into a graph with `n` being the number of states
65-
(m** MkGraph {n} edges) <- maybe (Left InvalidFSM) Right $ mkTGraph $ spec
65+
(m** MkGraph {n} edges) <- maybe (Left $ InvalidFSM "Cannot convert to graph") Right $ mkTGraph $ spec
6666
-- get the inital state as a fin
6767
st <- maybe (Left InvalidState) Right $ natToFin state m
6868
-- Convert the edge list into fins

src/JSONFormat.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ expectListListEdges : JSON -> Either String (List (List Nat, List Nat))
7474
expectListListEdges js = expectList js >>= traverse (expectPair expectListNat expectListNat)
7575

7676
public export
77-
TResult : TDefR 0
77+
TResult : TDefR 1
7878
TResult = TSum [T1, TFSMErr]
7979

src/Main.idr

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,39 @@ import Language.JSON
4848
%access public export
4949
%default total
5050

51-
checkFSM : String -> FSMCheck ()
52-
checkFSM fileContent = do
53-
content <- maybe (Left JSONError) Right (parse fileContent)
54-
fsm <- either (const $ Left InvalidFSM) Right (Typedefs.TermParse.deserialiseJSON FSMExec
55-
[ (Nat ** expectNat)
56-
, (List (Nat, Nat) ** expectListEdges)
57-
, (List Nat ** expectListNat)
58-
]
59-
content)
60-
(cat ** a ** b ** m) <- validateExec fsm
61-
let v = lastStep cat a b m
62-
pure ()
51+
mapError : (a -> b) -> Either a c -> Either b c
52+
mapError f (Left a) = Left (f a)
53+
mapError _ (Right v) = Right v
54+
55+
-- checkFSM : String -> FSMCheck ()
56+
-- checkFSM fileContent = do
57+
-- content <- maybe (Left JSONError) Right (parse fileContent)
58+
-- fsm <- mapErro (const $ InvalidFSM "could not convert from TDef") (Typedefs.TermParse.deserialiseJSON FSMExec
59+
-- [ (Nat ** expectNat)
60+
-- , (List (Nat, Nat) ** expectListEdges)
61+
-- , (List Nat ** expectListNat)
62+
-- ]
63+
-- content)
64+
-- (cat ** a ** b ** m) <- validateExec fsm
65+
-- let v = lastStep cat a b m
66+
-- pure ()
6367

6468
checkPetri : String -> FSMCheck ()
6569
checkPetri fileContent = do
6670
content <- maybe (Left JSONError) Right (parse fileContent)
67-
petri' <- either (const $ Left InvalidFSM) Right (Typedefs.TermParse.deserialiseJSON TPetriExec
71+
petri' <- mapError (const $ InvalidFSM "could not convert from TDef") (Typedefs.TermParse.deserialiseJSON TPetriExec
6872
[ (Nat ** expectNat)
6973
, (List (List Nat, List Nat) ** expectListListEdges)
7074
, (List Nat ** expectListNat)
7175
]
7276
content)
73-
petri <- maybe (Left InvalidFSM) Right (convertExec $ petri')
77+
petri <- mapError (InvalidFSM) (convertExec $ petri')
7478
let True = isJust $ composeWithId (Spec petri) (Path petri) (State petri)
75-
| Left InvalidFSM
79+
| Left InvalidPath
7680
pure ()
7781

7882

79-
toTDef : FSMCheck () -> Ty [] TResult
83+
toTDef : FSMCheck () -> Ty [String] TResult
8084
toTDef (Left err) = Right (toTDefErr err)
8185
toTDef (Right r) = Left r
8286

@@ -87,8 +91,8 @@ main = do
8791
| _ => putStrLn "Usage: fsm-oracle FILE"
8892
content <- (readFile filename)
8993
let asFSMCheck = either (const (Left FSError)) Right content
90-
let checkedFSM = asFSMCheck >>= checkFSM
91-
printLn (TermWrite.serialiseJSON [] [] TResult (toTDef checkedFSM))
94+
let checkedFSM = asFSMCheck >>= checkPetri
95+
printLn (TermWrite.serialiseJSON [String] [JString] TResult (toTDef checkedFSM))
9296

9397
<<<<<<< HEAD
9498
=======

src/Permutations/PermutationsCategory.idr

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,28 @@ permCompRightId {bs} (Ins ab' sw) with (shuffle sw (permId bs)) proof shprf
2323
| Ins bc' sw' = case insInjective $ trans shprf (shuffleId sw) of
2424
(Refl, Refl, Refl) => rewrite permCompRightId ab' in Refl
2525

26+
postulate
2627
shuffleComp : (abb : SwapDown as bs) -> (bc : Perm bs cs) -> (cd : Perm cs ds)
2728
-> Ins bc' ayc = shuffle abb bc
2829
-> Ins {ys=ds1} cd' ad1d = shuffle ayc cd
2930
-> Ins {ys=ds2} bd' ad2d = shuffle abb (permComp bc cd)
3031
-> (ds1 = ds2, ad1d = ad2d, bd' = permComp bc' cd')
31-
shuffleComp HereS (Ins _ swx) cd Refl eq2 eq3 with (shuffle swx cd)
32-
| Ins bc' sw' with (eq2, eq3)
33-
| (Refl, Refl) = (Refl, Refl, Refl)
34-
shuffleComp {ds} (ThereS aab) (Ins {ys=zs} bz bzc) cd eq1 eq2 eq3 with (shuffle aab bz) proof bcPrf
35-
| Ins {ys=xs} ax axz with (shuffle bzc cd)
36-
| Ins {ys=us} zu bud with (shuffle aab (permComp bz zu)) proof bdPrf
37-
| Ins {ys=ts} at atu with (shuffle axz zu) proof cdPrf
38-
| Ins {ys=qs} xq aqu with (shuffleComp aab bz zu bcPrf cdPrf bdPrf)
39-
| (r1, r2, r3) with (swComb axz bzc)
40-
| SW2 {ys=ws} bxw awc with (eq1)
41-
| Refl with (shuffle awc cd)
42-
| Ins {ys=vs} wv avd with (eq2)
43-
| Refl with (swComb atu bud)
44-
| SW2 {ys=ss} bts asd with (eq3)
45-
| Refl with (shuffle bxw wv)
46-
| Ins {ys=rs} xr brv = ?pleaseLeaveMeAlone
32+
--shuffleComp HereS (Ins _ swx) cd Refl eq2 eq3 with (shuffle swx cd)
33+
-- | Ins bc' sw' with (eq2, eq3)
34+
-- | (Refl, Refl) = (Refl, Refl, Refl)
35+
--shuffleComp {ds} (ThereS aab) (Ins {ys=zs} bz bzc) cd eq1 eq2 eq3 with (shuffle aab bz) proof bcPrf
36+
-- | Ins {ys=xs} ax axz with (shuffle bzc cd)
37+
-- | Ins {ys=us} zu bud with (shuffle aab (permComp bz zu)) proof bdPrf
38+
-- | Ins {ys=ts} at atu with (shuffle axz zu) proof cdPrf
39+
-- | Ins {ys=qs} xq aqu with (shuffleComp aab bz zu bcPrf cdPrf bdPrf)
40+
-- | (r1, r2, r3) with (swComb axz bzc)
41+
-- | SW2 {ys=ws} bxw awc with (eq1)
42+
-- | Refl with (shuffle awc cd)
43+
-- | Ins {ys=vs} wv avd with (eq2)
44+
-- | Refl with (swComb atu bud)
45+
-- | SW2 {ys=ss} bts asd with (eq3)
46+
-- | Refl with (shuffle bxw wv)
47+
-- | Ins {ys=rs} xr brv = ?pleaseLeaveMeAlone
4748
-- vs = ss
4849
-- avd = asd
4950
-- rs = ts

0 commit comments

Comments
 (0)