0

Following the answer in https://stackoverflow.com/a/26084087/8142021 I used the following haskell code to define a Modal Logic with GADTs.

data Plain
data Mod
data Formula t where
   Prop :: {propName :: String} -> Formula t
   Neg  :: Formula t -> Formula t
   Conj :: Formula t -> Formula t -> Formula t
   Disj :: Formula t -> Formula t -> Formula t
   Impl :: Formula t -> Formula t -> Formula t
   BiImpl :: Formula t -> Formula t -> Formula t
   MyModality :: Formula Mod -> Formula Mod 

type PlainFormula = Formula Plain
type ModalFormula = Formula Mod

Is there a way to change the type of a PlainFormula to a ModalFormula? Since PlainFormula is a subset of Modal Formula, I tried to define the following injection but it did not work.

toModal :: PlainFormula -> ModalFormula
toModal f = f
DDub
  • 3,884
  • 1
  • 5
  • 12
sfx
  • 103
  • 9
  • 1
    I wish there were a way to perform this in O(1). AFAIK, the only way is to recurse over all the formula and reconstruct it, in O(N). – chi Dec 28 '20 at 23:06
  • @chi I could define toModal using recursion as you said. I will use that for now. Thanks. – sfx Dec 29 '20 at 00:10
  • 1
    In this case, you could instead define `PlainFormula = forall t. PlainFormula t` which would give you this conversion for free. Maybe this approach could be generalized with some type classes instead of concrete types, so you would have `MyModality :: Modal t => Formula t -> Formula t`, and `type ModalFormula = forall t. Modal t => Formula t`. Might not be as fun to work with in practice as one would hope, though – luqui Dec 29 '20 at 08:47
  • PlainFormula may be a subset of ModalFormula but your data type doesn't allow this, i.e. you cannot `Conj` a modal formula and a plain formula. – n. m. could be an AI Dec 30 '20 at 22:43
  • @n.'pronouns'm. So, if I want to `Conj` a modal formula `f` and a plain formula `g`, I must convert `g` into a modal formula. That's what I tried to achieve. I could do it but at the cost of recursion. – sfx Dec 30 '20 at 22:59
  • @luqui I need to read more about type classes to understand your approach. Thanks. – sfx Dec 30 '20 at 23:03
  • I would do something like this https://pastebin.pl/view/2b47d737 so you don't need to convert and you can have an hierarchy of different modalities. – n. m. could be an AI Dec 30 '20 at 23:47

0 Answers0