-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
open CCOption.Infix | ||
|
||
type state = Done | Seen | Fresh | ||
|
||
exception Cycle of Variable.t list | ||
|
||
let find_cycle graph = | ||
let states = Variable.HMap.create 10 in | ||
Variable.Map.iter (fun v _ -> Variable.HMap.add states v Fresh) graph; | ||
let rec dfs v = | ||
match Variable.HMap.get_or ~default:Fresh states v with | ||
| Fresh -> | ||
Variable.HMap.add states v Seen; | ||
let res = | ||
let t = Variable.Map.get_or ~default:Type.dummy v graph in | ||
Iter.find_map dfs (Type.iter_vars t) | ||
in | ||
Variable.HMap.add states v Done; | ||
let+ u, path = res in | ||
if Variable.equal u v then raise (Cycle (u :: path)) else (u, v :: path) | ||
| Seen -> Some (v, [v]) | ||
| Done -> None | ||
in | ||
try | ||
Variable.Map.iter (fun v _ -> assert (CCOption.is_none (dfs v))) graph; | ||
None | ||
with Cycle l -> Some l | ||
|
||
let occur_check env = find_cycle (Env.vars env) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters