Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make user-defined types enumerable + searchable #318

Open
byorgey opened this issue Jan 21, 2022 · 5 comments
Open

Make user-defined types enumerable + searchable #318

byorgey opened this issue Jan 21, 2022 · 5 comments

Comments

@byorgey
Copy link
Member

byorgey commented Jan 21, 2022

Should be able to handle this similarly to #317.

@byorgey
Copy link
Member Author

byorgey commented Jan 21, 2022

type T(a) = Unit + a * T(a) * T(a)

!!! forall t : T(N). mirror(mirror(t)) == t
mirror : T(a) -> T(a)
mirror(left(unit)) = left(unit)
mirror(right(a,l,r)) = right(a, mirror(r), mirror(l))

yields

Error: the type
  Unit + ℕ × T(ℕ) × T(ℕ)
is not searchable (i.e. it cannot be used in a forall).

@byorgey
Copy link
Member Author

byorgey commented Jan 21, 2022

OK, but note to make this work, enumerate also has to handle user-defined types!

@byorgey
Copy link
Member Author

byorgey commented Jan 21, 2022

+isSearchable :: TyDefCtx -> Type -> Bool
+isSearchable tyDefs = go S.empty
+  where
+    go :: Set (String, [Type]) -> Type -> Bool
+    go _ TyProp         = False
+    go _ ty
+      | isNumTy ty              = True
+      | isFiniteTy ty           = True
+    go seen (TyUser t tys)
+      | (t,tys) `S.member` seen = True
+      | otherwise = case M.lookup t tyDefs of
+          Nothing -> error $ t ++ " not in tydefs!"
+          Just (TyDefBody _ body) -> go (S.insert (t,tys) seen) (body tys)
+    go seen (TyList ty)    = go seen ty
+    go seen (TySet ty)     = go seen ty
+    go seen (ty1 :+: ty2)  = go seen ty1 && go seen ty2
+    go seen (ty1 :*: ty2)  = go seen ty1 && go seen ty2
+    go seen (ty1 :->: ty2) = isFiniteTy ty1 && go seen ty2

@byorgey
Copy link
Member Author

byorgey commented Jan 21, 2022

Above is the code for isSearchable. Putting this off for now because getting enumerate to handle user-defined types is a bit tricky. First of all, we have to be able to call enumerate at runtime, when we no longer have access to the TyDefCtx (we could, somehow, but it would require some new plumbing). Alternatively, we could translate the encoding of types passed at runtime to use fixpoints, so that we could do without the TyDefCtx. Then we have to contrive to insert the infinite enumeration combinator somehow when dealing with an infinite user-defined type, or else it will just hang.

Alternatively, perhaps we could pre-compute a mapping from user-defined type names to enumerations, and make that available at runtime?

@byorgey byorgey added C-Project A larger project that may take multiple days. S-Nice to have Minor importance U-Constraint Solving U-Interpreter Z-Feature Request labels Jan 21, 2022
@byorgey
Copy link
Member Author

byorgey commented Mar 28, 2022

Using fixpoints sounds nice and tidy, but it would require some work when parsing type definitions: we would have to do some analysis to figure out mutually recursive groups of type definitions and then somehow translate each group into a single fixpoint.

@byorgey byorgey changed the title Make user-defined types searchable Make user-defined types enumerable + searchable Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant